blob: 7280e87fb216015a719b6c91de56dd9fa31067d1 [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) {
154 __ andl(length_loc.AsRegister<Register>(), Immediate(INT32_MAX));
155 }
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;
228 const uint32_t string_index = instruction_->AsLoadString()->GetStringIndex();
229 __ 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;
268 __ movl(calling_convention.GetRegisterAt(0), Immediate(cls_->GetTypeIndex()));
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();
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800315 Location arg0, arg1;
316 if (instruction_->IsInstanceOf()) {
317 arg0 = locations->InAt(1);
318 arg1 = locations->Out();
319 } else {
320 arg0 = locations->InAt(0);
321 arg1 = locations->InAt(1);
322 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000323 DCHECK(instruction_->IsCheckCast()
324 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000325
326 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
327 __ Bind(GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000328
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000329 if (!is_fatal_) {
330 SaveLiveRegisters(codegen, locations);
331 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000332
333 // We're moving two locations to locations that could overlap, so we need a parallel
334 // move resolver.
335 InvokeRuntimeCallingConvention calling_convention;
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800336 x86_codegen->EmitParallelMoves(arg0,
337 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
338 Primitive::kPrimNot,
339 arg1,
340 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
341 Primitive::kPrimNot);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000342 if (instruction_->IsInstanceOf()) {
Serban Constantinescuba45db02016-07-12 22:53:02 +0100343 x86_codegen->InvokeRuntime(kQuickInstanceofNonTrivial,
Alexandre Rames8158f282015-08-07 10:26:17 +0100344 instruction_,
345 instruction_->GetDexPc(),
346 this);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800347 CheckEntrypointTypes<kQuickInstanceofNonTrivial, size_t, mirror::Class*, mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000348 } else {
349 DCHECK(instruction_->IsCheckCast());
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800350 x86_codegen->InvokeRuntime(kQuickCheckInstanceOf,
351 instruction_,
352 instruction_->GetDexPc(),
353 this);
354 CheckEntrypointTypes<kQuickCheckInstanceOf, void, mirror::Object*, mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000355 }
356
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000357 if (!is_fatal_) {
358 if (instruction_->IsInstanceOf()) {
359 x86_codegen->Move32(locations->Out(), Location::RegisterLocation(EAX));
360 }
361 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray75374372015-09-17 17:12:19 +0000362
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000363 __ jmp(GetExitLabel());
364 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000365 }
366
Alexandre Rames9931f312015-06-19 14:47:01 +0100367 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathX86"; }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000368 bool IsFatal() const OVERRIDE { return is_fatal_; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100369
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000370 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000371 const bool is_fatal_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000372
373 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathX86);
374};
375
Andreas Gampe85b62f22015-09-09 13:15:38 -0700376class DeoptimizationSlowPathX86 : public SlowPathCode {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700377 public:
Aart Bik42249c32016-01-07 15:33:50 -0800378 explicit DeoptimizationSlowPathX86(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000379 : SlowPathCode(instruction) {}
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700380
381 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames8158f282015-08-07 10:26:17 +0100382 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700383 __ Bind(GetEntryLabel());
Serban Constantinescuba45db02016-07-12 22:53:02 +0100384 x86_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000385 CheckEntrypointTypes<kQuickDeoptimize, void, void>();
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700386 }
387
Alexandre Rames9931f312015-06-19 14:47:01 +0100388 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathX86"; }
389
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700390 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700391 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathX86);
392};
393
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100394class ArraySetSlowPathX86 : public SlowPathCode {
395 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000396 explicit ArraySetSlowPathX86(HInstruction* instruction) : SlowPathCode(instruction) {}
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100397
398 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
399 LocationSummary* locations = instruction_->GetLocations();
400 __ Bind(GetEntryLabel());
401 SaveLiveRegisters(codegen, locations);
402
403 InvokeRuntimeCallingConvention calling_convention;
404 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
405 parallel_move.AddMove(
406 locations->InAt(0),
407 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
408 Primitive::kPrimNot,
409 nullptr);
410 parallel_move.AddMove(
411 locations->InAt(1),
412 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
413 Primitive::kPrimInt,
414 nullptr);
415 parallel_move.AddMove(
416 locations->InAt(2),
417 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
418 Primitive::kPrimNot,
419 nullptr);
420 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
421
422 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Serban Constantinescuba45db02016-07-12 22:53:02 +0100423 x86_codegen->InvokeRuntime(kQuickAputObject, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000424 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100425 RestoreLiveRegisters(codegen, locations);
426 __ jmp(GetExitLabel());
427 }
428
429 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathX86"; }
430
431 private:
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100432 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathX86);
433};
434
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100435// Slow path marking an object reference `ref` during a read
436// barrier. The field `obj.field` in the object `obj` holding this
437// reference does not get updated by this slow path after marking (see
438// ReadBarrierMarkAndUpdateFieldSlowPathX86 below for that).
439//
440// This means that after the execution of this slow path, `ref` will
441// always be up-to-date, but `obj.field` may not; i.e., after the
442// flip, `ref` will be a to-space reference, but `obj.field` will
443// probably still be a from-space reference (unless it gets updated by
444// another thread, or if another thread installed another object
445// reference (different from `ref`) in `obj.field`).
Roland Levillain7c1559a2015-12-15 10:55:36 +0000446class ReadBarrierMarkSlowPathX86 : public SlowPathCode {
447 public:
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100448 ReadBarrierMarkSlowPathX86(HInstruction* instruction,
449 Location ref,
450 bool unpoison_ref_before_marking)
451 : SlowPathCode(instruction),
452 ref_(ref),
453 unpoison_ref_before_marking_(unpoison_ref_before_marking) {
Roland Levillain7c1559a2015-12-15 10:55:36 +0000454 DCHECK(kEmitCompilerReadBarrier);
455 }
456
457 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathX86"; }
458
459 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
460 LocationSummary* locations = instruction_->GetLocations();
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100461 Register ref_reg = ref_.AsRegister<Register>();
Roland Levillain7c1559a2015-12-15 10:55:36 +0000462 DCHECK(locations->CanCall());
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100463 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
Roland Levillain7c1559a2015-12-15 10:55:36 +0000464 DCHECK(instruction_->IsInstanceFieldGet() ||
465 instruction_->IsStaticFieldGet() ||
466 instruction_->IsArrayGet() ||
Roland Levillain16d9f942016-08-25 17:27:56 +0100467 instruction_->IsArraySet() ||
Roland Levillain7c1559a2015-12-15 10:55:36 +0000468 instruction_->IsLoadClass() ||
469 instruction_->IsLoadString() ||
470 instruction_->IsInstanceOf() ||
Roland Levillain3d312422016-06-23 13:53:42 +0100471 instruction_->IsCheckCast() ||
Roland Levillain0b671c02016-08-19 12:02:34 +0100472 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()) ||
473 (instruction_->IsInvokeStaticOrDirect() && instruction_->GetLocations()->Intrinsified()))
Roland Levillain7c1559a2015-12-15 10:55:36 +0000474 << "Unexpected instruction in read barrier marking slow path: "
475 << instruction_->DebugName();
476
477 __ Bind(GetEntryLabel());
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100478 if (unpoison_ref_before_marking_) {
Vladimir Marko953437b2016-08-24 08:30:46 +0000479 // Object* ref = ref_addr->AsMirrorPtr()
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100480 __ MaybeUnpoisonHeapReference(ref_reg);
Vladimir Marko953437b2016-08-24 08:30:46 +0000481 }
Roland Levillain4359e612016-07-20 11:32:19 +0100482 // No need to save live registers; it's taken care of by the
483 // entrypoint. Also, there is no need to update the stack mask,
484 // as this runtime call will not trigger a garbage collection.
Roland Levillain7c1559a2015-12-15 10:55:36 +0000485 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100486 DCHECK_NE(ref_reg, ESP);
487 DCHECK(0 <= ref_reg && ref_reg < kNumberOfCpuRegisters) << ref_reg;
Roland Levillain02b75802016-07-13 11:54:35 +0100488 // "Compact" slow path, saving two moves.
489 //
490 // Instead of using the standard runtime calling convention (input
491 // and output in EAX):
492 //
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100493 // EAX <- ref
Roland Levillain02b75802016-07-13 11:54:35 +0100494 // EAX <- ReadBarrierMark(EAX)
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100495 // ref <- EAX
Roland Levillain02b75802016-07-13 11:54:35 +0100496 //
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100497 // we just use rX (the register containing `ref`) as input and output
Roland Levillain02b75802016-07-13 11:54:35 +0100498 // of a dedicated entrypoint:
499 //
500 // rX <- ReadBarrierMarkRegX(rX)
501 //
502 int32_t entry_point_offset =
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100503 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kX86PointerSize>(ref_reg);
Roland Levillaindec8f632016-07-22 17:10:06 +0100504 // This runtime call does not require a stack map.
505 x86_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
Roland Levillain7c1559a2015-12-15 10:55:36 +0000506 __ jmp(GetExitLabel());
507 }
508
509 private:
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100510 // The location (register) of the marked object reference.
511 const Location ref_;
512 // Should the reference in `ref_` be unpoisoned prior to marking it?
513 const bool unpoison_ref_before_marking_;
Roland Levillain7c1559a2015-12-15 10:55:36 +0000514
515 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathX86);
516};
517
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100518// Slow path marking an object reference `ref` during a read barrier,
519// and if needed, atomically updating the field `obj.field` in the
520// object `obj` holding this reference after marking (contrary to
521// ReadBarrierMarkSlowPathX86 above, which never tries to update
522// `obj.field`).
523//
524// This means that after the execution of this slow path, both `ref`
525// and `obj.field` will be up-to-date; i.e., after the flip, both will
526// hold the same to-space reference (unless another thread installed
527// another object reference (different from `ref`) in `obj.field`).
528class ReadBarrierMarkAndUpdateFieldSlowPathX86 : public SlowPathCode {
529 public:
530 ReadBarrierMarkAndUpdateFieldSlowPathX86(HInstruction* instruction,
531 Location ref,
532 Register obj,
533 const Address& field_addr,
534 bool unpoison_ref_before_marking,
535 Register temp)
536 : SlowPathCode(instruction),
537 ref_(ref),
538 obj_(obj),
539 field_addr_(field_addr),
540 unpoison_ref_before_marking_(unpoison_ref_before_marking),
541 temp_(temp) {
542 DCHECK(kEmitCompilerReadBarrier);
543 }
544
545 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkAndUpdateFieldSlowPathX86"; }
546
547 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
548 LocationSummary* locations = instruction_->GetLocations();
549 Register ref_reg = ref_.AsRegister<Register>();
550 DCHECK(locations->CanCall());
551 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
552 // This slow path is only used by the UnsafeCASObject intrinsic.
553 DCHECK((instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
554 << "Unexpected instruction in read barrier marking and field updating slow path: "
555 << instruction_->DebugName();
556 DCHECK(instruction_->GetLocations()->Intrinsified());
557 DCHECK_EQ(instruction_->AsInvoke()->GetIntrinsic(), Intrinsics::kUnsafeCASObject);
558
559 __ Bind(GetEntryLabel());
560 if (unpoison_ref_before_marking_) {
561 // Object* ref = ref_addr->AsMirrorPtr()
562 __ MaybeUnpoisonHeapReference(ref_reg);
563 }
564
565 // Save the old (unpoisoned) reference.
566 __ movl(temp_, ref_reg);
567
568 // No need to save live registers; it's taken care of by the
569 // entrypoint. Also, there is no need to update the stack mask,
570 // as this runtime call will not trigger a garbage collection.
571 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
572 DCHECK_NE(ref_reg, ESP);
573 DCHECK(0 <= ref_reg && ref_reg < kNumberOfCpuRegisters) << ref_reg;
574 // "Compact" slow path, saving two moves.
575 //
576 // Instead of using the standard runtime calling convention (input
577 // and output in EAX):
578 //
579 // EAX <- ref
580 // EAX <- ReadBarrierMark(EAX)
581 // ref <- EAX
582 //
583 // we just use rX (the register containing `ref`) as input and output
584 // of a dedicated entrypoint:
585 //
586 // rX <- ReadBarrierMarkRegX(rX)
587 //
588 int32_t entry_point_offset =
589 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kX86PointerSize>(ref_reg);
590 // This runtime call does not require a stack map.
591 x86_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
592
593 // If the new reference is different from the old reference,
594 // update the field in the holder (`*field_addr`).
595 //
596 // Note that this field could also hold a different object, if
597 // another thread had concurrently changed it. In that case, the
598 // LOCK CMPXCHGL instruction in the compare-and-set (CAS)
599 // operation below would abort the CAS, leaving the field as-is.
600 NearLabel done;
601 __ cmpl(temp_, ref_reg);
602 __ j(kEqual, &done);
603
604 // Update the the holder's field atomically. This may fail if
605 // mutator updates before us, but it's OK. This is achieved
606 // using a strong compare-and-set (CAS) operation with relaxed
607 // memory synchronization ordering, where the expected value is
608 // the old reference and the desired value is the new reference.
609 // This operation is implemented with a 32-bit LOCK CMPXLCHG
610 // instruction, which requires the expected value (the old
611 // reference) to be in EAX. Save EAX beforehand, and move the
612 // expected value (stored in `temp_`) into EAX.
613 __ pushl(EAX);
614 __ movl(EAX, temp_);
615
616 // Convenience aliases.
617 Register base = obj_;
618 Register expected = EAX;
619 Register value = ref_reg;
620
621 bool base_equals_value = (base == value);
622 if (kPoisonHeapReferences) {
623 if (base_equals_value) {
624 // If `base` and `value` are the same register location, move
625 // `value` to a temporary register. This way, poisoning
626 // `value` won't invalidate `base`.
627 value = temp_;
628 __ movl(value, base);
629 }
630
631 // Check that the register allocator did not assign the location
632 // of `expected` (EAX) to `value` nor to `base`, so that heap
633 // poisoning (when enabled) works as intended below.
634 // - If `value` were equal to `expected`, both references would
635 // be poisoned twice, meaning they would not be poisoned at
636 // all, as heap poisoning uses address negation.
637 // - If `base` were equal to `expected`, poisoning `expected`
638 // would invalidate `base`.
639 DCHECK_NE(value, expected);
640 DCHECK_NE(base, expected);
641
642 __ PoisonHeapReference(expected);
643 __ PoisonHeapReference(value);
644 }
645
646 __ LockCmpxchgl(field_addr_, value);
647
648 // If heap poisoning is enabled, we need to unpoison the values
649 // that were poisoned earlier.
650 if (kPoisonHeapReferences) {
651 if (base_equals_value) {
652 // `value` has been moved to a temporary register, no need
653 // to unpoison it.
654 } else {
655 __ UnpoisonHeapReference(value);
656 }
657 // No need to unpoison `expected` (EAX), as it is be overwritten below.
658 }
659
660 // Restore EAX.
661 __ popl(EAX);
662
663 __ Bind(&done);
664 __ jmp(GetExitLabel());
665 }
666
667 private:
668 // The location (register) of the marked object reference.
669 const Location ref_;
670 // The register containing the object holding the marked object reference field.
671 const Register obj_;
672 // The address of the marked reference field. The base of this address must be `obj_`.
673 const Address field_addr_;
674
675 // Should the reference in `ref_` be unpoisoned prior to marking it?
676 const bool unpoison_ref_before_marking_;
677
678 const Register temp_;
679
680 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkAndUpdateFieldSlowPathX86);
681};
682
Roland Levillain0d5a2812015-11-13 10:07:31 +0000683// Slow path generating a read barrier for a heap reference.
684class ReadBarrierForHeapReferenceSlowPathX86 : public SlowPathCode {
685 public:
686 ReadBarrierForHeapReferenceSlowPathX86(HInstruction* instruction,
687 Location out,
688 Location ref,
689 Location obj,
690 uint32_t offset,
691 Location index)
David Srbecky9cd6d372016-02-09 15:24:47 +0000692 : SlowPathCode(instruction),
Roland Levillain0d5a2812015-11-13 10:07:31 +0000693 out_(out),
694 ref_(ref),
695 obj_(obj),
696 offset_(offset),
697 index_(index) {
698 DCHECK(kEmitCompilerReadBarrier);
699 // If `obj` is equal to `out` or `ref`, it means the initial object
700 // has been overwritten by (or after) the heap object reference load
701 // to be instrumented, e.g.:
702 //
703 // __ movl(out, Address(out, offset));
Roland Levillain7c1559a2015-12-15 10:55:36 +0000704 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000705 //
706 // In that case, we have lost the information about the original
707 // object, and the emitted read barrier cannot work properly.
708 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
709 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
710 }
711
712 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
713 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
714 LocationSummary* locations = instruction_->GetLocations();
715 Register reg_out = out_.AsRegister<Register>();
716 DCHECK(locations->CanCall());
717 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
Roland Levillain3d312422016-06-23 13:53:42 +0100718 DCHECK(instruction_->IsInstanceFieldGet() ||
719 instruction_->IsStaticFieldGet() ||
720 instruction_->IsArrayGet() ||
721 instruction_->IsInstanceOf() ||
722 instruction_->IsCheckCast() ||
Roland Levillaindec8f632016-07-22 17:10:06 +0100723 (instruction_->IsInvokeVirtual()) && instruction_->GetLocations()->Intrinsified())
Roland Levillain7c1559a2015-12-15 10:55:36 +0000724 << "Unexpected instruction in read barrier for heap reference slow path: "
725 << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000726
727 __ Bind(GetEntryLabel());
728 SaveLiveRegisters(codegen, locations);
729
730 // We may have to change the index's value, but as `index_` is a
731 // constant member (like other "inputs" of this slow path),
732 // introduce a copy of it, `index`.
733 Location index = index_;
734 if (index_.IsValid()) {
Roland Levillain3d312422016-06-23 13:53:42 +0100735 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
Roland Levillain0d5a2812015-11-13 10:07:31 +0000736 if (instruction_->IsArrayGet()) {
737 // Compute the actual memory offset and store it in `index`.
738 Register index_reg = index_.AsRegister<Register>();
739 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg));
740 if (codegen->IsCoreCalleeSaveRegister(index_reg)) {
741 // We are about to change the value of `index_reg` (see the
742 // calls to art::x86::X86Assembler::shll and
743 // art::x86::X86Assembler::AddImmediate below), but it has
744 // not been saved by the previous call to
745 // art::SlowPathCode::SaveLiveRegisters, as it is a
746 // callee-save register --
747 // art::SlowPathCode::SaveLiveRegisters does not consider
748 // callee-save registers, as it has been designed with the
749 // assumption that callee-save registers are supposed to be
750 // handled by the called function. So, as a callee-save
751 // register, `index_reg` _would_ eventually be saved onto
752 // the stack, but it would be too late: we would have
753 // changed its value earlier. Therefore, we manually save
754 // it here into another freely available register,
755 // `free_reg`, chosen of course among the caller-save
756 // registers (as a callee-save `free_reg` register would
757 // exhibit the same problem).
758 //
759 // Note we could have requested a temporary register from
760 // the register allocator instead; but we prefer not to, as
761 // this is a slow path, and we know we can find a
762 // caller-save register that is available.
763 Register free_reg = FindAvailableCallerSaveRegister(codegen);
764 __ movl(free_reg, index_reg);
765 index_reg = free_reg;
766 index = Location::RegisterLocation(index_reg);
767 } else {
768 // The initial register stored in `index_` has already been
769 // saved in the call to art::SlowPathCode::SaveLiveRegisters
770 // (as it is not a callee-save register), so we can freely
771 // use it.
772 }
773 // Shifting the index value contained in `index_reg` by the scale
774 // factor (2) cannot overflow in practice, as the runtime is
775 // unable to allocate object arrays with a size larger than
776 // 2^26 - 1 (that is, 2^28 - 4 bytes).
777 __ shll(index_reg, Immediate(TIMES_4));
778 static_assert(
779 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
780 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
781 __ AddImmediate(index_reg, Immediate(offset_));
782 } else {
Roland Levillain3d312422016-06-23 13:53:42 +0100783 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
784 // intrinsics, `index_` is not shifted by a scale factor of 2
785 // (as in the case of ArrayGet), as it is actually an offset
786 // to an object field within an object.
787 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000788 DCHECK(instruction_->GetLocations()->Intrinsified());
789 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
790 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
791 << instruction_->AsInvoke()->GetIntrinsic();
792 DCHECK_EQ(offset_, 0U);
793 DCHECK(index_.IsRegisterPair());
794 // UnsafeGet's offset location is a register pair, the low
795 // part contains the correct offset.
796 index = index_.ToLow();
797 }
798 }
799
800 // We're moving two or three locations to locations that could
801 // overlap, so we need a parallel move resolver.
802 InvokeRuntimeCallingConvention calling_convention;
803 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
804 parallel_move.AddMove(ref_,
805 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
806 Primitive::kPrimNot,
807 nullptr);
808 parallel_move.AddMove(obj_,
809 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
810 Primitive::kPrimNot,
811 nullptr);
812 if (index.IsValid()) {
813 parallel_move.AddMove(index,
814 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
815 Primitive::kPrimInt,
816 nullptr);
817 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
818 } else {
819 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
820 __ movl(calling_convention.GetRegisterAt(2), Immediate(offset_));
821 }
Serban Constantinescuba45db02016-07-12 22:53:02 +0100822 x86_codegen->InvokeRuntime(kQuickReadBarrierSlow, instruction_, instruction_->GetDexPc(), this);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000823 CheckEntrypointTypes<
824 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
825 x86_codegen->Move32(out_, Location::RegisterLocation(EAX));
826
827 RestoreLiveRegisters(codegen, locations);
828 __ jmp(GetExitLabel());
829 }
830
831 const char* GetDescription() const OVERRIDE { return "ReadBarrierForHeapReferenceSlowPathX86"; }
832
833 private:
834 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
835 size_t ref = static_cast<int>(ref_.AsRegister<Register>());
836 size_t obj = static_cast<int>(obj_.AsRegister<Register>());
837 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
838 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
839 return static_cast<Register>(i);
840 }
841 }
842 // We shall never fail to find a free caller-save register, as
843 // there are more than two core caller-save registers on x86
844 // (meaning it is possible to find one which is different from
845 // `ref` and `obj`).
846 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
847 LOG(FATAL) << "Could not find a free caller-save register";
848 UNREACHABLE();
849 }
850
Roland Levillain0d5a2812015-11-13 10:07:31 +0000851 const Location out_;
852 const Location ref_;
853 const Location obj_;
854 const uint32_t offset_;
855 // An additional location containing an index to an array.
856 // Only used for HArrayGet and the UnsafeGetObject &
857 // UnsafeGetObjectVolatile intrinsics.
858 const Location index_;
859
860 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathX86);
861};
862
863// Slow path generating a read barrier for a GC root.
864class ReadBarrierForRootSlowPathX86 : public SlowPathCode {
865 public:
866 ReadBarrierForRootSlowPathX86(HInstruction* instruction, Location out, Location root)
David Srbecky9cd6d372016-02-09 15:24:47 +0000867 : SlowPathCode(instruction), out_(out), root_(root) {
Roland Levillain7c1559a2015-12-15 10:55:36 +0000868 DCHECK(kEmitCompilerReadBarrier);
869 }
Roland Levillain0d5a2812015-11-13 10:07:31 +0000870
871 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
872 LocationSummary* locations = instruction_->GetLocations();
873 Register reg_out = out_.AsRegister<Register>();
874 DCHECK(locations->CanCall());
875 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
Roland Levillain7c1559a2015-12-15 10:55:36 +0000876 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
877 << "Unexpected instruction in read barrier for GC root slow path: "
878 << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000879
880 __ Bind(GetEntryLabel());
881 SaveLiveRegisters(codegen, locations);
882
883 InvokeRuntimeCallingConvention calling_convention;
884 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
885 x86_codegen->Move32(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), root_);
Serban Constantinescuba45db02016-07-12 22:53:02 +0100886 x86_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000887 instruction_,
888 instruction_->GetDexPc(),
889 this);
890 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
891 x86_codegen->Move32(out_, Location::RegisterLocation(EAX));
892
893 RestoreLiveRegisters(codegen, locations);
894 __ jmp(GetExitLabel());
895 }
896
897 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathX86"; }
898
899 private:
Roland Levillain0d5a2812015-11-13 10:07:31 +0000900 const Location out_;
901 const Location root_;
902
903 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathX86);
904};
905
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100906#undef __
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100907// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
908#define __ down_cast<X86Assembler*>(GetAssembler())-> // NOLINT
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100909
Aart Bike9f37602015-10-09 11:15:55 -0700910inline Condition X86Condition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -0700911 switch (cond) {
912 case kCondEQ: return kEqual;
913 case kCondNE: return kNotEqual;
914 case kCondLT: return kLess;
915 case kCondLE: return kLessEqual;
916 case kCondGT: return kGreater;
917 case kCondGE: return kGreaterEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700918 case kCondB: return kBelow;
919 case kCondBE: return kBelowEqual;
920 case kCondA: return kAbove;
921 case kCondAE: return kAboveEqual;
Dave Allison20dfc792014-06-16 20:44:29 -0700922 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100923 LOG(FATAL) << "Unreachable";
924 UNREACHABLE();
925}
926
Aart Bike9f37602015-10-09 11:15:55 -0700927// Maps signed condition to unsigned condition and FP condition to x86 name.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100928inline Condition X86UnsignedOrFPCondition(IfCondition cond) {
929 switch (cond) {
930 case kCondEQ: return kEqual;
931 case kCondNE: return kNotEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700932 // Signed to unsigned, and FP to x86 name.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100933 case kCondLT: return kBelow;
934 case kCondLE: return kBelowEqual;
935 case kCondGT: return kAbove;
936 case kCondGE: return kAboveEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700937 // Unsigned remain unchanged.
938 case kCondB: return kBelow;
939 case kCondBE: return kBelowEqual;
940 case kCondA: return kAbove;
941 case kCondAE: return kAboveEqual;
Roland Levillain4fa13f62015-07-06 18:11:54 +0100942 }
943 LOG(FATAL) << "Unreachable";
944 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -0700945}
946
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100947void CodeGeneratorX86::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100948 stream << Register(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100949}
950
951void CodeGeneratorX86::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100952 stream << XmmRegister(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100953}
954
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100955size_t CodeGeneratorX86::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
956 __ movl(Address(ESP, stack_index), static_cast<Register>(reg_id));
957 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100958}
959
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100960size_t CodeGeneratorX86::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
961 __ movl(static_cast<Register>(reg_id), Address(ESP, stack_index));
962 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100963}
964
Mark Mendell7c8d0092015-01-26 11:21:33 -0500965size_t CodeGeneratorX86::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
966 __ movsd(Address(ESP, stack_index), XmmRegister(reg_id));
967 return GetFloatingPointSpillSlotSize();
968}
969
970size_t CodeGeneratorX86::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
971 __ movsd(XmmRegister(reg_id), Address(ESP, stack_index));
972 return GetFloatingPointSpillSlotSize();
973}
974
Calin Juravle175dc732015-08-25 15:42:32 +0100975void CodeGeneratorX86::InvokeRuntime(QuickEntrypointEnum entrypoint,
976 HInstruction* instruction,
977 uint32_t dex_pc,
978 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +0100979 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Serban Constantinescuba45db02016-07-12 22:53:02 +0100980 GenerateInvokeRuntime(GetThreadOffset<kX86PointerSize>(entrypoint).Int32Value());
981 if (EntrypointRequiresStackMap(entrypoint)) {
982 RecordPcInfo(instruction, dex_pc, slow_path);
983 }
Alexandre Rames8158f282015-08-07 10:26:17 +0100984}
985
Roland Levillaindec8f632016-07-22 17:10:06 +0100986void CodeGeneratorX86::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
987 HInstruction* instruction,
988 SlowPathCode* slow_path) {
989 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
Serban Constantinescuba45db02016-07-12 22:53:02 +0100990 GenerateInvokeRuntime(entry_point_offset);
991}
992
993void CodeGeneratorX86::GenerateInvokeRuntime(int32_t entry_point_offset) {
Roland Levillaindec8f632016-07-22 17:10:06 +0100994 __ fs()->call(Address::Absolute(entry_point_offset));
995}
996
Mark Mendellfb8d2792015-03-31 22:16:59 -0400997CodeGeneratorX86::CodeGeneratorX86(HGraph* graph,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000998 const X86InstructionSetFeatures& isa_features,
999 const CompilerOptions& compiler_options,
1000 OptimizingCompilerStats* stats)
Mark Mendell5f874182015-03-04 15:42:45 -05001001 : CodeGenerator(graph,
1002 kNumberOfCpuRegisters,
1003 kNumberOfXmmRegisters,
1004 kNumberOfRegisterPairs,
1005 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
1006 arraysize(kCoreCalleeSaves))
1007 | (1 << kFakeReturnRegister),
Serban Constantinescuecc43662015-08-13 13:33:12 +01001008 0,
1009 compiler_options,
1010 stats),
Vladimir Marko225b6462015-09-28 12:17:40 +01001011 block_labels_(nullptr),
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001012 location_builder_(graph, this),
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001013 instruction_visitor_(graph, this),
Mark Mendellfb8d2792015-03-31 22:16:59 -04001014 move_resolver_(graph->GetArena(), this),
Vladimir Marko93205e32016-04-13 11:59:46 +01001015 assembler_(graph->GetArena()),
Vladimir Marko58155012015-08-19 12:49:41 +00001016 isa_features_(isa_features),
Vladimir Marko5233f932015-09-29 19:01:15 +01001017 method_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Mark Mendell805b3b52015-09-18 14:10:29 -04001018 relative_call_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko0f7dca42015-11-02 14:36:43 +00001019 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001020 simple_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1021 string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01001022 type_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko93205e32016-04-13 11:59:46 +01001023 constant_area_start_(-1),
1024 fixups_to_jump_tables_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1025 method_address_offset_(-1) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001026 // Use a fake return address register to mimic Quick.
1027 AddAllocatedRegister(Location::RegisterLocation(kFakeReturnRegister));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001028}
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001029
David Brazdil58282f42016-01-14 12:45:10 +00001030void CodeGeneratorX86::SetupBlockedRegisters() const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001031 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +01001032 blocked_core_registers_[ESP] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001033}
1034
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001035InstructionCodeGeneratorX86::InstructionCodeGeneratorX86(HGraph* graph, CodeGeneratorX86* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001036 : InstructionCodeGenerator(graph, codegen),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001037 assembler_(codegen->GetAssembler()),
1038 codegen_(codegen) {}
1039
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001040static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +01001041 return dwarf::Reg::X86Core(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001042}
1043
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001044void CodeGeneratorX86::GenerateFrameEntry() {
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001045 __ cfi().SetCurrentCFAOffset(kX86WordSize); // return address
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001046 __ Bind(&frame_entry_label_);
Roland Levillain199f3362014-11-27 17:15:16 +00001047 bool skip_overflow_check =
1048 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001049 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Calin Juravle93edf732015-01-20 20:14:07 +00001050
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001051 if (!skip_overflow_check) {
Nicolas Geoffray397f2e42014-07-23 12:57:19 +01001052 __ testl(EAX, Address(ESP, -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86))));
Nicolas Geoffray39468442014-09-02 15:17:15 +01001053 RecordPcInfo(nullptr, 0);
Nicolas Geoffray397f2e42014-07-23 12:57:19 +01001054 }
1055
Mark Mendell5f874182015-03-04 15:42:45 -05001056 if (HasEmptyFrame()) {
1057 return;
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001058 }
Mark Mendell5f874182015-03-04 15:42:45 -05001059
1060 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
1061 Register reg = kCoreCalleeSaves[i];
1062 if (allocated_registers_.ContainsCoreRegister(reg)) {
1063 __ pushl(reg);
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001064 __ cfi().AdjustCFAOffset(kX86WordSize);
1065 __ cfi().RelOffset(DWARFReg(reg), 0);
Mark Mendell5f874182015-03-04 15:42:45 -05001066 }
1067 }
1068
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001069 int adjust = GetFrameSize() - FrameEntrySpillSize();
1070 __ subl(ESP, Immediate(adjust));
1071 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001072 // Save the current method if we need it. Note that we do not
1073 // do this in HCurrentMethod, as the instruction might have been removed
1074 // in the SSA graph.
1075 if (RequiresCurrentMethod()) {
1076 __ movl(Address(ESP, kCurrentMethodStackOffset), kMethodRegisterArgument);
1077 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001078}
1079
1080void CodeGeneratorX86::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +01001081 __ cfi().RememberState();
1082 if (!HasEmptyFrame()) {
1083 int adjust = GetFrameSize() - FrameEntrySpillSize();
1084 __ addl(ESP, Immediate(adjust));
1085 __ cfi().AdjustCFAOffset(-adjust);
Mark Mendell5f874182015-03-04 15:42:45 -05001086
David Srbeckyc34dc932015-04-12 09:27:43 +01001087 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
1088 Register reg = kCoreCalleeSaves[i];
1089 if (allocated_registers_.ContainsCoreRegister(reg)) {
1090 __ popl(reg);
1091 __ cfi().AdjustCFAOffset(-static_cast<int>(kX86WordSize));
1092 __ cfi().Restore(DWARFReg(reg));
1093 }
Mark Mendell5f874182015-03-04 15:42:45 -05001094 }
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001095 }
David Srbeckyc34dc932015-04-12 09:27:43 +01001096 __ ret();
1097 __ cfi().RestoreState();
1098 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001099}
1100
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01001101void CodeGeneratorX86::Bind(HBasicBlock* block) {
1102 __ Bind(GetLabelOf(block));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001103}
1104
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001105Location InvokeDexCallingConventionVisitorX86::GetReturnLocation(Primitive::Type type) const {
1106 switch (type) {
1107 case Primitive::kPrimBoolean:
1108 case Primitive::kPrimByte:
1109 case Primitive::kPrimChar:
1110 case Primitive::kPrimShort:
1111 case Primitive::kPrimInt:
1112 case Primitive::kPrimNot:
1113 return Location::RegisterLocation(EAX);
1114
1115 case Primitive::kPrimLong:
1116 return Location::RegisterPairLocation(EAX, EDX);
1117
1118 case Primitive::kPrimVoid:
1119 return Location::NoLocation();
1120
1121 case Primitive::kPrimDouble:
1122 case Primitive::kPrimFloat:
1123 return Location::FpuRegisterLocation(XMM0);
1124 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +01001125
1126 UNREACHABLE();
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001127}
1128
1129Location InvokeDexCallingConventionVisitorX86::GetMethodLocation() const {
1130 return Location::RegisterLocation(kMethodRegisterArgument);
1131}
1132
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001133Location InvokeDexCallingConventionVisitorX86::GetNextLocation(Primitive::Type type) {
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001134 switch (type) {
1135 case Primitive::kPrimBoolean:
1136 case Primitive::kPrimByte:
1137 case Primitive::kPrimChar:
1138 case Primitive::kPrimShort:
1139 case Primitive::kPrimInt:
1140 case Primitive::kPrimNot: {
1141 uint32_t index = gp_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001142 stack_index_++;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001143 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001144 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001145 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001146 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001147 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001148 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001149
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001150 case Primitive::kPrimLong: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001151 uint32_t index = gp_index_;
1152 gp_index_ += 2;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001153 stack_index_ += 2;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001154 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001155 X86ManagedRegister pair = X86ManagedRegister::FromRegisterPair(
1156 calling_convention.GetRegisterPairAt(index));
1157 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001158 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001159 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
1160 }
1161 }
1162
1163 case Primitive::kPrimFloat: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001164 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001165 stack_index_++;
1166 if (index < calling_convention.GetNumberOfFpuRegisters()) {
1167 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
1168 } else {
1169 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
1170 }
1171 }
1172
1173 case Primitive::kPrimDouble: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001174 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001175 stack_index_ += 2;
1176 if (index < calling_convention.GetNumberOfFpuRegisters()) {
1177 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
1178 } else {
1179 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001180 }
1181 }
1182
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001183 case Primitive::kPrimVoid:
1184 LOG(FATAL) << "Unexpected parameter type " << type;
1185 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001186 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00001187 return Location::NoLocation();
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001188}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001189
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001190void CodeGeneratorX86::Move32(Location destination, Location source) {
1191 if (source.Equals(destination)) {
1192 return;
1193 }
1194 if (destination.IsRegister()) {
1195 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001196 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001197 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001198 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001199 } else {
1200 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001201 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001202 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001203 } else if (destination.IsFpuRegister()) {
1204 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001205 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001206 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001207 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001208 } else {
1209 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001210 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001211 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001212 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001213 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001214 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001215 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001216 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001217 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Mark Mendell7c8d0092015-01-26 11:21:33 -05001218 } else if (source.IsConstant()) {
1219 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001220 int32_t value = GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05001221 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001222 } else {
1223 DCHECK(source.IsStackSlot());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001224 __ pushl(Address(ESP, source.GetStackIndex()));
1225 __ popl(Address(ESP, destination.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001226 }
1227 }
1228}
1229
1230void CodeGeneratorX86::Move64(Location destination, Location source) {
1231 if (source.Equals(destination)) {
1232 return;
1233 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001234 if (destination.IsRegisterPair()) {
1235 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001236 EmitParallelMoves(
1237 Location::RegisterLocation(source.AsRegisterPairHigh<Register>()),
1238 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001239 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001240 Location::RegisterLocation(source.AsRegisterPairLow<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001241 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()),
1242 Primitive::kPrimInt);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001243 } else if (source.IsFpuRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001244 XmmRegister src_reg = source.AsFpuRegister<XmmRegister>();
1245 __ movd(destination.AsRegisterPairLow<Register>(), src_reg);
1246 __ psrlq(src_reg, Immediate(32));
1247 __ movd(destination.AsRegisterPairHigh<Register>(), src_reg);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001248 } else {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001249 // No conflict possible, so just do the moves.
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001250 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001251 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
1252 __ movl(destination.AsRegisterPairHigh<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001253 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
1254 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001255 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05001256 if (source.IsFpuRegister()) {
1257 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
1258 } else if (source.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001259 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01001260 } else if (source.IsRegisterPair()) {
1261 size_t elem_size = Primitive::ComponentSize(Primitive::kPrimInt);
1262 // Create stack space for 2 elements.
1263 __ subl(ESP, Immediate(2 * elem_size));
1264 __ movl(Address(ESP, 0), source.AsRegisterPairLow<Register>());
1265 __ movl(Address(ESP, elem_size), source.AsRegisterPairHigh<Register>());
1266 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
1267 // And remove the temporary stack space we allocated.
1268 __ addl(ESP, Immediate(2 * elem_size));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001269 } else {
1270 LOG(FATAL) << "Unimplemented";
1271 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001272 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001273 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001274 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001275 // No conflict possible, so just do the moves.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001276 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegisterPairLow<Register>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001277 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001278 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001279 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001280 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001281 } else if (source.IsConstant()) {
1282 HConstant* constant = source.GetConstant();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001283 DCHECK(constant->IsLongConstant() || constant->IsDoubleConstant());
1284 int64_t value = GetInt64ValueOf(constant);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001285 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(Low32Bits(value)));
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001286 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
1287 Immediate(High32Bits(value)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001288 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001289 DCHECK(source.IsDoubleStackSlot()) << source;
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001290 EmitParallelMoves(
1291 Location::StackSlot(source.GetStackIndex()),
1292 Location::StackSlot(destination.GetStackIndex()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001293 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001294 Location::StackSlot(source.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001295 Location::StackSlot(destination.GetHighStackIndex(kX86WordSize)),
1296 Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001297 }
1298 }
1299}
1300
Calin Juravle175dc732015-08-25 15:42:32 +01001301void CodeGeneratorX86::MoveConstant(Location location, int32_t value) {
1302 DCHECK(location.IsRegister());
1303 __ movl(location.AsRegister<Register>(), Immediate(value));
1304}
1305
Calin Juravlee460d1d2015-09-29 04:52:17 +01001306void CodeGeneratorX86::MoveLocation(Location dst, Location src, Primitive::Type dst_type) {
David Brazdil74eb1b22015-12-14 11:44:01 +00001307 HParallelMove move(GetGraph()->GetArena());
1308 if (dst_type == Primitive::kPrimLong && !src.IsConstant() && !src.IsFpuRegister()) {
1309 move.AddMove(src.ToLow(), dst.ToLow(), Primitive::kPrimInt, nullptr);
1310 move.AddMove(src.ToHigh(), dst.ToHigh(), Primitive::kPrimInt, nullptr);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001311 } else {
David Brazdil74eb1b22015-12-14 11:44:01 +00001312 move.AddMove(src, dst, dst_type, nullptr);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001313 }
David Brazdil74eb1b22015-12-14 11:44:01 +00001314 GetMoveResolver()->EmitNativeCode(&move);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001315}
1316
1317void CodeGeneratorX86::AddLocationAsTemp(Location location, LocationSummary* locations) {
1318 if (location.IsRegister()) {
1319 locations->AddTemp(location);
1320 } else if (location.IsRegisterPair()) {
1321 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
1322 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
1323 } else {
1324 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1325 }
1326}
1327
David Brazdilfc6a86a2015-06-26 10:33:45 +00001328void InstructionCodeGeneratorX86::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001329 DCHECK(!successor->IsExitBlock());
1330
1331 HBasicBlock* block = got->GetBlock();
1332 HInstruction* previous = got->GetPrevious();
1333
1334 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +00001335 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001336 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1337 return;
1338 }
1339
1340 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1341 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1342 }
1343 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001344 __ jmp(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001345 }
1346}
1347
David Brazdilfc6a86a2015-06-26 10:33:45 +00001348void LocationsBuilderX86::VisitGoto(HGoto* got) {
1349 got->SetLocations(nullptr);
1350}
1351
1352void InstructionCodeGeneratorX86::VisitGoto(HGoto* got) {
1353 HandleGoto(got, got->GetSuccessor());
1354}
1355
1356void LocationsBuilderX86::VisitTryBoundary(HTryBoundary* try_boundary) {
1357 try_boundary->SetLocations(nullptr);
1358}
1359
1360void InstructionCodeGeneratorX86::VisitTryBoundary(HTryBoundary* try_boundary) {
1361 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
1362 if (!successor->IsExitBlock()) {
1363 HandleGoto(try_boundary, successor);
1364 }
1365}
1366
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001367void LocationsBuilderX86::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001368 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001369}
1370
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001371void InstructionCodeGeneratorX86::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001372}
1373
Mark Mendell152408f2015-12-31 12:28:50 -05001374template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001375void InstructionCodeGeneratorX86::GenerateFPJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001376 LabelType* true_label,
1377 LabelType* false_label) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001378 if (cond->IsFPConditionTrueIfNaN()) {
1379 __ j(kUnordered, true_label);
1380 } else if (cond->IsFPConditionFalseIfNaN()) {
1381 __ j(kUnordered, false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001382 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001383 __ j(X86UnsignedOrFPCondition(cond->GetCondition()), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001384}
1385
Mark Mendell152408f2015-12-31 12:28:50 -05001386template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001387void InstructionCodeGeneratorX86::GenerateLongComparesAndJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001388 LabelType* true_label,
1389 LabelType* false_label) {
Mark Mendellc4701932015-04-10 13:18:51 -04001390 LocationSummary* locations = cond->GetLocations();
1391 Location left = locations->InAt(0);
1392 Location right = locations->InAt(1);
1393 IfCondition if_cond = cond->GetCondition();
1394
Mark Mendellc4701932015-04-10 13:18:51 -04001395 Register left_high = left.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001396 Register left_low = left.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001397 IfCondition true_high_cond = if_cond;
1398 IfCondition false_high_cond = cond->GetOppositeCondition();
Aart Bike9f37602015-10-09 11:15:55 -07001399 Condition final_condition = X86UnsignedOrFPCondition(if_cond); // unsigned on lower part
Mark Mendellc4701932015-04-10 13:18:51 -04001400
1401 // Set the conditions for the test, remembering that == needs to be
1402 // decided using the low words.
1403 switch (if_cond) {
1404 case kCondEQ:
Mark Mendellc4701932015-04-10 13:18:51 -04001405 case kCondNE:
Roland Levillain4fa13f62015-07-06 18:11:54 +01001406 // Nothing to do.
Mark Mendellc4701932015-04-10 13:18:51 -04001407 break;
1408 case kCondLT:
1409 false_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -04001410 break;
1411 case kCondLE:
1412 true_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -04001413 break;
1414 case kCondGT:
1415 false_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -04001416 break;
1417 case kCondGE:
1418 true_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -04001419 break;
Aart Bike9f37602015-10-09 11:15:55 -07001420 case kCondB:
1421 false_high_cond = kCondA;
1422 break;
1423 case kCondBE:
1424 true_high_cond = kCondB;
1425 break;
1426 case kCondA:
1427 false_high_cond = kCondB;
1428 break;
1429 case kCondAE:
1430 true_high_cond = kCondA;
1431 break;
Mark Mendellc4701932015-04-10 13:18:51 -04001432 }
1433
1434 if (right.IsConstant()) {
1435 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
Mark Mendellc4701932015-04-10 13:18:51 -04001436 int32_t val_high = High32Bits(value);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001437 int32_t val_low = Low32Bits(value);
Mark Mendellc4701932015-04-10 13:18:51 -04001438
Aart Bika19616e2016-02-01 18:57:58 -08001439 codegen_->Compare32BitValue(left_high, val_high);
Mark Mendellc4701932015-04-10 13:18:51 -04001440 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001441 __ j(X86Condition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001442 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001443 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001444 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001445 __ j(X86Condition(true_high_cond), true_label);
1446 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001447 }
1448 // Must be equal high, so compare the lows.
Aart Bika19616e2016-02-01 18:57:58 -08001449 codegen_->Compare32BitValue(left_low, val_low);
Mark Mendell8659e842016-02-16 10:41:46 -05001450 } else if (right.IsRegisterPair()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001451 Register right_high = right.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001452 Register right_low = right.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001453
1454 __ cmpl(left_high, right_high);
1455 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001456 __ j(X86Condition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001457 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001458 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001459 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001460 __ j(X86Condition(true_high_cond), true_label);
1461 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001462 }
1463 // Must be equal high, so compare the lows.
1464 __ cmpl(left_low, right_low);
Mark Mendell8659e842016-02-16 10:41:46 -05001465 } else {
1466 DCHECK(right.IsDoubleStackSlot());
1467 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
1468 if (if_cond == kCondNE) {
1469 __ j(X86Condition(true_high_cond), true_label);
1470 } else if (if_cond == kCondEQ) {
1471 __ j(X86Condition(false_high_cond), false_label);
1472 } else {
1473 __ j(X86Condition(true_high_cond), true_label);
1474 __ j(X86Condition(false_high_cond), false_label);
1475 }
1476 // Must be equal high, so compare the lows.
1477 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Mark Mendellc4701932015-04-10 13:18:51 -04001478 }
1479 // The last comparison might be unsigned.
1480 __ j(final_condition, true_label);
1481}
1482
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001483void InstructionCodeGeneratorX86::GenerateFPCompare(Location lhs,
1484 Location rhs,
1485 HInstruction* insn,
1486 bool is_double) {
1487 HX86LoadFromConstantTable* const_area = insn->InputAt(1)->AsX86LoadFromConstantTable();
1488 if (is_double) {
1489 if (rhs.IsFpuRegister()) {
1490 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1491 } else if (const_area != nullptr) {
1492 DCHECK(const_area->IsEmittedAtUseSite());
1493 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(),
1494 codegen_->LiteralDoubleAddress(
1495 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
1496 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
1497 } else {
1498 DCHECK(rhs.IsDoubleStackSlot());
1499 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(), Address(ESP, rhs.GetStackIndex()));
1500 }
1501 } else {
1502 if (rhs.IsFpuRegister()) {
1503 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1504 } else if (const_area != nullptr) {
1505 DCHECK(const_area->IsEmittedAtUseSite());
1506 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(),
1507 codegen_->LiteralFloatAddress(
1508 const_area->GetConstant()->AsFloatConstant()->GetValue(),
1509 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
1510 } else {
1511 DCHECK(rhs.IsStackSlot());
1512 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(), Address(ESP, rhs.GetStackIndex()));
1513 }
1514 }
1515}
1516
Mark Mendell152408f2015-12-31 12:28:50 -05001517template<class LabelType>
David Brazdil0debae72015-11-12 18:37:00 +00001518void InstructionCodeGeneratorX86::GenerateCompareTestAndBranch(HCondition* condition,
Mark Mendell152408f2015-12-31 12:28:50 -05001519 LabelType* true_target_in,
1520 LabelType* false_target_in) {
David Brazdil0debae72015-11-12 18:37:00 +00001521 // Generated branching requires both targets to be explicit. If either of the
1522 // targets is nullptr (fallthrough) use and bind `fallthrough_target` instead.
Mark Mendell152408f2015-12-31 12:28:50 -05001523 LabelType fallthrough_target;
1524 LabelType* true_target = true_target_in == nullptr ? &fallthrough_target : true_target_in;
1525 LabelType* false_target = false_target_in == nullptr ? &fallthrough_target : false_target_in;
David Brazdil0debae72015-11-12 18:37:00 +00001526
Mark Mendellc4701932015-04-10 13:18:51 -04001527 LocationSummary* locations = condition->GetLocations();
1528 Location left = locations->InAt(0);
1529 Location right = locations->InAt(1);
1530
Mark Mendellc4701932015-04-10 13:18:51 -04001531 Primitive::Type type = condition->InputAt(0)->GetType();
1532 switch (type) {
1533 case Primitive::kPrimLong:
1534 GenerateLongComparesAndJumps(condition, true_target, false_target);
1535 break;
1536 case Primitive::kPrimFloat:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001537 GenerateFPCompare(left, right, condition, false);
Mark Mendellc4701932015-04-10 13:18:51 -04001538 GenerateFPJumps(condition, true_target, false_target);
1539 break;
1540 case Primitive::kPrimDouble:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001541 GenerateFPCompare(left, right, condition, true);
Mark Mendellc4701932015-04-10 13:18:51 -04001542 GenerateFPJumps(condition, true_target, false_target);
1543 break;
1544 default:
1545 LOG(FATAL) << "Unexpected compare type " << type;
1546 }
1547
David Brazdil0debae72015-11-12 18:37:00 +00001548 if (false_target != &fallthrough_target) {
Mark Mendellc4701932015-04-10 13:18:51 -04001549 __ jmp(false_target);
1550 }
David Brazdil0debae72015-11-12 18:37:00 +00001551
1552 if (fallthrough_target.IsLinked()) {
1553 __ Bind(&fallthrough_target);
1554 }
Mark Mendellc4701932015-04-10 13:18:51 -04001555}
1556
David Brazdil0debae72015-11-12 18:37:00 +00001557static bool AreEflagsSetFrom(HInstruction* cond, HInstruction* branch) {
1558 // Moves may affect the eflags register (move zero uses xorl), so the EFLAGS
1559 // are set only strictly before `branch`. We can't use the eflags on long/FP
1560 // conditions if they are materialized due to the complex branching.
1561 return cond->IsCondition() &&
1562 cond->GetNext() == branch &&
1563 cond->InputAt(0)->GetType() != Primitive::kPrimLong &&
1564 !Primitive::IsFloatingPointType(cond->InputAt(0)->GetType());
1565}
1566
Mark Mendell152408f2015-12-31 12:28:50 -05001567template<class LabelType>
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001568void InstructionCodeGeneratorX86::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00001569 size_t condition_input_index,
Mark Mendell152408f2015-12-31 12:28:50 -05001570 LabelType* true_target,
1571 LabelType* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00001572 HInstruction* cond = instruction->InputAt(condition_input_index);
1573
1574 if (true_target == nullptr && false_target == nullptr) {
1575 // Nothing to do. The code always falls through.
1576 return;
1577 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00001578 // Constant condition, statically compared against "true" (integer value 1).
1579 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00001580 if (true_target != nullptr) {
1581 __ jmp(true_target);
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001582 }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001583 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00001584 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00001585 if (false_target != nullptr) {
1586 __ jmp(false_target);
1587 }
1588 }
1589 return;
1590 }
1591
1592 // The following code generates these patterns:
1593 // (1) true_target == nullptr && false_target != nullptr
1594 // - opposite condition true => branch to false_target
1595 // (2) true_target != nullptr && false_target == nullptr
1596 // - condition true => branch to true_target
1597 // (3) true_target != nullptr && false_target != nullptr
1598 // - condition true => branch to true_target
1599 // - branch to false_target
1600 if (IsBooleanValueOrMaterializedCondition(cond)) {
1601 if (AreEflagsSetFrom(cond, instruction)) {
1602 if (true_target == nullptr) {
1603 __ j(X86Condition(cond->AsCondition()->GetOppositeCondition()), false_target);
1604 } else {
1605 __ j(X86Condition(cond->AsCondition()->GetCondition()), true_target);
1606 }
1607 } else {
1608 // Materialized condition, compare against 0.
1609 Location lhs = instruction->GetLocations()->InAt(condition_input_index);
1610 if (lhs.IsRegister()) {
1611 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
1612 } else {
1613 __ cmpl(Address(ESP, lhs.GetStackIndex()), Immediate(0));
1614 }
1615 if (true_target == nullptr) {
1616 __ j(kEqual, false_target);
1617 } else {
1618 __ j(kNotEqual, true_target);
1619 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001620 }
1621 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001622 // Condition has not been materialized, use its inputs as the comparison and
1623 // its condition as the branch condition.
Mark Mendellb8b97692015-05-22 16:58:19 -04001624 HCondition* condition = cond->AsCondition();
David Brazdil0debae72015-11-12 18:37:00 +00001625
1626 // If this is a long or FP comparison that has been folded into
1627 // the HCondition, generate the comparison directly.
1628 Primitive::Type type = condition->InputAt(0)->GetType();
1629 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
1630 GenerateCompareTestAndBranch(condition, true_target, false_target);
1631 return;
1632 }
1633
1634 Location lhs = condition->GetLocations()->InAt(0);
1635 Location rhs = condition->GetLocations()->InAt(1);
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001636 // LHS is guaranteed to be in a register (see LocationsBuilderX86::HandleCondition).
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001637 codegen_->GenerateIntCompare(lhs, rhs);
David Brazdil0debae72015-11-12 18:37:00 +00001638 if (true_target == nullptr) {
1639 __ j(X86Condition(condition->GetOppositeCondition()), false_target);
1640 } else {
Mark Mendellb8b97692015-05-22 16:58:19 -04001641 __ j(X86Condition(condition->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -07001642 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001643 }
David Brazdil0debae72015-11-12 18:37:00 +00001644
1645 // If neither branch falls through (case 3), the conditional branch to `true_target`
1646 // was already emitted (case 2) and we need to emit a jump to `false_target`.
1647 if (true_target != nullptr && false_target != nullptr) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001648 __ jmp(false_target);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001649 }
1650}
1651
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001652void LocationsBuilderX86::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001653 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
1654 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001655 locations->SetInAt(0, Location::Any());
1656 }
1657}
1658
1659void InstructionCodeGeneratorX86::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001660 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
1661 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
1662 Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
1663 nullptr : codegen_->GetLabelOf(true_successor);
1664 Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
1665 nullptr : codegen_->GetLabelOf(false_successor);
1666 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001667}
1668
1669void LocationsBuilderX86::VisitDeoptimize(HDeoptimize* deoptimize) {
1670 LocationSummary* locations = new (GetGraph()->GetArena())
1671 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01001672 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
David Brazdil0debae72015-11-12 18:37:00 +00001673 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001674 locations->SetInAt(0, Location::Any());
1675 }
1676}
1677
1678void InstructionCodeGeneratorX86::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08001679 SlowPathCode* slow_path = deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathX86>(deoptimize);
David Brazdil74eb1b22015-12-14 11:44:01 +00001680 GenerateTestAndBranch<Label>(deoptimize,
1681 /* condition_input_index */ 0,
1682 slow_path->GetEntryLabel(),
1683 /* false_target */ nullptr);
1684}
1685
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001686static bool SelectCanUseCMOV(HSelect* select) {
1687 // There are no conditional move instructions for XMMs.
1688 if (Primitive::IsFloatingPointType(select->GetType())) {
1689 return false;
1690 }
1691
1692 // A FP condition doesn't generate the single CC that we need.
1693 // In 32 bit mode, a long condition doesn't generate a single CC either.
1694 HInstruction* condition = select->GetCondition();
1695 if (condition->IsCondition()) {
1696 Primitive::Type compare_type = condition->InputAt(0)->GetType();
1697 if (compare_type == Primitive::kPrimLong ||
1698 Primitive::IsFloatingPointType(compare_type)) {
1699 return false;
1700 }
1701 }
1702
1703 // We can generate a CMOV for this Select.
1704 return true;
1705}
1706
David Brazdil74eb1b22015-12-14 11:44:01 +00001707void LocationsBuilderX86::VisitSelect(HSelect* select) {
1708 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001709 if (Primitive::IsFloatingPointType(select->GetType())) {
David Brazdil74eb1b22015-12-14 11:44:01 +00001710 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001711 locations->SetInAt(1, Location::Any());
David Brazdil74eb1b22015-12-14 11:44:01 +00001712 } else {
1713 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001714 if (SelectCanUseCMOV(select)) {
1715 if (select->InputAt(1)->IsConstant()) {
1716 // Cmov can't handle a constant value.
1717 locations->SetInAt(1, Location::RequiresRegister());
1718 } else {
1719 locations->SetInAt(1, Location::Any());
1720 }
1721 } else {
1722 locations->SetInAt(1, Location::Any());
1723 }
David Brazdil74eb1b22015-12-14 11:44:01 +00001724 }
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001725 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
1726 locations->SetInAt(2, Location::RequiresRegister());
David Brazdil74eb1b22015-12-14 11:44:01 +00001727 }
1728 locations->SetOut(Location::SameAsFirstInput());
1729}
1730
1731void InstructionCodeGeneratorX86::VisitSelect(HSelect* select) {
1732 LocationSummary* locations = select->GetLocations();
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001733 DCHECK(locations->InAt(0).Equals(locations->Out()));
1734 if (SelectCanUseCMOV(select)) {
1735 // If both the condition and the source types are integer, we can generate
1736 // a CMOV to implement Select.
1737
1738 HInstruction* select_condition = select->GetCondition();
1739 Condition cond = kNotEqual;
1740
1741 // Figure out how to test the 'condition'.
1742 if (select_condition->IsCondition()) {
1743 HCondition* condition = select_condition->AsCondition();
1744 if (!condition->IsEmittedAtUseSite()) {
1745 // This was a previously materialized condition.
1746 // Can we use the existing condition code?
1747 if (AreEflagsSetFrom(condition, select)) {
1748 // Materialization was the previous instruction. Condition codes are right.
1749 cond = X86Condition(condition->GetCondition());
1750 } else {
1751 // No, we have to recreate the condition code.
1752 Register cond_reg = locations->InAt(2).AsRegister<Register>();
1753 __ testl(cond_reg, cond_reg);
1754 }
1755 } else {
1756 // We can't handle FP or long here.
1757 DCHECK_NE(condition->InputAt(0)->GetType(), Primitive::kPrimLong);
1758 DCHECK(!Primitive::IsFloatingPointType(condition->InputAt(0)->GetType()));
1759 LocationSummary* cond_locations = condition->GetLocations();
Roland Levillain0b671c02016-08-19 12:02:34 +01001760 codegen_->GenerateIntCompare(cond_locations->InAt(0), cond_locations->InAt(1));
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001761 cond = X86Condition(condition->GetCondition());
1762 }
1763 } else {
1764 // Must be a boolean condition, which needs to be compared to 0.
1765 Register cond_reg = locations->InAt(2).AsRegister<Register>();
1766 __ testl(cond_reg, cond_reg);
1767 }
1768
1769 // If the condition is true, overwrite the output, which already contains false.
1770 Location false_loc = locations->InAt(0);
1771 Location true_loc = locations->InAt(1);
1772 if (select->GetType() == Primitive::kPrimLong) {
1773 // 64 bit conditional move.
1774 Register false_high = false_loc.AsRegisterPairHigh<Register>();
1775 Register false_low = false_loc.AsRegisterPairLow<Register>();
1776 if (true_loc.IsRegisterPair()) {
1777 __ cmovl(cond, false_high, true_loc.AsRegisterPairHigh<Register>());
1778 __ cmovl(cond, false_low, true_loc.AsRegisterPairLow<Register>());
1779 } else {
1780 __ cmovl(cond, false_high, Address(ESP, true_loc.GetHighStackIndex(kX86WordSize)));
1781 __ cmovl(cond, false_low, Address(ESP, true_loc.GetStackIndex()));
1782 }
1783 } else {
1784 // 32 bit conditional move.
1785 Register false_reg = false_loc.AsRegister<Register>();
1786 if (true_loc.IsRegister()) {
1787 __ cmovl(cond, false_reg, true_loc.AsRegister<Register>());
1788 } else {
1789 __ cmovl(cond, false_reg, Address(ESP, true_loc.GetStackIndex()));
1790 }
1791 }
1792 } else {
1793 NearLabel false_target;
1794 GenerateTestAndBranch<NearLabel>(
1795 select, /* condition_input_index */ 2, /* true_target */ nullptr, &false_target);
1796 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
1797 __ Bind(&false_target);
1798 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001799}
1800
David Srbecky0cf44932015-12-09 14:09:59 +00001801void LocationsBuilderX86::VisitNativeDebugInfo(HNativeDebugInfo* info) {
1802 new (GetGraph()->GetArena()) LocationSummary(info);
1803}
1804
David Srbeckyd28f4a02016-03-14 17:14:24 +00001805void InstructionCodeGeneratorX86::VisitNativeDebugInfo(HNativeDebugInfo*) {
1806 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00001807}
1808
1809void CodeGeneratorX86::GenerateNop() {
1810 __ nop();
David Srbecky0cf44932015-12-09 14:09:59 +00001811}
1812
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001813void LocationsBuilderX86::HandleCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001814 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +01001815 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Mark Mendellc4701932015-04-10 13:18:51 -04001816 // Handle the long/FP comparisons made in instruction simplification.
1817 switch (cond->InputAt(0)->GetType()) {
1818 case Primitive::kPrimLong: {
1819 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell8659e842016-02-16 10:41:46 -05001820 locations->SetInAt(1, Location::Any());
David Brazdilb3e773e2016-01-26 11:28:37 +00001821 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001822 locations->SetOut(Location::RequiresRegister());
1823 }
1824 break;
1825 }
1826 case Primitive::kPrimFloat:
1827 case Primitive::kPrimDouble: {
1828 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001829 if (cond->InputAt(1)->IsX86LoadFromConstantTable()) {
1830 DCHECK(cond->InputAt(1)->IsEmittedAtUseSite());
1831 } else if (cond->InputAt(1)->IsConstant()) {
1832 locations->SetInAt(1, Location::RequiresFpuRegister());
1833 } else {
1834 locations->SetInAt(1, Location::Any());
1835 }
David Brazdilb3e773e2016-01-26 11:28:37 +00001836 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001837 locations->SetOut(Location::RequiresRegister());
1838 }
1839 break;
1840 }
1841 default:
1842 locations->SetInAt(0, Location::RequiresRegister());
1843 locations->SetInAt(1, Location::Any());
David Brazdilb3e773e2016-01-26 11:28:37 +00001844 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001845 // We need a byte register.
1846 locations->SetOut(Location::RegisterLocation(ECX));
1847 }
1848 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001849 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001850}
1851
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001852void InstructionCodeGeneratorX86::HandleCondition(HCondition* cond) {
David Brazdilb3e773e2016-01-26 11:28:37 +00001853 if (cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001854 return;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001855 }
Mark Mendellc4701932015-04-10 13:18:51 -04001856
1857 LocationSummary* locations = cond->GetLocations();
1858 Location lhs = locations->InAt(0);
1859 Location rhs = locations->InAt(1);
1860 Register reg = locations->Out().AsRegister<Register>();
Mark Mendell152408f2015-12-31 12:28:50 -05001861 NearLabel true_label, false_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001862
1863 switch (cond->InputAt(0)->GetType()) {
1864 default: {
1865 // Integer case.
1866
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01001867 // Clear output register: setb only sets the low byte.
Mark Mendellc4701932015-04-10 13:18:51 -04001868 __ xorl(reg, reg);
Roland Levillain0b671c02016-08-19 12:02:34 +01001869 codegen_->GenerateIntCompare(lhs, rhs);
Aart Bike9f37602015-10-09 11:15:55 -07001870 __ setb(X86Condition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001871 return;
1872 }
1873 case Primitive::kPrimLong:
1874 GenerateLongComparesAndJumps(cond, &true_label, &false_label);
1875 break;
1876 case Primitive::kPrimFloat:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001877 GenerateFPCompare(lhs, rhs, cond, false);
Mark Mendellc4701932015-04-10 13:18:51 -04001878 GenerateFPJumps(cond, &true_label, &false_label);
1879 break;
1880 case Primitive::kPrimDouble:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001881 GenerateFPCompare(lhs, rhs, cond, true);
Mark Mendellc4701932015-04-10 13:18:51 -04001882 GenerateFPJumps(cond, &true_label, &false_label);
1883 break;
1884 }
1885
1886 // Convert the jumps into the result.
Mark Mendell0c9497d2015-08-21 09:30:05 -04001887 NearLabel done_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001888
Roland Levillain4fa13f62015-07-06 18:11:54 +01001889 // False case: result = 0.
Mark Mendellc4701932015-04-10 13:18:51 -04001890 __ Bind(&false_label);
1891 __ xorl(reg, reg);
1892 __ jmp(&done_label);
1893
Roland Levillain4fa13f62015-07-06 18:11:54 +01001894 // True case: result = 1.
Mark Mendellc4701932015-04-10 13:18:51 -04001895 __ Bind(&true_label);
1896 __ movl(reg, Immediate(1));
1897 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07001898}
1899
1900void LocationsBuilderX86::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001901 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001902}
1903
1904void InstructionCodeGeneratorX86::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001905 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001906}
1907
1908void LocationsBuilderX86::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001909 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001910}
1911
1912void InstructionCodeGeneratorX86::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001913 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001914}
1915
1916void LocationsBuilderX86::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001917 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001918}
1919
1920void InstructionCodeGeneratorX86::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001921 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001922}
1923
1924void LocationsBuilderX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001925 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001926}
1927
1928void InstructionCodeGeneratorX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001929 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001930}
1931
1932void LocationsBuilderX86::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001933 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001934}
1935
1936void InstructionCodeGeneratorX86::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001937 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001938}
1939
1940void LocationsBuilderX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001941 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001942}
1943
1944void InstructionCodeGeneratorX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001945 HandleCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001946}
1947
Aart Bike9f37602015-10-09 11:15:55 -07001948void LocationsBuilderX86::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001949 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001950}
1951
1952void InstructionCodeGeneratorX86::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001953 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001954}
1955
1956void LocationsBuilderX86::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001957 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001958}
1959
1960void InstructionCodeGeneratorX86::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001961 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001962}
1963
1964void LocationsBuilderX86::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001965 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001966}
1967
1968void InstructionCodeGeneratorX86::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001969 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001970}
1971
1972void LocationsBuilderX86::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001973 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001974}
1975
1976void InstructionCodeGeneratorX86::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001977 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001978}
1979
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001980void LocationsBuilderX86::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001981 LocationSummary* locations =
1982 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001983 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001984}
1985
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001986void InstructionCodeGeneratorX86::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001987 // Will be generated at use site.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001988}
1989
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001990void LocationsBuilderX86::VisitNullConstant(HNullConstant* constant) {
1991 LocationSummary* locations =
1992 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1993 locations->SetOut(Location::ConstantLocation(constant));
1994}
1995
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001996void InstructionCodeGeneratorX86::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001997 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001998}
1999
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002000void LocationsBuilderX86::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002001 LocationSummary* locations =
2002 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002003 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002004}
2005
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002006void InstructionCodeGeneratorX86::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002007 // Will be generated at use site.
2008}
2009
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002010void LocationsBuilderX86::VisitFloatConstant(HFloatConstant* constant) {
2011 LocationSummary* locations =
2012 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2013 locations->SetOut(Location::ConstantLocation(constant));
2014}
2015
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002016void InstructionCodeGeneratorX86::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002017 // Will be generated at use site.
2018}
2019
2020void LocationsBuilderX86::VisitDoubleConstant(HDoubleConstant* constant) {
2021 LocationSummary* locations =
2022 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2023 locations->SetOut(Location::ConstantLocation(constant));
2024}
2025
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002026void InstructionCodeGeneratorX86::VisitDoubleConstant(HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002027 // Will be generated at use site.
2028}
2029
Calin Juravle27df7582015-04-17 19:12:31 +01002030void LocationsBuilderX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
2031 memory_barrier->SetLocations(nullptr);
2032}
2033
2034void InstructionCodeGeneratorX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00002035 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01002036}
2037
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002038void LocationsBuilderX86::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002039 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002040}
2041
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002042void InstructionCodeGeneratorX86::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002043 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002044}
2045
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002046void LocationsBuilderX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002047 LocationSummary* locations =
2048 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002049 switch (ret->InputAt(0)->GetType()) {
2050 case Primitive::kPrimBoolean:
2051 case Primitive::kPrimByte:
2052 case Primitive::kPrimChar:
2053 case Primitive::kPrimShort:
2054 case Primitive::kPrimInt:
2055 case Primitive::kPrimNot:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002056 locations->SetInAt(0, Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002057 break;
2058
2059 case Primitive::kPrimLong:
2060 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002061 0, Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002062 break;
2063
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002064 case Primitive::kPrimFloat:
2065 case Primitive::kPrimDouble:
2066 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002067 0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002068 break;
2069
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002070 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002071 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002072 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002073}
2074
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002075void InstructionCodeGeneratorX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002076 if (kIsDebugBuild) {
2077 switch (ret->InputAt(0)->GetType()) {
2078 case Primitive::kPrimBoolean:
2079 case Primitive::kPrimByte:
2080 case Primitive::kPrimChar:
2081 case Primitive::kPrimShort:
2082 case Primitive::kPrimInt:
2083 case Primitive::kPrimNot:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002084 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<Register>(), EAX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002085 break;
2086
2087 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002088 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairLow<Register>(), EAX);
2089 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairHigh<Register>(), EDX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002090 break;
2091
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002092 case Primitive::kPrimFloat:
2093 case Primitive::kPrimDouble:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002094 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>(), XMM0);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002095 break;
2096
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002097 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002098 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002099 }
2100 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002101 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002102}
2103
Calin Juravle175dc732015-08-25 15:42:32 +01002104void LocationsBuilderX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2105 // The trampoline uses the same calling convention as dex calling conventions,
2106 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
2107 // the method_idx.
2108 HandleInvoke(invoke);
2109}
2110
2111void InstructionCodeGeneratorX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2112 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
2113}
2114
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002115void LocationsBuilderX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002116 // Explicit clinit checks triggered by static invokes must have been pruned by
2117 // art::PrepareForRegisterAllocation.
2118 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002119
Mark Mendellfb8d2792015-03-31 22:16:59 -04002120 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002121 if (intrinsic.TryDispatch(invoke)) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00002122 if (invoke->GetLocations()->CanCall() && invoke->HasPcRelativeDexCache()) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00002123 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00002124 }
Mark Mendell09ed1a32015-03-25 08:30:06 -04002125 return;
2126 }
2127
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002128 HandleInvoke(invoke);
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002129
Vladimir Marko0f7dca42015-11-02 14:36:43 +00002130 // For PC-relative dex cache the invoke has an extra input, the PC-relative address base.
2131 if (invoke->HasPcRelativeDexCache()) {
Vladimir Markob4536b72015-11-24 13:45:23 +00002132 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00002133 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002134}
2135
Mark Mendell09ed1a32015-03-25 08:30:06 -04002136static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86* codegen) {
2137 if (invoke->GetLocations()->Intrinsified()) {
2138 IntrinsicCodeGeneratorX86 intrinsic(codegen);
2139 intrinsic.Dispatch(invoke);
2140 return true;
2141 }
2142 return false;
2143}
2144
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002145void InstructionCodeGeneratorX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002146 // Explicit clinit checks triggered by static invokes must have been pruned by
2147 // art::PrepareForRegisterAllocation.
2148 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002149
Mark Mendell09ed1a32015-03-25 08:30:06 -04002150 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2151 return;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002152 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002153
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002154 LocationSummary* locations = invoke->GetLocations();
Mark Mendell09ed1a32015-03-25 08:30:06 -04002155 codegen_->GenerateStaticOrDirectCall(
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002156 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Mingyao Yang8693fe12015-04-17 16:51:08 -07002157 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002158}
2159
2160void LocationsBuilderX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00002161 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
2162 if (intrinsic.TryDispatch(invoke)) {
2163 return;
2164 }
2165
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002166 HandleInvoke(invoke);
2167}
2168
2169void LocationsBuilderX86::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002170 InvokeDexCallingConventionVisitorX86 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002171 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002172}
2173
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002174void InstructionCodeGeneratorX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002175 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2176 return;
2177 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002178
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002179 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002180 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01002181 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002182}
2183
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002184void LocationsBuilderX86::VisitInvokeInterface(HInvokeInterface* invoke) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002185 // This call to HandleInvoke allocates a temporary (core) register
2186 // which is also used to transfer the hidden argument from FP to
2187 // core register.
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002188 HandleInvoke(invoke);
2189 // Add the hidden argument.
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002190 invoke->GetLocations()->AddTemp(Location::FpuRegisterLocation(XMM7));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002191}
2192
2193void InstructionCodeGeneratorX86::VisitInvokeInterface(HInvokeInterface* invoke) {
2194 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain0d5a2812015-11-13 10:07:31 +00002195 LocationSummary* locations = invoke->GetLocations();
2196 Register temp = locations->GetTemp(0).AsRegister<Register>();
2197 XmmRegister hidden_reg = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002198 Location receiver = locations->InAt(0);
2199 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2200
Roland Levillain0d5a2812015-11-13 10:07:31 +00002201 // Set the hidden argument. This is safe to do this here, as XMM7
2202 // won't be modified thereafter, before the `call` instruction.
2203 DCHECK_EQ(XMM7, hidden_reg);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002204 __ movl(temp, Immediate(invoke->GetDexMethodIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002205 __ movd(hidden_reg, temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002206
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002207 if (receiver.IsStackSlot()) {
2208 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002209 // /* HeapReference<Class> */ temp = temp->klass_
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002210 __ movl(temp, Address(temp, class_offset));
2211 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002212 // /* HeapReference<Class> */ temp = receiver->klass_
Roland Levillain271ab9c2014-11-27 15:23:57 +00002213 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002214 }
Roland Levillain4d027112015-07-01 15:41:14 +01002215 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00002216 // Instead of simply (possibly) unpoisoning `temp` here, we should
2217 // emit a read barrier for the previous class reference load.
2218 // However this is not required in practice, as this is an
2219 // intermediate/temporary reference and because the current
2220 // concurrent copying collector keeps the from-space memory
2221 // intact/accessible until the end of the marking phase (the
2222 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01002223 __ MaybeUnpoisonHeapReference(temp);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002224 // temp = temp->GetAddressOfIMT()
2225 __ movl(temp,
2226 Address(temp, mirror::Class::ImtPtrOffset(kX86PointerSize).Uint32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002227 // temp = temp->GetImtEntryAt(method_offset);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002228 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00002229 invoke->GetImtIndex(), kX86PointerSize));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002230 __ movl(temp, Address(temp, method_offset));
2231 // call temp->GetEntryPoint();
Roland Levillain0d5a2812015-11-13 10:07:31 +00002232 __ call(Address(temp,
Andreas Gampe542451c2016-07-26 09:02:02 -07002233 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86PointerSize).Int32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002234
2235 DCHECK(!codegen_->IsLeafMethod());
2236 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2237}
2238
Roland Levillain88cb1752014-10-20 16:36:47 +01002239void LocationsBuilderX86::VisitNeg(HNeg* neg) {
2240 LocationSummary* locations =
2241 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2242 switch (neg->GetResultType()) {
2243 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002244 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01002245 locations->SetInAt(0, Location::RequiresRegister());
2246 locations->SetOut(Location::SameAsFirstInput());
2247 break;
2248
Roland Levillain88cb1752014-10-20 16:36:47 +01002249 case Primitive::kPrimFloat:
Roland Levillain5368c212014-11-27 15:03:41 +00002250 locations->SetInAt(0, Location::RequiresFpuRegister());
2251 locations->SetOut(Location::SameAsFirstInput());
2252 locations->AddTemp(Location::RequiresRegister());
2253 locations->AddTemp(Location::RequiresFpuRegister());
2254 break;
2255
Roland Levillain88cb1752014-10-20 16:36:47 +01002256 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002257 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00002258 locations->SetOut(Location::SameAsFirstInput());
2259 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01002260 break;
2261
2262 default:
2263 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2264 }
2265}
2266
2267void InstructionCodeGeneratorX86::VisitNeg(HNeg* neg) {
2268 LocationSummary* locations = neg->GetLocations();
2269 Location out = locations->Out();
2270 Location in = locations->InAt(0);
2271 switch (neg->GetResultType()) {
2272 case Primitive::kPrimInt:
2273 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002274 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002275 __ negl(out.AsRegister<Register>());
Roland Levillain88cb1752014-10-20 16:36:47 +01002276 break;
2277
2278 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002279 DCHECK(in.IsRegisterPair());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002280 DCHECK(in.Equals(out));
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002281 __ negl(out.AsRegisterPairLow<Register>());
2282 // Negation is similar to subtraction from zero. The least
2283 // significant byte triggers a borrow when it is different from
2284 // zero; to take it into account, add 1 to the most significant
2285 // byte if the carry flag (CF) is set to 1 after the first NEGL
2286 // operation.
2287 __ adcl(out.AsRegisterPairHigh<Register>(), Immediate(0));
2288 __ negl(out.AsRegisterPairHigh<Register>());
2289 break;
2290
Roland Levillain5368c212014-11-27 15:03:41 +00002291 case Primitive::kPrimFloat: {
2292 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002293 Register constant = locations->GetTemp(0).AsRegister<Register>();
2294 XmmRegister mask = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002295 // Implement float negation with an exclusive or with value
2296 // 0x80000000 (mask for bit 31, representing the sign of a
2297 // single-precision floating-point number).
2298 __ movl(constant, Immediate(INT32_C(0x80000000)));
2299 __ movd(mask, constant);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002300 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00002301 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002302 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00002303
Roland Levillain5368c212014-11-27 15:03:41 +00002304 case Primitive::kPrimDouble: {
2305 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002306 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002307 // Implement double negation with an exclusive or with value
2308 // 0x8000000000000000 (mask for bit 63, representing the sign of
2309 // a double-precision floating-point number).
2310 __ LoadLongConstant(mask, INT64_C(0x8000000000000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002311 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01002312 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002313 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002314
2315 default:
2316 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2317 }
2318}
2319
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002320void LocationsBuilderX86::VisitX86FPNeg(HX86FPNeg* neg) {
2321 LocationSummary* locations =
2322 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2323 DCHECK(Primitive::IsFloatingPointType(neg->GetType()));
2324 locations->SetInAt(0, Location::RequiresFpuRegister());
2325 locations->SetInAt(1, Location::RequiresRegister());
2326 locations->SetOut(Location::SameAsFirstInput());
2327 locations->AddTemp(Location::RequiresFpuRegister());
2328}
2329
2330void InstructionCodeGeneratorX86::VisitX86FPNeg(HX86FPNeg* neg) {
2331 LocationSummary* locations = neg->GetLocations();
2332 Location out = locations->Out();
2333 DCHECK(locations->InAt(0).Equals(out));
2334
2335 Register constant_area = locations->InAt(1).AsRegister<Register>();
2336 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
2337 if (neg->GetType() == Primitive::kPrimFloat) {
2338 __ movss(mask, codegen_->LiteralInt32Address(INT32_C(0x80000000), constant_area));
2339 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
2340 } else {
2341 __ movsd(mask, codegen_->LiteralInt64Address(INT64_C(0x8000000000000000), constant_area));
2342 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
2343 }
2344}
2345
Roland Levillaindff1f282014-11-05 14:15:05 +00002346void LocationsBuilderX86::VisitTypeConversion(HTypeConversion* conversion) {
Roland Levillaindff1f282014-11-05 14:15:05 +00002347 Primitive::Type result_type = conversion->GetResultType();
2348 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002349 DCHECK_NE(result_type, input_type);
Roland Levillain624279f2014-12-04 11:54:28 +00002350
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002351 // The float-to-long and double-to-long type conversions rely on a
2352 // call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00002353 LocationSummary::CallKind call_kind =
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002354 ((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
2355 && result_type == Primitive::kPrimLong)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01002356 ? LocationSummary::kCallOnMainOnly
Roland Levillain624279f2014-12-04 11:54:28 +00002357 : LocationSummary::kNoCall;
2358 LocationSummary* locations =
2359 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
2360
David Brazdilb2bd1c52015-03-25 11:17:37 +00002361 // The Java language does not allow treating boolean as an integral type but
2362 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00002363
Roland Levillaindff1f282014-11-05 14:15:05 +00002364 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002365 case Primitive::kPrimByte:
2366 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002367 case Primitive::kPrimLong: {
2368 // Type conversion from long to byte is a result of code transformations.
2369 HInstruction* input = conversion->InputAt(0);
2370 Location input_location = input->IsConstant()
2371 ? Location::ConstantLocation(input->AsConstant())
2372 : Location::RegisterPairLocation(EAX, EDX);
2373 locations->SetInAt(0, input_location);
2374 // Make the output overlap to please the register allocator. This greatly simplifies
2375 // the validation of the linear scan implementation
2376 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2377 break;
2378 }
David Brazdil46e2a392015-03-16 17:31:52 +00002379 case Primitive::kPrimBoolean:
2380 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002381 case Primitive::kPrimShort:
2382 case Primitive::kPrimInt:
2383 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002384 // Processing a Dex `int-to-byte' instruction.
Mark Mendell5f874182015-03-04 15:42:45 -05002385 locations->SetInAt(0, Location::ByteRegisterOrConstant(ECX, conversion->InputAt(0)));
2386 // Make the output overlap to please the register allocator. This greatly simplifies
2387 // the validation of the linear scan implementation
2388 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Roland Levillain51d3fc42014-11-13 14:11:42 +00002389 break;
2390
2391 default:
2392 LOG(FATAL) << "Unexpected type conversion from " << input_type
2393 << " to " << result_type;
2394 }
2395 break;
2396
Roland Levillain01a8d712014-11-14 16:27:39 +00002397 case Primitive::kPrimShort:
2398 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002399 case Primitive::kPrimLong:
2400 // Type conversion from long to short is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002401 case Primitive::kPrimBoolean:
2402 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002403 case Primitive::kPrimByte:
2404 case Primitive::kPrimInt:
2405 case Primitive::kPrimChar:
2406 // Processing a Dex `int-to-short' instruction.
2407 locations->SetInAt(0, Location::Any());
2408 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2409 break;
2410
2411 default:
2412 LOG(FATAL) << "Unexpected type conversion from " << input_type
2413 << " to " << result_type;
2414 }
2415 break;
2416
Roland Levillain946e1432014-11-11 17:35:19 +00002417 case Primitive::kPrimInt:
2418 switch (input_type) {
2419 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002420 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002421 locations->SetInAt(0, Location::Any());
2422 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2423 break;
2424
2425 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00002426 // Processing a Dex `float-to-int' instruction.
2427 locations->SetInAt(0, Location::RequiresFpuRegister());
2428 locations->SetOut(Location::RequiresRegister());
2429 locations->AddTemp(Location::RequiresFpuRegister());
2430 break;
2431
Roland Levillain946e1432014-11-11 17:35:19 +00002432 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002433 // Processing a Dex `double-to-int' instruction.
2434 locations->SetInAt(0, Location::RequiresFpuRegister());
2435 locations->SetOut(Location::RequiresRegister());
2436 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00002437 break;
2438
2439 default:
2440 LOG(FATAL) << "Unexpected type conversion from " << input_type
2441 << " to " << result_type;
2442 }
2443 break;
2444
Roland Levillaindff1f282014-11-05 14:15:05 +00002445 case Primitive::kPrimLong:
2446 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002447 case Primitive::kPrimBoolean:
2448 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002449 case Primitive::kPrimByte:
2450 case Primitive::kPrimShort:
2451 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002452 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002453 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002454 locations->SetInAt(0, Location::RegisterLocation(EAX));
2455 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
2456 break;
2457
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002458 case Primitive::kPrimFloat:
Vladimir Marko949c91f2015-01-27 10:48:44 +00002459 case Primitive::kPrimDouble: {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002460 // Processing a Dex `float-to-long' or 'double-to-long' instruction.
Vladimir Marko949c91f2015-01-27 10:48:44 +00002461 InvokeRuntimeCallingConvention calling_convention;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002462 XmmRegister parameter = calling_convention.GetFpuRegisterAt(0);
2463 locations->SetInAt(0, Location::FpuRegisterLocation(parameter));
2464
Vladimir Marko949c91f2015-01-27 10:48:44 +00002465 // The runtime helper puts the result in EAX, EDX.
2466 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Vladimir Marko949c91f2015-01-27 10:48:44 +00002467 }
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002468 break;
Roland Levillaindff1f282014-11-05 14:15:05 +00002469
2470 default:
2471 LOG(FATAL) << "Unexpected type conversion from " << input_type
2472 << " to " << result_type;
2473 }
2474 break;
2475
Roland Levillain981e4542014-11-14 11:47:14 +00002476 case Primitive::kPrimChar:
2477 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002478 case Primitive::kPrimLong:
2479 // Type conversion from long to char is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002480 case Primitive::kPrimBoolean:
2481 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002482 case Primitive::kPrimByte:
2483 case Primitive::kPrimShort:
2484 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002485 // Processing a Dex `int-to-char' instruction.
2486 locations->SetInAt(0, Location::Any());
2487 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2488 break;
2489
2490 default:
2491 LOG(FATAL) << "Unexpected type conversion from " << input_type
2492 << " to " << result_type;
2493 }
2494 break;
2495
Roland Levillaindff1f282014-11-05 14:15:05 +00002496 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002497 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002498 case Primitive::kPrimBoolean:
2499 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002500 case Primitive::kPrimByte:
2501 case Primitive::kPrimShort:
2502 case Primitive::kPrimInt:
2503 case Primitive::kPrimChar:
2504 // Processing a Dex `int-to-float' instruction.
2505 locations->SetInAt(0, Location::RequiresRegister());
2506 locations->SetOut(Location::RequiresFpuRegister());
2507 break;
2508
2509 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002510 // Processing a Dex `long-to-float' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002511 locations->SetInAt(0, Location::Any());
2512 locations->SetOut(Location::Any());
Roland Levillain6d0e4832014-11-27 18:31:21 +00002513 break;
2514
Roland Levillaincff13742014-11-17 14:32:17 +00002515 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002516 // Processing a Dex `double-to-float' instruction.
2517 locations->SetInAt(0, Location::RequiresFpuRegister());
2518 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002519 break;
2520
2521 default:
2522 LOG(FATAL) << "Unexpected type conversion from " << input_type
2523 << " to " << result_type;
2524 };
2525 break;
2526
Roland Levillaindff1f282014-11-05 14:15:05 +00002527 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002528 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002529 case Primitive::kPrimBoolean:
2530 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002531 case Primitive::kPrimByte:
2532 case Primitive::kPrimShort:
2533 case Primitive::kPrimInt:
2534 case Primitive::kPrimChar:
2535 // Processing a Dex `int-to-double' instruction.
2536 locations->SetInAt(0, Location::RequiresRegister());
2537 locations->SetOut(Location::RequiresFpuRegister());
2538 break;
2539
2540 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002541 // Processing a Dex `long-to-double' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002542 locations->SetInAt(0, Location::Any());
2543 locations->SetOut(Location::Any());
Roland Levillain647b9ed2014-11-27 12:06:00 +00002544 break;
2545
Roland Levillaincff13742014-11-17 14:32:17 +00002546 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002547 // Processing a Dex `float-to-double' instruction.
2548 locations->SetInAt(0, Location::RequiresFpuRegister());
2549 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002550 break;
2551
2552 default:
2553 LOG(FATAL) << "Unexpected type conversion from " << input_type
2554 << " to " << result_type;
2555 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002556 break;
2557
2558 default:
2559 LOG(FATAL) << "Unexpected type conversion from " << input_type
2560 << " to " << result_type;
2561 }
2562}
2563
2564void InstructionCodeGeneratorX86::VisitTypeConversion(HTypeConversion* conversion) {
2565 LocationSummary* locations = conversion->GetLocations();
2566 Location out = locations->Out();
2567 Location in = locations->InAt(0);
2568 Primitive::Type result_type = conversion->GetResultType();
2569 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002570 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00002571 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002572 case Primitive::kPrimByte:
2573 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002574 case Primitive::kPrimLong:
2575 // Type conversion from long to byte is a result of code transformations.
2576 if (in.IsRegisterPair()) {
2577 __ movsxb(out.AsRegister<Register>(), in.AsRegisterPairLow<ByteRegister>());
2578 } else {
2579 DCHECK(in.GetConstant()->IsLongConstant());
2580 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2581 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
2582 }
2583 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002584 case Primitive::kPrimBoolean:
2585 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002586 case Primitive::kPrimShort:
2587 case Primitive::kPrimInt:
2588 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002589 // Processing a Dex `int-to-byte' instruction.
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002590 if (in.IsRegister()) {
2591 __ movsxb(out.AsRegister<Register>(), in.AsRegister<ByteRegister>());
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002592 } else {
2593 DCHECK(in.GetConstant()->IsIntConstant());
2594 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
2595 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
2596 }
Roland Levillain51d3fc42014-11-13 14:11:42 +00002597 break;
2598
2599 default:
2600 LOG(FATAL) << "Unexpected type conversion from " << input_type
2601 << " to " << result_type;
2602 }
2603 break;
2604
Roland Levillain01a8d712014-11-14 16:27:39 +00002605 case Primitive::kPrimShort:
2606 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002607 case Primitive::kPrimLong:
2608 // Type conversion from long to short is a result of code transformations.
2609 if (in.IsRegisterPair()) {
2610 __ movsxw(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
2611 } else if (in.IsDoubleStackSlot()) {
2612 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
2613 } else {
2614 DCHECK(in.GetConstant()->IsLongConstant());
2615 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2616 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
2617 }
2618 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002619 case Primitive::kPrimBoolean:
2620 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002621 case Primitive::kPrimByte:
2622 case Primitive::kPrimInt:
2623 case Primitive::kPrimChar:
2624 // Processing a Dex `int-to-short' instruction.
2625 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002626 __ movsxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain01a8d712014-11-14 16:27:39 +00002627 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002628 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain01a8d712014-11-14 16:27:39 +00002629 } else {
2630 DCHECK(in.GetConstant()->IsIntConstant());
2631 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002632 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
Roland Levillain01a8d712014-11-14 16:27:39 +00002633 }
2634 break;
2635
2636 default:
2637 LOG(FATAL) << "Unexpected type conversion from " << input_type
2638 << " to " << result_type;
2639 }
2640 break;
2641
Roland Levillain946e1432014-11-11 17:35:19 +00002642 case Primitive::kPrimInt:
2643 switch (input_type) {
2644 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002645 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002646 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002647 __ movl(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00002648 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002649 __ movl(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain946e1432014-11-11 17:35:19 +00002650 } else {
2651 DCHECK(in.IsConstant());
2652 DCHECK(in.GetConstant()->IsLongConstant());
2653 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002654 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00002655 }
2656 break;
2657
Roland Levillain3f8f9362014-12-02 17:45:01 +00002658 case Primitive::kPrimFloat: {
2659 // Processing a Dex `float-to-int' instruction.
2660 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2661 Register output = out.AsRegister<Register>();
2662 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002663 NearLabel done, nan;
Roland Levillain3f8f9362014-12-02 17:45:01 +00002664
2665 __ movl(output, Immediate(kPrimIntMax));
2666 // temp = int-to-float(output)
2667 __ cvtsi2ss(temp, output);
2668 // if input >= temp goto done
2669 __ comiss(input, temp);
2670 __ j(kAboveEqual, &done);
2671 // if input == NaN goto nan
2672 __ j(kUnordered, &nan);
2673 // output = float-to-int-truncate(input)
2674 __ cvttss2si(output, input);
2675 __ jmp(&done);
2676 __ Bind(&nan);
2677 // output = 0
2678 __ xorl(output, output);
2679 __ Bind(&done);
2680 break;
2681 }
2682
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002683 case Primitive::kPrimDouble: {
2684 // Processing a Dex `double-to-int' instruction.
2685 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2686 Register output = out.AsRegister<Register>();
2687 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002688 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002689
2690 __ movl(output, Immediate(kPrimIntMax));
2691 // temp = int-to-double(output)
2692 __ cvtsi2sd(temp, output);
2693 // if input >= temp goto done
2694 __ comisd(input, temp);
2695 __ j(kAboveEqual, &done);
2696 // if input == NaN goto nan
2697 __ j(kUnordered, &nan);
2698 // output = double-to-int-truncate(input)
2699 __ cvttsd2si(output, input);
2700 __ jmp(&done);
2701 __ Bind(&nan);
2702 // output = 0
2703 __ xorl(output, output);
2704 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00002705 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002706 }
Roland Levillain946e1432014-11-11 17:35:19 +00002707
2708 default:
2709 LOG(FATAL) << "Unexpected type conversion from " << input_type
2710 << " to " << result_type;
2711 }
2712 break;
2713
Roland Levillaindff1f282014-11-05 14:15:05 +00002714 case Primitive::kPrimLong:
2715 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002716 case Primitive::kPrimBoolean:
2717 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002718 case Primitive::kPrimByte:
2719 case Primitive::kPrimShort:
2720 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002721 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002722 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002723 DCHECK_EQ(out.AsRegisterPairLow<Register>(), EAX);
2724 DCHECK_EQ(out.AsRegisterPairHigh<Register>(), EDX);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002725 DCHECK_EQ(in.AsRegister<Register>(), EAX);
Roland Levillaindff1f282014-11-05 14:15:05 +00002726 __ cdq();
2727 break;
2728
2729 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00002730 // Processing a Dex `float-to-long' instruction.
Serban Constantinescuba45db02016-07-12 22:53:02 +01002731 codegen_->InvokeRuntime(kQuickF2l, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00002732 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
Roland Levillain624279f2014-12-04 11:54:28 +00002733 break;
2734
Roland Levillaindff1f282014-11-05 14:15:05 +00002735 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002736 // Processing a Dex `double-to-long' instruction.
Serban Constantinescuba45db02016-07-12 22:53:02 +01002737 codegen_->InvokeRuntime(kQuickD2l, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00002738 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
Roland Levillaindff1f282014-11-05 14:15:05 +00002739 break;
2740
2741 default:
2742 LOG(FATAL) << "Unexpected type conversion from " << input_type
2743 << " to " << result_type;
2744 }
2745 break;
2746
Roland Levillain981e4542014-11-14 11:47:14 +00002747 case Primitive::kPrimChar:
2748 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002749 case Primitive::kPrimLong:
2750 // Type conversion from long to short is a result of code transformations.
2751 if (in.IsRegisterPair()) {
2752 __ movzxw(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
2753 } else if (in.IsDoubleStackSlot()) {
2754 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
2755 } else {
2756 DCHECK(in.GetConstant()->IsLongConstant());
2757 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2758 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
2759 }
2760 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002761 case Primitive::kPrimBoolean:
2762 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002763 case Primitive::kPrimByte:
2764 case Primitive::kPrimShort:
2765 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002766 // Processing a Dex `Process a Dex `int-to-char'' instruction.
2767 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002768 __ movzxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain981e4542014-11-14 11:47:14 +00002769 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002770 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain981e4542014-11-14 11:47:14 +00002771 } else {
2772 DCHECK(in.GetConstant()->IsIntConstant());
2773 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002774 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
Roland Levillain981e4542014-11-14 11:47:14 +00002775 }
2776 break;
2777
2778 default:
2779 LOG(FATAL) << "Unexpected type conversion from " << input_type
2780 << " to " << result_type;
2781 }
2782 break;
2783
Roland Levillaindff1f282014-11-05 14:15:05 +00002784 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002785 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002786 case Primitive::kPrimBoolean:
2787 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002788 case Primitive::kPrimByte:
2789 case Primitive::kPrimShort:
2790 case Primitive::kPrimInt:
2791 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002792 // Processing a Dex `int-to-float' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002793 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002794 break;
2795
Roland Levillain6d0e4832014-11-27 18:31:21 +00002796 case Primitive::kPrimLong: {
2797 // Processing a Dex `long-to-float' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002798 size_t adjustment = 0;
Roland Levillain6d0e4832014-11-27 18:31:21 +00002799
Roland Levillain232ade02015-04-20 15:14:36 +01002800 // Create stack space for the call to
2801 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstps below.
2802 // TODO: enhance register allocator to ask for stack temporaries.
2803 if (!in.IsDoubleStackSlot() || !out.IsStackSlot()) {
2804 adjustment = Primitive::ComponentSize(Primitive::kPrimLong);
2805 __ subl(ESP, Immediate(adjustment));
2806 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002807
Roland Levillain232ade02015-04-20 15:14:36 +01002808 // Load the value to the FP stack, using temporaries if needed.
2809 PushOntoFPStack(in, 0, adjustment, false, true);
2810
2811 if (out.IsStackSlot()) {
2812 __ fstps(Address(ESP, out.GetStackIndex() + adjustment));
2813 } else {
2814 __ fstps(Address(ESP, 0));
2815 Location stack_temp = Location::StackSlot(0);
2816 codegen_->Move32(out, stack_temp);
2817 }
2818
2819 // Remove the temporary stack space we allocated.
2820 if (adjustment != 0) {
2821 __ addl(ESP, Immediate(adjustment));
2822 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002823 break;
2824 }
2825
Roland Levillaincff13742014-11-17 14:32:17 +00002826 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002827 // Processing a Dex `double-to-float' instruction.
2828 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002829 break;
2830
2831 default:
2832 LOG(FATAL) << "Unexpected type conversion from " << input_type
2833 << " to " << result_type;
2834 };
2835 break;
2836
Roland Levillaindff1f282014-11-05 14:15:05 +00002837 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002838 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002839 case Primitive::kPrimBoolean:
2840 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002841 case Primitive::kPrimByte:
2842 case Primitive::kPrimShort:
2843 case Primitive::kPrimInt:
2844 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002845 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002846 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002847 break;
2848
Roland Levillain647b9ed2014-11-27 12:06:00 +00002849 case Primitive::kPrimLong: {
2850 // Processing a Dex `long-to-double' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002851 size_t adjustment = 0;
Roland Levillain647b9ed2014-11-27 12:06:00 +00002852
Roland Levillain232ade02015-04-20 15:14:36 +01002853 // Create stack space for the call to
2854 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstpl below.
2855 // TODO: enhance register allocator to ask for stack temporaries.
2856 if (!in.IsDoubleStackSlot() || !out.IsDoubleStackSlot()) {
2857 adjustment = Primitive::ComponentSize(Primitive::kPrimLong);
2858 __ subl(ESP, Immediate(adjustment));
2859 }
2860
2861 // Load the value to the FP stack, using temporaries if needed.
2862 PushOntoFPStack(in, 0, adjustment, false, true);
2863
2864 if (out.IsDoubleStackSlot()) {
2865 __ fstpl(Address(ESP, out.GetStackIndex() + adjustment));
2866 } else {
2867 __ fstpl(Address(ESP, 0));
2868 Location stack_temp = Location::DoubleStackSlot(0);
2869 codegen_->Move64(out, stack_temp);
2870 }
2871
2872 // Remove the temporary stack space we allocated.
2873 if (adjustment != 0) {
2874 __ addl(ESP, Immediate(adjustment));
2875 }
Roland Levillain647b9ed2014-11-27 12:06:00 +00002876 break;
2877 }
2878
Roland Levillaincff13742014-11-17 14:32:17 +00002879 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002880 // Processing a Dex `float-to-double' instruction.
2881 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002882 break;
2883
2884 default:
2885 LOG(FATAL) << "Unexpected type conversion from " << input_type
2886 << " to " << result_type;
2887 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002888 break;
2889
2890 default:
2891 LOG(FATAL) << "Unexpected type conversion from " << input_type
2892 << " to " << result_type;
2893 }
2894}
2895
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002896void LocationsBuilderX86::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002897 LocationSummary* locations =
2898 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002899 switch (add->GetResultType()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002900 case Primitive::kPrimInt: {
2901 locations->SetInAt(0, Location::RequiresRegister());
2902 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
2903 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2904 break;
2905 }
2906
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002907 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002908 locations->SetInAt(0, Location::RequiresRegister());
2909 locations->SetInAt(1, Location::Any());
2910 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002911 break;
2912 }
2913
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002914 case Primitive::kPrimFloat:
2915 case Primitive::kPrimDouble: {
2916 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002917 if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
2918 DCHECK(add->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00002919 } else if (add->InputAt(1)->IsConstant()) {
2920 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002921 } else {
2922 locations->SetInAt(1, Location::Any());
2923 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002924 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002925 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002926 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002927
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002928 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002929 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
2930 break;
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002931 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002932}
2933
2934void InstructionCodeGeneratorX86::VisitAdd(HAdd* add) {
2935 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002936 Location first = locations->InAt(0);
2937 Location second = locations->InAt(1);
Mark Mendell09b84632015-02-13 17:48:38 -05002938 Location out = locations->Out();
2939
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002940 switch (add->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002941 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002942 if (second.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002943 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2944 __ addl(out.AsRegister<Register>(), second.AsRegister<Register>());
Mark Mendell33bf2452015-05-27 10:08:24 -04002945 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
2946 __ addl(out.AsRegister<Register>(), first.AsRegister<Register>());
Mark Mendell09b84632015-02-13 17:48:38 -05002947 } else {
2948 __ leal(out.AsRegister<Register>(), Address(
2949 first.AsRegister<Register>(), second.AsRegister<Register>(), TIMES_1, 0));
2950 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002951 } else if (second.IsConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002952 int32_t value = second.GetConstant()->AsIntConstant()->GetValue();
2953 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2954 __ addl(out.AsRegister<Register>(), Immediate(value));
2955 } else {
2956 __ leal(out.AsRegister<Register>(), Address(first.AsRegister<Register>(), value));
2957 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002958 } else {
Mark Mendell09b84632015-02-13 17:48:38 -05002959 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002960 __ addl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002961 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002962 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002963 }
2964
2965 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002966 if (second.IsRegisterPair()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002967 __ addl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
2968 __ adcl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002969 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002970 __ addl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
2971 __ adcl(first.AsRegisterPairHigh<Register>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002972 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002973 } else {
2974 DCHECK(second.IsConstant()) << second;
2975 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2976 __ addl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
2977 __ adcl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002978 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002979 break;
2980 }
2981
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002982 case Primitive::kPrimFloat: {
2983 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002984 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04002985 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
2986 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00002987 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04002988 __ addss(first.AsFpuRegister<XmmRegister>(),
2989 codegen_->LiteralFloatAddress(
2990 const_area->GetConstant()->AsFloatConstant()->GetValue(),
2991 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2992 } else {
2993 DCHECK(second.IsStackSlot());
2994 __ addss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002995 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002996 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002997 }
2998
2999 case Primitive::kPrimDouble: {
3000 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003001 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04003002 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
3003 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003004 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003005 __ addsd(first.AsFpuRegister<XmmRegister>(),
3006 codegen_->LiteralDoubleAddress(
3007 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3008 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3009 } else {
3010 DCHECK(second.IsDoubleStackSlot());
3011 __ addsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003012 }
3013 break;
3014 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003015
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003016 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003017 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003018 }
3019}
3020
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003021void LocationsBuilderX86::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003022 LocationSummary* locations =
3023 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003024 switch (sub->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003025 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003026 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003027 locations->SetInAt(0, Location::RequiresRegister());
3028 locations->SetInAt(1, Location::Any());
3029 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003030 break;
3031 }
Calin Juravle11351682014-10-23 15:38:15 +01003032 case Primitive::kPrimFloat:
3033 case Primitive::kPrimDouble: {
3034 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003035 if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
3036 DCHECK(sub->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003037 } else if (sub->InputAt(1)->IsConstant()) {
3038 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003039 } else {
3040 locations->SetInAt(1, Location::Any());
3041 }
Calin Juravle11351682014-10-23 15:38:15 +01003042 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003043 break;
Calin Juravle11351682014-10-23 15:38:15 +01003044 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003045
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003046 default:
Calin Juravle11351682014-10-23 15:38:15 +01003047 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003048 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003049}
3050
3051void InstructionCodeGeneratorX86::VisitSub(HSub* sub) {
3052 LocationSummary* locations = sub->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003053 Location first = locations->InAt(0);
3054 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01003055 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003056 switch (sub->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003057 case Primitive::kPrimInt: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003058 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003059 __ subl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003060 } else if (second.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003061 __ subl(first.AsRegister<Register>(),
3062 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003063 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003064 __ subl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003065 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003066 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003067 }
3068
3069 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003070 if (second.IsRegisterPair()) {
Calin Juravle11351682014-10-23 15:38:15 +01003071 __ subl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
3072 __ sbbl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003073 } else if (second.IsDoubleStackSlot()) {
Calin Juravle11351682014-10-23 15:38:15 +01003074 __ subl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003075 __ sbbl(first.AsRegisterPairHigh<Register>(),
3076 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003077 } else {
3078 DCHECK(second.IsConstant()) << second;
3079 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3080 __ subl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
3081 __ sbbl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003082 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003083 break;
3084 }
3085
Calin Juravle11351682014-10-23 15:38:15 +01003086 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003087 if (second.IsFpuRegister()) {
3088 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3089 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
3090 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003091 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003092 __ subss(first.AsFpuRegister<XmmRegister>(),
3093 codegen_->LiteralFloatAddress(
3094 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3095 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3096 } else {
3097 DCHECK(second.IsStackSlot());
3098 __ subss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3099 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003100 break;
Calin Juravle11351682014-10-23 15:38:15 +01003101 }
3102
3103 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003104 if (second.IsFpuRegister()) {
3105 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3106 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
3107 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003108 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003109 __ subsd(first.AsFpuRegister<XmmRegister>(),
3110 codegen_->LiteralDoubleAddress(
3111 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3112 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3113 } else {
3114 DCHECK(second.IsDoubleStackSlot());
3115 __ subsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3116 }
Calin Juravle11351682014-10-23 15:38:15 +01003117 break;
3118 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003119
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003120 default:
Calin Juravle11351682014-10-23 15:38:15 +01003121 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003122 }
3123}
3124
Calin Juravle34bacdf2014-10-07 20:23:36 +01003125void LocationsBuilderX86::VisitMul(HMul* mul) {
3126 LocationSummary* locations =
3127 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
3128 switch (mul->GetResultType()) {
3129 case Primitive::kPrimInt:
3130 locations->SetInAt(0, Location::RequiresRegister());
3131 locations->SetInAt(1, Location::Any());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003132 if (mul->InputAt(1)->IsIntConstant()) {
3133 // Can use 3 operand multiply.
3134 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3135 } else {
3136 locations->SetOut(Location::SameAsFirstInput());
3137 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003138 break;
3139 case Primitive::kPrimLong: {
3140 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003141 locations->SetInAt(1, Location::Any());
3142 locations->SetOut(Location::SameAsFirstInput());
3143 // Needed for imul on 32bits with 64bits output.
3144 locations->AddTemp(Location::RegisterLocation(EAX));
3145 locations->AddTemp(Location::RegisterLocation(EDX));
3146 break;
3147 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003148 case Primitive::kPrimFloat:
3149 case Primitive::kPrimDouble: {
3150 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003151 if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3152 DCHECK(mul->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003153 } else if (mul->InputAt(1)->IsConstant()) {
3154 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003155 } else {
3156 locations->SetInAt(1, Location::Any());
3157 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003158 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003159 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003160 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003161
3162 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003163 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003164 }
3165}
3166
3167void InstructionCodeGeneratorX86::VisitMul(HMul* mul) {
3168 LocationSummary* locations = mul->GetLocations();
3169 Location first = locations->InAt(0);
3170 Location second = locations->InAt(1);
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003171 Location out = locations->Out();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003172
3173 switch (mul->GetResultType()) {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003174 case Primitive::kPrimInt:
3175 // The constant may have ended up in a register, so test explicitly to avoid
3176 // problems where the output may not be the same as the first operand.
3177 if (mul->InputAt(1)->IsIntConstant()) {
3178 Immediate imm(mul->InputAt(1)->AsIntConstant()->GetValue());
3179 __ imull(out.AsRegister<Register>(), first.AsRegister<Register>(), imm);
3180 } else if (second.IsRegister()) {
3181 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003182 __ imull(first.AsRegister<Register>(), second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003183 } else {
3184 DCHECK(second.IsStackSlot());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003185 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003186 __ imull(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01003187 }
3188 break;
Calin Juravle34bacdf2014-10-07 20:23:36 +01003189
3190 case Primitive::kPrimLong: {
Calin Juravle34bacdf2014-10-07 20:23:36 +01003191 Register in1_hi = first.AsRegisterPairHigh<Register>();
3192 Register in1_lo = first.AsRegisterPairLow<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003193 Register eax = locations->GetTemp(0).AsRegister<Register>();
3194 Register edx = locations->GetTemp(1).AsRegister<Register>();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003195
3196 DCHECK_EQ(EAX, eax);
3197 DCHECK_EQ(EDX, edx);
3198
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003199 // input: in1 - 64 bits, in2 - 64 bits.
Calin Juravle34bacdf2014-10-07 20:23:36 +01003200 // output: in1
3201 // formula: in1.hi : in1.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
3202 // parts: in1.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
3203 // parts: in1.lo = (in1.lo * in2.lo)[31:0]
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003204 if (second.IsConstant()) {
3205 DCHECK(second.GetConstant()->IsLongConstant());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003206
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003207 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3208 int32_t low_value = Low32Bits(value);
3209 int32_t high_value = High32Bits(value);
3210 Immediate low(low_value);
3211 Immediate high(high_value);
3212
3213 __ movl(eax, high);
3214 // eax <- in1.lo * in2.hi
3215 __ imull(eax, in1_lo);
3216 // in1.hi <- in1.hi * in2.lo
3217 __ imull(in1_hi, low);
3218 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3219 __ addl(in1_hi, eax);
3220 // move in2_lo to eax to prepare for double precision
3221 __ movl(eax, low);
3222 // edx:eax <- in1.lo * in2.lo
3223 __ mull(in1_lo);
3224 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3225 __ addl(in1_hi, edx);
3226 // in1.lo <- (in1.lo * in2.lo)[31:0];
3227 __ movl(in1_lo, eax);
3228 } else if (second.IsRegisterPair()) {
3229 Register in2_hi = second.AsRegisterPairHigh<Register>();
3230 Register in2_lo = second.AsRegisterPairLow<Register>();
3231
3232 __ movl(eax, in2_hi);
3233 // eax <- in1.lo * in2.hi
3234 __ imull(eax, in1_lo);
3235 // in1.hi <- in1.hi * in2.lo
3236 __ imull(in1_hi, in2_lo);
3237 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3238 __ addl(in1_hi, eax);
3239 // move in1_lo to eax to prepare for double precision
3240 __ movl(eax, in1_lo);
3241 // edx:eax <- in1.lo * in2.lo
3242 __ mull(in2_lo);
3243 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3244 __ addl(in1_hi, edx);
3245 // in1.lo <- (in1.lo * in2.lo)[31:0];
3246 __ movl(in1_lo, eax);
3247 } else {
3248 DCHECK(second.IsDoubleStackSlot()) << second;
3249 Address in2_hi(ESP, second.GetHighStackIndex(kX86WordSize));
3250 Address in2_lo(ESP, second.GetStackIndex());
3251
3252 __ movl(eax, in2_hi);
3253 // eax <- in1.lo * in2.hi
3254 __ imull(eax, in1_lo);
3255 // in1.hi <- in1.hi * in2.lo
3256 __ imull(in1_hi, in2_lo);
3257 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3258 __ addl(in1_hi, eax);
3259 // move in1_lo to eax to prepare for double precision
3260 __ movl(eax, in1_lo);
3261 // edx:eax <- in1.lo * in2.lo
3262 __ mull(in2_lo);
3263 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3264 __ addl(in1_hi, edx);
3265 // in1.lo <- (in1.lo * in2.lo)[31:0];
3266 __ movl(in1_lo, eax);
3267 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003268
3269 break;
3270 }
3271
Calin Juravleb5bfa962014-10-21 18:02:24 +01003272 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003273 DCHECK(first.Equals(locations->Out()));
3274 if (second.IsFpuRegister()) {
3275 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3276 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3277 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003278 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003279 __ mulss(first.AsFpuRegister<XmmRegister>(),
3280 codegen_->LiteralFloatAddress(
3281 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3282 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3283 } else {
3284 DCHECK(second.IsStackSlot());
3285 __ mulss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3286 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003287 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003288 }
3289
3290 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003291 DCHECK(first.Equals(locations->Out()));
3292 if (second.IsFpuRegister()) {
3293 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3294 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3295 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003296 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003297 __ mulsd(first.AsFpuRegister<XmmRegister>(),
3298 codegen_->LiteralDoubleAddress(
3299 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3300 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3301 } else {
3302 DCHECK(second.IsDoubleStackSlot());
3303 __ mulsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3304 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003305 break;
3306 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003307
3308 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003309 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003310 }
3311}
3312
Roland Levillain232ade02015-04-20 15:14:36 +01003313void InstructionCodeGeneratorX86::PushOntoFPStack(Location source,
3314 uint32_t temp_offset,
3315 uint32_t stack_adjustment,
3316 bool is_fp,
3317 bool is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003318 if (source.IsStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01003319 DCHECK(!is_wide);
3320 if (is_fp) {
3321 __ flds(Address(ESP, source.GetStackIndex() + stack_adjustment));
3322 } else {
3323 __ filds(Address(ESP, source.GetStackIndex() + stack_adjustment));
3324 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003325 } else if (source.IsDoubleStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01003326 DCHECK(is_wide);
3327 if (is_fp) {
3328 __ fldl(Address(ESP, source.GetStackIndex() + stack_adjustment));
3329 } else {
3330 __ fildl(Address(ESP, source.GetStackIndex() + stack_adjustment));
3331 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003332 } else {
3333 // Write the value to the temporary location on the stack and load to FP stack.
Roland Levillain232ade02015-04-20 15:14:36 +01003334 if (!is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003335 Location stack_temp = Location::StackSlot(temp_offset);
3336 codegen_->Move32(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01003337 if (is_fp) {
3338 __ flds(Address(ESP, temp_offset));
3339 } else {
3340 __ filds(Address(ESP, temp_offset));
3341 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003342 } else {
3343 Location stack_temp = Location::DoubleStackSlot(temp_offset);
3344 codegen_->Move64(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01003345 if (is_fp) {
3346 __ fldl(Address(ESP, temp_offset));
3347 } else {
3348 __ fildl(Address(ESP, temp_offset));
3349 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003350 }
3351 }
3352}
3353
3354void InstructionCodeGeneratorX86::GenerateRemFP(HRem *rem) {
3355 Primitive::Type type = rem->GetResultType();
3356 bool is_float = type == Primitive::kPrimFloat;
3357 size_t elem_size = Primitive::ComponentSize(type);
3358 LocationSummary* locations = rem->GetLocations();
3359 Location first = locations->InAt(0);
3360 Location second = locations->InAt(1);
3361 Location out = locations->Out();
3362
3363 // Create stack space for 2 elements.
3364 // TODO: enhance register allocator to ask for stack temporaries.
3365 __ subl(ESP, Immediate(2 * elem_size));
3366
3367 // Load the values to the FP stack in reverse order, using temporaries if needed.
Roland Levillain232ade02015-04-20 15:14:36 +01003368 const bool is_wide = !is_float;
3369 PushOntoFPStack(second, elem_size, 2 * elem_size, /* is_fp */ true, is_wide);
3370 PushOntoFPStack(first, 0, 2 * elem_size, /* is_fp */ true, is_wide);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003371
3372 // Loop doing FPREM until we stabilize.
Mark Mendell0c9497d2015-08-21 09:30:05 -04003373 NearLabel retry;
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003374 __ Bind(&retry);
3375 __ fprem();
3376
3377 // Move FP status to AX.
3378 __ fstsw();
3379
3380 // And see if the argument reduction is complete. This is signaled by the
3381 // C2 FPU flag bit set to 0.
3382 __ andl(EAX, Immediate(kC2ConditionMask));
3383 __ j(kNotEqual, &retry);
3384
3385 // We have settled on the final value. Retrieve it into an XMM register.
3386 // Store FP top of stack to real stack.
3387 if (is_float) {
3388 __ fsts(Address(ESP, 0));
3389 } else {
3390 __ fstl(Address(ESP, 0));
3391 }
3392
3393 // Pop the 2 items from the FP stack.
3394 __ fucompp();
3395
3396 // Load the value from the stack into an XMM register.
3397 DCHECK(out.IsFpuRegister()) << out;
3398 if (is_float) {
3399 __ movss(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
3400 } else {
3401 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
3402 }
3403
3404 // And remove the temporary stack space we allocated.
3405 __ addl(ESP, Immediate(2 * elem_size));
3406}
3407
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003408
3409void InstructionCodeGeneratorX86::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3410 DCHECK(instruction->IsDiv() || instruction->IsRem());
3411
3412 LocationSummary* locations = instruction->GetLocations();
3413 DCHECK(locations->InAt(1).IsConstant());
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003414 DCHECK(locations->InAt(1).GetConstant()->IsIntConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003415
3416 Register out_register = locations->Out().AsRegister<Register>();
3417 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003418 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003419
3420 DCHECK(imm == 1 || imm == -1);
3421
3422 if (instruction->IsRem()) {
3423 __ xorl(out_register, out_register);
3424 } else {
3425 __ movl(out_register, input_register);
3426 if (imm == -1) {
3427 __ negl(out_register);
3428 }
3429 }
3430}
3431
3432
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003433void InstructionCodeGeneratorX86::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003434 LocationSummary* locations = instruction->GetLocations();
3435
3436 Register out_register = locations->Out().AsRegister<Register>();
3437 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003438 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003439 DCHECK(IsPowerOfTwo(AbsOrMin(imm)));
3440 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003441
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003442 Register num = locations->GetTemp(0).AsRegister<Register>();
3443
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003444 __ leal(num, Address(input_register, abs_imm - 1));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003445 __ testl(input_register, input_register);
3446 __ cmovl(kGreaterEqual, num, input_register);
3447 int shift = CTZ(imm);
3448 __ sarl(num, Immediate(shift));
3449
3450 if (imm < 0) {
3451 __ negl(num);
3452 }
3453
3454 __ movl(out_register, num);
3455}
3456
3457void InstructionCodeGeneratorX86::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3458 DCHECK(instruction->IsDiv() || instruction->IsRem());
3459
3460 LocationSummary* locations = instruction->GetLocations();
3461 int imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
3462
3463 Register eax = locations->InAt(0).AsRegister<Register>();
3464 Register out = locations->Out().AsRegister<Register>();
3465 Register num;
3466 Register edx;
3467
3468 if (instruction->IsDiv()) {
3469 edx = locations->GetTemp(0).AsRegister<Register>();
3470 num = locations->GetTemp(1).AsRegister<Register>();
3471 } else {
3472 edx = locations->Out().AsRegister<Register>();
3473 num = locations->GetTemp(0).AsRegister<Register>();
3474 }
3475
3476 DCHECK_EQ(EAX, eax);
3477 DCHECK_EQ(EDX, edx);
3478 if (instruction->IsDiv()) {
3479 DCHECK_EQ(EAX, out);
3480 } else {
3481 DCHECK_EQ(EDX, out);
3482 }
3483
3484 int64_t magic;
3485 int shift;
3486 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
3487
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003488 // Save the numerator.
3489 __ movl(num, eax);
3490
3491 // EAX = magic
3492 __ movl(eax, Immediate(magic));
3493
3494 // EDX:EAX = magic * numerator
3495 __ imull(num);
3496
3497 if (imm > 0 && magic < 0) {
3498 // EDX += num
3499 __ addl(edx, num);
3500 } else if (imm < 0 && magic > 0) {
3501 __ subl(edx, num);
3502 }
3503
3504 // Shift if needed.
3505 if (shift != 0) {
3506 __ sarl(edx, Immediate(shift));
3507 }
3508
3509 // EDX += 1 if EDX < 0
3510 __ movl(eax, edx);
3511 __ shrl(edx, Immediate(31));
3512 __ addl(edx, eax);
3513
3514 if (instruction->IsRem()) {
3515 __ movl(eax, num);
3516 __ imull(edx, Immediate(imm));
3517 __ subl(eax, edx);
3518 __ movl(edx, eax);
3519 } else {
3520 __ movl(eax, edx);
3521 }
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003522}
3523
Calin Juravlebacfec32014-11-14 15:54:36 +00003524void InstructionCodeGeneratorX86::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3525 DCHECK(instruction->IsDiv() || instruction->IsRem());
3526
3527 LocationSummary* locations = instruction->GetLocations();
3528 Location out = locations->Out();
3529 Location first = locations->InAt(0);
3530 Location second = locations->InAt(1);
3531 bool is_div = instruction->IsDiv();
3532
3533 switch (instruction->GetResultType()) {
3534 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003535 DCHECK_EQ(EAX, first.AsRegister<Register>());
3536 DCHECK_EQ(is_div ? EAX : EDX, out.AsRegister<Register>());
Calin Juravlebacfec32014-11-14 15:54:36 +00003537
Vladimir Marko13c86fd2015-11-11 12:37:46 +00003538 if (second.IsConstant()) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003539 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003540
3541 if (imm == 0) {
3542 // Do not generate anything for 0. DivZeroCheck would forbid any generated code.
3543 } else if (imm == 1 || imm == -1) {
3544 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003545 } else if (is_div && IsPowerOfTwo(AbsOrMin(imm))) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003546 DivByPowerOfTwo(instruction->AsDiv());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003547 } else {
3548 DCHECK(imm <= -2 || imm >= 2);
3549 GenerateDivRemWithAnyConstant(instruction);
3550 }
3551 } else {
David Srbecky9cd6d372016-02-09 15:24:47 +00003552 SlowPathCode* slow_path = new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86(
3553 instruction, out.AsRegister<Register>(), is_div);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003554 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00003555
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003556 Register second_reg = second.AsRegister<Register>();
3557 // 0x80000000/-1 triggers an arithmetic exception!
3558 // Dividing by -1 is actually negation and -0x800000000 = 0x80000000 so
3559 // it's safe to just use negl instead of more complex comparisons.
Calin Juravlebacfec32014-11-14 15:54:36 +00003560
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003561 __ cmpl(second_reg, Immediate(-1));
3562 __ j(kEqual, slow_path->GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +00003563
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003564 // edx:eax <- sign-extended of eax
3565 __ cdq();
3566 // eax = quotient, edx = remainder
3567 __ idivl(second_reg);
3568 __ Bind(slow_path->GetExitLabel());
3569 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003570 break;
3571 }
3572
3573 case Primitive::kPrimLong: {
3574 InvokeRuntimeCallingConvention calling_convention;
3575 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
3576 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
3577 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
3578 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
3579 DCHECK_EQ(EAX, out.AsRegisterPairLow<Register>());
3580 DCHECK_EQ(EDX, out.AsRegisterPairHigh<Register>());
3581
3582 if (is_div) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01003583 codegen_->InvokeRuntime(kQuickLdiv, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003584 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003585 } else {
Serban Constantinescuba45db02016-07-12 22:53:02 +01003586 codegen_->InvokeRuntime(kQuickLmod, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003587 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003588 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003589 break;
3590 }
3591
3592 default:
3593 LOG(FATAL) << "Unexpected type for GenerateDivRemIntegral " << instruction->GetResultType();
3594 }
3595}
3596
Calin Juravle7c4954d2014-10-28 16:57:40 +00003597void LocationsBuilderX86::VisitDiv(HDiv* div) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003598 LocationSummary::CallKind call_kind = (div->GetResultType() == Primitive::kPrimLong)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003599 ? LocationSummary::kCallOnMainOnly
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003600 : LocationSummary::kNoCall;
3601 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
3602
Calin Juravle7c4954d2014-10-28 16:57:40 +00003603 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00003604 case Primitive::kPrimInt: {
3605 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003606 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00003607 locations->SetOut(Location::SameAsFirstInput());
3608 // Intel uses edx:eax as the dividend.
3609 locations->AddTemp(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003610 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3611 // which enforces results to be in EAX and EDX, things are simpler if we use EAX also as
3612 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003613 if (div->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003614 locations->AddTemp(Location::RequiresRegister());
3615 }
Calin Juravled0d48522014-11-04 16:40:20 +00003616 break;
3617 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003618 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003619 InvokeRuntimeCallingConvention calling_convention;
3620 locations->SetInAt(0, Location::RegisterPairLocation(
3621 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3622 locations->SetInAt(1, Location::RegisterPairLocation(
3623 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3624 // Runtime helper puts the result in EAX, EDX.
3625 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Calin Juravle7c4954d2014-10-28 16:57:40 +00003626 break;
3627 }
3628 case Primitive::kPrimFloat:
3629 case Primitive::kPrimDouble: {
3630 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003631 if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3632 DCHECK(div->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003633 } else if (div->InputAt(1)->IsConstant()) {
3634 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003635 } else {
3636 locations->SetInAt(1, Location::Any());
3637 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003638 locations->SetOut(Location::SameAsFirstInput());
3639 break;
3640 }
3641
3642 default:
3643 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3644 }
3645}
3646
3647void InstructionCodeGeneratorX86::VisitDiv(HDiv* div) {
3648 LocationSummary* locations = div->GetLocations();
3649 Location first = locations->InAt(0);
3650 Location second = locations->InAt(1);
Calin Juravle7c4954d2014-10-28 16:57:40 +00003651
3652 switch (div->GetResultType()) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003653 case Primitive::kPrimInt:
Calin Juravle7c4954d2014-10-28 16:57:40 +00003654 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003655 GenerateDivRemIntegral(div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00003656 break;
3657 }
3658
3659 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003660 if (second.IsFpuRegister()) {
3661 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3662 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3663 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003664 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003665 __ divss(first.AsFpuRegister<XmmRegister>(),
3666 codegen_->LiteralFloatAddress(
3667 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3668 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3669 } else {
3670 DCHECK(second.IsStackSlot());
3671 __ divss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3672 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003673 break;
3674 }
3675
3676 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003677 if (second.IsFpuRegister()) {
3678 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3679 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3680 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003681 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003682 __ divsd(first.AsFpuRegister<XmmRegister>(),
3683 codegen_->LiteralDoubleAddress(
3684 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3685 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3686 } else {
3687 DCHECK(second.IsDoubleStackSlot());
3688 __ divsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3689 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003690 break;
3691 }
3692
3693 default:
3694 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3695 }
3696}
3697
Calin Juravlebacfec32014-11-14 15:54:36 +00003698void LocationsBuilderX86::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00003699 Primitive::Type type = rem->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003700
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003701 LocationSummary::CallKind call_kind = (rem->GetResultType() == Primitive::kPrimLong)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003702 ? LocationSummary::kCallOnMainOnly
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003703 : LocationSummary::kNoCall;
3704 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
Calin Juravlebacfec32014-11-14 15:54:36 +00003705
Calin Juravled2ec87d2014-12-08 14:24:46 +00003706 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003707 case Primitive::kPrimInt: {
3708 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003709 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00003710 locations->SetOut(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003711 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3712 // which enforces results to be in EAX and EDX, things are simpler if we use EDX also as
3713 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003714 if (rem->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003715 locations->AddTemp(Location::RequiresRegister());
3716 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003717 break;
3718 }
3719 case Primitive::kPrimLong: {
3720 InvokeRuntimeCallingConvention calling_convention;
3721 locations->SetInAt(0, Location::RegisterPairLocation(
3722 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3723 locations->SetInAt(1, Location::RegisterPairLocation(
3724 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3725 // Runtime helper puts the result in EAX, EDX.
3726 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
3727 break;
3728 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003729 case Primitive::kPrimDouble:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003730 case Primitive::kPrimFloat: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003731 locations->SetInAt(0, Location::Any());
3732 locations->SetInAt(1, Location::Any());
3733 locations->SetOut(Location::RequiresFpuRegister());
3734 locations->AddTemp(Location::RegisterLocation(EAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00003735 break;
3736 }
3737
3738 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003739 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003740 }
3741}
3742
3743void InstructionCodeGeneratorX86::VisitRem(HRem* rem) {
3744 Primitive::Type type = rem->GetResultType();
3745 switch (type) {
3746 case Primitive::kPrimInt:
3747 case Primitive::kPrimLong: {
3748 GenerateDivRemIntegral(rem);
3749 break;
3750 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003751 case Primitive::kPrimFloat:
Calin Juravlebacfec32014-11-14 15:54:36 +00003752 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003753 GenerateRemFP(rem);
Calin Juravlebacfec32014-11-14 15:54:36 +00003754 break;
3755 }
3756 default:
3757 LOG(FATAL) << "Unexpected rem type " << type;
3758 }
3759}
3760
Calin Juravled0d48522014-11-04 16:40:20 +00003761void LocationsBuilderX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003762 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003763 switch (instruction->GetType()) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003764 case Primitive::kPrimBoolean:
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003765 case Primitive::kPrimByte:
3766 case Primitive::kPrimChar:
3767 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003768 case Primitive::kPrimInt: {
3769 locations->SetInAt(0, Location::Any());
3770 break;
3771 }
3772 case Primitive::kPrimLong: {
3773 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
3774 if (!instruction->IsConstant()) {
3775 locations->AddTemp(Location::RequiresRegister());
3776 }
3777 break;
3778 }
3779 default:
3780 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
3781 }
Calin Juravled0d48522014-11-04 16:40:20 +00003782}
3783
3784void InstructionCodeGeneratorX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003785 SlowPathCode* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86(instruction);
Calin Juravled0d48522014-11-04 16:40:20 +00003786 codegen_->AddSlowPath(slow_path);
3787
3788 LocationSummary* locations = instruction->GetLocations();
3789 Location value = locations->InAt(0);
3790
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003791 switch (instruction->GetType()) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003792 case Primitive::kPrimBoolean:
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003793 case Primitive::kPrimByte:
3794 case Primitive::kPrimChar:
3795 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003796 case Primitive::kPrimInt: {
3797 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003798 __ testl(value.AsRegister<Register>(), value.AsRegister<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003799 __ j(kEqual, slow_path->GetEntryLabel());
3800 } else if (value.IsStackSlot()) {
3801 __ cmpl(Address(ESP, value.GetStackIndex()), Immediate(0));
3802 __ j(kEqual, slow_path->GetEntryLabel());
3803 } else {
3804 DCHECK(value.IsConstant()) << value;
3805 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01003806 __ jmp(slow_path->GetEntryLabel());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003807 }
3808 }
3809 break;
Calin Juravled0d48522014-11-04 16:40:20 +00003810 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003811 case Primitive::kPrimLong: {
3812 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003813 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003814 __ movl(temp, value.AsRegisterPairLow<Register>());
3815 __ orl(temp, value.AsRegisterPairHigh<Register>());
3816 __ j(kEqual, slow_path->GetEntryLabel());
3817 } else {
3818 DCHECK(value.IsConstant()) << value;
3819 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
3820 __ jmp(slow_path->GetEntryLabel());
3821 }
3822 }
3823 break;
3824 }
3825 default:
3826 LOG(FATAL) << "Unexpected type for HDivZeroCheck" << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00003827 }
Calin Juravled0d48522014-11-04 16:40:20 +00003828}
3829
Calin Juravle9aec02f2014-11-18 23:06:35 +00003830void LocationsBuilderX86::HandleShift(HBinaryOperation* op) {
3831 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3832
3833 LocationSummary* locations =
3834 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
3835
3836 switch (op->GetResultType()) {
Mark P Mendell73945692015-04-29 14:56:17 +00003837 case Primitive::kPrimInt:
Calin Juravle9aec02f2014-11-18 23:06:35 +00003838 case Primitive::kPrimLong: {
Mark P Mendell73945692015-04-29 14:56:17 +00003839 // Can't have Location::Any() and output SameAsFirstInput()
Calin Juravle9aec02f2014-11-18 23:06:35 +00003840 locations->SetInAt(0, Location::RequiresRegister());
Mark P Mendell73945692015-04-29 14:56:17 +00003841 // The shift count needs to be in CL or a constant.
3842 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, op->InputAt(1)));
Calin Juravle9aec02f2014-11-18 23:06:35 +00003843 locations->SetOut(Location::SameAsFirstInput());
3844 break;
3845 }
3846 default:
3847 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
3848 }
3849}
3850
3851void InstructionCodeGeneratorX86::HandleShift(HBinaryOperation* op) {
3852 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3853
3854 LocationSummary* locations = op->GetLocations();
3855 Location first = locations->InAt(0);
3856 Location second = locations->InAt(1);
3857 DCHECK(first.Equals(locations->Out()));
3858
3859 switch (op->GetResultType()) {
3860 case Primitive::kPrimInt: {
Mark P Mendell73945692015-04-29 14:56:17 +00003861 DCHECK(first.IsRegister());
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003862 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003863 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003864 Register second_reg = second.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003865 DCHECK_EQ(ECX, second_reg);
3866 if (op->IsShl()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003867 __ shll(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003868 } else if (op->IsShr()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003869 __ sarl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003870 } else {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003871 __ shrl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003872 }
3873 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003874 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance;
Mark P Mendell73945692015-04-29 14:56:17 +00003875 if (shift == 0) {
3876 return;
3877 }
3878 Immediate imm(shift);
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003879 if (op->IsShl()) {
3880 __ shll(first_reg, imm);
3881 } else if (op->IsShr()) {
3882 __ sarl(first_reg, imm);
3883 } else {
3884 __ shrl(first_reg, imm);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003885 }
3886 }
3887 break;
3888 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003889 case Primitive::kPrimLong: {
Mark P Mendell73945692015-04-29 14:56:17 +00003890 if (second.IsRegister()) {
3891 Register second_reg = second.AsRegister<Register>();
3892 DCHECK_EQ(ECX, second_reg);
3893 if (op->IsShl()) {
3894 GenerateShlLong(first, second_reg);
3895 } else if (op->IsShr()) {
3896 GenerateShrLong(first, second_reg);
3897 } else {
3898 GenerateUShrLong(first, second_reg);
3899 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003900 } else {
Mark P Mendell73945692015-04-29 14:56:17 +00003901 // Shift by a constant.
Roland Levillain5b5b9312016-03-22 14:57:31 +00003902 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance;
Mark P Mendell73945692015-04-29 14:56:17 +00003903 // Nothing to do if the shift is 0, as the input is already the output.
3904 if (shift != 0) {
3905 if (op->IsShl()) {
3906 GenerateShlLong(first, shift);
3907 } else if (op->IsShr()) {
3908 GenerateShrLong(first, shift);
3909 } else {
3910 GenerateUShrLong(first, shift);
3911 }
3912 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003913 }
3914 break;
3915 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003916 default:
3917 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
3918 }
3919}
3920
Mark P Mendell73945692015-04-29 14:56:17 +00003921void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, int shift) {
3922 Register low = loc.AsRegisterPairLow<Register>();
3923 Register high = loc.AsRegisterPairHigh<Register>();
Mark Mendellba56d062015-05-05 21:34:03 -04003924 if (shift == 1) {
3925 // This is just an addition.
3926 __ addl(low, low);
3927 __ adcl(high, high);
3928 } else if (shift == 32) {
Mark P Mendell73945692015-04-29 14:56:17 +00003929 // Shift by 32 is easy. High gets low, and low gets 0.
3930 codegen_->EmitParallelMoves(
3931 loc.ToLow(),
3932 loc.ToHigh(),
3933 Primitive::kPrimInt,
3934 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
3935 loc.ToLow(),
3936 Primitive::kPrimInt);
3937 } else if (shift > 32) {
3938 // Low part becomes 0. High part is low part << (shift-32).
3939 __ movl(high, low);
3940 __ shll(high, Immediate(shift - 32));
3941 __ xorl(low, low);
3942 } else {
3943 // Between 1 and 31.
3944 __ shld(high, low, Immediate(shift));
3945 __ shll(low, Immediate(shift));
3946 }
3947}
3948
Calin Juravle9aec02f2014-11-18 23:06:35 +00003949void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04003950 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00003951 __ shld(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>(), shifter);
3952 __ shll(loc.AsRegisterPairLow<Register>(), shifter);
3953 __ testl(shifter, Immediate(32));
3954 __ j(kEqual, &done);
3955 __ movl(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>());
3956 __ movl(loc.AsRegisterPairLow<Register>(), Immediate(0));
3957 __ Bind(&done);
3958}
3959
Mark P Mendell73945692015-04-29 14:56:17 +00003960void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, int shift) {
3961 Register low = loc.AsRegisterPairLow<Register>();
3962 Register high = loc.AsRegisterPairHigh<Register>();
3963 if (shift == 32) {
3964 // Need to copy the sign.
3965 DCHECK_NE(low, high);
3966 __ movl(low, high);
3967 __ sarl(high, Immediate(31));
3968 } else if (shift > 32) {
3969 DCHECK_NE(low, high);
3970 // High part becomes sign. Low part is shifted by shift - 32.
3971 __ movl(low, high);
3972 __ sarl(high, Immediate(31));
3973 __ sarl(low, Immediate(shift - 32));
3974 } else {
3975 // Between 1 and 31.
3976 __ shrd(low, high, Immediate(shift));
3977 __ sarl(high, Immediate(shift));
3978 }
3979}
3980
Calin Juravle9aec02f2014-11-18 23:06:35 +00003981void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04003982 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00003983 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
3984 __ sarl(loc.AsRegisterPairHigh<Register>(), shifter);
3985 __ testl(shifter, Immediate(32));
3986 __ j(kEqual, &done);
3987 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
3988 __ sarl(loc.AsRegisterPairHigh<Register>(), Immediate(31));
3989 __ Bind(&done);
3990}
3991
Mark P Mendell73945692015-04-29 14:56:17 +00003992void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, int shift) {
3993 Register low = loc.AsRegisterPairLow<Register>();
3994 Register high = loc.AsRegisterPairHigh<Register>();
3995 if (shift == 32) {
3996 // Shift by 32 is easy. Low gets high, and high gets 0.
3997 codegen_->EmitParallelMoves(
3998 loc.ToHigh(),
3999 loc.ToLow(),
4000 Primitive::kPrimInt,
4001 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
4002 loc.ToHigh(),
4003 Primitive::kPrimInt);
4004 } else if (shift > 32) {
4005 // Low part is high >> (shift - 32). High part becomes 0.
4006 __ movl(low, high);
4007 __ shrl(low, Immediate(shift - 32));
4008 __ xorl(high, high);
4009 } else {
4010 // Between 1 and 31.
4011 __ shrd(low, high, Immediate(shift));
4012 __ shrl(high, Immediate(shift));
4013 }
4014}
4015
Calin Juravle9aec02f2014-11-18 23:06:35 +00004016void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004017 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00004018 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
4019 __ shrl(loc.AsRegisterPairHigh<Register>(), shifter);
4020 __ testl(shifter, Immediate(32));
4021 __ j(kEqual, &done);
4022 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
4023 __ movl(loc.AsRegisterPairHigh<Register>(), Immediate(0));
4024 __ Bind(&done);
4025}
4026
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004027void LocationsBuilderX86::VisitRor(HRor* ror) {
4028 LocationSummary* locations =
4029 new (GetGraph()->GetArena()) LocationSummary(ror, LocationSummary::kNoCall);
4030
4031 switch (ror->GetResultType()) {
4032 case Primitive::kPrimLong:
4033 // Add the temporary needed.
4034 locations->AddTemp(Location::RequiresRegister());
4035 FALLTHROUGH_INTENDED;
4036 case Primitive::kPrimInt:
4037 locations->SetInAt(0, Location::RequiresRegister());
4038 // The shift count needs to be in CL (unless it is a constant).
4039 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, ror->InputAt(1)));
4040 locations->SetOut(Location::SameAsFirstInput());
4041 break;
4042 default:
4043 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
4044 UNREACHABLE();
4045 }
4046}
4047
4048void InstructionCodeGeneratorX86::VisitRor(HRor* ror) {
4049 LocationSummary* locations = ror->GetLocations();
4050 Location first = locations->InAt(0);
4051 Location second = locations->InAt(1);
4052
4053 if (ror->GetResultType() == Primitive::kPrimInt) {
4054 Register first_reg = first.AsRegister<Register>();
4055 if (second.IsRegister()) {
4056 Register second_reg = second.AsRegister<Register>();
4057 __ rorl(first_reg, second_reg);
4058 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004059 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004060 __ rorl(first_reg, imm);
4061 }
4062 return;
4063 }
4064
4065 DCHECK_EQ(ror->GetResultType(), Primitive::kPrimLong);
4066 Register first_reg_lo = first.AsRegisterPairLow<Register>();
4067 Register first_reg_hi = first.AsRegisterPairHigh<Register>();
4068 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
4069 if (second.IsRegister()) {
4070 Register second_reg = second.AsRegister<Register>();
4071 DCHECK_EQ(second_reg, ECX);
4072 __ movl(temp_reg, first_reg_hi);
4073 __ shrd(first_reg_hi, first_reg_lo, second_reg);
4074 __ shrd(first_reg_lo, temp_reg, second_reg);
4075 __ movl(temp_reg, first_reg_hi);
4076 __ testl(second_reg, Immediate(32));
4077 __ cmovl(kNotEqual, first_reg_hi, first_reg_lo);
4078 __ cmovl(kNotEqual, first_reg_lo, temp_reg);
4079 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004080 int32_t shift_amt = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance;
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004081 if (shift_amt == 0) {
4082 // Already fine.
4083 return;
4084 }
4085 if (shift_amt == 32) {
4086 // Just swap.
4087 __ movl(temp_reg, first_reg_lo);
4088 __ movl(first_reg_lo, first_reg_hi);
4089 __ movl(first_reg_hi, temp_reg);
4090 return;
4091 }
4092
4093 Immediate imm(shift_amt);
4094 // Save the constents of the low value.
4095 __ movl(temp_reg, first_reg_lo);
4096
4097 // Shift right into low, feeding bits from high.
4098 __ shrd(first_reg_lo, first_reg_hi, imm);
4099
4100 // Shift right into high, feeding bits from the original low.
4101 __ shrd(first_reg_hi, temp_reg, imm);
4102
4103 // Swap if needed.
4104 if (shift_amt > 32) {
4105 __ movl(temp_reg, first_reg_lo);
4106 __ movl(first_reg_lo, first_reg_hi);
4107 __ movl(first_reg_hi, temp_reg);
4108 }
4109 }
4110}
4111
Calin Juravle9aec02f2014-11-18 23:06:35 +00004112void LocationsBuilderX86::VisitShl(HShl* shl) {
4113 HandleShift(shl);
4114}
4115
4116void InstructionCodeGeneratorX86::VisitShl(HShl* shl) {
4117 HandleShift(shl);
4118}
4119
4120void LocationsBuilderX86::VisitShr(HShr* shr) {
4121 HandleShift(shr);
4122}
4123
4124void InstructionCodeGeneratorX86::VisitShr(HShr* shr) {
4125 HandleShift(shr);
4126}
4127
4128void LocationsBuilderX86::VisitUShr(HUShr* ushr) {
4129 HandleShift(ushr);
4130}
4131
4132void InstructionCodeGeneratorX86::VisitUShr(HUShr* ushr) {
4133 HandleShift(ushr);
4134}
4135
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004136void LocationsBuilderX86::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004137 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004138 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004139 locations->SetOut(Location::RegisterLocation(EAX));
David Brazdil6de19382016-01-08 17:37:10 +00004140 if (instruction->IsStringAlloc()) {
4141 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
4142 } else {
4143 InvokeRuntimeCallingConvention calling_convention;
4144 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4145 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
4146 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004147}
4148
4149void InstructionCodeGeneratorX86::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01004150 // Note: if heap poisoning is enabled, the entry point takes cares
4151 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00004152 if (instruction->IsStringAlloc()) {
4153 // String is allocated through StringFactory. Call NewEmptyString entry point.
4154 Register temp = instruction->GetLocations()->GetTemp(0).AsRegister<Register>();
Andreas Gampe542451c2016-07-26 09:02:02 -07004155 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86PointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00004156 __ fs()->movl(temp, Address::Absolute(QUICK_ENTRY_POINT(pNewEmptyString)));
4157 __ call(Address(temp, code_offset.Int32Value()));
4158 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4159 } else {
Serban Constantinescuba45db02016-07-12 22:53:02 +01004160 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
David Brazdil6de19382016-01-08 17:37:10 +00004161 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
4162 DCHECK(!codegen_->IsLeafMethod());
4163 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004164}
4165
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004166void LocationsBuilderX86::VisitNewArray(HNewArray* instruction) {
4167 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004168 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004169 locations->SetOut(Location::RegisterLocation(EAX));
4170 InvokeRuntimeCallingConvention calling_convention;
4171 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08004172 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004173 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004174}
4175
4176void InstructionCodeGeneratorX86::VisitNewArray(HNewArray* instruction) {
4177 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004178 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
Roland Levillain4d027112015-07-01 15:41:14 +01004179 // Note: if heap poisoning is enabled, the entry point takes cares
4180 // of poisoning the reference.
Serban Constantinescuba45db02016-07-12 22:53:02 +01004181 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00004182 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004183 DCHECK(!codegen_->IsLeafMethod());
4184}
4185
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004186void LocationsBuilderX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004187 LocationSummary* locations =
4188 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01004189 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
4190 if (location.IsStackSlot()) {
4191 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4192 } else if (location.IsDoubleStackSlot()) {
4193 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004194 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01004195 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004196}
4197
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004198void InstructionCodeGeneratorX86::VisitParameterValue(
4199 HParameterValue* instruction ATTRIBUTE_UNUSED) {
4200}
4201
4202void LocationsBuilderX86::VisitCurrentMethod(HCurrentMethod* instruction) {
4203 LocationSummary* locations =
4204 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4205 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
4206}
4207
4208void InstructionCodeGeneratorX86::VisitCurrentMethod(HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004209}
4210
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004211void LocationsBuilderX86::VisitClassTableGet(HClassTableGet* instruction) {
4212 LocationSummary* locations =
4213 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4214 locations->SetInAt(0, Location::RequiresRegister());
4215 locations->SetOut(Location::RequiresRegister());
4216}
4217
4218void InstructionCodeGeneratorX86::VisitClassTableGet(HClassTableGet* instruction) {
4219 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004220 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004221 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004222 instruction->GetIndex(), kX86PointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004223 __ movl(locations->Out().AsRegister<Register>(),
4224 Address(locations->InAt(0).AsRegister<Register>(), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004225 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004226 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00004227 instruction->GetIndex(), kX86PointerSize));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004228 __ movl(locations->Out().AsRegister<Register>(),
4229 Address(locations->InAt(0).AsRegister<Register>(),
4230 mirror::Class::ImtPtrOffset(kX86PointerSize).Uint32Value()));
4231 // temp = temp->GetImtEntryAt(method_offset);
4232 __ movl(locations->Out().AsRegister<Register>(),
4233 Address(locations->Out().AsRegister<Register>(), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004234 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004235}
4236
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004237void LocationsBuilderX86::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004238 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004239 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01004240 locations->SetInAt(0, Location::RequiresRegister());
4241 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004242}
4243
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004244void InstructionCodeGeneratorX86::VisitNot(HNot* not_) {
4245 LocationSummary* locations = not_->GetLocations();
Roland Levillain70566432014-10-24 16:20:17 +01004246 Location in = locations->InAt(0);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01004247 Location out = locations->Out();
Roland Levillain70566432014-10-24 16:20:17 +01004248 DCHECK(in.Equals(out));
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00004249 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004250 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00004251 __ notl(out.AsRegister<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004252 break;
4253
4254 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01004255 __ notl(out.AsRegisterPairLow<Register>());
4256 __ notl(out.AsRegisterPairHigh<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004257 break;
4258
4259 default:
4260 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
4261 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004262}
4263
David Brazdil66d126e2015-04-03 16:02:44 +01004264void LocationsBuilderX86::VisitBooleanNot(HBooleanNot* bool_not) {
4265 LocationSummary* locations =
4266 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
4267 locations->SetInAt(0, Location::RequiresRegister());
4268 locations->SetOut(Location::SameAsFirstInput());
4269}
4270
4271void InstructionCodeGeneratorX86::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01004272 LocationSummary* locations = bool_not->GetLocations();
4273 Location in = locations->InAt(0);
4274 Location out = locations->Out();
4275 DCHECK(in.Equals(out));
4276 __ xorl(out.AsRegister<Register>(), Immediate(1));
4277}
4278
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004279void LocationsBuilderX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004280 LocationSummary* locations =
4281 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00004282 switch (compare->InputAt(0)->GetType()) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00004283 case Primitive::kPrimBoolean:
4284 case Primitive::kPrimByte:
4285 case Primitive::kPrimShort:
4286 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08004287 case Primitive::kPrimInt:
Calin Juravleddb7df22014-11-25 20:56:51 +00004288 case Primitive::kPrimLong: {
4289 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravleddb7df22014-11-25 20:56:51 +00004290 locations->SetInAt(1, Location::Any());
4291 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4292 break;
4293 }
4294 case Primitive::kPrimFloat:
4295 case Primitive::kPrimDouble: {
4296 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004297 if (compare->InputAt(1)->IsX86LoadFromConstantTable()) {
4298 DCHECK(compare->InputAt(1)->IsEmittedAtUseSite());
4299 } else if (compare->InputAt(1)->IsConstant()) {
4300 locations->SetInAt(1, Location::RequiresFpuRegister());
4301 } else {
4302 locations->SetInAt(1, Location::Any());
4303 }
Calin Juravleddb7df22014-11-25 20:56:51 +00004304 locations->SetOut(Location::RequiresRegister());
4305 break;
4306 }
4307 default:
4308 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
4309 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004310}
4311
4312void InstructionCodeGeneratorX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004313 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004314 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00004315 Location left = locations->InAt(0);
4316 Location right = locations->InAt(1);
4317
Mark Mendell0c9497d2015-08-21 09:30:05 -04004318 NearLabel less, greater, done;
Aart Bika19616e2016-02-01 18:57:58 -08004319 Condition less_cond = kLess;
4320
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004321 switch (compare->InputAt(0)->GetType()) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00004322 case Primitive::kPrimBoolean:
4323 case Primitive::kPrimByte:
4324 case Primitive::kPrimShort:
4325 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08004326 case Primitive::kPrimInt: {
Roland Levillain0b671c02016-08-19 12:02:34 +01004327 codegen_->GenerateIntCompare(left, right);
Aart Bika19616e2016-02-01 18:57:58 -08004328 break;
4329 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004330 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004331 Register left_low = left.AsRegisterPairLow<Register>();
4332 Register left_high = left.AsRegisterPairHigh<Register>();
4333 int32_t val_low = 0;
4334 int32_t val_high = 0;
4335 bool right_is_const = false;
4336
4337 if (right.IsConstant()) {
4338 DCHECK(right.GetConstant()->IsLongConstant());
4339 right_is_const = true;
4340 int64_t val = right.GetConstant()->AsLongConstant()->GetValue();
4341 val_low = Low32Bits(val);
4342 val_high = High32Bits(val);
4343 }
4344
Calin Juravleddb7df22014-11-25 20:56:51 +00004345 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004346 __ cmpl(left_high, right.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004347 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004348 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004349 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004350 DCHECK(right_is_const) << right;
Aart Bika19616e2016-02-01 18:57:58 -08004351 codegen_->Compare32BitValue(left_high, val_high);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004352 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004353 __ j(kLess, &less); // Signed compare.
4354 __ j(kGreater, &greater); // Signed compare.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004355 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004356 __ cmpl(left_low, right.AsRegisterPairLow<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004357 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004358 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004359 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004360 DCHECK(right_is_const) << right;
Aart Bika19616e2016-02-01 18:57:58 -08004361 codegen_->Compare32BitValue(left_low, val_low);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004362 }
Aart Bika19616e2016-02-01 18:57:58 -08004363 less_cond = kBelow; // for CF (unsigned).
Calin Juravleddb7df22014-11-25 20:56:51 +00004364 break;
4365 }
4366 case Primitive::kPrimFloat: {
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004367 GenerateFPCompare(left, right, compare, false);
Calin Juravleddb7df22014-11-25 20:56:51 +00004368 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08004369 less_cond = kBelow; // for CF (floats).
Calin Juravleddb7df22014-11-25 20:56:51 +00004370 break;
4371 }
4372 case Primitive::kPrimDouble: {
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004373 GenerateFPCompare(left, right, compare, true);
Calin Juravleddb7df22014-11-25 20:56:51 +00004374 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08004375 less_cond = kBelow; // for CF (floats).
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004376 break;
4377 }
4378 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00004379 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004380 }
Aart Bika19616e2016-02-01 18:57:58 -08004381
Calin Juravleddb7df22014-11-25 20:56:51 +00004382 __ movl(out, Immediate(0));
4383 __ j(kEqual, &done);
Aart Bika19616e2016-02-01 18:57:58 -08004384 __ j(less_cond, &less);
Calin Juravleddb7df22014-11-25 20:56:51 +00004385
4386 __ Bind(&greater);
4387 __ movl(out, Immediate(1));
4388 __ jmp(&done);
4389
4390 __ Bind(&less);
4391 __ movl(out, Immediate(-1));
4392
4393 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004394}
4395
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004396void LocationsBuilderX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004397 LocationSummary* locations =
4398 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko372f10e2016-05-17 16:30:10 +01004399 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01004400 locations->SetInAt(i, Location::Any());
4401 }
4402 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004403}
4404
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004405void InstructionCodeGeneratorX86::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004406 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004407}
4408
Roland Levillain7c1559a2015-12-15 10:55:36 +00004409void CodeGeneratorX86::GenerateMemoryBarrier(MemBarrierKind kind) {
Calin Juravle52c48962014-12-16 17:02:57 +00004410 /*
4411 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
4412 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
4413 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
4414 */
4415 switch (kind) {
4416 case MemBarrierKind::kAnyAny: {
Mark P Mendell17077d82015-12-16 19:15:59 +00004417 MemoryFence();
Calin Juravle52c48962014-12-16 17:02:57 +00004418 break;
4419 }
4420 case MemBarrierKind::kAnyStore:
4421 case MemBarrierKind::kLoadAny:
4422 case MemBarrierKind::kStoreStore: {
4423 // nop
4424 break;
4425 }
Mark Mendell7aa04a12016-01-27 22:39:07 -05004426 case MemBarrierKind::kNTStoreStore:
4427 // Non-Temporal Store/Store needs an explicit fence.
4428 MemoryFence(/* non-temporal */ true);
4429 break;
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004430 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004431}
4432
Vladimir Markodc151b22015-10-15 18:02:30 +01004433HInvokeStaticOrDirect::DispatchInfo CodeGeneratorX86::GetSupportedInvokeStaticOrDirectDispatch(
4434 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004435 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00004436 HInvokeStaticOrDirect::DispatchInfo dispatch_info = desired_dispatch_info;
4437
4438 // We disable pc-relative load when there is an irreducible loop, as the optimization
4439 // is incompatible with it.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004440 // TODO: Create as many X86ComputeBaseMethodAddress instructions
4441 // as needed for methods with irreducible loops.
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00004442 if (GetGraph()->HasIrreducibleLoops() &&
4443 (dispatch_info.method_load_kind ==
4444 HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative)) {
4445 dispatch_info.method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod;
4446 }
4447 switch (dispatch_info.code_ptr_location) {
Vladimir Markodc151b22015-10-15 18:02:30 +01004448 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
4449 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
4450 // For direct code, we actually prefer to call via the code pointer from ArtMethod*.
4451 // (Though the direct CALL ptr16:32 is available for consideration).
4452 return HInvokeStaticOrDirect::DispatchInfo {
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00004453 dispatch_info.method_load_kind,
Vladimir Markodc151b22015-10-15 18:02:30 +01004454 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00004455 dispatch_info.method_load_data,
Vladimir Markodc151b22015-10-15 18:02:30 +01004456 0u
4457 };
4458 default:
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00004459 return dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01004460 }
4461}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004462
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004463Register CodeGeneratorX86::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
4464 Register temp) {
4465 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
Vladimir Markoc53c0792015-11-19 15:48:33 +00004466 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004467 if (!invoke->GetLocations()->Intrinsified()) {
4468 return location.AsRegister<Register>();
4469 }
4470 // For intrinsics we allow any location, so it may be on the stack.
4471 if (!location.IsRegister()) {
4472 __ movl(temp, Address(ESP, location.GetStackIndex()));
4473 return temp;
4474 }
4475 // For register locations, check if the register was saved. If so, get it from the stack.
4476 // Note: There is a chance that the register was saved but not overwritten, so we could
4477 // save one load. However, since this is just an intrinsic slow path we prefer this
4478 // simple and more robust approach rather that trying to determine if that's the case.
4479 SlowPathCode* slow_path = GetCurrentSlowPath();
Serguei Katkov288c7a82016-05-16 11:53:15 +06004480 if (slow_path != nullptr) {
4481 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
4482 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
4483 __ movl(temp, Address(ESP, stack_offset));
4484 return temp;
4485 }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004486 }
4487 return location.AsRegister<Register>();
4488}
4489
Serguei Katkov288c7a82016-05-16 11:53:15 +06004490Location CodeGeneratorX86::GenerateCalleeMethodStaticOrDirectCall(HInvokeStaticOrDirect* invoke,
4491 Location temp) {
Vladimir Marko58155012015-08-19 12:49:41 +00004492 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
4493 switch (invoke->GetMethodLoadKind()) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004494 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
Vladimir Marko58155012015-08-19 12:49:41 +00004495 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004496 uint32_t offset =
4497 GetThreadOffset<kX86PointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
4498 __ fs()->movl(temp.AsRegister<Register>(), Address::Absolute(offset));
Vladimir Marko58155012015-08-19 12:49:41 +00004499 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004500 }
Vladimir Marko58155012015-08-19 12:49:41 +00004501 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00004502 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004503 break;
4504 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
4505 __ movl(temp.AsRegister<Register>(), Immediate(invoke->GetMethodAddress()));
4506 break;
4507 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004508 __ movl(temp.AsRegister<Register>(), Immediate(/* placeholder */ 0));
Vladimir Markoaad75c62016-10-03 08:46:48 +00004509 method_patches_.emplace_back(*invoke->GetTargetMethod().dex_file,
4510 invoke->GetTargetMethod().dex_method_index);
Vladimir Marko58155012015-08-19 12:49:41 +00004511 __ Bind(&method_patches_.back().label); // Bind the label at the end of the "movl" insn.
4512 break;
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004513 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
4514 Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
4515 temp.AsRegister<Register>());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004516 __ movl(temp.AsRegister<Register>(), Address(base_reg, kDummy32BitOffset));
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004517 // Bind a new fixup label at the end of the "movl" insn.
4518 uint32_t offset = invoke->GetDexCacheArrayOffset();
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004519 __ Bind(NewPcRelativeDexCacheArrayPatch(invoke->GetDexFile(), offset));
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004520 break;
4521 }
Vladimir Marko58155012015-08-19 12:49:41 +00004522 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004523 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004524 Register method_reg;
4525 Register reg = temp.AsRegister<Register>();
4526 if (current_method.IsRegister()) {
4527 method_reg = current_method.AsRegister<Register>();
4528 } else {
David Brazdil58282f42016-01-14 12:45:10 +00004529 DCHECK(invoke->GetLocations()->Intrinsified());
Vladimir Marko58155012015-08-19 12:49:41 +00004530 DCHECK(!current_method.IsValid());
4531 method_reg = reg;
4532 __ movl(reg, Address(ESP, kCurrentMethodStackOffset));
4533 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00004534 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +01004535 __ movl(reg, Address(method_reg,
4536 ArtMethod::DexCacheResolvedMethodsOffset(kX86PointerSize).Int32Value()));
Vladimir Marko40ecb122016-04-06 17:33:41 +01004537 // temp = temp[index_in_cache];
4538 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
4539 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Vladimir Marko58155012015-08-19 12:49:41 +00004540 __ movl(reg, Address(reg, CodeGenerator::GetCachePointerOffset(index_in_cache)));
4541 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +01004542 }
Vladimir Marko58155012015-08-19 12:49:41 +00004543 }
Serguei Katkov288c7a82016-05-16 11:53:15 +06004544 return callee_method;
4545}
4546
4547void CodeGeneratorX86::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
4548 Location callee_method = GenerateCalleeMethodStaticOrDirectCall(invoke, temp);
Vladimir Marko58155012015-08-19 12:49:41 +00004549
4550 switch (invoke->GetCodePtrLocation()) {
4551 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
4552 __ call(GetFrameEntryLabel());
4553 break;
4554 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative: {
Vladimir Markoaad75c62016-10-03 08:46:48 +00004555 relative_call_patches_.emplace_back(*invoke->GetTargetMethod().dex_file,
4556 invoke->GetTargetMethod().dex_method_index);
Vladimir Marko58155012015-08-19 12:49:41 +00004557 Label* label = &relative_call_patches_.back().label;
4558 __ call(label); // Bind to the patch label, override at link time.
4559 __ Bind(label); // Bind the label at the end of the "call" insn.
4560 break;
4561 }
4562 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
4563 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
Vladimir Markodc151b22015-10-15 18:02:30 +01004564 // Filtered out by GetSupportedInvokeStaticOrDirectDispatch().
4565 LOG(FATAL) << "Unsupported";
4566 UNREACHABLE();
Vladimir Marko58155012015-08-19 12:49:41 +00004567 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
4568 // (callee_method + offset_of_quick_compiled_code)()
4569 __ call(Address(callee_method.AsRegister<Register>(),
4570 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07004571 kX86PointerSize).Int32Value()));
Vladimir Marko58155012015-08-19 12:49:41 +00004572 break;
Mark Mendell09ed1a32015-03-25 08:30:06 -04004573 }
4574
4575 DCHECK(!IsLeafMethod());
Mark Mendell09ed1a32015-03-25 08:30:06 -04004576}
4577
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004578void CodeGeneratorX86::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_in) {
4579 Register temp = temp_in.AsRegister<Register>();
4580 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
4581 invoke->GetVTableIndex(), kX86PointerSize).Uint32Value();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004582
4583 // Use the calling convention instead of the location of the receiver, as
4584 // intrinsics may have put the receiver in a different register. In the intrinsics
4585 // slow path, the arguments have been moved to the right place, so here we are
4586 // guaranteed that the receiver is the first register of the calling convention.
4587 InvokeDexCallingConvention calling_convention;
4588 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004589 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004590 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004591 __ movl(temp, Address(receiver, class_offset));
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004592 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00004593 // Instead of simply (possibly) unpoisoning `temp` here, we should
4594 // emit a read barrier for the previous class reference load.
4595 // However this is not required in practice, as this is an
4596 // intermediate/temporary reference and because the current
4597 // concurrent copying collector keeps the from-space memory
4598 // intact/accessible until the end of the marking phase (the
4599 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004600 __ MaybeUnpoisonHeapReference(temp);
4601 // temp = temp->GetMethodAt(method_offset);
4602 __ movl(temp, Address(temp, method_offset));
4603 // call temp->GetEntryPoint();
4604 __ call(Address(
Andreas Gampe542451c2016-07-26 09:02:02 -07004605 temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86PointerSize).Int32Value()));
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004606}
4607
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004608void CodeGeneratorX86::RecordSimplePatch() {
4609 if (GetCompilerOptions().GetIncludePatchInformation()) {
4610 simple_patches_.emplace_back();
4611 __ Bind(&simple_patches_.back());
4612 }
4613}
4614
Vladimir Markoaad75c62016-10-03 08:46:48 +00004615void CodeGeneratorX86::RecordBootStringPatch(HLoadString* load_string) {
4616 DCHECK(GetCompilerOptions().IsBootImage());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004617 string_patches_.emplace_back(load_string->GetDexFile(), load_string->GetStringIndex());
4618 __ Bind(&string_patches_.back().label);
4619}
4620
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004621void CodeGeneratorX86::RecordTypePatch(HLoadClass* load_class) {
4622 type_patches_.emplace_back(load_class->GetDexFile(), load_class->GetTypeIndex());
4623 __ Bind(&type_patches_.back().label);
4624}
4625
Vladimir Markoaad75c62016-10-03 08:46:48 +00004626Label* CodeGeneratorX86::NewStringBssEntryPatch(HLoadString* load_string) {
4627 DCHECK(!GetCompilerOptions().IsBootImage());
4628 string_patches_.emplace_back(load_string->GetDexFile(), load_string->GetStringIndex());
4629 return &string_patches_.back().label;
4630}
4631
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004632Label* CodeGeneratorX86::NewPcRelativeDexCacheArrayPatch(const DexFile& dex_file,
4633 uint32_t element_offset) {
4634 // Add the patch entry and bind its label at the end of the instruction.
4635 pc_relative_dex_cache_patches_.emplace_back(dex_file, element_offset);
4636 return &pc_relative_dex_cache_patches_.back().label;
4637}
4638
Vladimir Markoaad75c62016-10-03 08:46:48 +00004639// The label points to the end of the "movl" or another instruction but the literal offset
4640// for method patch needs to point to the embedded constant which occupies the last 4 bytes.
4641constexpr uint32_t kLabelPositionToLiteralOffsetAdjustment = 4u;
4642
4643template <LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
4644inline void CodeGeneratorX86::EmitPcRelativeLinkerPatches(
4645 const ArenaDeque<PatchInfo<Label>>& infos,
4646 ArenaVector<LinkerPatch>* linker_patches) {
4647 for (const PatchInfo<Label>& info : infos) {
4648 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
4649 linker_patches->push_back(
4650 Factory(literal_offset, &info.dex_file, GetMethodAddressOffset(), info.index));
4651 }
4652}
4653
Vladimir Marko58155012015-08-19 12:49:41 +00004654void CodeGeneratorX86::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
4655 DCHECK(linker_patches->empty());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004656 size_t size =
4657 method_patches_.size() +
4658 relative_call_patches_.size() +
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004659 pc_relative_dex_cache_patches_.size() +
4660 simple_patches_.size() +
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004661 string_patches_.size() +
4662 type_patches_.size();
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004663 linker_patches->reserve(size);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004664 for (const PatchInfo<Label>& info : method_patches_) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004665 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Markoaad75c62016-10-03 08:46:48 +00004666 linker_patches->push_back(LinkerPatch::MethodPatch(literal_offset, &info.dex_file, info.index));
Vladimir Marko58155012015-08-19 12:49:41 +00004667 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00004668 for (const PatchInfo<Label>& info : relative_call_patches_) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004669 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Markoaad75c62016-10-03 08:46:48 +00004670 linker_patches->push_back(
4671 LinkerPatch::RelativeCodePatch(literal_offset, &info.dex_file, info.index));
Vladimir Marko58155012015-08-19 12:49:41 +00004672 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00004673 EmitPcRelativeLinkerPatches<LinkerPatch::DexCacheArrayPatch>(pc_relative_dex_cache_patches_,
4674 linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004675 for (const Label& label : simple_patches_) {
4676 uint32_t literal_offset = label.Position() - kLabelPositionToLiteralOffsetAdjustment;
4677 linker_patches->push_back(LinkerPatch::RecordPosition(literal_offset));
4678 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00004679 if (!GetCompilerOptions().IsBootImage()) {
4680 EmitPcRelativeLinkerPatches<LinkerPatch::StringBssEntryPatch>(string_patches_, linker_patches);
4681 } else if (GetCompilerOptions().GetCompilePic()) {
4682 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeStringPatch>(string_patches_, linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004683 } else {
Vladimir Markoaad75c62016-10-03 08:46:48 +00004684 for (const PatchInfo<Label>& info : string_patches_) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004685 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Markoaad75c62016-10-03 08:46:48 +00004686 linker_patches->push_back(
4687 LinkerPatch::StringPatch(literal_offset, &info.dex_file, info.index));
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004688 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00004689 }
4690 if (GetCompilerOptions().GetCompilePic()) {
4691 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeTypePatch>(type_patches_, linker_patches);
4692 } else {
4693 for (const PatchInfo<Label>& info : type_patches_) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004694 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Markoaad75c62016-10-03 08:46:48 +00004695 linker_patches->push_back(LinkerPatch::TypePatch(literal_offset, &info.dex_file, info.index));
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004696 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004697 }
Vladimir Marko58155012015-08-19 12:49:41 +00004698}
4699
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004700void CodeGeneratorX86::MarkGCCard(Register temp,
4701 Register card,
4702 Register object,
4703 Register value,
4704 bool value_can_be_null) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004705 NearLabel is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004706 if (value_can_be_null) {
4707 __ testl(value, value);
4708 __ j(kEqual, &is_null);
4709 }
Andreas Gampe542451c2016-07-26 09:02:02 -07004710 __ fs()->movl(card, Address::Absolute(Thread::CardTableOffset<kX86PointerSize>().Int32Value()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004711 __ movl(temp, object);
4712 __ shrl(temp, Immediate(gc::accounting::CardTable::kCardShift));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00004713 __ movb(Address(temp, card, TIMES_1, 0),
4714 X86ManagedRegister::FromCpuRegister(card).AsByteRegister());
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004715 if (value_can_be_null) {
4716 __ Bind(&is_null);
4717 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004718}
4719
Calin Juravle52c48962014-12-16 17:02:57 +00004720void LocationsBuilderX86::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
4721 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain0d5a2812015-11-13 10:07:31 +00004722
4723 bool object_field_get_with_read_barrier =
4724 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004725 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00004726 new (GetGraph()->GetArena()) LocationSummary(instruction,
4727 kEmitCompilerReadBarrier ?
4728 LocationSummary::kCallOnSlowPath :
4729 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01004730 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004731 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01004732 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004733 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004734
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004735 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4736 locations->SetOut(Location::RequiresFpuRegister());
4737 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004738 // The output overlaps in case of long: we don't want the low move
4739 // to overwrite the object's location. Likewise, in the case of
4740 // an object field get with read barriers enabled, we do not want
4741 // the move to overwrite the object's location, as we need it to emit
4742 // the read barrier.
4743 locations->SetOut(
4744 Location::RequiresRegister(),
4745 (object_field_get_with_read_barrier || instruction->GetType() == Primitive::kPrimLong) ?
4746 Location::kOutputOverlap :
4747 Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004748 }
Calin Juravle52c48962014-12-16 17:02:57 +00004749
4750 if (field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong)) {
4751 // Long values can be loaded atomically into an XMM using movsd.
Roland Levillain7c1559a2015-12-15 10:55:36 +00004752 // So we use an XMM register as a temp to achieve atomicity (first
4753 // load the temp into the XMM and then copy the XMM into the
4754 // output, 32 bits at a time).
Calin Juravle52c48962014-12-16 17:02:57 +00004755 locations->AddTemp(Location::RequiresFpuRegister());
4756 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004757}
4758
Calin Juravle52c48962014-12-16 17:02:57 +00004759void InstructionCodeGeneratorX86::HandleFieldGet(HInstruction* instruction,
4760 const FieldInfo& field_info) {
4761 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004762
Calin Juravle52c48962014-12-16 17:02:57 +00004763 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004764 Location base_loc = locations->InAt(0);
4765 Register base = base_loc.AsRegister<Register>();
Calin Juravle52c48962014-12-16 17:02:57 +00004766 Location out = locations->Out();
4767 bool is_volatile = field_info.IsVolatile();
4768 Primitive::Type field_type = field_info.GetFieldType();
4769 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4770
4771 switch (field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004772 case Primitive::kPrimBoolean: {
Calin Juravle52c48962014-12-16 17:02:57 +00004773 __ movzxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004774 break;
4775 }
4776
4777 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00004778 __ movsxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004779 break;
4780 }
4781
4782 case Primitive::kPrimShort: {
Calin Juravle52c48962014-12-16 17:02:57 +00004783 __ movsxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004784 break;
4785 }
4786
4787 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00004788 __ movzxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004789 break;
4790 }
4791
4792 case Primitive::kPrimInt:
Calin Juravle52c48962014-12-16 17:02:57 +00004793 __ movl(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004794 break;
Roland Levillain7c1559a2015-12-15 10:55:36 +00004795
4796 case Primitive::kPrimNot: {
4797 // /* HeapReference<Object> */ out = *(base + offset)
4798 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004799 // Note that a potential implicit null check is handled in this
4800 // CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier call.
4801 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00004802 instruction, out, base, offset, /* needs_null_check */ true);
Roland Levillain7c1559a2015-12-15 10:55:36 +00004803 if (is_volatile) {
4804 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4805 }
4806 } else {
4807 __ movl(out.AsRegister<Register>(), Address(base, offset));
4808 codegen_->MaybeRecordImplicitNullCheck(instruction);
4809 if (is_volatile) {
4810 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4811 }
4812 // If read barriers are enabled, emit read barriers other than
4813 // Baker's using a slow path (and also unpoison the loaded
4814 // reference, if heap poisoning is enabled).
4815 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
4816 }
4817 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004818 }
4819
4820 case Primitive::kPrimLong: {
Calin Juravle52c48962014-12-16 17:02:57 +00004821 if (is_volatile) {
4822 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4823 __ movsd(temp, Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004824 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004825 __ movd(out.AsRegisterPairLow<Register>(), temp);
4826 __ psrlq(temp, Immediate(32));
4827 __ movd(out.AsRegisterPairHigh<Register>(), temp);
4828 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004829 DCHECK_NE(base, out.AsRegisterPairLow<Register>());
Calin Juravle52c48962014-12-16 17:02:57 +00004830 __ movl(out.AsRegisterPairLow<Register>(), Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004831 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004832 __ movl(out.AsRegisterPairHigh<Register>(), Address(base, kX86WordSize + offset));
4833 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004834 break;
4835 }
4836
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004837 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00004838 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004839 break;
4840 }
4841
4842 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00004843 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004844 break;
4845 }
4846
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004847 case Primitive::kPrimVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00004848 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004849 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004850 }
Calin Juravle52c48962014-12-16 17:02:57 +00004851
Roland Levillain7c1559a2015-12-15 10:55:36 +00004852 if (field_type == Primitive::kPrimNot || field_type == Primitive::kPrimLong) {
4853 // Potential implicit null checks, in the case of reference or
4854 // long fields, are handled in the previous switch statement.
4855 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00004856 codegen_->MaybeRecordImplicitNullCheck(instruction);
4857 }
4858
Calin Juravle52c48962014-12-16 17:02:57 +00004859 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004860 if (field_type == Primitive::kPrimNot) {
4861 // Memory barriers, in the case of references, are also handled
4862 // in the previous switch statement.
4863 } else {
4864 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4865 }
Roland Levillain4d027112015-07-01 15:41:14 +01004866 }
Calin Juravle52c48962014-12-16 17:02:57 +00004867}
4868
4869void LocationsBuilderX86::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
4870 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4871
4872 LocationSummary* locations =
4873 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4874 locations->SetInAt(0, Location::RequiresRegister());
4875 bool is_volatile = field_info.IsVolatile();
4876 Primitive::Type field_type = field_info.GetFieldType();
4877 bool is_byte_type = (field_type == Primitive::kPrimBoolean)
4878 || (field_type == Primitive::kPrimByte);
4879
4880 // The register allocator does not support multiple
4881 // inputs that die at entry with one in a specific register.
4882 if (is_byte_type) {
4883 // Ensure the value is in a byte register.
4884 locations->SetInAt(1, Location::RegisterLocation(EAX));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004885 } else if (Primitive::IsFloatingPointType(field_type)) {
Mark Mendell81489372015-11-04 11:30:41 -05004886 if (is_volatile && field_type == Primitive::kPrimDouble) {
4887 // In order to satisfy the semantics of volatile, this must be a single instruction store.
4888 locations->SetInAt(1, Location::RequiresFpuRegister());
4889 } else {
4890 locations->SetInAt(1, Location::FpuRegisterOrConstant(instruction->InputAt(1)));
4891 }
4892 } else if (is_volatile && field_type == Primitive::kPrimLong) {
4893 // In order to satisfy the semantics of volatile, this must be a single instruction store.
Calin Juravle52c48962014-12-16 17:02:57 +00004894 locations->SetInAt(1, Location::RequiresRegister());
Mark Mendell81489372015-11-04 11:30:41 -05004895
Calin Juravle52c48962014-12-16 17:02:57 +00004896 // 64bits value can be atomically written to an address with movsd and an XMM register.
4897 // We need two XMM registers because there's no easier way to (bit) copy a register pair
4898 // into a single XMM register (we copy each pair part into the XMMs and then interleave them).
4899 // NB: We could make the register allocator understand fp_reg <-> core_reg moves but given the
4900 // isolated cases when we need this it isn't worth adding the extra complexity.
4901 locations->AddTemp(Location::RequiresFpuRegister());
4902 locations->AddTemp(Location::RequiresFpuRegister());
Mark Mendell81489372015-11-04 11:30:41 -05004903 } else {
4904 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4905
4906 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
4907 // Temporary registers for the write barrier.
4908 locations->AddTemp(Location::RequiresRegister()); // May be used for reference poisoning too.
4909 // Ensure the card is in a byte register.
4910 locations->AddTemp(Location::RegisterLocation(ECX));
4911 }
Calin Juravle52c48962014-12-16 17:02:57 +00004912 }
4913}
4914
4915void InstructionCodeGeneratorX86::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004916 const FieldInfo& field_info,
4917 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00004918 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4919
4920 LocationSummary* locations = instruction->GetLocations();
4921 Register base = locations->InAt(0).AsRegister<Register>();
4922 Location value = locations->InAt(1);
4923 bool is_volatile = field_info.IsVolatile();
4924 Primitive::Type field_type = field_info.GetFieldType();
4925 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01004926 bool needs_write_barrier =
4927 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00004928
4929 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004930 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
Calin Juravle52c48962014-12-16 17:02:57 +00004931 }
4932
Mark Mendell81489372015-11-04 11:30:41 -05004933 bool maybe_record_implicit_null_check_done = false;
4934
Calin Juravle52c48962014-12-16 17:02:57 +00004935 switch (field_type) {
4936 case Primitive::kPrimBoolean:
4937 case Primitive::kPrimByte: {
4938 __ movb(Address(base, offset), value.AsRegister<ByteRegister>());
4939 break;
4940 }
4941
4942 case Primitive::kPrimShort:
4943 case Primitive::kPrimChar: {
Mark Mendell81489372015-11-04 11:30:41 -05004944 if (value.IsConstant()) {
4945 int16_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4946 __ movw(Address(base, offset), Immediate(v));
4947 } else {
4948 __ movw(Address(base, offset), value.AsRegister<Register>());
4949 }
Calin Juravle52c48962014-12-16 17:02:57 +00004950 break;
4951 }
4952
4953 case Primitive::kPrimInt:
4954 case Primitive::kPrimNot: {
Roland Levillain4d027112015-07-01 15:41:14 +01004955 if (kPoisonHeapReferences && needs_write_barrier) {
4956 // Note that in the case where `value` is a null reference,
4957 // we do not enter this block, as the reference does not
4958 // need poisoning.
4959 DCHECK_EQ(field_type, Primitive::kPrimNot);
4960 Register temp = locations->GetTemp(0).AsRegister<Register>();
4961 __ movl(temp, value.AsRegister<Register>());
4962 __ PoisonHeapReference(temp);
4963 __ movl(Address(base, offset), temp);
Mark Mendell81489372015-11-04 11:30:41 -05004964 } else if (value.IsConstant()) {
4965 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4966 __ movl(Address(base, offset), Immediate(v));
Roland Levillain4d027112015-07-01 15:41:14 +01004967 } else {
Nicolas Geoffray03971632016-03-17 10:44:24 +00004968 DCHECK(value.IsRegister()) << value;
Roland Levillain4d027112015-07-01 15:41:14 +01004969 __ movl(Address(base, offset), value.AsRegister<Register>());
4970 }
Calin Juravle52c48962014-12-16 17:02:57 +00004971 break;
4972 }
4973
4974 case Primitive::kPrimLong: {
4975 if (is_volatile) {
4976 XmmRegister temp1 = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4977 XmmRegister temp2 = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
4978 __ movd(temp1, value.AsRegisterPairLow<Register>());
4979 __ movd(temp2, value.AsRegisterPairHigh<Register>());
4980 __ punpckldq(temp1, temp2);
4981 __ movsd(Address(base, offset), temp1);
Calin Juravle77520bc2015-01-12 18:45:46 +00004982 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell81489372015-11-04 11:30:41 -05004983 } else if (value.IsConstant()) {
4984 int64_t v = CodeGenerator::GetInt64ValueOf(value.GetConstant());
4985 __ movl(Address(base, offset), Immediate(Low32Bits(v)));
4986 codegen_->MaybeRecordImplicitNullCheck(instruction);
4987 __ movl(Address(base, kX86WordSize + offset), Immediate(High32Bits(v)));
Calin Juravle52c48962014-12-16 17:02:57 +00004988 } else {
4989 __ movl(Address(base, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00004990 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004991 __ movl(Address(base, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
4992 }
Mark Mendell81489372015-11-04 11:30:41 -05004993 maybe_record_implicit_null_check_done = true;
Calin Juravle52c48962014-12-16 17:02:57 +00004994 break;
4995 }
4996
4997 case Primitive::kPrimFloat: {
Mark Mendell81489372015-11-04 11:30:41 -05004998 if (value.IsConstant()) {
4999 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
5000 __ movl(Address(base, offset), Immediate(v));
5001 } else {
5002 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
5003 }
Calin Juravle52c48962014-12-16 17:02:57 +00005004 break;
5005 }
5006
5007 case Primitive::kPrimDouble: {
Mark Mendell81489372015-11-04 11:30:41 -05005008 if (value.IsConstant()) {
5009 int64_t v = CodeGenerator::GetInt64ValueOf(value.GetConstant());
5010 __ movl(Address(base, offset), Immediate(Low32Bits(v)));
5011 codegen_->MaybeRecordImplicitNullCheck(instruction);
5012 __ movl(Address(base, kX86WordSize + offset), Immediate(High32Bits(v)));
5013 maybe_record_implicit_null_check_done = true;
5014 } else {
5015 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
5016 }
Calin Juravle52c48962014-12-16 17:02:57 +00005017 break;
5018 }
5019
5020 case Primitive::kPrimVoid:
5021 LOG(FATAL) << "Unreachable type " << field_type;
5022 UNREACHABLE();
5023 }
5024
Mark Mendell81489372015-11-04 11:30:41 -05005025 if (!maybe_record_implicit_null_check_done) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005026 codegen_->MaybeRecordImplicitNullCheck(instruction);
5027 }
5028
Roland Levillain4d027112015-07-01 15:41:14 +01005029 if (needs_write_barrier) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005030 Register temp = locations->GetTemp(0).AsRegister<Register>();
5031 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005032 codegen_->MarkGCCard(temp, card, base, value.AsRegister<Register>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00005033 }
5034
Calin Juravle52c48962014-12-16 17:02:57 +00005035 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005036 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
Calin Juravle52c48962014-12-16 17:02:57 +00005037 }
5038}
5039
5040void LocationsBuilderX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
5041 HandleFieldGet(instruction, instruction->GetFieldInfo());
5042}
5043
5044void InstructionCodeGeneratorX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
5045 HandleFieldGet(instruction, instruction->GetFieldInfo());
5046}
5047
5048void LocationsBuilderX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
5049 HandleFieldSet(instruction, instruction->GetFieldInfo());
5050}
5051
5052void InstructionCodeGeneratorX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005053 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00005054}
5055
5056void LocationsBuilderX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
5057 HandleFieldSet(instruction, instruction->GetFieldInfo());
5058}
5059
5060void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005061 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00005062}
5063
5064void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
5065 HandleFieldGet(instruction, instruction->GetFieldInfo());
5066}
5067
5068void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
5069 HandleFieldGet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005070}
5071
Calin Juravlee460d1d2015-09-29 04:52:17 +01005072void LocationsBuilderX86::VisitUnresolvedInstanceFieldGet(
5073 HUnresolvedInstanceFieldGet* instruction) {
5074 FieldAccessCallingConventionX86 calling_convention;
5075 codegen_->CreateUnresolvedFieldLocationSummary(
5076 instruction, instruction->GetFieldType(), calling_convention);
5077}
5078
5079void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldGet(
5080 HUnresolvedInstanceFieldGet* instruction) {
5081 FieldAccessCallingConventionX86 calling_convention;
5082 codegen_->GenerateUnresolvedFieldAccess(instruction,
5083 instruction->GetFieldType(),
5084 instruction->GetFieldIndex(),
5085 instruction->GetDexPc(),
5086 calling_convention);
5087}
5088
5089void LocationsBuilderX86::VisitUnresolvedInstanceFieldSet(
5090 HUnresolvedInstanceFieldSet* instruction) {
5091 FieldAccessCallingConventionX86 calling_convention;
5092 codegen_->CreateUnresolvedFieldLocationSummary(
5093 instruction, instruction->GetFieldType(), calling_convention);
5094}
5095
5096void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldSet(
5097 HUnresolvedInstanceFieldSet* instruction) {
5098 FieldAccessCallingConventionX86 calling_convention;
5099 codegen_->GenerateUnresolvedFieldAccess(instruction,
5100 instruction->GetFieldType(),
5101 instruction->GetFieldIndex(),
5102 instruction->GetDexPc(),
5103 calling_convention);
5104}
5105
5106void LocationsBuilderX86::VisitUnresolvedStaticFieldGet(
5107 HUnresolvedStaticFieldGet* instruction) {
5108 FieldAccessCallingConventionX86 calling_convention;
5109 codegen_->CreateUnresolvedFieldLocationSummary(
5110 instruction, instruction->GetFieldType(), calling_convention);
5111}
5112
5113void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldGet(
5114 HUnresolvedStaticFieldGet* instruction) {
5115 FieldAccessCallingConventionX86 calling_convention;
5116 codegen_->GenerateUnresolvedFieldAccess(instruction,
5117 instruction->GetFieldType(),
5118 instruction->GetFieldIndex(),
5119 instruction->GetDexPc(),
5120 calling_convention);
5121}
5122
5123void LocationsBuilderX86::VisitUnresolvedStaticFieldSet(
5124 HUnresolvedStaticFieldSet* instruction) {
5125 FieldAccessCallingConventionX86 calling_convention;
5126 codegen_->CreateUnresolvedFieldLocationSummary(
5127 instruction, instruction->GetFieldType(), calling_convention);
5128}
5129
5130void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldSet(
5131 HUnresolvedStaticFieldSet* instruction) {
5132 FieldAccessCallingConventionX86 calling_convention;
5133 codegen_->GenerateUnresolvedFieldAccess(instruction,
5134 instruction->GetFieldType(),
5135 instruction->GetFieldIndex(),
5136 instruction->GetDexPc(),
5137 calling_convention);
5138}
5139
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005140void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005141 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
5142 Location loc = codegen_->GetCompilerOptions().GetImplicitNullChecks()
5143 ? Location::RequiresRegister()
5144 : Location::Any();
5145 locations->SetInAt(0, loc);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005146}
5147
Calin Juravle2ae48182016-03-16 14:05:09 +00005148void CodeGeneratorX86::GenerateImplicitNullCheck(HNullCheck* instruction) {
5149 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005150 return;
5151 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005152 LocationSummary* locations = instruction->GetLocations();
5153 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00005154
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005155 __ testl(EAX, Address(obj.AsRegister<Register>(), 0));
Calin Juravle2ae48182016-03-16 14:05:09 +00005156 RecordPcInfo(instruction, instruction->GetDexPc());
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005157}
5158
Calin Juravle2ae48182016-03-16 14:05:09 +00005159void CodeGeneratorX86::GenerateExplicitNullCheck(HNullCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07005160 SlowPathCode* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00005161 AddSlowPath(slow_path);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005162
5163 LocationSummary* locations = instruction->GetLocations();
5164 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005165
5166 if (obj.IsRegister()) {
Mark Mendell42514f62015-03-31 11:34:22 -04005167 __ testl(obj.AsRegister<Register>(), obj.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005168 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005169 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005170 } else {
5171 DCHECK(obj.IsConstant()) << obj;
David Brazdil77a48ae2015-09-15 12:34:04 +00005172 DCHECK(obj.GetConstant()->IsNullConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005173 __ jmp(slow_path->GetEntryLabel());
5174 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005175 }
5176 __ j(kEqual, slow_path->GetEntryLabel());
5177}
5178
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005179void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00005180 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005181}
5182
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005183void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005184 bool object_array_get_with_read_barrier =
5185 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005186 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00005187 new (GetGraph()->GetArena()) LocationSummary(instruction,
5188 object_array_get_with_read_barrier ?
5189 LocationSummary::kCallOnSlowPath :
5190 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01005191 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005192 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01005193 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005194 locations->SetInAt(0, Location::RequiresRegister());
5195 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005196 if (Primitive::IsFloatingPointType(instruction->GetType())) {
5197 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5198 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005199 // The output overlaps in case of long: we don't want the low move
5200 // to overwrite the array's location. Likewise, in the case of an
5201 // object array get with read barriers enabled, we do not want the
5202 // move to overwrite the array's location, as we need it to emit
5203 // the read barrier.
5204 locations->SetOut(
5205 Location::RequiresRegister(),
5206 (instruction->GetType() == Primitive::kPrimLong || object_array_get_with_read_barrier) ?
5207 Location::kOutputOverlap :
5208 Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005209 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005210}
5211
5212void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
5213 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005214 Location obj_loc = locations->InAt(0);
5215 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005216 Location index = locations->InAt(1);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005217 Location out_loc = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005218 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005219
Calin Juravle77520bc2015-01-12 18:45:46 +00005220 Primitive::Type type = instruction->GetType();
5221 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005222 case Primitive::kPrimBoolean: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005223 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005224 __ movzxb(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005225 break;
5226 }
5227
5228 case Primitive::kPrimByte: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005229 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005230 __ movsxb(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005231 break;
5232 }
5233
5234 case Primitive::kPrimShort: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005235 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005236 __ movsxw(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005237 break;
5238 }
5239
5240 case Primitive::kPrimChar: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005241 Register out = out_loc.AsRegister<Register>();
jessicahandojo4877b792016-09-08 19:49:13 -07005242 if (mirror::kUseStringCompression && instruction->IsStringCharAt()) {
5243 // Branch cases into compressed and uncompressed for each index's type.
5244 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
5245 NearLabel done, not_compressed;
5246 __ cmpl(Address(obj, count_offset), Immediate(0));
5247 codegen_->MaybeRecordImplicitNullCheck(instruction);
5248 __ j(kGreaterEqual, &not_compressed);
5249 __ movzxb(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_1, data_offset));
5250 __ jmp(&done);
5251 __ Bind(&not_compressed);
5252 __ movzxw(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_2, data_offset));
5253 __ Bind(&done);
5254 } else {
5255 // Common case for charAt of array of char or when string compression's
5256 // feature is turned off.
5257 __ movzxw(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_2, data_offset));
5258 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005259 break;
5260 }
5261
Roland Levillain7c1559a2015-12-15 10:55:36 +00005262 case Primitive::kPrimInt: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005263 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005264 __ movl(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005265 break;
5266 }
5267
Roland Levillain7c1559a2015-12-15 10:55:36 +00005268 case Primitive::kPrimNot: {
5269 static_assert(
5270 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
5271 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain7c1559a2015-12-15 10:55:36 +00005272 // /* HeapReference<Object> */ out =
5273 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
5274 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005275 // Note that a potential implicit null check is handled in this
5276 // CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier call.
5277 codegen_->GenerateArrayLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00005278 instruction, out_loc, obj, data_offset, index, /* needs_null_check */ true);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005279 } else {
5280 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005281 __ movl(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset));
5282 codegen_->MaybeRecordImplicitNullCheck(instruction);
5283 // If read barriers are enabled, emit read barriers other than
5284 // Baker's using a slow path (and also unpoison the loaded
5285 // reference, if heap poisoning is enabled).
Roland Levillain7c1559a2015-12-15 10:55:36 +00005286 if (index.IsConstant()) {
5287 uint32_t offset =
5288 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Roland Levillain7c1559a2015-12-15 10:55:36 +00005289 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
5290 } else {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005291 codegen_->MaybeGenerateReadBarrierSlow(
5292 instruction, out_loc, out_loc, obj_loc, data_offset, index);
5293 }
5294 }
5295 break;
5296 }
5297
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005298 case Primitive::kPrimLong: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005299 DCHECK_NE(obj, out_loc.AsRegisterPairLow<Register>());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005300 __ movl(out_loc.AsRegisterPairLow<Register>(),
5301 CodeGeneratorX86::ArrayAddress(obj, index, TIMES_8, data_offset));
5302 codegen_->MaybeRecordImplicitNullCheck(instruction);
5303 __ movl(out_loc.AsRegisterPairHigh<Register>(),
5304 CodeGeneratorX86::ArrayAddress(obj, index, TIMES_8, data_offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005305 break;
5306 }
5307
Mark Mendell7c8d0092015-01-26 11:21:33 -05005308 case Primitive::kPrimFloat: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005309 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005310 __ movss(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005311 break;
5312 }
5313
5314 case Primitive::kPrimDouble: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005315 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005316 __ movsd(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_8, data_offset));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005317 break;
5318 }
5319
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005320 case Primitive::kPrimVoid:
Calin Juravle77520bc2015-01-12 18:45:46 +00005321 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07005322 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005323 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005324
Roland Levillain7c1559a2015-12-15 10:55:36 +00005325 if (type == Primitive::kPrimNot || type == Primitive::kPrimLong) {
5326 // Potential implicit null checks, in the case of reference or
5327 // long arrays, are handled in the previous switch statement.
5328 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00005329 codegen_->MaybeRecordImplicitNullCheck(instruction);
5330 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005331}
5332
5333void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01005334 Primitive::Type value_type = instruction->GetComponentType();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005335
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005336 bool needs_write_barrier =
5337 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Roland Levillain0d5a2812015-11-13 10:07:31 +00005338 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005339
Nicolas Geoffray39468442014-09-02 15:17:15 +01005340 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
5341 instruction,
Vladimir Marko8d49fd72016-08-25 15:20:47 +01005342 may_need_runtime_call_for_type_check ?
Roland Levillain0d5a2812015-11-13 10:07:31 +00005343 LocationSummary::kCallOnSlowPath :
5344 LocationSummary::kNoCall);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005345
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005346 bool is_byte_type = (value_type == Primitive::kPrimBoolean)
5347 || (value_type == Primitive::kPrimByte);
5348 // We need the inputs to be different than the output in case of long operation.
5349 // In case of a byte operation, the register allocator does not support multiple
5350 // inputs that die at entry with one in a specific register.
5351 locations->SetInAt(0, Location::RequiresRegister());
5352 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
5353 if (is_byte_type) {
5354 // Ensure the value is in a byte register.
5355 locations->SetInAt(2, Location::ByteRegisterOrConstant(EAX, instruction->InputAt(2)));
5356 } else if (Primitive::IsFloatingPointType(value_type)) {
Mark Mendell81489372015-11-04 11:30:41 -05005357 locations->SetInAt(2, Location::FpuRegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005358 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005359 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
5360 }
5361 if (needs_write_barrier) {
5362 // Temporary registers for the write barrier.
5363 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
5364 // Ensure the card is in a byte register.
Roland Levillain4f6b0b52015-11-23 19:29:22 +00005365 locations->AddTemp(Location::RegisterLocation(ECX));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005366 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005367}
5368
5369void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
5370 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005371 Location array_loc = locations->InAt(0);
5372 Register array = array_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005373 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005374 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005375 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005376 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
5377 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5378 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005379 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005380 bool needs_write_barrier =
5381 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005382
5383 switch (value_type) {
5384 case Primitive::kPrimBoolean:
5385 case Primitive::kPrimByte: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005386 uint32_t offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005387 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_1, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005388 if (value.IsRegister()) {
5389 __ movb(address, value.AsRegister<ByteRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005390 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005391 __ movb(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005392 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005393 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005394 break;
5395 }
5396
5397 case Primitive::kPrimShort:
5398 case Primitive::kPrimChar: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005399 uint32_t offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005400 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_2, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005401 if (value.IsRegister()) {
5402 __ movw(address, value.AsRegister<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005403 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005404 __ movw(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005405 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005406 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005407 break;
5408 }
5409
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005410 case Primitive::kPrimNot: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005411 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005412 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_4, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005413
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005414 if (!value.IsRegister()) {
5415 // Just setting null.
5416 DCHECK(instruction->InputAt(2)->IsNullConstant());
5417 DCHECK(value.IsConstant()) << value;
5418 __ movl(address, Immediate(0));
Calin Juravle77520bc2015-01-12 18:45:46 +00005419 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005420 DCHECK(!needs_write_barrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005421 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005422 break;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005423 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005424
5425 DCHECK(needs_write_barrier);
5426 Register register_value = value.AsRegister<Register>();
Roland Levillain16d9f942016-08-25 17:27:56 +01005427 // We cannot use a NearLabel for `done`, as its range may be too
5428 // short when Baker read barriers are enabled.
5429 Label done;
5430 NearLabel not_null, do_put;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005431 SlowPathCode* slow_path = nullptr;
Roland Levillain16d9f942016-08-25 17:27:56 +01005432 Location temp_loc = locations->GetTemp(0);
5433 Register temp = temp_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005434 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005435 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathX86(instruction);
5436 codegen_->AddSlowPath(slow_path);
5437 if (instruction->GetValueCanBeNull()) {
5438 __ testl(register_value, register_value);
5439 __ j(kNotEqual, &not_null);
5440 __ movl(address, Immediate(0));
5441 codegen_->MaybeRecordImplicitNullCheck(instruction);
5442 __ jmp(&done);
5443 __ Bind(&not_null);
5444 }
5445
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005446 // Note that when Baker read barriers are enabled, the type
5447 // checks are performed without read barriers. This is fine,
5448 // even in the case where a class object is in the from-space
5449 // after the flip, as a comparison involving such a type would
5450 // not produce a false positive; it may of course produce a
5451 // false negative, in which case we would take the ArraySet
5452 // slow path.
Roland Levillain16d9f942016-08-25 17:27:56 +01005453
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005454 // /* HeapReference<Class> */ temp = array->klass_
5455 __ movl(temp, Address(array, class_offset));
5456 codegen_->MaybeRecordImplicitNullCheck(instruction);
5457 __ MaybeUnpoisonHeapReference(temp);
Roland Levillain16d9f942016-08-25 17:27:56 +01005458
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005459 // /* HeapReference<Class> */ temp = temp->component_type_
5460 __ movl(temp, Address(temp, component_offset));
5461 // If heap poisoning is enabled, no need to unpoison `temp`
5462 // nor the object reference in `register_value->klass`, as
5463 // we are comparing two poisoned references.
5464 __ cmpl(temp, Address(register_value, class_offset));
Roland Levillain16d9f942016-08-25 17:27:56 +01005465
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005466 if (instruction->StaticTypeOfArrayIsObjectArray()) {
5467 __ j(kEqual, &do_put);
5468 // If heap poisoning is enabled, the `temp` reference has
5469 // not been unpoisoned yet; unpoison it now.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005470 __ MaybeUnpoisonHeapReference(temp);
5471
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005472 // If heap poisoning is enabled, no need to unpoison the
5473 // heap reference loaded below, as it is only used for a
5474 // comparison with null.
5475 __ cmpl(Address(temp, super_offset), Immediate(0));
5476 __ j(kNotEqual, slow_path->GetEntryLabel());
5477 __ Bind(&do_put);
5478 } else {
5479 __ j(kNotEqual, slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005480 }
5481 }
5482
5483 if (kPoisonHeapReferences) {
5484 __ movl(temp, register_value);
5485 __ PoisonHeapReference(temp);
5486 __ movl(address, temp);
5487 } else {
5488 __ movl(address, register_value);
5489 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005490 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005491 codegen_->MaybeRecordImplicitNullCheck(instruction);
5492 }
5493
5494 Register card = locations->GetTemp(1).AsRegister<Register>();
5495 codegen_->MarkGCCard(
5496 temp, card, array, value.AsRegister<Register>(), instruction->GetValueCanBeNull());
5497 __ Bind(&done);
5498
5499 if (slow_path != nullptr) {
5500 __ Bind(slow_path->GetExitLabel());
5501 }
5502
5503 break;
5504 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005505
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005506 case Primitive::kPrimInt: {
5507 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005508 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_4, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005509 if (value.IsRegister()) {
5510 __ movl(address, value.AsRegister<Register>());
5511 } else {
5512 DCHECK(value.IsConstant()) << value;
5513 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
5514 __ movl(address, Immediate(v));
5515 }
5516 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005517 break;
5518 }
5519
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005520 case Primitive::kPrimLong: {
5521 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005522 if (value.IsRegisterPair()) {
5523 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset),
5524 value.AsRegisterPairLow<Register>());
5525 codegen_->MaybeRecordImplicitNullCheck(instruction);
5526 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset + kX86WordSize),
5527 value.AsRegisterPairHigh<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005528 } else {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005529 DCHECK(value.IsConstant());
5530 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
5531 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset),
5532 Immediate(Low32Bits(val)));
5533 codegen_->MaybeRecordImplicitNullCheck(instruction);
5534 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset + kX86WordSize),
5535 Immediate(High32Bits(val)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005536 }
5537 break;
5538 }
5539
Mark Mendell7c8d0092015-01-26 11:21:33 -05005540 case Primitive::kPrimFloat: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005541 uint32_t offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005542 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_4, offset);
Mark Mendell81489372015-11-04 11:30:41 -05005543 if (value.IsFpuRegister()) {
5544 __ movss(address, value.AsFpuRegister<XmmRegister>());
5545 } else {
5546 DCHECK(value.IsConstant());
5547 int32_t v = bit_cast<int32_t, float>(value.GetConstant()->AsFloatConstant()->GetValue());
5548 __ movl(address, Immediate(v));
5549 }
5550 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005551 break;
5552 }
5553
5554 case Primitive::kPrimDouble: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005555 uint32_t offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005556 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, offset);
Mark Mendell81489372015-11-04 11:30:41 -05005557 if (value.IsFpuRegister()) {
5558 __ movsd(address, value.AsFpuRegister<XmmRegister>());
5559 } else {
5560 DCHECK(value.IsConstant());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005561 Address address_hi =
5562 CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, offset + kX86WordSize);
Mark Mendell81489372015-11-04 11:30:41 -05005563 int64_t v = bit_cast<int64_t, double>(value.GetConstant()->AsDoubleConstant()->GetValue());
5564 __ movl(address, Immediate(Low32Bits(v)));
5565 codegen_->MaybeRecordImplicitNullCheck(instruction);
5566 __ movl(address_hi, Immediate(High32Bits(v)));
5567 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005568 break;
5569 }
5570
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005571 case Primitive::kPrimVoid:
5572 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07005573 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005574 }
5575}
5576
5577void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
5578 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005579 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellee8d9712016-07-12 11:13:15 -04005580 if (!instruction->IsEmittedAtUseSite()) {
5581 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5582 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005583}
5584
5585void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
Mark Mendellee8d9712016-07-12 11:13:15 -04005586 if (instruction->IsEmittedAtUseSite()) {
5587 return;
5588 }
5589
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005590 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01005591 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00005592 Register obj = locations->InAt(0).AsRegister<Register>();
5593 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005594 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005595 codegen_->MaybeRecordImplicitNullCheck(instruction);
jessicahandojo4877b792016-09-08 19:49:13 -07005596 // Mask out most significant bit in case the array is String's array of char.
5597 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
5598 __ andl(out, Immediate(INT32_MAX));
5599 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005600}
5601
5602void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005603 RegisterSet caller_saves = RegisterSet::Empty();
5604 InvokeRuntimeCallingConvention calling_convention;
5605 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5606 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
5607 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Mark Mendellf60c90b2015-03-04 15:12:59 -05005608 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendellee8d9712016-07-12 11:13:15 -04005609 HInstruction* length = instruction->InputAt(1);
5610 if (!length->IsEmittedAtUseSite()) {
5611 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
5612 }
jessicahandojo4877b792016-09-08 19:49:13 -07005613 // Need register to see array's length.
5614 if (mirror::kUseStringCompression && instruction->IsStringCharAt()) {
5615 locations->AddTemp(Location::RequiresRegister());
5616 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005617}
5618
5619void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
jessicahandojo4877b792016-09-08 19:49:13 -07005620 const bool is_string_compressed_char_at =
5621 mirror::kUseStringCompression && instruction->IsStringCharAt();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005622 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05005623 Location index_loc = locations->InAt(0);
5624 Location length_loc = locations->InAt(1);
Andreas Gampe85b62f22015-09-09 13:15:38 -07005625 SlowPathCode* slow_path =
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01005626 new (GetGraph()->GetArena()) BoundsCheckSlowPathX86(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005627
Mark Mendell99dbd682015-04-22 16:18:52 -04005628 if (length_loc.IsConstant()) {
5629 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
5630 if (index_loc.IsConstant()) {
5631 // BCE will remove the bounds check if we are guarenteed to pass.
5632 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5633 if (index < 0 || index >= length) {
5634 codegen_->AddSlowPath(slow_path);
5635 __ jmp(slow_path->GetEntryLabel());
5636 } else {
5637 // Some optimization after BCE may have generated this, and we should not
5638 // generate a bounds check if it is a valid range.
5639 }
5640 return;
5641 }
5642
5643 // We have to reverse the jump condition because the length is the constant.
5644 Register index_reg = index_loc.AsRegister<Register>();
5645 __ cmpl(index_reg, Immediate(length));
5646 codegen_->AddSlowPath(slow_path);
5647 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005648 } else {
Mark Mendellee8d9712016-07-12 11:13:15 -04005649 HInstruction* array_length = instruction->InputAt(1);
5650 if (array_length->IsEmittedAtUseSite()) {
5651 // Address the length field in the array.
5652 DCHECK(array_length->IsArrayLength());
5653 uint32_t len_offset = CodeGenerator::GetArrayLengthOffset(array_length->AsArrayLength());
5654 Location array_loc = array_length->GetLocations()->InAt(0);
5655 Address array_len(array_loc.AsRegister<Register>(), len_offset);
jessicahandojo4877b792016-09-08 19:49:13 -07005656 if (is_string_compressed_char_at) {
5657 Register length_reg = locations->GetTemp(0).AsRegister<Register>();
5658 __ movl(length_reg, array_len);
5659 codegen_->MaybeRecordImplicitNullCheck(array_length);
5660 __ andl(length_reg, Immediate(INT32_MAX));
5661 codegen_->GenerateIntCompare(length_reg, index_loc);
Mark Mendellee8d9712016-07-12 11:13:15 -04005662 } else {
jessicahandojo4877b792016-09-08 19:49:13 -07005663 // Checking bounds for general case:
5664 // Array of char or string's array with feature compression off.
5665 if (index_loc.IsConstant()) {
5666 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5667 __ cmpl(array_len, Immediate(value));
5668 } else {
5669 __ cmpl(array_len, index_loc.AsRegister<Register>());
5670 }
5671 codegen_->MaybeRecordImplicitNullCheck(array_length);
Mark Mendellee8d9712016-07-12 11:13:15 -04005672 }
Mark Mendell99dbd682015-04-22 16:18:52 -04005673 } else {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005674 codegen_->GenerateIntCompare(length_loc, index_loc);
Mark Mendell99dbd682015-04-22 16:18:52 -04005675 }
5676 codegen_->AddSlowPath(slow_path);
5677 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005678 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005679}
5680
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005681void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005682 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005683}
5684
5685void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005686 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5687}
5688
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005689void LocationsBuilderX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Marko70e97462016-08-09 11:04:26 +01005690 LocationSummary* locations =
5691 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01005692 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005693}
5694
5695void InstructionCodeGeneratorX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005696 HBasicBlock* block = instruction->GetBlock();
5697 if (block->GetLoopInformation() != nullptr) {
5698 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5699 // The back edge will generate the suspend check.
5700 return;
5701 }
5702 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5703 // The goto will generate the suspend check.
5704 return;
5705 }
5706 GenerateSuspendCheck(instruction, nullptr);
5707}
5708
5709void InstructionCodeGeneratorX86::GenerateSuspendCheck(HSuspendCheck* instruction,
5710 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005711 SuspendCheckSlowPathX86* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005712 down_cast<SuspendCheckSlowPathX86*>(instruction->GetSlowPath());
5713 if (slow_path == nullptr) {
5714 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathX86(instruction, successor);
5715 instruction->SetSlowPath(slow_path);
5716 codegen_->AddSlowPath(slow_path);
5717 if (successor != nullptr) {
5718 DCHECK(successor->IsLoopHeader());
5719 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
5720 }
5721 } else {
5722 DCHECK_EQ(slow_path->GetSuccessor(), successor);
5723 }
5724
Andreas Gampe542451c2016-07-26 09:02:02 -07005725 __ fs()->cmpw(Address::Absolute(Thread::ThreadFlagsOffset<kX86PointerSize>().Int32Value()),
Roland Levillain7c1559a2015-12-15 10:55:36 +00005726 Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005727 if (successor == nullptr) {
5728 __ j(kNotEqual, slow_path->GetEntryLabel());
5729 __ Bind(slow_path->GetReturnLabel());
5730 } else {
5731 __ j(kEqual, codegen_->GetLabelOf(successor));
5732 __ jmp(slow_path->GetEntryLabel());
5733 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005734}
5735
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005736X86Assembler* ParallelMoveResolverX86::GetAssembler() const {
5737 return codegen_->GetAssembler();
5738}
5739
Mark Mendell7c8d0092015-01-26 11:21:33 -05005740void ParallelMoveResolverX86::MoveMemoryToMemory32(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005741 ScratchRegisterScope ensure_scratch(
5742 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5743 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5744 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5745 __ movl(temp_reg, Address(ESP, src + stack_offset));
5746 __ movl(Address(ESP, dst + stack_offset), temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005747}
5748
5749void ParallelMoveResolverX86::MoveMemoryToMemory64(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005750 ScratchRegisterScope ensure_scratch(
5751 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5752 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5753 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5754 __ movl(temp_reg, Address(ESP, src + stack_offset));
5755 __ movl(Address(ESP, dst + stack_offset), temp_reg);
5756 __ movl(temp_reg, Address(ESP, src + stack_offset + kX86WordSize));
5757 __ movl(Address(ESP, dst + stack_offset + kX86WordSize), temp_reg);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005758}
5759
5760void ParallelMoveResolverX86::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005761 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005762 Location source = move->GetSource();
5763 Location destination = move->GetDestination();
5764
5765 if (source.IsRegister()) {
5766 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005767 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
David Brazdil74eb1b22015-12-14 11:44:01 +00005768 } else if (destination.IsFpuRegister()) {
5769 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005770 } else {
5771 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00005772 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005773 }
David Brazdil74eb1b22015-12-14 11:44:01 +00005774 } else if (source.IsRegisterPair()) {
5775 size_t elem_size = Primitive::ComponentSize(Primitive::kPrimInt);
5776 // Create stack space for 2 elements.
5777 __ subl(ESP, Immediate(2 * elem_size));
5778 __ movl(Address(ESP, 0), source.AsRegisterPairLow<Register>());
5779 __ movl(Address(ESP, elem_size), source.AsRegisterPairHigh<Register>());
5780 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
5781 // And remove the temporary stack space we allocated.
5782 __ addl(ESP, Immediate(2 * elem_size));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005783 } else if (source.IsFpuRegister()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00005784 if (destination.IsRegister()) {
5785 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
5786 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005787 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
David Brazdil74eb1b22015-12-14 11:44:01 +00005788 } else if (destination.IsRegisterPair()) {
5789 XmmRegister src_reg = source.AsFpuRegister<XmmRegister>();
5790 __ movd(destination.AsRegisterPairLow<Register>(), src_reg);
5791 __ psrlq(src_reg, Immediate(32));
5792 __ movd(destination.AsRegisterPairHigh<Register>(), src_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005793 } else if (destination.IsStackSlot()) {
5794 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
5795 } else {
5796 DCHECK(destination.IsDoubleStackSlot());
5797 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
5798 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005799 } else if (source.IsStackSlot()) {
5800 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005801 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005802 } else if (destination.IsFpuRegister()) {
5803 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005804 } else {
5805 DCHECK(destination.IsStackSlot());
Mark Mendell7c8d0092015-01-26 11:21:33 -05005806 MoveMemoryToMemory32(destination.GetStackIndex(), source.GetStackIndex());
5807 }
5808 } else if (source.IsDoubleStackSlot()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00005809 if (destination.IsRegisterPair()) {
5810 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
5811 __ movl(destination.AsRegisterPairHigh<Register>(),
5812 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
5813 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005814 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
5815 } else {
5816 DCHECK(destination.IsDoubleStackSlot()) << destination;
5817 MoveMemoryToMemory64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005818 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005819 } else if (source.IsConstant()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005820 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00005821 if (constant->IsIntConstant() || constant->IsNullConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05005822 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005823 if (destination.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05005824 if (value == 0) {
5825 __ xorl(destination.AsRegister<Register>(), destination.AsRegister<Register>());
5826 } else {
5827 __ movl(destination.AsRegister<Register>(), Immediate(value));
5828 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005829 } else {
5830 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell09b84632015-02-13 17:48:38 -05005831 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005832 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005833 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005834 float fp_value = constant->AsFloatConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00005835 int32_t value = bit_cast<int32_t, float>(fp_value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005836 Immediate imm(value);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005837 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005838 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
5839 if (value == 0) {
5840 // Easy handling of 0.0.
5841 __ xorps(dest, dest);
5842 } else {
5843 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005844 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5845 Register temp = static_cast<Register>(ensure_scratch.GetRegister());
5846 __ movl(temp, Immediate(value));
5847 __ movd(dest, temp);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005848 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005849 } else {
5850 DCHECK(destination.IsStackSlot()) << destination;
5851 __ movl(Address(ESP, destination.GetStackIndex()), imm);
5852 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005853 } else if (constant->IsLongConstant()) {
5854 int64_t value = constant->AsLongConstant()->GetValue();
5855 int32_t low_value = Low32Bits(value);
5856 int32_t high_value = High32Bits(value);
5857 Immediate low(low_value);
5858 Immediate high(high_value);
5859 if (destination.IsDoubleStackSlot()) {
5860 __ movl(Address(ESP, destination.GetStackIndex()), low);
5861 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
5862 } else {
5863 __ movl(destination.AsRegisterPairLow<Register>(), low);
5864 __ movl(destination.AsRegisterPairHigh<Register>(), high);
5865 }
5866 } else {
5867 DCHECK(constant->IsDoubleConstant());
5868 double dbl_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00005869 int64_t value = bit_cast<int64_t, double>(dbl_value);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005870 int32_t low_value = Low32Bits(value);
5871 int32_t high_value = High32Bits(value);
5872 Immediate low(low_value);
5873 Immediate high(high_value);
5874 if (destination.IsFpuRegister()) {
5875 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
5876 if (value == 0) {
5877 // Easy handling of 0.0.
5878 __ xorpd(dest, dest);
5879 } else {
5880 __ pushl(high);
5881 __ pushl(low);
5882 __ movsd(dest, Address(ESP, 0));
5883 __ addl(ESP, Immediate(8));
5884 }
5885 } else {
5886 DCHECK(destination.IsDoubleStackSlot()) << destination;
5887 __ movl(Address(ESP, destination.GetStackIndex()), low);
5888 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
5889 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005890 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005891 } else {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00005892 LOG(FATAL) << "Unimplemented move: " << destination << " <- " << source;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005893 }
5894}
5895
Mark Mendella5c19ce2015-04-01 12:51:05 -04005896void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005897 Register suggested_scratch = reg == EAX ? EBX : EAX;
5898 ScratchRegisterScope ensure_scratch(
5899 this, reg, suggested_scratch, codegen_->GetNumberOfCoreRegisters());
5900
5901 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5902 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset));
5903 __ movl(Address(ESP, mem + stack_offset), reg);
5904 __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005905}
5906
Mark Mendell7c8d0092015-01-26 11:21:33 -05005907void ParallelMoveResolverX86::Exchange32(XmmRegister reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005908 ScratchRegisterScope ensure_scratch(
5909 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5910
5911 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5912 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5913 __ movl(temp_reg, Address(ESP, mem + stack_offset));
5914 __ movss(Address(ESP, mem + stack_offset), reg);
5915 __ movd(reg, temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005916}
5917
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005918void ParallelMoveResolverX86::Exchange(int mem1, int mem2) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005919 ScratchRegisterScope ensure_scratch1(
5920 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005921
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005922 Register suggested_scratch = ensure_scratch1.GetRegister() == EAX ? EBX : EAX;
5923 ScratchRegisterScope ensure_scratch2(
5924 this, ensure_scratch1.GetRegister(), suggested_scratch, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005925
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005926 int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0;
5927 stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0;
5928 __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset));
5929 __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset));
5930 __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister()));
5931 __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005932}
5933
5934void ParallelMoveResolverX86::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005935 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005936 Location source = move->GetSource();
5937 Location destination = move->GetDestination();
5938
5939 if (source.IsRegister() && destination.IsRegister()) {
Mark Mendell90979812015-07-28 16:41:21 -04005940 // Use XOR swap algorithm to avoid serializing XCHG instruction or using a temporary.
5941 DCHECK_NE(destination.AsRegister<Register>(), source.AsRegister<Register>());
5942 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
5943 __ xorl(source.AsRegister<Register>(), destination.AsRegister<Register>());
5944 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005945 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005946 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005947 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005948 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005949 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
5950 Exchange(destination.GetStackIndex(), source.GetStackIndex());
Mark Mendell7c8d0092015-01-26 11:21:33 -05005951 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
5952 // Use XOR Swap algorithm to avoid a temporary.
5953 DCHECK_NE(source.reg(), destination.reg());
5954 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
5955 __ xorpd(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
5956 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
5957 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
5958 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
5959 } else if (destination.IsFpuRegister() && source.IsStackSlot()) {
5960 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005961 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
5962 // Take advantage of the 16 bytes in the XMM register.
5963 XmmRegister reg = source.AsFpuRegister<XmmRegister>();
5964 Address stack(ESP, destination.GetStackIndex());
5965 // Load the double into the high doubleword.
5966 __ movhpd(reg, stack);
5967
5968 // Store the low double into the destination.
5969 __ movsd(stack, reg);
5970
5971 // Move the high double to the low double.
5972 __ psrldq(reg, Immediate(8));
5973 } else if (destination.IsFpuRegister() && source.IsDoubleStackSlot()) {
5974 // Take advantage of the 16 bytes in the XMM register.
5975 XmmRegister reg = destination.AsFpuRegister<XmmRegister>();
5976 Address stack(ESP, source.GetStackIndex());
5977 // Load the double into the high doubleword.
5978 __ movhpd(reg, stack);
5979
5980 // Store the low double into the destination.
5981 __ movsd(stack, reg);
5982
5983 // Move the high double to the low double.
5984 __ psrldq(reg, Immediate(8));
5985 } else if (destination.IsDoubleStackSlot() && source.IsDoubleStackSlot()) {
5986 Exchange(destination.GetStackIndex(), source.GetStackIndex());
5987 Exchange(destination.GetHighStackIndex(kX86WordSize), source.GetHighStackIndex(kX86WordSize));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005988 } else {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005989 LOG(FATAL) << "Unimplemented: source: " << source << ", destination: " << destination;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005990 }
5991}
5992
5993void ParallelMoveResolverX86::SpillScratch(int reg) {
5994 __ pushl(static_cast<Register>(reg));
5995}
5996
5997void ParallelMoveResolverX86::RestoreScratch(int reg) {
5998 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005999}
6000
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006001HLoadClass::LoadKind CodeGeneratorX86::GetSupportedLoadClassKind(
6002 HLoadClass::LoadKind desired_class_load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006003 switch (desired_class_load_kind) {
6004 case HLoadClass::LoadKind::kReferrersClass:
6005 break;
6006 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
6007 DCHECK(!GetCompilerOptions().GetCompilePic());
6008 break;
6009 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
6010 DCHECK(GetCompilerOptions().GetCompilePic());
6011 FALLTHROUGH_INTENDED;
6012 case HLoadClass::LoadKind::kDexCachePcRelative:
6013 DCHECK(!Runtime::Current()->UseJitCompilation()); // Note: boot image is also non-JIT.
6014 // We disable pc-relative load when there is an irreducible loop, as the optimization
6015 // is incompatible with it.
6016 // TODO: Create as many X86ComputeBaseMethodAddress instructions as needed for methods
6017 // with irreducible loops.
6018 if (GetGraph()->HasIrreducibleLoops()) {
6019 return HLoadClass::LoadKind::kDexCacheViaMethod;
6020 }
6021 break;
6022 case HLoadClass::LoadKind::kBootImageAddress:
6023 break;
6024 case HLoadClass::LoadKind::kDexCacheAddress:
6025 DCHECK(Runtime::Current()->UseJitCompilation());
6026 break;
6027 case HLoadClass::LoadKind::kDexCacheViaMethod:
6028 break;
6029 }
6030 return desired_class_load_kind;
6031}
6032
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006033void LocationsBuilderX86::VisitLoadClass(HLoadClass* cls) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006034 if (cls->NeedsAccessCheck()) {
6035 InvokeRuntimeCallingConvention calling_convention;
6036 CodeGenerator::CreateLoadClassLocationSummary(
6037 cls,
6038 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
6039 Location::RegisterLocation(EAX),
6040 /* code_generator_supports_read_barrier */ true);
6041 return;
6042 }
6043
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006044 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
6045 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006046 ? LocationSummary::kCallOnSlowPath
6047 : LocationSummary::kNoCall;
6048 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006049 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006050 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01006051 }
6052
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006053 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
6054 if (load_kind == HLoadClass::LoadKind::kReferrersClass ||
6055 load_kind == HLoadClass::LoadKind::kDexCacheViaMethod ||
6056 load_kind == HLoadClass::LoadKind::kBootImageLinkTimePcRelative ||
6057 load_kind == HLoadClass::LoadKind::kDexCachePcRelative) {
6058 locations->SetInAt(0, Location::RequiresRegister());
6059 }
6060 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006061}
6062
6063void InstructionCodeGeneratorX86::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01006064 LocationSummary* locations = cls->GetLocations();
Calin Juravle98893e12015-10-02 21:05:03 +01006065 if (cls->NeedsAccessCheck()) {
6066 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex());
Serban Constantinescuba45db02016-07-12 22:53:02 +01006067 codegen_->InvokeRuntime(kQuickInitializeTypeAndVerifyAccess, cls, cls->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006068 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Calin Juravle580b6092015-10-06 17:35:58 +01006069 return;
6070 }
6071
Roland Levillain0d5a2812015-11-13 10:07:31 +00006072 Location out_loc = locations->Out();
6073 Register out = out_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006074
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006075 bool generate_null_check = false;
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006076 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006077 switch (cls->GetLoadKind()) {
6078 case HLoadClass::LoadKind::kReferrersClass: {
6079 DCHECK(!cls->CanCallRuntime());
6080 DCHECK(!cls->MustGenerateClinitCheck());
6081 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
6082 Register current_method = locations->InAt(0).AsRegister<Register>();
6083 GenerateGcRootFieldLoad(
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006084 cls,
6085 out_loc,
6086 Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()),
Roland Levillain00468f32016-10-27 18:02:48 +01006087 /* fixup_label */ nullptr,
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006088 requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006089 break;
6090 }
6091 case HLoadClass::LoadKind::kBootImageLinkTimeAddress: {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006092 DCHECK(!requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006093 __ movl(out, Immediate(/* placeholder */ 0));
6094 codegen_->RecordTypePatch(cls);
6095 break;
6096 }
6097 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006098 DCHECK(!requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006099 Register method_address = locations->InAt(0).AsRegister<Register>();
6100 __ leal(out, Address(method_address, CodeGeneratorX86::kDummy32BitOffset));
6101 codegen_->RecordTypePatch(cls);
6102 break;
6103 }
6104 case HLoadClass::LoadKind::kBootImageAddress: {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006105 DCHECK(!requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006106 DCHECK_NE(cls->GetAddress(), 0u);
6107 uint32_t address = dchecked_integral_cast<uint32_t>(cls->GetAddress());
6108 __ movl(out, Immediate(address));
6109 codegen_->RecordSimplePatch();
6110 break;
6111 }
6112 case HLoadClass::LoadKind::kDexCacheAddress: {
6113 DCHECK_NE(cls->GetAddress(), 0u);
6114 uint32_t address = dchecked_integral_cast<uint32_t>(cls->GetAddress());
6115 // /* GcRoot<mirror::Class> */ out = *address
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006116 GenerateGcRootFieldLoad(cls,
6117 out_loc,
6118 Address::Absolute(address),
Roland Levillain00468f32016-10-27 18:02:48 +01006119 /* fixup_label */ nullptr,
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006120 requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006121 generate_null_check = !cls->IsInDexCache();
6122 break;
6123 }
6124 case HLoadClass::LoadKind::kDexCachePcRelative: {
6125 Register base_reg = locations->InAt(0).AsRegister<Register>();
6126 uint32_t offset = cls->GetDexCacheElementOffset();
6127 Label* fixup_label = codegen_->NewPcRelativeDexCacheArrayPatch(cls->GetDexFile(), offset);
6128 // /* GcRoot<mirror::Class> */ out = *(base + offset) /* PC-relative */
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006129 GenerateGcRootFieldLoad(cls,
6130 out_loc,
6131 Address(base_reg, CodeGeneratorX86::kDummy32BitOffset),
6132 fixup_label,
6133 requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006134 generate_null_check = !cls->IsInDexCache();
6135 break;
6136 }
6137 case HLoadClass::LoadKind::kDexCacheViaMethod: {
6138 // /* GcRoot<mirror::Class>[] */ out =
6139 // current_method.ptr_sized_fields_->dex_cache_resolved_types_
6140 Register current_method = locations->InAt(0).AsRegister<Register>();
6141 __ movl(out, Address(current_method,
6142 ArtMethod::DexCacheResolvedTypesOffset(kX86PointerSize).Int32Value()));
6143 // /* GcRoot<mirror::Class> */ out = out[type_index]
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006144 GenerateGcRootFieldLoad(cls,
6145 out_loc,
6146 Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())),
Roland Levillain00468f32016-10-27 18:02:48 +01006147 /* fixup_label */ nullptr,
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006148 requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006149 generate_null_check = !cls->IsInDexCache();
6150 break;
6151 }
6152 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006153
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006154 if (generate_null_check || cls->MustGenerateClinitCheck()) {
6155 DCHECK(cls->CanCallRuntime());
6156 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
6157 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
6158 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006159
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006160 if (generate_null_check) {
6161 __ testl(out, out);
6162 __ j(kEqual, slow_path->GetEntryLabel());
6163 }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006164
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006165 if (cls->MustGenerateClinitCheck()) {
6166 GenerateClassInitializationCheck(slow_path, out);
6167 } else {
6168 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006169 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006170 }
6171}
6172
6173void LocationsBuilderX86::VisitClinitCheck(HClinitCheck* check) {
6174 LocationSummary* locations =
6175 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
6176 locations->SetInAt(0, Location::RequiresRegister());
6177 if (check->HasUses()) {
6178 locations->SetOut(Location::SameAsFirstInput());
6179 }
6180}
6181
6182void InstructionCodeGeneratorX86::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006183 // We assume the class to not be null.
Andreas Gampe85b62f22015-09-09 13:15:38 -07006184 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006185 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006186 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00006187 GenerateClassInitializationCheck(slow_path,
6188 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006189}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006190
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006191void InstructionCodeGeneratorX86::GenerateClassInitializationCheck(
Andreas Gampe85b62f22015-09-09 13:15:38 -07006192 SlowPathCode* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006193 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
6194 Immediate(mirror::Class::kStatusInitialized));
6195 __ j(kLess, slow_path->GetEntryLabel());
6196 __ Bind(slow_path->GetExitLabel());
6197 // No need for memory fence, thanks to the X86 memory model.
6198}
6199
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006200HLoadString::LoadKind CodeGeneratorX86::GetSupportedLoadStringKind(
6201 HLoadString::LoadKind desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006202 switch (desired_string_load_kind) {
6203 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
6204 DCHECK(!GetCompilerOptions().GetCompilePic());
6205 break;
6206 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
6207 DCHECK(GetCompilerOptions().GetCompilePic());
6208 FALLTHROUGH_INTENDED;
Vladimir Markoaad75c62016-10-03 08:46:48 +00006209 case HLoadString::LoadKind::kBssEntry:
Calin Juravleffc87072016-04-20 14:22:09 +01006210 DCHECK(!Runtime::Current()->UseJitCompilation()); // Note: boot image is also non-JIT.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006211 // We disable pc-relative load when there is an irreducible loop, as the optimization
6212 // is incompatible with it.
6213 // TODO: Create as many X86ComputeBaseMethodAddress instructions as needed for methods
6214 // with irreducible loops.
6215 if (GetGraph()->HasIrreducibleLoops()) {
6216 return HLoadString::LoadKind::kDexCacheViaMethod;
6217 }
6218 break;
6219 case HLoadString::LoadKind::kBootImageAddress:
6220 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006221 case HLoadString::LoadKind::kDexCacheViaMethod:
6222 break;
6223 }
6224 return desired_string_load_kind;
6225}
6226
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006227void LocationsBuilderX86::VisitLoadString(HLoadString* load) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006228 LocationSummary::CallKind call_kind = (load->NeedsEnvironment() || kEmitCompilerReadBarrier)
Vladimir Markoaad75c62016-10-03 08:46:48 +00006229 ? ((load->GetLoadKind() == HLoadString::LoadKind::kDexCacheViaMethod)
6230 ? LocationSummary::kCallOnMainOnly
6231 : LocationSummary::kCallOnSlowPath)
Nicolas Geoffray917d0162015-11-24 18:25:35 +00006232 : LocationSummary::kNoCall;
6233 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006234 HLoadString::LoadKind load_kind = load->GetLoadKind();
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006235 if (load_kind == HLoadString::LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markoaad75c62016-10-03 08:46:48 +00006236 load_kind == HLoadString::LoadKind::kBssEntry) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006237 locations->SetInAt(0, Location::RequiresRegister());
6238 }
Christina Wadsworth175d09b2016-08-31 16:26:01 -07006239 if (load_kind == HLoadString::LoadKind::kDexCacheViaMethod) {
6240 locations->SetOut(Location::RegisterLocation(EAX));
6241 } else {
6242 locations->SetOut(Location::RequiresRegister());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006243 if (load_kind == HLoadString::LoadKind::kBssEntry) {
6244 if (!kUseReadBarrier || kUseBakerReadBarrier) {
6245 // Rely on the pResolveString and/or marking to save everything.
6246 RegisterSet caller_saves = RegisterSet::Empty();
6247 InvokeRuntimeCallingConvention calling_convention;
6248 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6249 locations->SetCustomSlowPathCallerSaves(caller_saves);
6250 } else {
6251 // For non-Baker read barrier we have a temp-clobbering call.
6252 }
6253 }
Christina Wadsworth175d09b2016-08-31 16:26:01 -07006254 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006255}
6256
6257void InstructionCodeGeneratorX86::VisitLoadString(HLoadString* load) {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01006258 LocationSummary* locations = load->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006259 Location out_loc = locations->Out();
6260 Register out = out_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006261
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006262 switch (load->GetLoadKind()) {
6263 case HLoadString::LoadKind::kBootImageLinkTimeAddress: {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006264 __ movl(out, Immediate(/* placeholder */ 0));
Vladimir Markoaad75c62016-10-03 08:46:48 +00006265 codegen_->RecordBootStringPatch(load);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006266 return; // No dex cache slow path.
6267 }
6268 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006269 Register method_address = locations->InAt(0).AsRegister<Register>();
6270 __ leal(out, Address(method_address, CodeGeneratorX86::kDummy32BitOffset));
Vladimir Markoaad75c62016-10-03 08:46:48 +00006271 codegen_->RecordBootStringPatch(load);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006272 return; // No dex cache slow path.
6273 }
6274 case HLoadString::LoadKind::kBootImageAddress: {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006275 DCHECK_NE(load->GetAddress(), 0u);
6276 uint32_t address = dchecked_integral_cast<uint32_t>(load->GetAddress());
6277 __ movl(out, Immediate(address));
6278 codegen_->RecordSimplePatch();
6279 return; // No dex cache slow path.
6280 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00006281 case HLoadString::LoadKind::kBssEntry: {
6282 Register method_address = locations->InAt(0).AsRegister<Register>();
6283 Address address = Address(method_address, CodeGeneratorX86::kDummy32BitOffset);
6284 Label* fixup_label = codegen_->NewStringBssEntryPatch(load);
Nicolas Geoffray07c919f2016-11-09 17:29:03 +00006285 // /* GcRoot<mirror::Class> */ out = *address /* PC-relative */
Roland Levillain00468f32016-10-27 18:02:48 +01006286 GenerateGcRootFieldLoad(load, out_loc, address, fixup_label, kEmitCompilerReadBarrier);
Vladimir Markoaad75c62016-10-03 08:46:48 +00006287 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86(load);
6288 codegen_->AddSlowPath(slow_path);
6289 __ testl(out, out);
6290 __ j(kEqual, slow_path->GetEntryLabel());
6291 __ Bind(slow_path->GetExitLabel());
6292 return;
6293 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006294 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07006295 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006296 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006297
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07006298 // TODO: Re-add the compiler code to do string dex cache lookup again.
Christina Wadsworth175d09b2016-08-31 16:26:01 -07006299 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006300 DCHECK_EQ(calling_convention.GetRegisterAt(0), out);
Christina Wadsworth175d09b2016-08-31 16:26:01 -07006301 __ movl(calling_convention.GetRegisterAt(0), Immediate(load->GetStringIndex()));
6302 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
6303 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006304}
6305
David Brazdilcb1c0552015-08-04 16:22:25 +01006306static Address GetExceptionTlsAddress() {
Andreas Gampe542451c2016-07-26 09:02:02 -07006307 return Address::Absolute(Thread::ExceptionOffset<kX86PointerSize>().Int32Value());
David Brazdilcb1c0552015-08-04 16:22:25 +01006308}
6309
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006310void LocationsBuilderX86::VisitLoadException(HLoadException* load) {
6311 LocationSummary* locations =
6312 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
6313 locations->SetOut(Location::RequiresRegister());
6314}
6315
6316void InstructionCodeGeneratorX86::VisitLoadException(HLoadException* load) {
David Brazdilcb1c0552015-08-04 16:22:25 +01006317 __ fs()->movl(load->GetLocations()->Out().AsRegister<Register>(), GetExceptionTlsAddress());
6318}
6319
6320void LocationsBuilderX86::VisitClearException(HClearException* clear) {
6321 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
6322}
6323
6324void InstructionCodeGeneratorX86::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
6325 __ fs()->movl(GetExceptionTlsAddress(), Immediate(0));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006326}
6327
6328void LocationsBuilderX86::VisitThrow(HThrow* instruction) {
6329 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006330 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006331 InvokeRuntimeCallingConvention calling_convention;
6332 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6333}
6334
6335void InstructionCodeGeneratorX86::VisitThrow(HThrow* instruction) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01006336 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006337 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006338}
6339
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006340// Temp is used for read barrier.
6341static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
6342 if (kEmitCompilerReadBarrier &&
Vladimir Marko953437b2016-08-24 08:30:46 +00006343 !kUseBakerReadBarrier &&
6344 (type_check_kind == TypeCheckKind::kAbstractClassCheck ||
Roland Levillain7c1559a2015-12-15 10:55:36 +00006345 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006346 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
6347 return 1;
6348 }
6349 return 0;
6350}
6351
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006352// Interface case has 3 temps, one for holding the number of interfaces, one for the current
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006353// interface pointer, one for loading the current interface.
6354// The other checks have one temp for loading the object's class.
6355static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
6356 if (type_check_kind == TypeCheckKind::kInterfaceCheck && !kPoisonHeapReferences) {
6357 return 2;
6358 }
6359 return 1 + NumberOfInstanceOfTemps(type_check_kind);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006360}
6361
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006362void LocationsBuilderX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006363 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain0d5a2812015-11-13 10:07:31 +00006364 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01006365 bool baker_read_barrier_slow_path = false;
Roland Levillain0d5a2812015-11-13 10:07:31 +00006366 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006367 case TypeCheckKind::kExactCheck:
6368 case TypeCheckKind::kAbstractClassCheck:
6369 case TypeCheckKind::kClassHierarchyCheck:
6370 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006371 call_kind =
6372 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Vladimir Marko70e97462016-08-09 11:04:26 +01006373 baker_read_barrier_slow_path = kUseBakerReadBarrier;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006374 break;
6375 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006376 case TypeCheckKind::kUnresolvedCheck:
6377 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006378 call_kind = LocationSummary::kCallOnSlowPath;
6379 break;
6380 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006381
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006382 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01006383 if (baker_read_barrier_slow_path) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006384 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01006385 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006386 locations->SetInAt(0, Location::RequiresRegister());
6387 locations->SetInAt(1, Location::Any());
6388 // Note that TypeCheckSlowPathX86 uses this "out" register too.
6389 locations->SetOut(Location::RequiresRegister());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006390 // When read barriers are enabled, we need a temporary register for some cases.
6391 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006392}
6393
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006394void InstructionCodeGeneratorX86::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006395 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006396 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006397 Location obj_loc = locations->InAt(0);
6398 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006399 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006400 Location out_loc = locations->Out();
6401 Register out = out_loc.AsRegister<Register>();
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006402 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
6403 DCHECK_LE(num_temps, 1u);
6404 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006405 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006406 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6407 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6408 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Andreas Gampe85b62f22015-09-09 13:15:38 -07006409 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006410 NearLabel done, zero;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006411
6412 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006413 // Avoid null check if we know obj is not null.
6414 if (instruction->MustDoNullCheck()) {
6415 __ testl(obj, obj);
6416 __ j(kEqual, &zero);
6417 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006418
Roland Levillain0d5a2812015-11-13 10:07:31 +00006419 // /* HeapReference<Class> */ out = obj->klass_
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006420 GenerateReferenceLoadTwoRegisters(instruction,
6421 out_loc,
6422 obj_loc,
6423 class_offset,
6424 kEmitCompilerReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006425
Roland Levillain7c1559a2015-12-15 10:55:36 +00006426 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006427 case TypeCheckKind::kExactCheck: {
6428 if (cls.IsRegister()) {
6429 __ cmpl(out, cls.AsRegister<Register>());
6430 } else {
6431 DCHECK(cls.IsStackSlot()) << cls;
6432 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6433 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006434
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006435 // Classes must be equal for the instanceof to succeed.
6436 __ j(kNotEqual, &zero);
6437 __ movl(out, Immediate(1));
6438 __ jmp(&done);
6439 break;
6440 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006441
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006442 case TypeCheckKind::kAbstractClassCheck: {
6443 // If the class is abstract, we eagerly fetch the super class of the
6444 // object to avoid doing a comparison we know will fail.
6445 NearLabel loop;
6446 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006447 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006448 GenerateReferenceLoadOneRegister(instruction,
6449 out_loc,
6450 super_offset,
6451 maybe_temp_loc,
6452 kEmitCompilerReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006453 __ testl(out, out);
6454 // If `out` is null, we use it for the result, and jump to `done`.
6455 __ j(kEqual, &done);
6456 if (cls.IsRegister()) {
6457 __ cmpl(out, cls.AsRegister<Register>());
6458 } else {
6459 DCHECK(cls.IsStackSlot()) << cls;
6460 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6461 }
6462 __ j(kNotEqual, &loop);
6463 __ movl(out, Immediate(1));
6464 if (zero.IsLinked()) {
6465 __ jmp(&done);
6466 }
6467 break;
6468 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006469
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006470 case TypeCheckKind::kClassHierarchyCheck: {
6471 // Walk over the class hierarchy to find a match.
6472 NearLabel loop, success;
6473 __ Bind(&loop);
6474 if (cls.IsRegister()) {
6475 __ cmpl(out, cls.AsRegister<Register>());
6476 } else {
6477 DCHECK(cls.IsStackSlot()) << cls;
6478 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6479 }
6480 __ j(kEqual, &success);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006481 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006482 GenerateReferenceLoadOneRegister(instruction,
6483 out_loc,
6484 super_offset,
6485 maybe_temp_loc,
6486 kEmitCompilerReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006487 __ testl(out, out);
6488 __ j(kNotEqual, &loop);
6489 // If `out` is null, we use it for the result, and jump to `done`.
6490 __ jmp(&done);
6491 __ Bind(&success);
6492 __ movl(out, Immediate(1));
6493 if (zero.IsLinked()) {
6494 __ jmp(&done);
6495 }
6496 break;
6497 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006498
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006499 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006500 // Do an exact check.
6501 NearLabel exact_check;
6502 if (cls.IsRegister()) {
6503 __ cmpl(out, cls.AsRegister<Register>());
6504 } else {
6505 DCHECK(cls.IsStackSlot()) << cls;
6506 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6507 }
6508 __ j(kEqual, &exact_check);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006509 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006510 // /* HeapReference<Class> */ out = out->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006511 GenerateReferenceLoadOneRegister(instruction,
6512 out_loc,
6513 component_offset,
6514 maybe_temp_loc,
6515 kEmitCompilerReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006516 __ testl(out, out);
6517 // If `out` is null, we use it for the result, and jump to `done`.
6518 __ j(kEqual, &done);
6519 __ cmpw(Address(out, primitive_offset), Immediate(Primitive::kPrimNot));
6520 __ j(kNotEqual, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006521 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006522 __ movl(out, Immediate(1));
6523 __ jmp(&done);
6524 break;
6525 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006526
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006527 case TypeCheckKind::kArrayCheck: {
6528 if (cls.IsRegister()) {
6529 __ cmpl(out, cls.AsRegister<Register>());
6530 } else {
6531 DCHECK(cls.IsStackSlot()) << cls;
6532 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6533 }
6534 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain0d5a2812015-11-13 10:07:31 +00006535 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
6536 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006537 codegen_->AddSlowPath(slow_path);
6538 __ j(kNotEqual, slow_path->GetEntryLabel());
6539 __ movl(out, Immediate(1));
6540 if (zero.IsLinked()) {
6541 __ jmp(&done);
6542 }
6543 break;
6544 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006545
Calin Juravle98893e12015-10-02 21:05:03 +01006546 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006547 case TypeCheckKind::kInterfaceCheck: {
6548 // Note that we indeed only call on slow path, but we always go
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006549 // into the slow path for the unresolved and interface check
Roland Levillain0d5a2812015-11-13 10:07:31 +00006550 // cases.
6551 //
6552 // We cannot directly call the InstanceofNonTrivial runtime
6553 // entry point without resorting to a type checking slow path
6554 // here (i.e. by calling InvokeRuntime directly), as it would
6555 // require to assign fixed registers for the inputs of this
6556 // HInstanceOf instruction (following the runtime calling
6557 // convention), which might be cluttered by the potential first
6558 // read barrier emission at the beginning of this method.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006559 //
6560 // TODO: Introduce a new runtime entry point taking the object
6561 // to test (instead of its class) as argument, and let it deal
6562 // with the read barrier issues. This will let us refactor this
6563 // case of the `switch` code as it was previously (with a direct
6564 // call to the runtime not using a type checking slow path).
6565 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006566 DCHECK(locations->OnlyCallsOnSlowPath());
6567 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
6568 /* is_fatal */ false);
6569 codegen_->AddSlowPath(slow_path);
6570 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006571 if (zero.IsLinked()) {
6572 __ jmp(&done);
6573 }
6574 break;
6575 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006576 }
6577
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006578 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006579 __ Bind(&zero);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006580 __ xorl(out, out);
6581 }
6582
6583 if (done.IsLinked()) {
6584 __ Bind(&done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006585 }
6586
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006587 if (slow_path != nullptr) {
6588 __ Bind(slow_path->GetExitLabel());
6589 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006590}
6591
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006592void LocationsBuilderX86::VisitCheckCast(HCheckCast* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006593 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
6594 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006595 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
6596 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006597 case TypeCheckKind::kExactCheck:
6598 case TypeCheckKind::kAbstractClassCheck:
6599 case TypeCheckKind::kClassHierarchyCheck:
6600 case TypeCheckKind::kArrayObjectCheck:
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006601 case TypeCheckKind::kInterfaceCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006602 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
6603 LocationSummary::kCallOnSlowPath :
6604 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006605 break;
6606 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006607 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006608 call_kind = LocationSummary::kCallOnSlowPath;
6609 break;
6610 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006611 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
6612 locations->SetInAt(0, Location::RequiresRegister());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006613 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
6614 // Require a register for the interface check since there is a loop that compares the class to
6615 // a memory address.
6616 locations->SetInAt(1, Location::RequiresRegister());
6617 } else {
6618 locations->SetInAt(1, Location::Any());
6619 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006620 // Note that TypeCheckSlowPathX86 uses this "temp" register too.
6621 locations->AddTemp(Location::RequiresRegister());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006622 // When read barriers are enabled, we need an additional temporary register for some cases.
6623 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
6624}
6625
6626static bool IsTypeCheckSlowPathFatal(TypeCheckKind type_check_kind, bool throws_into_catch) {
6627 switch (type_check_kind) {
6628 case TypeCheckKind::kExactCheck:
6629 case TypeCheckKind::kAbstractClassCheck:
6630 case TypeCheckKind::kClassHierarchyCheck:
6631 case TypeCheckKind::kArrayObjectCheck:
6632 return !throws_into_catch && !kEmitCompilerReadBarrier;
6633 case TypeCheckKind::kInterfaceCheck:
6634 return !throws_into_catch && !kEmitCompilerReadBarrier && !kPoisonHeapReferences;
6635 case TypeCheckKind::kArrayCheck:
6636 case TypeCheckKind::kUnresolvedCheck:
6637 return false;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006638 }
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006639 LOG(FATAL) << "Unreachable";
6640 UNREACHABLE();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006641}
6642
6643void InstructionCodeGeneratorX86::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006644 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006645 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006646 Location obj_loc = locations->InAt(0);
6647 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006648 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006649 Location temp_loc = locations->GetTemp(0);
6650 Register temp = temp_loc.AsRegister<Register>();
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006651 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
6652 DCHECK_GE(num_temps, 1u);
6653 DCHECK_LE(num_temps, 2u);
6654 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
6655 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
6656 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6657 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6658 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
6659 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
6660 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
6661 const uint32_t object_array_data_offset =
6662 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006663
Roland Levillain0d5a2812015-11-13 10:07:31 +00006664 bool is_type_check_slow_path_fatal =
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006665 IsTypeCheckSlowPathFatal(type_check_kind, instruction->CanThrowIntoCatchBlock());
6666
Roland Levillain0d5a2812015-11-13 10:07:31 +00006667 SlowPathCode* type_check_slow_path =
6668 new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
6669 is_type_check_slow_path_fatal);
6670 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006671
Roland Levillain0d5a2812015-11-13 10:07:31 +00006672 NearLabel done;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006673 // Avoid null check if we know obj is not null.
6674 if (instruction->MustDoNullCheck()) {
6675 __ testl(obj, obj);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006676 __ j(kEqual, &done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006677 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006678
Roland Levillain0d5a2812015-11-13 10:07:31 +00006679 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006680 case TypeCheckKind::kExactCheck:
6681 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006682 // /* HeapReference<Class> */ temp = obj->klass_
6683 GenerateReferenceLoadTwoRegisters(instruction,
6684 temp_loc,
6685 obj_loc,
6686 class_offset,
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006687 /*emit_read_barrier*/ false);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006688
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006689 if (cls.IsRegister()) {
6690 __ cmpl(temp, cls.AsRegister<Register>());
6691 } else {
6692 DCHECK(cls.IsStackSlot()) << cls;
6693 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6694 }
6695 // Jump to slow path for throwing the exception or doing a
6696 // more involved array check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006697 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006698 break;
6699 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006700
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006701 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006702 // /* HeapReference<Class> */ temp = obj->klass_
6703 GenerateReferenceLoadTwoRegisters(instruction,
6704 temp_loc,
6705 obj_loc,
6706 class_offset,
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006707 /*emit_read_barrier*/ false);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006708
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006709 // If the class is abstract, we eagerly fetch the super class of the
6710 // object to avoid doing a comparison we know will fail.
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006711 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006712 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006713 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006714 GenerateReferenceLoadOneRegister(instruction,
6715 temp_loc,
6716 super_offset,
6717 maybe_temp2_loc,
6718 /*emit_read_barrier*/ false);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006719
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006720 // If the class reference currently in `temp` is null, jump to the slow path to throw the
6721 // exception.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006722 __ testl(temp, temp);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006723 __ j(kZero, type_check_slow_path->GetEntryLabel());
Roland Levillain0d5a2812015-11-13 10:07:31 +00006724
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006725 // Otherwise, compare the classes
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006726 if (cls.IsRegister()) {
6727 __ cmpl(temp, cls.AsRegister<Register>());
6728 } else {
6729 DCHECK(cls.IsStackSlot()) << cls;
6730 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6731 }
6732 __ j(kNotEqual, &loop);
6733 break;
6734 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006735
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006736 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006737 // /* HeapReference<Class> */ temp = obj->klass_
6738 GenerateReferenceLoadTwoRegisters(instruction,
6739 temp_loc,
6740 obj_loc,
6741 class_offset,
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006742 /*emit_read_barrier*/ false);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006743
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006744 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006745 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006746 __ Bind(&loop);
6747 if (cls.IsRegister()) {
6748 __ cmpl(temp, cls.AsRegister<Register>());
6749 } else {
6750 DCHECK(cls.IsStackSlot()) << cls;
6751 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6752 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006753 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006754
Roland Levillain0d5a2812015-11-13 10:07:31 +00006755 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006756 GenerateReferenceLoadOneRegister(instruction,
6757 temp_loc,
6758 super_offset,
6759 maybe_temp2_loc,
6760 /*emit_read_barrier*/ false);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006761
6762 // If the class reference currently in `temp` is not null, jump
6763 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006764 __ testl(temp, temp);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006765 __ j(kNotZero, &loop);
6766 // Otherwise, jump to the slow path to throw the exception.;
Roland Levillain0d5a2812015-11-13 10:07:31 +00006767 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006768 break;
6769 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006770
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006771 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006772 // /* HeapReference<Class> */ temp = obj->klass_
6773 GenerateReferenceLoadTwoRegisters(instruction,
6774 temp_loc,
6775 obj_loc,
6776 class_offset,
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006777 /*emit_read_barrier*/ false);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006778
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006779 // Do an exact check.
6780 if (cls.IsRegister()) {
6781 __ cmpl(temp, cls.AsRegister<Register>());
6782 } else {
6783 DCHECK(cls.IsStackSlot()) << cls;
6784 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6785 }
6786 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006787
6788 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006789 // /* HeapReference<Class> */ temp = temp->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006790 GenerateReferenceLoadOneRegister(instruction,
6791 temp_loc,
6792 component_offset,
6793 maybe_temp2_loc,
6794 /*emit_read_barrier*/ false);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006795
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006796 // If the component type is null (i.e. the object not an array), jump to the slow path to
6797 // throw the exception. Otherwise proceed with the check.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006798 __ testl(temp, temp);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006799 __ j(kZero, type_check_slow_path->GetEntryLabel());
Roland Levillain0d5a2812015-11-13 10:07:31 +00006800
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006801 __ cmpw(Address(temp, primitive_offset), Immediate(Primitive::kPrimNot));
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006802 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006803 break;
6804 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006805
Calin Juravle98893e12015-10-02 21:05:03 +01006806 case TypeCheckKind::kUnresolvedCheck:
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006807 // We always go into the type check slow path for the unresolved check case.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006808 // We cannot directly call the CheckCast runtime entry point
6809 // without resorting to a type checking slow path here (i.e. by
6810 // calling InvokeRuntime directly), as it would require to
6811 // assign fixed registers for the inputs of this HInstanceOf
6812 // instruction (following the runtime calling convention), which
6813 // might be cluttered by the potential first read barrier
6814 // emission at the beginning of this method.
6815 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006816 break;
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006817
6818 case TypeCheckKind::kInterfaceCheck: {
6819 // Fast path for the interface check. Since we compare with a memory location in the inner
6820 // loop we would need to have cls poisoned. However unpoisoning cls would reset the
6821 // conditional flags and cause the conditional jump to be incorrect. Therefore we just jump
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006822 // to the slow path if we are running under poisoning.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006823 if (!kPoisonHeapReferences) {
6824 // /* HeapReference<Class> */ temp = obj->klass_
6825 GenerateReferenceLoadTwoRegisters(instruction,
6826 temp_loc,
6827 obj_loc,
6828 class_offset,
6829 /*emit_read_barrier*/ false);
6830
6831 // Try to avoid read barriers to improve the fast path. We can not get false positives by
6832 // doing this.
6833 // /* HeapReference<Class> */ temp = obj->klass_
6834 GenerateReferenceLoadTwoRegisters(instruction,
6835 temp_loc,
6836 obj_loc,
6837 class_offset,
6838 /*emit_read_barrier*/ false);
6839
6840 // /* HeapReference<Class> */ temp = temp->iftable_
6841 GenerateReferenceLoadTwoRegisters(instruction,
6842 temp_loc,
6843 temp_loc,
6844 iftable_offset,
6845 /*emit_read_barrier*/ false);
6846 NearLabel is_null;
6847 // Null iftable means it is empty.
6848 __ testl(temp, temp);
6849 __ j(kZero, &is_null);
6850
6851 // Loop through the iftable and check if any class matches.
6852 __ movl(maybe_temp2_loc.AsRegister<Register>(), Address(temp, array_length_offset));
6853
6854 NearLabel start_loop;
6855 __ Bind(&start_loop);
6856 __ cmpl(cls.AsRegister<Register>(), Address(temp, object_array_data_offset));
6857 __ j(kEqual, &done); // Return if same class.
6858 // Go to next interface.
6859 __ addl(temp, Immediate(2 * kHeapReferenceSize));
6860 __ subl(maybe_temp2_loc.AsRegister<Register>(), Immediate(2));
6861 __ j(kNotZero, &start_loop);
6862 __ Bind(&is_null);
6863 }
6864
6865 __ jmp(type_check_slow_path->GetEntryLabel());
6866 break;
6867 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006868 }
6869 __ Bind(&done);
6870
Roland Levillain0d5a2812015-11-13 10:07:31 +00006871 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006872}
6873
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006874void LocationsBuilderX86::VisitMonitorOperation(HMonitorOperation* instruction) {
6875 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006876 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006877 InvokeRuntimeCallingConvention calling_convention;
6878 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6879}
6880
6881void InstructionCodeGeneratorX86::VisitMonitorOperation(HMonitorOperation* instruction) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01006882 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject
6883 : kQuickUnlockObject,
Alexandre Rames8158f282015-08-07 10:26:17 +01006884 instruction,
Serban Constantinescuba45db02016-07-12 22:53:02 +01006885 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006886 if (instruction->IsEnter()) {
6887 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
6888 } else {
6889 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
6890 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006891}
6892
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006893void LocationsBuilderX86::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
6894void LocationsBuilderX86::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
6895void LocationsBuilderX86::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
6896
6897void LocationsBuilderX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
6898 LocationSummary* locations =
6899 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6900 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
6901 || instruction->GetResultType() == Primitive::kPrimLong);
6902 locations->SetInAt(0, Location::RequiresRegister());
6903 locations->SetInAt(1, Location::Any());
6904 locations->SetOut(Location::SameAsFirstInput());
6905}
6906
6907void InstructionCodeGeneratorX86::VisitAnd(HAnd* instruction) {
6908 HandleBitwiseOperation(instruction);
6909}
6910
6911void InstructionCodeGeneratorX86::VisitOr(HOr* instruction) {
6912 HandleBitwiseOperation(instruction);
6913}
6914
6915void InstructionCodeGeneratorX86::VisitXor(HXor* instruction) {
6916 HandleBitwiseOperation(instruction);
6917}
6918
6919void InstructionCodeGeneratorX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
6920 LocationSummary* locations = instruction->GetLocations();
6921 Location first = locations->InAt(0);
6922 Location second = locations->InAt(1);
6923 DCHECK(first.Equals(locations->Out()));
6924
6925 if (instruction->GetResultType() == Primitive::kPrimInt) {
6926 if (second.IsRegister()) {
6927 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006928 __ andl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006929 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006930 __ orl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006931 } else {
6932 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006933 __ xorl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006934 }
6935 } else if (second.IsConstant()) {
6936 if (instruction->IsAnd()) {
Roland Levillain199f3362014-11-27 17:15:16 +00006937 __ andl(first.AsRegister<Register>(),
6938 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006939 } else if (instruction->IsOr()) {
Roland Levillain199f3362014-11-27 17:15:16 +00006940 __ orl(first.AsRegister<Register>(),
6941 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006942 } else {
6943 DCHECK(instruction->IsXor());
Roland Levillain199f3362014-11-27 17:15:16 +00006944 __ xorl(first.AsRegister<Register>(),
6945 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006946 }
6947 } else {
6948 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006949 __ andl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006950 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006951 __ orl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006952 } else {
6953 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006954 __ xorl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006955 }
6956 }
6957 } else {
6958 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
6959 if (second.IsRegisterPair()) {
6960 if (instruction->IsAnd()) {
6961 __ andl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
6962 __ andl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
6963 } else if (instruction->IsOr()) {
6964 __ orl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
6965 __ orl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
6966 } else {
6967 DCHECK(instruction->IsXor());
6968 __ xorl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
6969 __ xorl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
6970 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006971 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006972 if (instruction->IsAnd()) {
6973 __ andl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
6974 __ andl(first.AsRegisterPairHigh<Register>(),
6975 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
6976 } else if (instruction->IsOr()) {
6977 __ orl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
6978 __ orl(first.AsRegisterPairHigh<Register>(),
6979 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
6980 } else {
6981 DCHECK(instruction->IsXor());
6982 __ xorl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
6983 __ xorl(first.AsRegisterPairHigh<Register>(),
6984 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
6985 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006986 } else {
6987 DCHECK(second.IsConstant()) << second;
6988 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006989 int32_t low_value = Low32Bits(value);
6990 int32_t high_value = High32Bits(value);
6991 Immediate low(low_value);
6992 Immediate high(high_value);
6993 Register first_low = first.AsRegisterPairLow<Register>();
6994 Register first_high = first.AsRegisterPairHigh<Register>();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006995 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006996 if (low_value == 0) {
6997 __ xorl(first_low, first_low);
6998 } else if (low_value != -1) {
6999 __ andl(first_low, low);
7000 }
7001 if (high_value == 0) {
7002 __ xorl(first_high, first_high);
7003 } else if (high_value != -1) {
7004 __ andl(first_high, high);
7005 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007006 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04007007 if (low_value != 0) {
7008 __ orl(first_low, low);
7009 }
7010 if (high_value != 0) {
7011 __ orl(first_high, high);
7012 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007013 } else {
7014 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04007015 if (low_value != 0) {
7016 __ xorl(first_low, low);
7017 }
7018 if (high_value != 0) {
7019 __ xorl(first_high, high);
7020 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007021 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007022 }
7023 }
7024}
7025
Roland Levillain7c1559a2015-12-15 10:55:36 +00007026void InstructionCodeGeneratorX86::GenerateReferenceLoadOneRegister(HInstruction* instruction,
7027 Location out,
7028 uint32_t offset,
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007029 Location maybe_temp,
7030 bool emit_read_barrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007031 Register out_reg = out.AsRegister<Register>();
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007032 if (emit_read_barrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007033 if (kUseBakerReadBarrier) {
7034 // Load with fast path based Baker's read barrier.
7035 // /* HeapReference<Object> */ out = *(out + offset)
7036 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00007037 instruction, out, out_reg, offset, /* needs_null_check */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007038 } else {
7039 // Load with slow path based read barrier.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00007040 // Save the value of `out` into `maybe_temp` before overwriting it
Roland Levillain7c1559a2015-12-15 10:55:36 +00007041 // in the following move operation, as we will need it for the
7042 // read barrier below.
Vladimir Marko953437b2016-08-24 08:30:46 +00007043 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillain95e7ffc2016-01-22 11:57:25 +00007044 __ movl(maybe_temp.AsRegister<Register>(), out_reg);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007045 // /* HeapReference<Object> */ out = *(out + offset)
7046 __ movl(out_reg, Address(out_reg, offset));
Roland Levillain95e7ffc2016-01-22 11:57:25 +00007047 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007048 }
7049 } else {
7050 // Plain load with no read barrier.
7051 // /* HeapReference<Object> */ out = *(out + offset)
7052 __ movl(out_reg, Address(out_reg, offset));
7053 __ MaybeUnpoisonHeapReference(out_reg);
7054 }
7055}
7056
7057void InstructionCodeGeneratorX86::GenerateReferenceLoadTwoRegisters(HInstruction* instruction,
7058 Location out,
7059 Location obj,
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007060 uint32_t offset,
7061 bool emit_read_barrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007062 Register out_reg = out.AsRegister<Register>();
7063 Register obj_reg = obj.AsRegister<Register>();
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007064 if (emit_read_barrier) {
7065 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007066 if (kUseBakerReadBarrier) {
7067 // Load with fast path based Baker's read barrier.
7068 // /* HeapReference<Object> */ out = *(obj + offset)
7069 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00007070 instruction, out, obj_reg, offset, /* needs_null_check */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007071 } else {
7072 // Load with slow path based read barrier.
7073 // /* HeapReference<Object> */ out = *(obj + offset)
7074 __ movl(out_reg, Address(obj_reg, offset));
7075 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
7076 }
7077 } else {
7078 // Plain load with no read barrier.
7079 // /* HeapReference<Object> */ out = *(obj + offset)
7080 __ movl(out_reg, Address(obj_reg, offset));
7081 __ MaybeUnpoisonHeapReference(out_reg);
7082 }
7083}
7084
7085void InstructionCodeGeneratorX86::GenerateGcRootFieldLoad(HInstruction* instruction,
7086 Location root,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007087 const Address& address,
Mathieu Chartier31b12e32016-09-02 17:11:57 -07007088 Label* fixup_label,
7089 bool requires_read_barrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007090 Register root_reg = root.AsRegister<Register>();
Mathieu Chartier31b12e32016-09-02 17:11:57 -07007091 if (requires_read_barrier) {
7092 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007093 if (kUseBakerReadBarrier) {
7094 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
7095 // Baker's read barrier are used:
7096 //
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007097 // root = *address;
Roland Levillain7c1559a2015-12-15 10:55:36 +00007098 // if (Thread::Current()->GetIsGcMarking()) {
7099 // root = ReadBarrier::Mark(root)
7100 // }
7101
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007102 // /* GcRoot<mirror::Object> */ root = *address
7103 __ movl(root_reg, address);
7104 if (fixup_label != nullptr) {
7105 __ Bind(fixup_label);
7106 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00007107 static_assert(
7108 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
7109 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
7110 "have different sizes.");
7111 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
7112 "art::mirror::CompressedReference<mirror::Object> and int32_t "
7113 "have different sizes.");
7114
Vladimir Marko953437b2016-08-24 08:30:46 +00007115 // Slow path marking the GC root `root`.
7116 SlowPathCode* slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathX86(
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007117 instruction, root, /* unpoison_ref_before_marking */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007118 codegen_->AddSlowPath(slow_path);
7119
Andreas Gampe542451c2016-07-26 09:02:02 -07007120 __ fs()->cmpl(Address::Absolute(Thread::IsGcMarkingOffset<kX86PointerSize>().Int32Value()),
Roland Levillain7c1559a2015-12-15 10:55:36 +00007121 Immediate(0));
7122 __ j(kNotEqual, slow_path->GetEntryLabel());
7123 __ Bind(slow_path->GetExitLabel());
7124 } else {
7125 // GC root loaded through a slow path for read barriers other
7126 // than Baker's.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007127 // /* GcRoot<mirror::Object>* */ root = address
7128 __ leal(root_reg, address);
7129 if (fixup_label != nullptr) {
7130 __ Bind(fixup_label);
7131 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00007132 // /* mirror::Object* */ root = root->Read()
7133 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
7134 }
7135 } else {
7136 // Plain GC root load with no read barrier.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007137 // /* GcRoot<mirror::Object> */ root = *address
7138 __ movl(root_reg, address);
7139 if (fixup_label != nullptr) {
7140 __ Bind(fixup_label);
7141 }
Roland Levillaine3f43ac2016-01-19 15:07:47 +00007142 // Note that GC roots are not affected by heap poisoning, thus we
7143 // do not have to unpoison `root_reg` here.
Roland Levillain7c1559a2015-12-15 10:55:36 +00007144 }
7145}
7146
7147void CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
7148 Location ref,
7149 Register obj,
7150 uint32_t offset,
Roland Levillain7c1559a2015-12-15 10:55:36 +00007151 bool needs_null_check) {
7152 DCHECK(kEmitCompilerReadBarrier);
7153 DCHECK(kUseBakerReadBarrier);
7154
7155 // /* HeapReference<Object> */ ref = *(obj + offset)
7156 Address src(obj, offset);
Vladimir Marko953437b2016-08-24 08:30:46 +00007157 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, needs_null_check);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007158}
7159
7160void CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
7161 Location ref,
7162 Register obj,
7163 uint32_t data_offset,
7164 Location index,
Roland Levillain7c1559a2015-12-15 10:55:36 +00007165 bool needs_null_check) {
7166 DCHECK(kEmitCompilerReadBarrier);
7167 DCHECK(kUseBakerReadBarrier);
7168
Roland Levillain3d312422016-06-23 13:53:42 +01007169 static_assert(
7170 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
7171 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain7c1559a2015-12-15 10:55:36 +00007172 // /* HeapReference<Object> */ ref =
7173 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007174 Address src = CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset);
Vladimir Marko953437b2016-08-24 08:30:46 +00007175 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, needs_null_check);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007176}
7177
7178void CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
7179 Location ref,
7180 Register obj,
7181 const Address& src,
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007182 bool needs_null_check,
7183 bool always_update_field,
7184 Register* temp) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007185 DCHECK(kEmitCompilerReadBarrier);
7186 DCHECK(kUseBakerReadBarrier);
7187
7188 // In slow path based read barriers, the read barrier call is
7189 // inserted after the original load. However, in fast path based
7190 // Baker's read barriers, we need to perform the load of
7191 // mirror::Object::monitor_ *before* the original reference load.
7192 // This load-load ordering is required by the read barrier.
7193 // The fast path/slow path (for Baker's algorithm) should look like:
7194 //
7195 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
7196 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
7197 // HeapReference<Object> ref = *src; // Original reference load.
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07007198 // bool is_gray = (rb_state == ReadBarrier::GrayState());
Roland Levillain7c1559a2015-12-15 10:55:36 +00007199 // if (is_gray) {
7200 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
7201 // }
7202 //
7203 // Note: the original implementation in ReadBarrier::Barrier is
7204 // slightly more complex as:
7205 // - it implements the load-load fence using a data dependency on
Roland Levillaine3f43ac2016-01-19 15:07:47 +00007206 // the high-bits of rb_state, which are expected to be all zeroes
7207 // (we use CodeGeneratorX86::GenerateMemoryBarrier instead here,
7208 // which is a no-op thanks to the x86 memory model);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007209 // - it performs additional checks that we do not do here for
7210 // performance reasons.
7211
7212 Register ref_reg = ref.AsRegister<Register>();
Roland Levillain7c1559a2015-12-15 10:55:36 +00007213 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
7214
Vladimir Marko953437b2016-08-24 08:30:46 +00007215 // Given the numeric representation, it's enough to check the low bit of the rb_state.
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07007216 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
7217 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
Vladimir Marko953437b2016-08-24 08:30:46 +00007218 constexpr uint32_t gray_byte_position = LockWord::kReadBarrierStateShift / kBitsPerByte;
7219 constexpr uint32_t gray_bit_position = LockWord::kReadBarrierStateShift % kBitsPerByte;
7220 constexpr int32_t test_value = static_cast<int8_t>(1 << gray_bit_position);
7221
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07007222 // if (rb_state == ReadBarrier::GrayState())
Vladimir Marko953437b2016-08-24 08:30:46 +00007223 // ref = ReadBarrier::Mark(ref);
7224 // At this point, just do the "if" and make sure that flags are preserved until the branch.
7225 __ testb(Address(obj, monitor_offset + gray_byte_position), Immediate(test_value));
Roland Levillain7c1559a2015-12-15 10:55:36 +00007226 if (needs_null_check) {
7227 MaybeRecordImplicitNullCheck(instruction);
7228 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00007229
7230 // Load fence to prevent load-load reordering.
7231 // Note that this is a no-op, thanks to the x86 memory model.
7232 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
7233
7234 // The actual reference load.
7235 // /* HeapReference<Object> */ ref = *src
Vladimir Marko953437b2016-08-24 08:30:46 +00007236 __ movl(ref_reg, src); // Flags are unaffected.
7237
7238 // Note: Reference unpoisoning modifies the flags, so we need to delay it after the branch.
7239 // Slow path marking the object `ref` when it is gray.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007240 SlowPathCode* slow_path;
7241 if (always_update_field) {
7242 DCHECK(temp != nullptr);
7243 slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkAndUpdateFieldSlowPathX86(
7244 instruction, ref, obj, src, /* unpoison_ref_before_marking */ true, *temp);
7245 } else {
7246 slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathX86(
7247 instruction, ref, /* unpoison_ref_before_marking */ true);
7248 }
Vladimir Marko953437b2016-08-24 08:30:46 +00007249 AddSlowPath(slow_path);
7250
7251 // We have done the "if" of the gray bit check above, now branch based on the flags.
7252 __ j(kNotZero, slow_path->GetEntryLabel());
Roland Levillain7c1559a2015-12-15 10:55:36 +00007253
7254 // Object* ref = ref_addr->AsMirrorPtr()
7255 __ MaybeUnpoisonHeapReference(ref_reg);
7256
Roland Levillain7c1559a2015-12-15 10:55:36 +00007257 __ Bind(slow_path->GetExitLabel());
7258}
7259
7260void CodeGeneratorX86::GenerateReadBarrierSlow(HInstruction* instruction,
7261 Location out,
7262 Location ref,
7263 Location obj,
7264 uint32_t offset,
7265 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007266 DCHECK(kEmitCompilerReadBarrier);
7267
Roland Levillain7c1559a2015-12-15 10:55:36 +00007268 // Insert a slow path based read barrier *after* the reference load.
7269 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00007270 // If heap poisoning is enabled, the unpoisoning of the loaded
7271 // reference will be carried out by the runtime within the slow
7272 // path.
7273 //
7274 // Note that `ref` currently does not get unpoisoned (when heap
7275 // poisoning is enabled), which is alright as the `ref` argument is
7276 // not used by the artReadBarrierSlow entry point.
7277 //
7278 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
7279 SlowPathCode* slow_path = new (GetGraph()->GetArena())
7280 ReadBarrierForHeapReferenceSlowPathX86(instruction, out, ref, obj, offset, index);
7281 AddSlowPath(slow_path);
7282
Roland Levillain0d5a2812015-11-13 10:07:31 +00007283 __ jmp(slow_path->GetEntryLabel());
7284 __ Bind(slow_path->GetExitLabel());
7285}
7286
Roland Levillain7c1559a2015-12-15 10:55:36 +00007287void CodeGeneratorX86::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
7288 Location out,
7289 Location ref,
7290 Location obj,
7291 uint32_t offset,
7292 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007293 if (kEmitCompilerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007294 // Baker's read barriers shall be handled by the fast path
7295 // (CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier).
7296 DCHECK(!kUseBakerReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007297 // If heap poisoning is enabled, unpoisoning will be taken care of
7298 // by the runtime within the slow path.
Roland Levillain7c1559a2015-12-15 10:55:36 +00007299 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007300 } else if (kPoisonHeapReferences) {
7301 __ UnpoisonHeapReference(out.AsRegister<Register>());
7302 }
7303}
7304
Roland Levillain7c1559a2015-12-15 10:55:36 +00007305void CodeGeneratorX86::GenerateReadBarrierForRootSlow(HInstruction* instruction,
7306 Location out,
7307 Location root) {
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 GC root load.
7311 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00007312 // Note that GC roots are not affected by heap poisoning, so we do
7313 // not need to do anything special for this here.
7314 SlowPathCode* slow_path =
7315 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathX86(instruction, out, root);
7316 AddSlowPath(slow_path);
7317
Roland Levillain0d5a2812015-11-13 10:07:31 +00007318 __ jmp(slow_path->GetEntryLabel());
7319 __ Bind(slow_path->GetExitLabel());
7320}
7321
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007322void LocationsBuilderX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00007323 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00007324 LOG(FATAL) << "Unreachable";
7325}
7326
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007327void InstructionCodeGeneratorX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00007328 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00007329 LOG(FATAL) << "Unreachable";
7330}
7331
Mark Mendellfe57faa2015-09-18 09:26:15 -04007332// Simple implementation of packed switch - generate cascaded compare/jumps.
7333void LocationsBuilderX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7334 LocationSummary* locations =
7335 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
7336 locations->SetInAt(0, Location::RequiresRegister());
7337}
7338
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007339void InstructionCodeGeneratorX86::GenPackedSwitchWithCompares(Register value_reg,
7340 int32_t lower_bound,
7341 uint32_t num_entries,
7342 HBasicBlock* switch_block,
7343 HBasicBlock* default_block) {
7344 // Figure out the correct compare values and jump conditions.
7345 // Handle the first compare/branch as a special case because it might
7346 // jump to the default case.
7347 DCHECK_GT(num_entries, 2u);
7348 Condition first_condition;
7349 uint32_t index;
7350 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
7351 if (lower_bound != 0) {
7352 first_condition = kLess;
7353 __ cmpl(value_reg, Immediate(lower_bound));
7354 __ j(first_condition, codegen_->GetLabelOf(default_block));
7355 __ j(kEqual, codegen_->GetLabelOf(successors[0]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007356
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007357 index = 1;
7358 } else {
7359 // Handle all the compare/jumps below.
7360 first_condition = kBelow;
7361 index = 0;
7362 }
7363
7364 // Handle the rest of the compare/jumps.
7365 for (; index + 1 < num_entries; index += 2) {
7366 int32_t compare_to_value = lower_bound + index + 1;
7367 __ cmpl(value_reg, Immediate(compare_to_value));
7368 // Jump to successors[index] if value < case_value[index].
7369 __ j(first_condition, codegen_->GetLabelOf(successors[index]));
7370 // Jump to successors[index + 1] if value == case_value[index + 1].
7371 __ j(kEqual, codegen_->GetLabelOf(successors[index + 1]));
7372 }
7373
7374 if (index != num_entries) {
7375 // There are an odd number of entries. Handle the last one.
7376 DCHECK_EQ(index + 1, num_entries);
7377 __ cmpl(value_reg, Immediate(lower_bound + index));
7378 __ j(kEqual, codegen_->GetLabelOf(successors[index]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007379 }
7380
7381 // And the default for any other value.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007382 if (!codegen_->GoesToNextBlock(switch_block, default_block)) {
7383 __ jmp(codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007384 }
7385}
7386
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007387void InstructionCodeGeneratorX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7388 int32_t lower_bound = switch_instr->GetStartValue();
7389 uint32_t num_entries = switch_instr->GetNumEntries();
7390 LocationSummary* locations = switch_instr->GetLocations();
7391 Register value_reg = locations->InAt(0).AsRegister<Register>();
7392
7393 GenPackedSwitchWithCompares(value_reg,
7394 lower_bound,
7395 num_entries,
7396 switch_instr->GetBlock(),
7397 switch_instr->GetDefaultBlock());
7398}
7399
Mark Mendell805b3b52015-09-18 14:10:29 -04007400void LocationsBuilderX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
7401 LocationSummary* locations =
7402 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
7403 locations->SetInAt(0, Location::RequiresRegister());
7404
7405 // Constant area pointer.
7406 locations->SetInAt(1, Location::RequiresRegister());
7407
7408 // And the temporary we need.
7409 locations->AddTemp(Location::RequiresRegister());
7410}
7411
7412void InstructionCodeGeneratorX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
7413 int32_t lower_bound = switch_instr->GetStartValue();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007414 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendell805b3b52015-09-18 14:10:29 -04007415 LocationSummary* locations = switch_instr->GetLocations();
7416 Register value_reg = locations->InAt(0).AsRegister<Register>();
7417 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
7418
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007419 if (num_entries <= kPackedSwitchJumpTableThreshold) {
7420 GenPackedSwitchWithCompares(value_reg,
7421 lower_bound,
7422 num_entries,
7423 switch_instr->GetBlock(),
7424 default_block);
7425 return;
7426 }
7427
Mark Mendell805b3b52015-09-18 14:10:29 -04007428 // Optimizing has a jump area.
7429 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
7430 Register constant_area = locations->InAt(1).AsRegister<Register>();
7431
7432 // Remove the bias, if needed.
7433 if (lower_bound != 0) {
7434 __ leal(temp_reg, Address(value_reg, -lower_bound));
7435 value_reg = temp_reg;
7436 }
7437
7438 // Is the value in range?
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007439 DCHECK_GE(num_entries, 1u);
Mark Mendell805b3b52015-09-18 14:10:29 -04007440 __ cmpl(value_reg, Immediate(num_entries - 1));
7441 __ j(kAbove, codegen_->GetLabelOf(default_block));
7442
7443 // We are in the range of the table.
7444 // Load (target-constant_area) from the jump table, indexing by the value.
7445 __ movl(temp_reg, codegen_->LiteralCaseTable(switch_instr, constant_area, value_reg));
7446
7447 // Compute the actual target address by adding in constant_area.
7448 __ addl(temp_reg, constant_area);
7449
7450 // And jump.
7451 __ jmp(temp_reg);
7452}
7453
Mark Mendell0616ae02015-04-17 12:49:27 -04007454void LocationsBuilderX86::VisitX86ComputeBaseMethodAddress(
7455 HX86ComputeBaseMethodAddress* insn) {
7456 LocationSummary* locations =
7457 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
7458 locations->SetOut(Location::RequiresRegister());
7459}
7460
7461void InstructionCodeGeneratorX86::VisitX86ComputeBaseMethodAddress(
7462 HX86ComputeBaseMethodAddress* insn) {
7463 LocationSummary* locations = insn->GetLocations();
7464 Register reg = locations->Out().AsRegister<Register>();
7465
7466 // Generate call to next instruction.
7467 Label next_instruction;
7468 __ call(&next_instruction);
7469 __ Bind(&next_instruction);
7470
7471 // Remember this offset for later use with constant area.
7472 codegen_->SetMethodAddressOffset(GetAssembler()->CodeSize());
7473
7474 // Grab the return address off the stack.
7475 __ popl(reg);
7476}
7477
7478void LocationsBuilderX86::VisitX86LoadFromConstantTable(
7479 HX86LoadFromConstantTable* insn) {
7480 LocationSummary* locations =
7481 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
7482
7483 locations->SetInAt(0, Location::RequiresRegister());
7484 locations->SetInAt(1, Location::ConstantLocation(insn->GetConstant()));
7485
7486 // If we don't need to be materialized, we only need the inputs to be set.
David Brazdilb3e773e2016-01-26 11:28:37 +00007487 if (insn->IsEmittedAtUseSite()) {
Mark Mendell0616ae02015-04-17 12:49:27 -04007488 return;
7489 }
7490
7491 switch (insn->GetType()) {
7492 case Primitive::kPrimFloat:
7493 case Primitive::kPrimDouble:
7494 locations->SetOut(Location::RequiresFpuRegister());
7495 break;
7496
7497 case Primitive::kPrimInt:
7498 locations->SetOut(Location::RequiresRegister());
7499 break;
7500
7501 default:
7502 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
7503 }
7504}
7505
7506void InstructionCodeGeneratorX86::VisitX86LoadFromConstantTable(HX86LoadFromConstantTable* insn) {
David Brazdilb3e773e2016-01-26 11:28:37 +00007507 if (insn->IsEmittedAtUseSite()) {
Mark Mendell0616ae02015-04-17 12:49:27 -04007508 return;
7509 }
7510
7511 LocationSummary* locations = insn->GetLocations();
7512 Location out = locations->Out();
7513 Register const_area = locations->InAt(0).AsRegister<Register>();
7514 HConstant *value = insn->GetConstant();
7515
7516 switch (insn->GetType()) {
7517 case Primitive::kPrimFloat:
7518 __ movss(out.AsFpuRegister<XmmRegister>(),
7519 codegen_->LiteralFloatAddress(value->AsFloatConstant()->GetValue(), const_area));
7520 break;
7521
7522 case Primitive::kPrimDouble:
7523 __ movsd(out.AsFpuRegister<XmmRegister>(),
7524 codegen_->LiteralDoubleAddress(value->AsDoubleConstant()->GetValue(), const_area));
7525 break;
7526
7527 case Primitive::kPrimInt:
7528 __ movl(out.AsRegister<Register>(),
7529 codegen_->LiteralInt32Address(value->AsIntConstant()->GetValue(), const_area));
7530 break;
7531
7532 default:
7533 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
7534 }
7535}
7536
Mark Mendell0616ae02015-04-17 12:49:27 -04007537/**
7538 * Class to handle late fixup of offsets into constant area.
7539 */
Vladimir Marko5233f932015-09-29 19:01:15 +01007540class RIPFixup : public AssemblerFixup, public ArenaObject<kArenaAllocCodeGenerator> {
Mark Mendell0616ae02015-04-17 12:49:27 -04007541 public:
Mark Mendell805b3b52015-09-18 14:10:29 -04007542 RIPFixup(CodeGeneratorX86& codegen, size_t offset)
7543 : codegen_(&codegen), offset_into_constant_area_(offset) {}
7544
7545 protected:
7546 void SetOffset(size_t offset) { offset_into_constant_area_ = offset; }
7547
7548 CodeGeneratorX86* codegen_;
Mark Mendell0616ae02015-04-17 12:49:27 -04007549
7550 private:
7551 void Process(const MemoryRegion& region, int pos) OVERRIDE {
7552 // Patch the correct offset for the instruction. The place to patch is the
7553 // last 4 bytes of the instruction.
7554 // The value to patch is the distance from the offset in the constant area
7555 // from the address computed by the HX86ComputeBaseMethodAddress instruction.
Mark Mendell805b3b52015-09-18 14:10:29 -04007556 int32_t constant_offset = codegen_->ConstantAreaStart() + offset_into_constant_area_;
7557 int32_t relative_position = constant_offset - codegen_->GetMethodAddressOffset();;
Mark Mendell0616ae02015-04-17 12:49:27 -04007558
7559 // Patch in the right value.
7560 region.StoreUnaligned<int32_t>(pos - 4, relative_position);
7561 }
7562
Mark Mendell0616ae02015-04-17 12:49:27 -04007563 // Location in constant area that the fixup refers to.
Mark Mendell805b3b52015-09-18 14:10:29 -04007564 int32_t offset_into_constant_area_;
Mark Mendell0616ae02015-04-17 12:49:27 -04007565};
7566
Mark Mendell805b3b52015-09-18 14:10:29 -04007567/**
7568 * Class to handle late fixup of offsets to a jump table that will be created in the
7569 * constant area.
7570 */
7571class JumpTableRIPFixup : public RIPFixup {
7572 public:
7573 JumpTableRIPFixup(CodeGeneratorX86& codegen, HX86PackedSwitch* switch_instr)
7574 : RIPFixup(codegen, static_cast<size_t>(-1)), switch_instr_(switch_instr) {}
7575
7576 void CreateJumpTable() {
7577 X86Assembler* assembler = codegen_->GetAssembler();
7578
7579 // Ensure that the reference to the jump table has the correct offset.
7580 const int32_t offset_in_constant_table = assembler->ConstantAreaSize();
7581 SetOffset(offset_in_constant_table);
7582
7583 // The label values in the jump table are computed relative to the
7584 // instruction addressing the constant area.
7585 const int32_t relative_offset = codegen_->GetMethodAddressOffset();
7586
7587 // Populate the jump table with the correct values for the jump table.
7588 int32_t num_entries = switch_instr_->GetNumEntries();
7589 HBasicBlock* block = switch_instr_->GetBlock();
7590 const ArenaVector<HBasicBlock*>& successors = block->GetSuccessors();
7591 // The value that we want is the target offset - the position of the table.
7592 for (int32_t i = 0; i < num_entries; i++) {
7593 HBasicBlock* b = successors[i];
7594 Label* l = codegen_->GetLabelOf(b);
7595 DCHECK(l->IsBound());
7596 int32_t offset_to_block = l->Position() - relative_offset;
7597 assembler->AppendInt32(offset_to_block);
7598 }
7599 }
7600
7601 private:
7602 const HX86PackedSwitch* switch_instr_;
7603};
7604
7605void CodeGeneratorX86::Finalize(CodeAllocator* allocator) {
7606 // Generate the constant area if needed.
7607 X86Assembler* assembler = GetAssembler();
7608 if (!assembler->IsConstantAreaEmpty() || !fixups_to_jump_tables_.empty()) {
7609 // Align to 4 byte boundary to reduce cache misses, as the data is 4 and 8
7610 // byte values.
7611 assembler->Align(4, 0);
7612 constant_area_start_ = assembler->CodeSize();
7613
7614 // Populate any jump tables.
7615 for (auto jump_table : fixups_to_jump_tables_) {
7616 jump_table->CreateJumpTable();
7617 }
7618
7619 // And now add the constant area to the generated code.
7620 assembler->AddConstantArea();
7621 }
7622
7623 // And finish up.
7624 CodeGenerator::Finalize(allocator);
7625}
7626
Mark Mendell0616ae02015-04-17 12:49:27 -04007627Address CodeGeneratorX86::LiteralDoubleAddress(double v, Register reg) {
7628 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddDouble(v));
7629 return Address(reg, kDummy32BitOffset, fixup);
7630}
7631
7632Address CodeGeneratorX86::LiteralFloatAddress(float v, Register reg) {
7633 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddFloat(v));
7634 return Address(reg, kDummy32BitOffset, fixup);
7635}
7636
7637Address CodeGeneratorX86::LiteralInt32Address(int32_t v, Register reg) {
7638 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt32(v));
7639 return Address(reg, kDummy32BitOffset, fixup);
7640}
7641
7642Address CodeGeneratorX86::LiteralInt64Address(int64_t v, Register reg) {
7643 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt64(v));
7644 return Address(reg, kDummy32BitOffset, fixup);
7645}
7646
Aart Bika19616e2016-02-01 18:57:58 -08007647void CodeGeneratorX86::Load32BitValue(Register dest, int32_t value) {
7648 if (value == 0) {
7649 __ xorl(dest, dest);
7650 } else {
7651 __ movl(dest, Immediate(value));
7652 }
7653}
7654
7655void CodeGeneratorX86::Compare32BitValue(Register dest, int32_t value) {
7656 if (value == 0) {
7657 __ testl(dest, dest);
7658 } else {
7659 __ cmpl(dest, Immediate(value));
7660 }
7661}
7662
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007663void CodeGeneratorX86::GenerateIntCompare(Location lhs, Location rhs) {
7664 Register lhs_reg = lhs.AsRegister<Register>();
jessicahandojo4877b792016-09-08 19:49:13 -07007665 GenerateIntCompare(lhs_reg, rhs);
7666}
7667
7668void CodeGeneratorX86::GenerateIntCompare(Register lhs, Location rhs) {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007669 if (rhs.IsConstant()) {
7670 int32_t value = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
jessicahandojo4877b792016-09-08 19:49:13 -07007671 Compare32BitValue(lhs, value);
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007672 } else if (rhs.IsStackSlot()) {
jessicahandojo4877b792016-09-08 19:49:13 -07007673 __ cmpl(lhs, Address(ESP, rhs.GetStackIndex()));
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007674 } else {
jessicahandojo4877b792016-09-08 19:49:13 -07007675 __ cmpl(lhs, rhs.AsRegister<Register>());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007676 }
7677}
7678
7679Address CodeGeneratorX86::ArrayAddress(Register obj,
7680 Location index,
7681 ScaleFactor scale,
7682 uint32_t data_offset) {
7683 return index.IsConstant() ?
7684 Address(obj, (index.GetConstant()->AsIntConstant()->GetValue() << scale) + data_offset) :
7685 Address(obj, index.AsRegister<Register>(), scale, data_offset);
7686}
7687
Mark Mendell805b3b52015-09-18 14:10:29 -04007688Address CodeGeneratorX86::LiteralCaseTable(HX86PackedSwitch* switch_instr,
7689 Register reg,
7690 Register value) {
7691 // Create a fixup to be used to create and address the jump table.
7692 JumpTableRIPFixup* table_fixup =
7693 new (GetGraph()->GetArena()) JumpTableRIPFixup(*this, switch_instr);
7694
7695 // We have to populate the jump tables.
7696 fixups_to_jump_tables_.push_back(table_fixup);
7697
7698 // We want a scaled address, as we are extracting the correct offset from the table.
7699 return Address(reg, value, TIMES_4, kDummy32BitOffset, table_fixup);
7700}
7701
Andreas Gampe85b62f22015-09-09 13:15:38 -07007702// TODO: target as memory.
7703void CodeGeneratorX86::MoveFromReturnRegister(Location target, Primitive::Type type) {
7704 if (!target.IsValid()) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007705 DCHECK_EQ(type, Primitive::kPrimVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07007706 return;
7707 }
7708
7709 DCHECK_NE(type, Primitive::kPrimVoid);
7710
7711 Location return_loc = InvokeDexCallingConventionVisitorX86().GetReturnLocation(type);
7712 if (target.Equals(return_loc)) {
7713 return;
7714 }
7715
7716 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
7717 // with the else branch.
7718 if (type == Primitive::kPrimLong) {
7719 HParallelMove parallel_move(GetGraph()->GetArena());
7720 parallel_move.AddMove(return_loc.ToLow(), target.ToLow(), Primitive::kPrimInt, nullptr);
7721 parallel_move.AddMove(return_loc.ToHigh(), target.ToHigh(), Primitive::kPrimInt, nullptr);
7722 GetMoveResolver()->EmitNativeCode(&parallel_move);
7723 } else {
7724 // Let the parallel move resolver take care of all of this.
7725 HParallelMove parallel_move(GetGraph()->GetArena());
7726 parallel_move.AddMove(return_loc, target, type, nullptr);
7727 GetMoveResolver()->EmitNativeCode(&parallel_move);
7728 }
7729}
7730
Roland Levillain4d027112015-07-01 15:41:14 +01007731#undef __
7732
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00007733} // namespace x86
7734} // namespace art