blob: 0b235996650103f778bd2c79653b36c128a203a4 [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();
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100315 Location object_class = instruction_->IsCheckCast() ? locations->GetTemp(0)
316 : locations->Out();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000317 DCHECK(instruction_->IsCheckCast()
318 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000319
320 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
321 __ Bind(GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000322
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000323 if (!is_fatal_) {
324 SaveLiveRegisters(codegen, locations);
325 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000326
327 // We're moving two locations to locations that could overlap, so we need a parallel
328 // move resolver.
329 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000330 x86_codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100331 locations->InAt(1),
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000332 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100333 Primitive::kPrimNot,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100334 object_class,
Nicolas Geoffray90218252015-04-15 11:56:51 +0100335 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
336 Primitive::kPrimNot);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000337
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000338 if (instruction_->IsInstanceOf()) {
Serban Constantinescuba45db02016-07-12 22:53:02 +0100339 x86_codegen->InvokeRuntime(kQuickInstanceofNonTrivial,
Alexandre Rames8158f282015-08-07 10:26:17 +0100340 instruction_,
341 instruction_->GetDexPc(),
342 this);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000343 CheckEntrypointTypes<
Andreas Gampe67409972016-07-19 22:34:53 -0700344 kQuickInstanceofNonTrivial, size_t, const mirror::Class*, const mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000345 } else {
346 DCHECK(instruction_->IsCheckCast());
Serban Constantinescuba45db02016-07-12 22:53:02 +0100347 x86_codegen->InvokeRuntime(kQuickCheckCast, instruction_, instruction_->GetDexPc(), this);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000348 CheckEntrypointTypes<kQuickCheckCast, void, const mirror::Class*, const mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000349 }
350
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000351 if (!is_fatal_) {
352 if (instruction_->IsInstanceOf()) {
353 x86_codegen->Move32(locations->Out(), Location::RegisterLocation(EAX));
354 }
355 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray75374372015-09-17 17:12:19 +0000356
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000357 __ jmp(GetExitLabel());
358 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000359 }
360
Alexandre Rames9931f312015-06-19 14:47:01 +0100361 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathX86"; }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000362 bool IsFatal() const OVERRIDE { return is_fatal_; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100363
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000364 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000365 const bool is_fatal_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000366
367 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathX86);
368};
369
Andreas Gampe85b62f22015-09-09 13:15:38 -0700370class DeoptimizationSlowPathX86 : public SlowPathCode {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700371 public:
Aart Bik42249c32016-01-07 15:33:50 -0800372 explicit DeoptimizationSlowPathX86(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000373 : SlowPathCode(instruction) {}
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700374
375 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames8158f282015-08-07 10:26:17 +0100376 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700377 __ Bind(GetEntryLabel());
Serban Constantinescuba45db02016-07-12 22:53:02 +0100378 x86_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000379 CheckEntrypointTypes<kQuickDeoptimize, void, void>();
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700380 }
381
Alexandre Rames9931f312015-06-19 14:47:01 +0100382 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathX86"; }
383
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700384 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700385 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathX86);
386};
387
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100388class ArraySetSlowPathX86 : public SlowPathCode {
389 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000390 explicit ArraySetSlowPathX86(HInstruction* instruction) : SlowPathCode(instruction) {}
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100391
392 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
393 LocationSummary* locations = instruction_->GetLocations();
394 __ Bind(GetEntryLabel());
395 SaveLiveRegisters(codegen, locations);
396
397 InvokeRuntimeCallingConvention calling_convention;
398 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
399 parallel_move.AddMove(
400 locations->InAt(0),
401 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
402 Primitive::kPrimNot,
403 nullptr);
404 parallel_move.AddMove(
405 locations->InAt(1),
406 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
407 Primitive::kPrimInt,
408 nullptr);
409 parallel_move.AddMove(
410 locations->InAt(2),
411 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
412 Primitive::kPrimNot,
413 nullptr);
414 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
415
416 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Serban Constantinescuba45db02016-07-12 22:53:02 +0100417 x86_codegen->InvokeRuntime(kQuickAputObject, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000418 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100419 RestoreLiveRegisters(codegen, locations);
420 __ jmp(GetExitLabel());
421 }
422
423 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathX86"; }
424
425 private:
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100426 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathX86);
427};
428
Roland Levillain7c1559a2015-12-15 10:55:36 +0000429// Slow path marking an object during a read barrier.
430class ReadBarrierMarkSlowPathX86 : public SlowPathCode {
431 public:
Vladimir Marko953437b2016-08-24 08:30:46 +0000432 ReadBarrierMarkSlowPathX86(HInstruction* instruction, Location obj, bool unpoison)
433 : SlowPathCode(instruction), obj_(obj), unpoison_(unpoison) {
Roland Levillain7c1559a2015-12-15 10:55:36 +0000434 DCHECK(kEmitCompilerReadBarrier);
435 }
436
437 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathX86"; }
438
439 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
440 LocationSummary* locations = instruction_->GetLocations();
Roland Levillain02b75802016-07-13 11:54:35 +0100441 Register reg = obj_.AsRegister<Register>();
Roland Levillain7c1559a2015-12-15 10:55:36 +0000442 DCHECK(locations->CanCall());
Roland Levillain02b75802016-07-13 11:54:35 +0100443 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg));
Roland Levillain7c1559a2015-12-15 10:55:36 +0000444 DCHECK(instruction_->IsInstanceFieldGet() ||
445 instruction_->IsStaticFieldGet() ||
446 instruction_->IsArrayGet() ||
Roland Levillain16d9f942016-08-25 17:27:56 +0100447 instruction_->IsArraySet() ||
Roland Levillain7c1559a2015-12-15 10:55:36 +0000448 instruction_->IsLoadClass() ||
449 instruction_->IsLoadString() ||
450 instruction_->IsInstanceOf() ||
Roland Levillain3d312422016-06-23 13:53:42 +0100451 instruction_->IsCheckCast() ||
Roland Levillain0b671c02016-08-19 12:02:34 +0100452 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()) ||
453 (instruction_->IsInvokeStaticOrDirect() && instruction_->GetLocations()->Intrinsified()))
Roland Levillain7c1559a2015-12-15 10:55:36 +0000454 << "Unexpected instruction in read barrier marking slow path: "
455 << instruction_->DebugName();
456
457 __ Bind(GetEntryLabel());
Vladimir Marko953437b2016-08-24 08:30:46 +0000458 if (unpoison_) {
459 // Object* ref = ref_addr->AsMirrorPtr()
460 __ MaybeUnpoisonHeapReference(reg);
461 }
Roland Levillain4359e612016-07-20 11:32:19 +0100462 // No need to save live registers; it's taken care of by the
463 // entrypoint. Also, there is no need to update the stack mask,
464 // as this runtime call will not trigger a garbage collection.
Roland Levillain7c1559a2015-12-15 10:55:36 +0000465 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Roland Levillain02b75802016-07-13 11:54:35 +0100466 DCHECK_NE(reg, ESP);
467 DCHECK(0 <= reg && reg < kNumberOfCpuRegisters) << reg;
468 // "Compact" slow path, saving two moves.
469 //
470 // Instead of using the standard runtime calling convention (input
471 // and output in EAX):
472 //
473 // EAX <- obj
474 // EAX <- ReadBarrierMark(EAX)
475 // obj <- EAX
476 //
477 // we just use rX (the register holding `obj`) as input and output
478 // of a dedicated entrypoint:
479 //
480 // rX <- ReadBarrierMarkRegX(rX)
481 //
482 int32_t entry_point_offset =
Andreas Gampe542451c2016-07-26 09:02:02 -0700483 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kX86PointerSize>(reg);
Roland Levillaindec8f632016-07-22 17:10:06 +0100484 // This runtime call does not require a stack map.
485 x86_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
Roland Levillain7c1559a2015-12-15 10:55:36 +0000486 __ jmp(GetExitLabel());
487 }
488
489 private:
Roland Levillain7c1559a2015-12-15 10:55:36 +0000490 const Location obj_;
Vladimir Marko953437b2016-08-24 08:30:46 +0000491 const bool unpoison_;
Roland Levillain7c1559a2015-12-15 10:55:36 +0000492
493 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathX86);
494};
495
Roland Levillain0d5a2812015-11-13 10:07:31 +0000496// Slow path generating a read barrier for a heap reference.
497class ReadBarrierForHeapReferenceSlowPathX86 : public SlowPathCode {
498 public:
499 ReadBarrierForHeapReferenceSlowPathX86(HInstruction* instruction,
500 Location out,
501 Location ref,
502 Location obj,
503 uint32_t offset,
504 Location index)
David Srbecky9cd6d372016-02-09 15:24:47 +0000505 : SlowPathCode(instruction),
Roland Levillain0d5a2812015-11-13 10:07:31 +0000506 out_(out),
507 ref_(ref),
508 obj_(obj),
509 offset_(offset),
510 index_(index) {
511 DCHECK(kEmitCompilerReadBarrier);
512 // If `obj` is equal to `out` or `ref`, it means the initial object
513 // has been overwritten by (or after) the heap object reference load
514 // to be instrumented, e.g.:
515 //
516 // __ movl(out, Address(out, offset));
Roland Levillain7c1559a2015-12-15 10:55:36 +0000517 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000518 //
519 // In that case, we have lost the information about the original
520 // object, and the emitted read barrier cannot work properly.
521 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
522 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
523 }
524
525 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
526 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
527 LocationSummary* locations = instruction_->GetLocations();
528 Register reg_out = out_.AsRegister<Register>();
529 DCHECK(locations->CanCall());
530 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
Roland Levillain3d312422016-06-23 13:53:42 +0100531 DCHECK(instruction_->IsInstanceFieldGet() ||
532 instruction_->IsStaticFieldGet() ||
533 instruction_->IsArrayGet() ||
534 instruction_->IsInstanceOf() ||
535 instruction_->IsCheckCast() ||
Roland Levillaindec8f632016-07-22 17:10:06 +0100536 (instruction_->IsInvokeVirtual()) && instruction_->GetLocations()->Intrinsified())
Roland Levillain7c1559a2015-12-15 10:55:36 +0000537 << "Unexpected instruction in read barrier for heap reference slow path: "
538 << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000539
540 __ Bind(GetEntryLabel());
541 SaveLiveRegisters(codegen, locations);
542
543 // We may have to change the index's value, but as `index_` is a
544 // constant member (like other "inputs" of this slow path),
545 // introduce a copy of it, `index`.
546 Location index = index_;
547 if (index_.IsValid()) {
Roland Levillain3d312422016-06-23 13:53:42 +0100548 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
Roland Levillain0d5a2812015-11-13 10:07:31 +0000549 if (instruction_->IsArrayGet()) {
550 // Compute the actual memory offset and store it in `index`.
551 Register index_reg = index_.AsRegister<Register>();
552 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg));
553 if (codegen->IsCoreCalleeSaveRegister(index_reg)) {
554 // We are about to change the value of `index_reg` (see the
555 // calls to art::x86::X86Assembler::shll and
556 // art::x86::X86Assembler::AddImmediate below), but it has
557 // not been saved by the previous call to
558 // art::SlowPathCode::SaveLiveRegisters, as it is a
559 // callee-save register --
560 // art::SlowPathCode::SaveLiveRegisters does not consider
561 // callee-save registers, as it has been designed with the
562 // assumption that callee-save registers are supposed to be
563 // handled by the called function. So, as a callee-save
564 // register, `index_reg` _would_ eventually be saved onto
565 // the stack, but it would be too late: we would have
566 // changed its value earlier. Therefore, we manually save
567 // it here into another freely available register,
568 // `free_reg`, chosen of course among the caller-save
569 // registers (as a callee-save `free_reg` register would
570 // exhibit the same problem).
571 //
572 // Note we could have requested a temporary register from
573 // the register allocator instead; but we prefer not to, as
574 // this is a slow path, and we know we can find a
575 // caller-save register that is available.
576 Register free_reg = FindAvailableCallerSaveRegister(codegen);
577 __ movl(free_reg, index_reg);
578 index_reg = free_reg;
579 index = Location::RegisterLocation(index_reg);
580 } else {
581 // The initial register stored in `index_` has already been
582 // saved in the call to art::SlowPathCode::SaveLiveRegisters
583 // (as it is not a callee-save register), so we can freely
584 // use it.
585 }
586 // Shifting the index value contained in `index_reg` by the scale
587 // factor (2) cannot overflow in practice, as the runtime is
588 // unable to allocate object arrays with a size larger than
589 // 2^26 - 1 (that is, 2^28 - 4 bytes).
590 __ shll(index_reg, Immediate(TIMES_4));
591 static_assert(
592 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
593 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
594 __ AddImmediate(index_reg, Immediate(offset_));
595 } else {
Roland Levillain3d312422016-06-23 13:53:42 +0100596 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
597 // intrinsics, `index_` is not shifted by a scale factor of 2
598 // (as in the case of ArrayGet), as it is actually an offset
599 // to an object field within an object.
600 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000601 DCHECK(instruction_->GetLocations()->Intrinsified());
602 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
603 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
604 << instruction_->AsInvoke()->GetIntrinsic();
605 DCHECK_EQ(offset_, 0U);
606 DCHECK(index_.IsRegisterPair());
607 // UnsafeGet's offset location is a register pair, the low
608 // part contains the correct offset.
609 index = index_.ToLow();
610 }
611 }
612
613 // We're moving two or three locations to locations that could
614 // overlap, so we need a parallel move resolver.
615 InvokeRuntimeCallingConvention calling_convention;
616 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
617 parallel_move.AddMove(ref_,
618 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
619 Primitive::kPrimNot,
620 nullptr);
621 parallel_move.AddMove(obj_,
622 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
623 Primitive::kPrimNot,
624 nullptr);
625 if (index.IsValid()) {
626 parallel_move.AddMove(index,
627 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
628 Primitive::kPrimInt,
629 nullptr);
630 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
631 } else {
632 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
633 __ movl(calling_convention.GetRegisterAt(2), Immediate(offset_));
634 }
Serban Constantinescuba45db02016-07-12 22:53:02 +0100635 x86_codegen->InvokeRuntime(kQuickReadBarrierSlow, instruction_, instruction_->GetDexPc(), this);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000636 CheckEntrypointTypes<
637 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
638 x86_codegen->Move32(out_, Location::RegisterLocation(EAX));
639
640 RestoreLiveRegisters(codegen, locations);
641 __ jmp(GetExitLabel());
642 }
643
644 const char* GetDescription() const OVERRIDE { return "ReadBarrierForHeapReferenceSlowPathX86"; }
645
646 private:
647 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
648 size_t ref = static_cast<int>(ref_.AsRegister<Register>());
649 size_t obj = static_cast<int>(obj_.AsRegister<Register>());
650 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
651 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
652 return static_cast<Register>(i);
653 }
654 }
655 // We shall never fail to find a free caller-save register, as
656 // there are more than two core caller-save registers on x86
657 // (meaning it is possible to find one which is different from
658 // `ref` and `obj`).
659 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
660 LOG(FATAL) << "Could not find a free caller-save register";
661 UNREACHABLE();
662 }
663
Roland Levillain0d5a2812015-11-13 10:07:31 +0000664 const Location out_;
665 const Location ref_;
666 const Location obj_;
667 const uint32_t offset_;
668 // An additional location containing an index to an array.
669 // Only used for HArrayGet and the UnsafeGetObject &
670 // UnsafeGetObjectVolatile intrinsics.
671 const Location index_;
672
673 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathX86);
674};
675
676// Slow path generating a read barrier for a GC root.
677class ReadBarrierForRootSlowPathX86 : public SlowPathCode {
678 public:
679 ReadBarrierForRootSlowPathX86(HInstruction* instruction, Location out, Location root)
David Srbecky9cd6d372016-02-09 15:24:47 +0000680 : SlowPathCode(instruction), out_(out), root_(root) {
Roland Levillain7c1559a2015-12-15 10:55:36 +0000681 DCHECK(kEmitCompilerReadBarrier);
682 }
Roland Levillain0d5a2812015-11-13 10:07:31 +0000683
684 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
685 LocationSummary* locations = instruction_->GetLocations();
686 Register reg_out = out_.AsRegister<Register>();
687 DCHECK(locations->CanCall());
688 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
Roland Levillain7c1559a2015-12-15 10:55:36 +0000689 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
690 << "Unexpected instruction in read barrier for GC root slow path: "
691 << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000692
693 __ Bind(GetEntryLabel());
694 SaveLiveRegisters(codegen, locations);
695
696 InvokeRuntimeCallingConvention calling_convention;
697 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
698 x86_codegen->Move32(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), root_);
Serban Constantinescuba45db02016-07-12 22:53:02 +0100699 x86_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000700 instruction_,
701 instruction_->GetDexPc(),
702 this);
703 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
704 x86_codegen->Move32(out_, Location::RegisterLocation(EAX));
705
706 RestoreLiveRegisters(codegen, locations);
707 __ jmp(GetExitLabel());
708 }
709
710 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathX86"; }
711
712 private:
Roland Levillain0d5a2812015-11-13 10:07:31 +0000713 const Location out_;
714 const Location root_;
715
716 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathX86);
717};
718
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100719#undef __
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100720// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
721#define __ down_cast<X86Assembler*>(GetAssembler())-> // NOLINT
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100722
Aart Bike9f37602015-10-09 11:15:55 -0700723inline Condition X86Condition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -0700724 switch (cond) {
725 case kCondEQ: return kEqual;
726 case kCondNE: return kNotEqual;
727 case kCondLT: return kLess;
728 case kCondLE: return kLessEqual;
729 case kCondGT: return kGreater;
730 case kCondGE: return kGreaterEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700731 case kCondB: return kBelow;
732 case kCondBE: return kBelowEqual;
733 case kCondA: return kAbove;
734 case kCondAE: return kAboveEqual;
Dave Allison20dfc792014-06-16 20:44:29 -0700735 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100736 LOG(FATAL) << "Unreachable";
737 UNREACHABLE();
738}
739
Aart Bike9f37602015-10-09 11:15:55 -0700740// Maps signed condition to unsigned condition and FP condition to x86 name.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100741inline Condition X86UnsignedOrFPCondition(IfCondition cond) {
742 switch (cond) {
743 case kCondEQ: return kEqual;
744 case kCondNE: return kNotEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700745 // Signed to unsigned, and FP to x86 name.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100746 case kCondLT: return kBelow;
747 case kCondLE: return kBelowEqual;
748 case kCondGT: return kAbove;
749 case kCondGE: return kAboveEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700750 // Unsigned remain unchanged.
751 case kCondB: return kBelow;
752 case kCondBE: return kBelowEqual;
753 case kCondA: return kAbove;
754 case kCondAE: return kAboveEqual;
Roland Levillain4fa13f62015-07-06 18:11:54 +0100755 }
756 LOG(FATAL) << "Unreachable";
757 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -0700758}
759
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100760void CodeGeneratorX86::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100761 stream << Register(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100762}
763
764void CodeGeneratorX86::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100765 stream << XmmRegister(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100766}
767
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100768size_t CodeGeneratorX86::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
769 __ movl(Address(ESP, stack_index), static_cast<Register>(reg_id));
770 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100771}
772
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100773size_t CodeGeneratorX86::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
774 __ movl(static_cast<Register>(reg_id), Address(ESP, stack_index));
775 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100776}
777
Mark Mendell7c8d0092015-01-26 11:21:33 -0500778size_t CodeGeneratorX86::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
779 __ movsd(Address(ESP, stack_index), XmmRegister(reg_id));
780 return GetFloatingPointSpillSlotSize();
781}
782
783size_t CodeGeneratorX86::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
784 __ movsd(XmmRegister(reg_id), Address(ESP, stack_index));
785 return GetFloatingPointSpillSlotSize();
786}
787
Calin Juravle175dc732015-08-25 15:42:32 +0100788void CodeGeneratorX86::InvokeRuntime(QuickEntrypointEnum entrypoint,
789 HInstruction* instruction,
790 uint32_t dex_pc,
791 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +0100792 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Serban Constantinescuba45db02016-07-12 22:53:02 +0100793 GenerateInvokeRuntime(GetThreadOffset<kX86PointerSize>(entrypoint).Int32Value());
794 if (EntrypointRequiresStackMap(entrypoint)) {
795 RecordPcInfo(instruction, dex_pc, slow_path);
796 }
Alexandre Rames8158f282015-08-07 10:26:17 +0100797}
798
Roland Levillaindec8f632016-07-22 17:10:06 +0100799void CodeGeneratorX86::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
800 HInstruction* instruction,
801 SlowPathCode* slow_path) {
802 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
Serban Constantinescuba45db02016-07-12 22:53:02 +0100803 GenerateInvokeRuntime(entry_point_offset);
804}
805
806void CodeGeneratorX86::GenerateInvokeRuntime(int32_t entry_point_offset) {
Roland Levillaindec8f632016-07-22 17:10:06 +0100807 __ fs()->call(Address::Absolute(entry_point_offset));
808}
809
Mark Mendellfb8d2792015-03-31 22:16:59 -0400810CodeGeneratorX86::CodeGeneratorX86(HGraph* graph,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000811 const X86InstructionSetFeatures& isa_features,
812 const CompilerOptions& compiler_options,
813 OptimizingCompilerStats* stats)
Mark Mendell5f874182015-03-04 15:42:45 -0500814 : CodeGenerator(graph,
815 kNumberOfCpuRegisters,
816 kNumberOfXmmRegisters,
817 kNumberOfRegisterPairs,
818 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
819 arraysize(kCoreCalleeSaves))
820 | (1 << kFakeReturnRegister),
Serban Constantinescuecc43662015-08-13 13:33:12 +0100821 0,
822 compiler_options,
823 stats),
Vladimir Marko225b6462015-09-28 12:17:40 +0100824 block_labels_(nullptr),
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100825 location_builder_(graph, this),
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100826 instruction_visitor_(graph, this),
Mark Mendellfb8d2792015-03-31 22:16:59 -0400827 move_resolver_(graph->GetArena(), this),
Vladimir Marko93205e32016-04-13 11:59:46 +0100828 assembler_(graph->GetArena()),
Vladimir Marko58155012015-08-19 12:49:41 +0000829 isa_features_(isa_features),
Vladimir Marko5233f932015-09-29 19:01:15 +0100830 method_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Mark Mendell805b3b52015-09-18 14:10:29 -0400831 relative_call_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000832 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000833 simple_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
834 string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100835 type_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko93205e32016-04-13 11:59:46 +0100836 constant_area_start_(-1),
837 fixups_to_jump_tables_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
838 method_address_offset_(-1) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000839 // Use a fake return address register to mimic Quick.
840 AddAllocatedRegister(Location::RegisterLocation(kFakeReturnRegister));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100841}
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100842
David Brazdil58282f42016-01-14 12:45:10 +0000843void CodeGeneratorX86::SetupBlockedRegisters() const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100844 // Don't allocate the dalvik style register pair passing.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100845 blocked_register_pairs_[ECX_EDX] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100846
847 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100848 blocked_core_registers_[ESP] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100849
Calin Juravle34bacdf2014-10-07 20:23:36 +0100850 UpdateBlockedPairRegisters();
851}
852
853void CodeGeneratorX86::UpdateBlockedPairRegisters() const {
854 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
855 X86ManagedRegister current =
856 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
857 if (blocked_core_registers_[current.AsRegisterPairLow()]
858 || blocked_core_registers_[current.AsRegisterPairHigh()]) {
859 blocked_register_pairs_[i] = true;
860 }
861 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100862}
863
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100864InstructionCodeGeneratorX86::InstructionCodeGeneratorX86(HGraph* graph, CodeGeneratorX86* codegen)
Aart Bik42249c32016-01-07 15:33:50 -0800865 : InstructionCodeGenerator(graph, codegen),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100866 assembler_(codegen->GetAssembler()),
867 codegen_(codegen) {}
868
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100869static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +0100870 return dwarf::Reg::X86Core(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100871}
872
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000873void CodeGeneratorX86::GenerateFrameEntry() {
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100874 __ cfi().SetCurrentCFAOffset(kX86WordSize); // return address
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000875 __ Bind(&frame_entry_label_);
Roland Levillain199f3362014-11-27 17:15:16 +0000876 bool skip_overflow_check =
877 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000878 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Calin Juravle93edf732015-01-20 20:14:07 +0000879
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000880 if (!skip_overflow_check) {
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100881 __ testl(EAX, Address(ESP, -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86))));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100882 RecordPcInfo(nullptr, 0);
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100883 }
884
Mark Mendell5f874182015-03-04 15:42:45 -0500885 if (HasEmptyFrame()) {
886 return;
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000887 }
Mark Mendell5f874182015-03-04 15:42:45 -0500888
889 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
890 Register reg = kCoreCalleeSaves[i];
891 if (allocated_registers_.ContainsCoreRegister(reg)) {
892 __ pushl(reg);
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100893 __ cfi().AdjustCFAOffset(kX86WordSize);
894 __ cfi().RelOffset(DWARFReg(reg), 0);
Mark Mendell5f874182015-03-04 15:42:45 -0500895 }
896 }
897
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100898 int adjust = GetFrameSize() - FrameEntrySpillSize();
899 __ subl(ESP, Immediate(adjust));
900 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100901 __ movl(Address(ESP, kCurrentMethodStackOffset), kMethodRegisterArgument);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000902}
903
904void CodeGeneratorX86::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +0100905 __ cfi().RememberState();
906 if (!HasEmptyFrame()) {
907 int adjust = GetFrameSize() - FrameEntrySpillSize();
908 __ addl(ESP, Immediate(adjust));
909 __ cfi().AdjustCFAOffset(-adjust);
Mark Mendell5f874182015-03-04 15:42:45 -0500910
David Srbeckyc34dc932015-04-12 09:27:43 +0100911 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
912 Register reg = kCoreCalleeSaves[i];
913 if (allocated_registers_.ContainsCoreRegister(reg)) {
914 __ popl(reg);
915 __ cfi().AdjustCFAOffset(-static_cast<int>(kX86WordSize));
916 __ cfi().Restore(DWARFReg(reg));
917 }
Mark Mendell5f874182015-03-04 15:42:45 -0500918 }
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000919 }
David Srbeckyc34dc932015-04-12 09:27:43 +0100920 __ ret();
921 __ cfi().RestoreState();
922 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000923}
924
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100925void CodeGeneratorX86::Bind(HBasicBlock* block) {
926 __ Bind(GetLabelOf(block));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000927}
928
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100929Location InvokeDexCallingConventionVisitorX86::GetReturnLocation(Primitive::Type type) const {
930 switch (type) {
931 case Primitive::kPrimBoolean:
932 case Primitive::kPrimByte:
933 case Primitive::kPrimChar:
934 case Primitive::kPrimShort:
935 case Primitive::kPrimInt:
936 case Primitive::kPrimNot:
937 return Location::RegisterLocation(EAX);
938
939 case Primitive::kPrimLong:
940 return Location::RegisterPairLocation(EAX, EDX);
941
942 case Primitive::kPrimVoid:
943 return Location::NoLocation();
944
945 case Primitive::kPrimDouble:
946 case Primitive::kPrimFloat:
947 return Location::FpuRegisterLocation(XMM0);
948 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +0100949
950 UNREACHABLE();
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100951}
952
953Location InvokeDexCallingConventionVisitorX86::GetMethodLocation() const {
954 return Location::RegisterLocation(kMethodRegisterArgument);
955}
956
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100957Location InvokeDexCallingConventionVisitorX86::GetNextLocation(Primitive::Type type) {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100958 switch (type) {
959 case Primitive::kPrimBoolean:
960 case Primitive::kPrimByte:
961 case Primitive::kPrimChar:
962 case Primitive::kPrimShort:
963 case Primitive::kPrimInt:
964 case Primitive::kPrimNot: {
965 uint32_t index = gp_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000966 stack_index_++;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100967 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100968 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100969 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000970 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100971 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100972 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100973
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000974 case Primitive::kPrimLong: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100975 uint32_t index = gp_index_;
976 gp_index_ += 2;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000977 stack_index_ += 2;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100978 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100979 X86ManagedRegister pair = X86ManagedRegister::FromRegisterPair(
980 calling_convention.GetRegisterPairAt(index));
981 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100982 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000983 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
984 }
985 }
986
987 case Primitive::kPrimFloat: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100988 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000989 stack_index_++;
990 if (index < calling_convention.GetNumberOfFpuRegisters()) {
991 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
992 } else {
993 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
994 }
995 }
996
997 case Primitive::kPrimDouble: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100998 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000999 stack_index_ += 2;
1000 if (index < calling_convention.GetNumberOfFpuRegisters()) {
1001 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
1002 } else {
1003 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001004 }
1005 }
1006
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001007 case Primitive::kPrimVoid:
1008 LOG(FATAL) << "Unexpected parameter type " << type;
1009 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001010 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00001011 return Location::NoLocation();
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001012}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001013
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001014void CodeGeneratorX86::Move32(Location destination, Location source) {
1015 if (source.Equals(destination)) {
1016 return;
1017 }
1018 if (destination.IsRegister()) {
1019 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001020 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001021 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001022 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001023 } else {
1024 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001025 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001026 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001027 } else if (destination.IsFpuRegister()) {
1028 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001029 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001030 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001031 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001032 } else {
1033 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001034 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001035 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001036 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001037 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001038 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001039 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001040 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001041 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Mark Mendell7c8d0092015-01-26 11:21:33 -05001042 } else if (source.IsConstant()) {
1043 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001044 int32_t value = GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05001045 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001046 } else {
1047 DCHECK(source.IsStackSlot());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001048 __ pushl(Address(ESP, source.GetStackIndex()));
1049 __ popl(Address(ESP, destination.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001050 }
1051 }
1052}
1053
1054void CodeGeneratorX86::Move64(Location destination, Location source) {
1055 if (source.Equals(destination)) {
1056 return;
1057 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001058 if (destination.IsRegisterPair()) {
1059 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001060 EmitParallelMoves(
1061 Location::RegisterLocation(source.AsRegisterPairHigh<Register>()),
1062 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001063 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001064 Location::RegisterLocation(source.AsRegisterPairLow<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001065 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()),
1066 Primitive::kPrimInt);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001067 } else if (source.IsFpuRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001068 XmmRegister src_reg = source.AsFpuRegister<XmmRegister>();
1069 __ movd(destination.AsRegisterPairLow<Register>(), src_reg);
1070 __ psrlq(src_reg, Immediate(32));
1071 __ movd(destination.AsRegisterPairHigh<Register>(), src_reg);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001072 } else {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001073 // No conflict possible, so just do the moves.
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001074 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001075 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
1076 __ movl(destination.AsRegisterPairHigh<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001077 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
1078 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001079 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05001080 if (source.IsFpuRegister()) {
1081 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
1082 } else if (source.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001083 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01001084 } else if (source.IsRegisterPair()) {
1085 size_t elem_size = Primitive::ComponentSize(Primitive::kPrimInt);
1086 // Create stack space for 2 elements.
1087 __ subl(ESP, Immediate(2 * elem_size));
1088 __ movl(Address(ESP, 0), source.AsRegisterPairLow<Register>());
1089 __ movl(Address(ESP, elem_size), source.AsRegisterPairHigh<Register>());
1090 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
1091 // And remove the temporary stack space we allocated.
1092 __ addl(ESP, Immediate(2 * elem_size));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001093 } else {
1094 LOG(FATAL) << "Unimplemented";
1095 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001096 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001097 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001098 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001099 // No conflict possible, so just do the moves.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001100 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegisterPairLow<Register>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001101 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001102 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001103 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001104 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001105 } else if (source.IsConstant()) {
1106 HConstant* constant = source.GetConstant();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001107 DCHECK(constant->IsLongConstant() || constant->IsDoubleConstant());
1108 int64_t value = GetInt64ValueOf(constant);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001109 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(Low32Bits(value)));
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001110 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
1111 Immediate(High32Bits(value)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001112 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001113 DCHECK(source.IsDoubleStackSlot()) << source;
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001114 EmitParallelMoves(
1115 Location::StackSlot(source.GetStackIndex()),
1116 Location::StackSlot(destination.GetStackIndex()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001117 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001118 Location::StackSlot(source.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001119 Location::StackSlot(destination.GetHighStackIndex(kX86WordSize)),
1120 Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001121 }
1122 }
1123}
1124
Calin Juravle175dc732015-08-25 15:42:32 +01001125void CodeGeneratorX86::MoveConstant(Location location, int32_t value) {
1126 DCHECK(location.IsRegister());
1127 __ movl(location.AsRegister<Register>(), Immediate(value));
1128}
1129
Calin Juravlee460d1d2015-09-29 04:52:17 +01001130void CodeGeneratorX86::MoveLocation(Location dst, Location src, Primitive::Type dst_type) {
David Brazdil74eb1b22015-12-14 11:44:01 +00001131 HParallelMove move(GetGraph()->GetArena());
1132 if (dst_type == Primitive::kPrimLong && !src.IsConstant() && !src.IsFpuRegister()) {
1133 move.AddMove(src.ToLow(), dst.ToLow(), Primitive::kPrimInt, nullptr);
1134 move.AddMove(src.ToHigh(), dst.ToHigh(), Primitive::kPrimInt, nullptr);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001135 } else {
David Brazdil74eb1b22015-12-14 11:44:01 +00001136 move.AddMove(src, dst, dst_type, nullptr);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001137 }
David Brazdil74eb1b22015-12-14 11:44:01 +00001138 GetMoveResolver()->EmitNativeCode(&move);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001139}
1140
1141void CodeGeneratorX86::AddLocationAsTemp(Location location, LocationSummary* locations) {
1142 if (location.IsRegister()) {
1143 locations->AddTemp(location);
1144 } else if (location.IsRegisterPair()) {
1145 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
1146 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
1147 } else {
1148 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1149 }
1150}
1151
David Brazdilfc6a86a2015-06-26 10:33:45 +00001152void InstructionCodeGeneratorX86::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001153 DCHECK(!successor->IsExitBlock());
1154
1155 HBasicBlock* block = got->GetBlock();
1156 HInstruction* previous = got->GetPrevious();
1157
1158 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +00001159 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001160 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1161 return;
1162 }
1163
1164 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1165 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1166 }
1167 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001168 __ jmp(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001169 }
1170}
1171
David Brazdilfc6a86a2015-06-26 10:33:45 +00001172void LocationsBuilderX86::VisitGoto(HGoto* got) {
1173 got->SetLocations(nullptr);
1174}
1175
1176void InstructionCodeGeneratorX86::VisitGoto(HGoto* got) {
1177 HandleGoto(got, got->GetSuccessor());
1178}
1179
1180void LocationsBuilderX86::VisitTryBoundary(HTryBoundary* try_boundary) {
1181 try_boundary->SetLocations(nullptr);
1182}
1183
1184void InstructionCodeGeneratorX86::VisitTryBoundary(HTryBoundary* try_boundary) {
1185 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
1186 if (!successor->IsExitBlock()) {
1187 HandleGoto(try_boundary, successor);
1188 }
1189}
1190
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001191void LocationsBuilderX86::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001192 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001193}
1194
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001195void InstructionCodeGeneratorX86::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001196}
1197
Mark Mendell152408f2015-12-31 12:28:50 -05001198template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001199void InstructionCodeGeneratorX86::GenerateFPJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001200 LabelType* true_label,
1201 LabelType* false_label) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001202 if (cond->IsFPConditionTrueIfNaN()) {
1203 __ j(kUnordered, true_label);
1204 } else if (cond->IsFPConditionFalseIfNaN()) {
1205 __ j(kUnordered, false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001206 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001207 __ j(X86UnsignedOrFPCondition(cond->GetCondition()), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001208}
1209
Mark Mendell152408f2015-12-31 12:28:50 -05001210template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001211void InstructionCodeGeneratorX86::GenerateLongComparesAndJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001212 LabelType* true_label,
1213 LabelType* false_label) {
Mark Mendellc4701932015-04-10 13:18:51 -04001214 LocationSummary* locations = cond->GetLocations();
1215 Location left = locations->InAt(0);
1216 Location right = locations->InAt(1);
1217 IfCondition if_cond = cond->GetCondition();
1218
Mark Mendellc4701932015-04-10 13:18:51 -04001219 Register left_high = left.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001220 Register left_low = left.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001221 IfCondition true_high_cond = if_cond;
1222 IfCondition false_high_cond = cond->GetOppositeCondition();
Aart Bike9f37602015-10-09 11:15:55 -07001223 Condition final_condition = X86UnsignedOrFPCondition(if_cond); // unsigned on lower part
Mark Mendellc4701932015-04-10 13:18:51 -04001224
1225 // Set the conditions for the test, remembering that == needs to be
1226 // decided using the low words.
1227 switch (if_cond) {
1228 case kCondEQ:
Mark Mendellc4701932015-04-10 13:18:51 -04001229 case kCondNE:
Roland Levillain4fa13f62015-07-06 18:11:54 +01001230 // Nothing to do.
Mark Mendellc4701932015-04-10 13:18:51 -04001231 break;
1232 case kCondLT:
1233 false_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -04001234 break;
1235 case kCondLE:
1236 true_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -04001237 break;
1238 case kCondGT:
1239 false_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -04001240 break;
1241 case kCondGE:
1242 true_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -04001243 break;
Aart Bike9f37602015-10-09 11:15:55 -07001244 case kCondB:
1245 false_high_cond = kCondA;
1246 break;
1247 case kCondBE:
1248 true_high_cond = kCondB;
1249 break;
1250 case kCondA:
1251 false_high_cond = kCondB;
1252 break;
1253 case kCondAE:
1254 true_high_cond = kCondA;
1255 break;
Mark Mendellc4701932015-04-10 13:18:51 -04001256 }
1257
1258 if (right.IsConstant()) {
1259 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
Mark Mendellc4701932015-04-10 13:18:51 -04001260 int32_t val_high = High32Bits(value);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001261 int32_t val_low = Low32Bits(value);
Mark Mendellc4701932015-04-10 13:18:51 -04001262
Aart Bika19616e2016-02-01 18:57:58 -08001263 codegen_->Compare32BitValue(left_high, val_high);
Mark Mendellc4701932015-04-10 13:18:51 -04001264 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001265 __ j(X86Condition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001266 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001267 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001268 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001269 __ j(X86Condition(true_high_cond), true_label);
1270 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001271 }
1272 // Must be equal high, so compare the lows.
Aart Bika19616e2016-02-01 18:57:58 -08001273 codegen_->Compare32BitValue(left_low, val_low);
Mark Mendell8659e842016-02-16 10:41:46 -05001274 } else if (right.IsRegisterPair()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001275 Register right_high = right.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001276 Register right_low = right.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001277
1278 __ cmpl(left_high, right_high);
1279 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001280 __ j(X86Condition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001281 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001282 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001283 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001284 __ j(X86Condition(true_high_cond), true_label);
1285 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001286 }
1287 // Must be equal high, so compare the lows.
1288 __ cmpl(left_low, right_low);
Mark Mendell8659e842016-02-16 10:41:46 -05001289 } else {
1290 DCHECK(right.IsDoubleStackSlot());
1291 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
1292 if (if_cond == kCondNE) {
1293 __ j(X86Condition(true_high_cond), true_label);
1294 } else if (if_cond == kCondEQ) {
1295 __ j(X86Condition(false_high_cond), false_label);
1296 } else {
1297 __ j(X86Condition(true_high_cond), true_label);
1298 __ j(X86Condition(false_high_cond), false_label);
1299 }
1300 // Must be equal high, so compare the lows.
1301 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Mark Mendellc4701932015-04-10 13:18:51 -04001302 }
1303 // The last comparison might be unsigned.
1304 __ j(final_condition, true_label);
1305}
1306
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001307void InstructionCodeGeneratorX86::GenerateFPCompare(Location lhs,
1308 Location rhs,
1309 HInstruction* insn,
1310 bool is_double) {
1311 HX86LoadFromConstantTable* const_area = insn->InputAt(1)->AsX86LoadFromConstantTable();
1312 if (is_double) {
1313 if (rhs.IsFpuRegister()) {
1314 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1315 } else if (const_area != nullptr) {
1316 DCHECK(const_area->IsEmittedAtUseSite());
1317 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(),
1318 codegen_->LiteralDoubleAddress(
1319 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
1320 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
1321 } else {
1322 DCHECK(rhs.IsDoubleStackSlot());
1323 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(), Address(ESP, rhs.GetStackIndex()));
1324 }
1325 } else {
1326 if (rhs.IsFpuRegister()) {
1327 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1328 } else if (const_area != nullptr) {
1329 DCHECK(const_area->IsEmittedAtUseSite());
1330 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(),
1331 codegen_->LiteralFloatAddress(
1332 const_area->GetConstant()->AsFloatConstant()->GetValue(),
1333 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
1334 } else {
1335 DCHECK(rhs.IsStackSlot());
1336 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(), Address(ESP, rhs.GetStackIndex()));
1337 }
1338 }
1339}
1340
Mark Mendell152408f2015-12-31 12:28:50 -05001341template<class LabelType>
David Brazdil0debae72015-11-12 18:37:00 +00001342void InstructionCodeGeneratorX86::GenerateCompareTestAndBranch(HCondition* condition,
Mark Mendell152408f2015-12-31 12:28:50 -05001343 LabelType* true_target_in,
1344 LabelType* false_target_in) {
David Brazdil0debae72015-11-12 18:37:00 +00001345 // Generated branching requires both targets to be explicit. If either of the
1346 // targets is nullptr (fallthrough) use and bind `fallthrough_target` instead.
Mark Mendell152408f2015-12-31 12:28:50 -05001347 LabelType fallthrough_target;
1348 LabelType* true_target = true_target_in == nullptr ? &fallthrough_target : true_target_in;
1349 LabelType* false_target = false_target_in == nullptr ? &fallthrough_target : false_target_in;
David Brazdil0debae72015-11-12 18:37:00 +00001350
Mark Mendellc4701932015-04-10 13:18:51 -04001351 LocationSummary* locations = condition->GetLocations();
1352 Location left = locations->InAt(0);
1353 Location right = locations->InAt(1);
1354
Mark Mendellc4701932015-04-10 13:18:51 -04001355 Primitive::Type type = condition->InputAt(0)->GetType();
1356 switch (type) {
1357 case Primitive::kPrimLong:
1358 GenerateLongComparesAndJumps(condition, true_target, false_target);
1359 break;
1360 case Primitive::kPrimFloat:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001361 GenerateFPCompare(left, right, condition, false);
Mark Mendellc4701932015-04-10 13:18:51 -04001362 GenerateFPJumps(condition, true_target, false_target);
1363 break;
1364 case Primitive::kPrimDouble:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001365 GenerateFPCompare(left, right, condition, true);
Mark Mendellc4701932015-04-10 13:18:51 -04001366 GenerateFPJumps(condition, true_target, false_target);
1367 break;
1368 default:
1369 LOG(FATAL) << "Unexpected compare type " << type;
1370 }
1371
David Brazdil0debae72015-11-12 18:37:00 +00001372 if (false_target != &fallthrough_target) {
Mark Mendellc4701932015-04-10 13:18:51 -04001373 __ jmp(false_target);
1374 }
David Brazdil0debae72015-11-12 18:37:00 +00001375
1376 if (fallthrough_target.IsLinked()) {
1377 __ Bind(&fallthrough_target);
1378 }
Mark Mendellc4701932015-04-10 13:18:51 -04001379}
1380
David Brazdil0debae72015-11-12 18:37:00 +00001381static bool AreEflagsSetFrom(HInstruction* cond, HInstruction* branch) {
1382 // Moves may affect the eflags register (move zero uses xorl), so the EFLAGS
1383 // are set only strictly before `branch`. We can't use the eflags on long/FP
1384 // conditions if they are materialized due to the complex branching.
1385 return cond->IsCondition() &&
1386 cond->GetNext() == branch &&
1387 cond->InputAt(0)->GetType() != Primitive::kPrimLong &&
1388 !Primitive::IsFloatingPointType(cond->InputAt(0)->GetType());
1389}
1390
Mark Mendell152408f2015-12-31 12:28:50 -05001391template<class LabelType>
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001392void InstructionCodeGeneratorX86::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00001393 size_t condition_input_index,
Mark Mendell152408f2015-12-31 12:28:50 -05001394 LabelType* true_target,
1395 LabelType* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00001396 HInstruction* cond = instruction->InputAt(condition_input_index);
1397
1398 if (true_target == nullptr && false_target == nullptr) {
1399 // Nothing to do. The code always falls through.
1400 return;
1401 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00001402 // Constant condition, statically compared against "true" (integer value 1).
1403 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00001404 if (true_target != nullptr) {
1405 __ jmp(true_target);
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001406 }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001407 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00001408 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00001409 if (false_target != nullptr) {
1410 __ jmp(false_target);
1411 }
1412 }
1413 return;
1414 }
1415
1416 // The following code generates these patterns:
1417 // (1) true_target == nullptr && false_target != nullptr
1418 // - opposite condition true => branch to false_target
1419 // (2) true_target != nullptr && false_target == nullptr
1420 // - condition true => branch to true_target
1421 // (3) true_target != nullptr && false_target != nullptr
1422 // - condition true => branch to true_target
1423 // - branch to false_target
1424 if (IsBooleanValueOrMaterializedCondition(cond)) {
1425 if (AreEflagsSetFrom(cond, instruction)) {
1426 if (true_target == nullptr) {
1427 __ j(X86Condition(cond->AsCondition()->GetOppositeCondition()), false_target);
1428 } else {
1429 __ j(X86Condition(cond->AsCondition()->GetCondition()), true_target);
1430 }
1431 } else {
1432 // Materialized condition, compare against 0.
1433 Location lhs = instruction->GetLocations()->InAt(condition_input_index);
1434 if (lhs.IsRegister()) {
1435 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
1436 } else {
1437 __ cmpl(Address(ESP, lhs.GetStackIndex()), Immediate(0));
1438 }
1439 if (true_target == nullptr) {
1440 __ j(kEqual, false_target);
1441 } else {
1442 __ j(kNotEqual, true_target);
1443 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001444 }
1445 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001446 // Condition has not been materialized, use its inputs as the comparison and
1447 // its condition as the branch condition.
Mark Mendellb8b97692015-05-22 16:58:19 -04001448 HCondition* condition = cond->AsCondition();
David Brazdil0debae72015-11-12 18:37:00 +00001449
1450 // If this is a long or FP comparison that has been folded into
1451 // the HCondition, generate the comparison directly.
1452 Primitive::Type type = condition->InputAt(0)->GetType();
1453 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
1454 GenerateCompareTestAndBranch(condition, true_target, false_target);
1455 return;
1456 }
1457
1458 Location lhs = condition->GetLocations()->InAt(0);
1459 Location rhs = condition->GetLocations()->InAt(1);
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001460 // LHS is guaranteed to be in a register (see LocationsBuilderX86::HandleCondition).
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001461 codegen_->GenerateIntCompare(lhs, rhs);
David Brazdil0debae72015-11-12 18:37:00 +00001462 if (true_target == nullptr) {
1463 __ j(X86Condition(condition->GetOppositeCondition()), false_target);
1464 } else {
Mark Mendellb8b97692015-05-22 16:58:19 -04001465 __ j(X86Condition(condition->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -07001466 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001467 }
David Brazdil0debae72015-11-12 18:37:00 +00001468
1469 // If neither branch falls through (case 3), the conditional branch to `true_target`
1470 // was already emitted (case 2) and we need to emit a jump to `false_target`.
1471 if (true_target != nullptr && false_target != nullptr) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001472 __ jmp(false_target);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001473 }
1474}
1475
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001476void LocationsBuilderX86::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001477 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
1478 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001479 locations->SetInAt(0, Location::Any());
1480 }
1481}
1482
1483void InstructionCodeGeneratorX86::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001484 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
1485 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
1486 Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
1487 nullptr : codegen_->GetLabelOf(true_successor);
1488 Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
1489 nullptr : codegen_->GetLabelOf(false_successor);
1490 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001491}
1492
1493void LocationsBuilderX86::VisitDeoptimize(HDeoptimize* deoptimize) {
1494 LocationSummary* locations = new (GetGraph()->GetArena())
1495 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01001496 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
David Brazdil0debae72015-11-12 18:37:00 +00001497 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001498 locations->SetInAt(0, Location::Any());
1499 }
1500}
1501
1502void InstructionCodeGeneratorX86::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08001503 SlowPathCode* slow_path = deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathX86>(deoptimize);
David Brazdil74eb1b22015-12-14 11:44:01 +00001504 GenerateTestAndBranch<Label>(deoptimize,
1505 /* condition_input_index */ 0,
1506 slow_path->GetEntryLabel(),
1507 /* false_target */ nullptr);
1508}
1509
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001510static bool SelectCanUseCMOV(HSelect* select) {
1511 // There are no conditional move instructions for XMMs.
1512 if (Primitive::IsFloatingPointType(select->GetType())) {
1513 return false;
1514 }
1515
1516 // A FP condition doesn't generate the single CC that we need.
1517 // In 32 bit mode, a long condition doesn't generate a single CC either.
1518 HInstruction* condition = select->GetCondition();
1519 if (condition->IsCondition()) {
1520 Primitive::Type compare_type = condition->InputAt(0)->GetType();
1521 if (compare_type == Primitive::kPrimLong ||
1522 Primitive::IsFloatingPointType(compare_type)) {
1523 return false;
1524 }
1525 }
1526
1527 // We can generate a CMOV for this Select.
1528 return true;
1529}
1530
David Brazdil74eb1b22015-12-14 11:44:01 +00001531void LocationsBuilderX86::VisitSelect(HSelect* select) {
1532 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001533 if (Primitive::IsFloatingPointType(select->GetType())) {
David Brazdil74eb1b22015-12-14 11:44:01 +00001534 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001535 locations->SetInAt(1, Location::Any());
David Brazdil74eb1b22015-12-14 11:44:01 +00001536 } else {
1537 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001538 if (SelectCanUseCMOV(select)) {
1539 if (select->InputAt(1)->IsConstant()) {
1540 // Cmov can't handle a constant value.
1541 locations->SetInAt(1, Location::RequiresRegister());
1542 } else {
1543 locations->SetInAt(1, Location::Any());
1544 }
1545 } else {
1546 locations->SetInAt(1, Location::Any());
1547 }
David Brazdil74eb1b22015-12-14 11:44:01 +00001548 }
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001549 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
1550 locations->SetInAt(2, Location::RequiresRegister());
David Brazdil74eb1b22015-12-14 11:44:01 +00001551 }
1552 locations->SetOut(Location::SameAsFirstInput());
1553}
1554
1555void InstructionCodeGeneratorX86::VisitSelect(HSelect* select) {
1556 LocationSummary* locations = select->GetLocations();
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001557 DCHECK(locations->InAt(0).Equals(locations->Out()));
1558 if (SelectCanUseCMOV(select)) {
1559 // If both the condition and the source types are integer, we can generate
1560 // a CMOV to implement Select.
1561
1562 HInstruction* select_condition = select->GetCondition();
1563 Condition cond = kNotEqual;
1564
1565 // Figure out how to test the 'condition'.
1566 if (select_condition->IsCondition()) {
1567 HCondition* condition = select_condition->AsCondition();
1568 if (!condition->IsEmittedAtUseSite()) {
1569 // This was a previously materialized condition.
1570 // Can we use the existing condition code?
1571 if (AreEflagsSetFrom(condition, select)) {
1572 // Materialization was the previous instruction. Condition codes are right.
1573 cond = X86Condition(condition->GetCondition());
1574 } else {
1575 // No, we have to recreate the condition code.
1576 Register cond_reg = locations->InAt(2).AsRegister<Register>();
1577 __ testl(cond_reg, cond_reg);
1578 }
1579 } else {
1580 // We can't handle FP or long here.
1581 DCHECK_NE(condition->InputAt(0)->GetType(), Primitive::kPrimLong);
1582 DCHECK(!Primitive::IsFloatingPointType(condition->InputAt(0)->GetType()));
1583 LocationSummary* cond_locations = condition->GetLocations();
Roland Levillain0b671c02016-08-19 12:02:34 +01001584 codegen_->GenerateIntCompare(cond_locations->InAt(0), cond_locations->InAt(1));
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001585 cond = X86Condition(condition->GetCondition());
1586 }
1587 } else {
1588 // Must be a boolean condition, which needs to be compared to 0.
1589 Register cond_reg = locations->InAt(2).AsRegister<Register>();
1590 __ testl(cond_reg, cond_reg);
1591 }
1592
1593 // If the condition is true, overwrite the output, which already contains false.
1594 Location false_loc = locations->InAt(0);
1595 Location true_loc = locations->InAt(1);
1596 if (select->GetType() == Primitive::kPrimLong) {
1597 // 64 bit conditional move.
1598 Register false_high = false_loc.AsRegisterPairHigh<Register>();
1599 Register false_low = false_loc.AsRegisterPairLow<Register>();
1600 if (true_loc.IsRegisterPair()) {
1601 __ cmovl(cond, false_high, true_loc.AsRegisterPairHigh<Register>());
1602 __ cmovl(cond, false_low, true_loc.AsRegisterPairLow<Register>());
1603 } else {
1604 __ cmovl(cond, false_high, Address(ESP, true_loc.GetHighStackIndex(kX86WordSize)));
1605 __ cmovl(cond, false_low, Address(ESP, true_loc.GetStackIndex()));
1606 }
1607 } else {
1608 // 32 bit conditional move.
1609 Register false_reg = false_loc.AsRegister<Register>();
1610 if (true_loc.IsRegister()) {
1611 __ cmovl(cond, false_reg, true_loc.AsRegister<Register>());
1612 } else {
1613 __ cmovl(cond, false_reg, Address(ESP, true_loc.GetStackIndex()));
1614 }
1615 }
1616 } else {
1617 NearLabel false_target;
1618 GenerateTestAndBranch<NearLabel>(
1619 select, /* condition_input_index */ 2, /* true_target */ nullptr, &false_target);
1620 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
1621 __ Bind(&false_target);
1622 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001623}
1624
David Srbecky0cf44932015-12-09 14:09:59 +00001625void LocationsBuilderX86::VisitNativeDebugInfo(HNativeDebugInfo* info) {
1626 new (GetGraph()->GetArena()) LocationSummary(info);
1627}
1628
David Srbeckyd28f4a02016-03-14 17:14:24 +00001629void InstructionCodeGeneratorX86::VisitNativeDebugInfo(HNativeDebugInfo*) {
1630 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00001631}
1632
1633void CodeGeneratorX86::GenerateNop() {
1634 __ nop();
David Srbecky0cf44932015-12-09 14:09:59 +00001635}
1636
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001637void LocationsBuilderX86::HandleCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001638 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +01001639 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Mark Mendellc4701932015-04-10 13:18:51 -04001640 // Handle the long/FP comparisons made in instruction simplification.
1641 switch (cond->InputAt(0)->GetType()) {
1642 case Primitive::kPrimLong: {
1643 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell8659e842016-02-16 10:41:46 -05001644 locations->SetInAt(1, Location::Any());
David Brazdilb3e773e2016-01-26 11:28:37 +00001645 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001646 locations->SetOut(Location::RequiresRegister());
1647 }
1648 break;
1649 }
1650 case Primitive::kPrimFloat:
1651 case Primitive::kPrimDouble: {
1652 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001653 if (cond->InputAt(1)->IsX86LoadFromConstantTable()) {
1654 DCHECK(cond->InputAt(1)->IsEmittedAtUseSite());
1655 } else if (cond->InputAt(1)->IsConstant()) {
1656 locations->SetInAt(1, Location::RequiresFpuRegister());
1657 } else {
1658 locations->SetInAt(1, Location::Any());
1659 }
David Brazdilb3e773e2016-01-26 11:28:37 +00001660 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001661 locations->SetOut(Location::RequiresRegister());
1662 }
1663 break;
1664 }
1665 default:
1666 locations->SetInAt(0, Location::RequiresRegister());
1667 locations->SetInAt(1, Location::Any());
David Brazdilb3e773e2016-01-26 11:28:37 +00001668 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001669 // We need a byte register.
1670 locations->SetOut(Location::RegisterLocation(ECX));
1671 }
1672 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001673 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001674}
1675
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001676void InstructionCodeGeneratorX86::HandleCondition(HCondition* cond) {
David Brazdilb3e773e2016-01-26 11:28:37 +00001677 if (cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001678 return;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001679 }
Mark Mendellc4701932015-04-10 13:18:51 -04001680
1681 LocationSummary* locations = cond->GetLocations();
1682 Location lhs = locations->InAt(0);
1683 Location rhs = locations->InAt(1);
1684 Register reg = locations->Out().AsRegister<Register>();
Mark Mendell152408f2015-12-31 12:28:50 -05001685 NearLabel true_label, false_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001686
1687 switch (cond->InputAt(0)->GetType()) {
1688 default: {
1689 // Integer case.
1690
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01001691 // Clear output register: setb only sets the low byte.
Mark Mendellc4701932015-04-10 13:18:51 -04001692 __ xorl(reg, reg);
Roland Levillain0b671c02016-08-19 12:02:34 +01001693 codegen_->GenerateIntCompare(lhs, rhs);
Aart Bike9f37602015-10-09 11:15:55 -07001694 __ setb(X86Condition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001695 return;
1696 }
1697 case Primitive::kPrimLong:
1698 GenerateLongComparesAndJumps(cond, &true_label, &false_label);
1699 break;
1700 case Primitive::kPrimFloat:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001701 GenerateFPCompare(lhs, rhs, cond, false);
Mark Mendellc4701932015-04-10 13:18:51 -04001702 GenerateFPJumps(cond, &true_label, &false_label);
1703 break;
1704 case Primitive::kPrimDouble:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001705 GenerateFPCompare(lhs, rhs, cond, true);
Mark Mendellc4701932015-04-10 13:18:51 -04001706 GenerateFPJumps(cond, &true_label, &false_label);
1707 break;
1708 }
1709
1710 // Convert the jumps into the result.
Mark Mendell0c9497d2015-08-21 09:30:05 -04001711 NearLabel done_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001712
Roland Levillain4fa13f62015-07-06 18:11:54 +01001713 // False case: result = 0.
Mark Mendellc4701932015-04-10 13:18:51 -04001714 __ Bind(&false_label);
1715 __ xorl(reg, reg);
1716 __ jmp(&done_label);
1717
Roland Levillain4fa13f62015-07-06 18:11:54 +01001718 // True case: result = 1.
Mark Mendellc4701932015-04-10 13:18:51 -04001719 __ Bind(&true_label);
1720 __ movl(reg, Immediate(1));
1721 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07001722}
1723
1724void LocationsBuilderX86::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001725 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001726}
1727
1728void InstructionCodeGeneratorX86::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001729 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001730}
1731
1732void LocationsBuilderX86::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001733 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001734}
1735
1736void InstructionCodeGeneratorX86::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001737 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001738}
1739
1740void LocationsBuilderX86::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001741 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001742}
1743
1744void InstructionCodeGeneratorX86::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001745 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001746}
1747
1748void LocationsBuilderX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001749 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001750}
1751
1752void InstructionCodeGeneratorX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001753 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001754}
1755
1756void LocationsBuilderX86::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001757 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001758}
1759
1760void InstructionCodeGeneratorX86::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001761 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001762}
1763
1764void LocationsBuilderX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001765 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001766}
1767
1768void InstructionCodeGeneratorX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001769 HandleCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001770}
1771
Aart Bike9f37602015-10-09 11:15:55 -07001772void LocationsBuilderX86::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001773 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001774}
1775
1776void InstructionCodeGeneratorX86::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001777 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001778}
1779
1780void LocationsBuilderX86::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001781 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001782}
1783
1784void InstructionCodeGeneratorX86::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001785 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001786}
1787
1788void LocationsBuilderX86::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001789 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001790}
1791
1792void InstructionCodeGeneratorX86::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001793 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001794}
1795
1796void LocationsBuilderX86::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001797 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001798}
1799
1800void InstructionCodeGeneratorX86::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001801 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001802}
1803
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001804void LocationsBuilderX86::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001805 LocationSummary* locations =
1806 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001807 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001808}
1809
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001810void InstructionCodeGeneratorX86::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001811 // Will be generated at use site.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001812}
1813
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001814void LocationsBuilderX86::VisitNullConstant(HNullConstant* constant) {
1815 LocationSummary* locations =
1816 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1817 locations->SetOut(Location::ConstantLocation(constant));
1818}
1819
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001820void InstructionCodeGeneratorX86::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001821 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001822}
1823
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001824void LocationsBuilderX86::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001825 LocationSummary* locations =
1826 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001827 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001828}
1829
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001830void InstructionCodeGeneratorX86::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001831 // Will be generated at use site.
1832}
1833
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001834void LocationsBuilderX86::VisitFloatConstant(HFloatConstant* constant) {
1835 LocationSummary* locations =
1836 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1837 locations->SetOut(Location::ConstantLocation(constant));
1838}
1839
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001840void InstructionCodeGeneratorX86::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001841 // Will be generated at use site.
1842}
1843
1844void LocationsBuilderX86::VisitDoubleConstant(HDoubleConstant* constant) {
1845 LocationSummary* locations =
1846 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1847 locations->SetOut(Location::ConstantLocation(constant));
1848}
1849
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001850void InstructionCodeGeneratorX86::VisitDoubleConstant(HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001851 // Will be generated at use site.
1852}
1853
Calin Juravle27df7582015-04-17 19:12:31 +01001854void LocationsBuilderX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1855 memory_barrier->SetLocations(nullptr);
1856}
1857
1858void InstructionCodeGeneratorX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00001859 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01001860}
1861
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001862void LocationsBuilderX86::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001863 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001864}
1865
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001866void InstructionCodeGeneratorX86::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001867 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001868}
1869
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001870void LocationsBuilderX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001871 LocationSummary* locations =
1872 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001873 switch (ret->InputAt(0)->GetType()) {
1874 case Primitive::kPrimBoolean:
1875 case Primitive::kPrimByte:
1876 case Primitive::kPrimChar:
1877 case Primitive::kPrimShort:
1878 case Primitive::kPrimInt:
1879 case Primitive::kPrimNot:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001880 locations->SetInAt(0, Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001881 break;
1882
1883 case Primitive::kPrimLong:
1884 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001885 0, Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001886 break;
1887
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001888 case Primitive::kPrimFloat:
1889 case Primitive::kPrimDouble:
1890 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001891 0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001892 break;
1893
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001894 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001895 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001896 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001897}
1898
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001899void InstructionCodeGeneratorX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001900 if (kIsDebugBuild) {
1901 switch (ret->InputAt(0)->GetType()) {
1902 case Primitive::kPrimBoolean:
1903 case Primitive::kPrimByte:
1904 case Primitive::kPrimChar:
1905 case Primitive::kPrimShort:
1906 case Primitive::kPrimInt:
1907 case Primitive::kPrimNot:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001908 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<Register>(), EAX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001909 break;
1910
1911 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001912 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairLow<Register>(), EAX);
1913 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairHigh<Register>(), EDX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001914 break;
1915
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001916 case Primitive::kPrimFloat:
1917 case Primitive::kPrimDouble:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001918 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>(), XMM0);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001919 break;
1920
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001921 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001922 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001923 }
1924 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001925 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001926}
1927
Calin Juravle175dc732015-08-25 15:42:32 +01001928void LocationsBuilderX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
1929 // The trampoline uses the same calling convention as dex calling conventions,
1930 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
1931 // the method_idx.
1932 HandleInvoke(invoke);
1933}
1934
1935void InstructionCodeGeneratorX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
1936 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
1937}
1938
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001939void LocationsBuilderX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00001940 // Explicit clinit checks triggered by static invokes must have been pruned by
1941 // art::PrepareForRegisterAllocation.
1942 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001943
Mark Mendellfb8d2792015-03-31 22:16:59 -04001944 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001945 if (intrinsic.TryDispatch(invoke)) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00001946 if (invoke->GetLocations()->CanCall() && invoke->HasPcRelativeDexCache()) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00001947 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00001948 }
Mark Mendell09ed1a32015-03-25 08:30:06 -04001949 return;
1950 }
1951
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001952 HandleInvoke(invoke);
Nicolas Geoffray94015b92015-06-04 18:21:04 +01001953
Vladimir Marko0f7dca42015-11-02 14:36:43 +00001954 // For PC-relative dex cache the invoke has an extra input, the PC-relative address base.
1955 if (invoke->HasPcRelativeDexCache()) {
Vladimir Markob4536b72015-11-24 13:45:23 +00001956 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00001957 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001958}
1959
Mark Mendell09ed1a32015-03-25 08:30:06 -04001960static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86* codegen) {
1961 if (invoke->GetLocations()->Intrinsified()) {
1962 IntrinsicCodeGeneratorX86 intrinsic(codegen);
1963 intrinsic.Dispatch(invoke);
1964 return true;
1965 }
1966 return false;
1967}
1968
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001969void InstructionCodeGeneratorX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00001970 // Explicit clinit checks triggered by static invokes must have been pruned by
1971 // art::PrepareForRegisterAllocation.
1972 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001973
Mark Mendell09ed1a32015-03-25 08:30:06 -04001974 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1975 return;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001976 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001977
Nicolas Geoffray94015b92015-06-04 18:21:04 +01001978 LocationSummary* locations = invoke->GetLocations();
Mark Mendell09ed1a32015-03-25 08:30:06 -04001979 codegen_->GenerateStaticOrDirectCall(
Nicolas Geoffray94015b92015-06-04 18:21:04 +01001980 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Mingyao Yang8693fe12015-04-17 16:51:08 -07001981 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001982}
1983
1984void LocationsBuilderX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00001985 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
1986 if (intrinsic.TryDispatch(invoke)) {
1987 return;
1988 }
1989
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001990 HandleInvoke(invoke);
1991}
1992
1993void LocationsBuilderX86::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001994 InvokeDexCallingConventionVisitorX86 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001995 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001996}
1997
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001998void InstructionCodeGeneratorX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampebfb5ba92015-09-01 15:45:02 +00001999 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2000 return;
2001 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002002
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002003 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002004 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01002005 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002006}
2007
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002008void LocationsBuilderX86::VisitInvokeInterface(HInvokeInterface* invoke) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002009 // This call to HandleInvoke allocates a temporary (core) register
2010 // which is also used to transfer the hidden argument from FP to
2011 // core register.
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002012 HandleInvoke(invoke);
2013 // Add the hidden argument.
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002014 invoke->GetLocations()->AddTemp(Location::FpuRegisterLocation(XMM7));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002015}
2016
2017void InstructionCodeGeneratorX86::VisitInvokeInterface(HInvokeInterface* invoke) {
2018 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain0d5a2812015-11-13 10:07:31 +00002019 LocationSummary* locations = invoke->GetLocations();
2020 Register temp = locations->GetTemp(0).AsRegister<Register>();
2021 XmmRegister hidden_reg = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002022 Location receiver = locations->InAt(0);
2023 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2024
Roland Levillain0d5a2812015-11-13 10:07:31 +00002025 // Set the hidden argument. This is safe to do this here, as XMM7
2026 // won't be modified thereafter, before the `call` instruction.
2027 DCHECK_EQ(XMM7, hidden_reg);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002028 __ movl(temp, Immediate(invoke->GetDexMethodIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002029 __ movd(hidden_reg, temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002030
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002031 if (receiver.IsStackSlot()) {
2032 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002033 // /* HeapReference<Class> */ temp = temp->klass_
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002034 __ movl(temp, Address(temp, class_offset));
2035 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002036 // /* HeapReference<Class> */ temp = receiver->klass_
Roland Levillain271ab9c2014-11-27 15:23:57 +00002037 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002038 }
Roland Levillain4d027112015-07-01 15:41:14 +01002039 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00002040 // Instead of simply (possibly) unpoisoning `temp` here, we should
2041 // emit a read barrier for the previous class reference load.
2042 // However this is not required in practice, as this is an
2043 // intermediate/temporary reference and because the current
2044 // concurrent copying collector keeps the from-space memory
2045 // intact/accessible until the end of the marking phase (the
2046 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01002047 __ MaybeUnpoisonHeapReference(temp);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002048 // temp = temp->GetAddressOfIMT()
2049 __ movl(temp,
2050 Address(temp, mirror::Class::ImtPtrOffset(kX86PointerSize).Uint32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002051 // temp = temp->GetImtEntryAt(method_offset);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002052 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00002053 invoke->GetImtIndex(), kX86PointerSize));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002054 __ movl(temp, Address(temp, method_offset));
2055 // call temp->GetEntryPoint();
Roland Levillain0d5a2812015-11-13 10:07:31 +00002056 __ call(Address(temp,
Andreas Gampe542451c2016-07-26 09:02:02 -07002057 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86PointerSize).Int32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002058
2059 DCHECK(!codegen_->IsLeafMethod());
2060 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2061}
2062
Roland Levillain88cb1752014-10-20 16:36:47 +01002063void LocationsBuilderX86::VisitNeg(HNeg* neg) {
2064 LocationSummary* locations =
2065 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2066 switch (neg->GetResultType()) {
2067 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002068 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01002069 locations->SetInAt(0, Location::RequiresRegister());
2070 locations->SetOut(Location::SameAsFirstInput());
2071 break;
2072
Roland Levillain88cb1752014-10-20 16:36:47 +01002073 case Primitive::kPrimFloat:
Roland Levillain5368c212014-11-27 15:03:41 +00002074 locations->SetInAt(0, Location::RequiresFpuRegister());
2075 locations->SetOut(Location::SameAsFirstInput());
2076 locations->AddTemp(Location::RequiresRegister());
2077 locations->AddTemp(Location::RequiresFpuRegister());
2078 break;
2079
Roland Levillain88cb1752014-10-20 16:36:47 +01002080 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002081 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00002082 locations->SetOut(Location::SameAsFirstInput());
2083 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01002084 break;
2085
2086 default:
2087 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2088 }
2089}
2090
2091void InstructionCodeGeneratorX86::VisitNeg(HNeg* neg) {
2092 LocationSummary* locations = neg->GetLocations();
2093 Location out = locations->Out();
2094 Location in = locations->InAt(0);
2095 switch (neg->GetResultType()) {
2096 case Primitive::kPrimInt:
2097 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002098 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002099 __ negl(out.AsRegister<Register>());
Roland Levillain88cb1752014-10-20 16:36:47 +01002100 break;
2101
2102 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002103 DCHECK(in.IsRegisterPair());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002104 DCHECK(in.Equals(out));
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002105 __ negl(out.AsRegisterPairLow<Register>());
2106 // Negation is similar to subtraction from zero. The least
2107 // significant byte triggers a borrow when it is different from
2108 // zero; to take it into account, add 1 to the most significant
2109 // byte if the carry flag (CF) is set to 1 after the first NEGL
2110 // operation.
2111 __ adcl(out.AsRegisterPairHigh<Register>(), Immediate(0));
2112 __ negl(out.AsRegisterPairHigh<Register>());
2113 break;
2114
Roland Levillain5368c212014-11-27 15:03:41 +00002115 case Primitive::kPrimFloat: {
2116 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002117 Register constant = locations->GetTemp(0).AsRegister<Register>();
2118 XmmRegister mask = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002119 // Implement float negation with an exclusive or with value
2120 // 0x80000000 (mask for bit 31, representing the sign of a
2121 // single-precision floating-point number).
2122 __ movl(constant, Immediate(INT32_C(0x80000000)));
2123 __ movd(mask, constant);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002124 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00002125 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002126 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00002127
Roland Levillain5368c212014-11-27 15:03:41 +00002128 case Primitive::kPrimDouble: {
2129 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002130 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002131 // Implement double negation with an exclusive or with value
2132 // 0x8000000000000000 (mask for bit 63, representing the sign of
2133 // a double-precision floating-point number).
2134 __ LoadLongConstant(mask, INT64_C(0x8000000000000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002135 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01002136 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002137 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002138
2139 default:
2140 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2141 }
2142}
2143
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002144void LocationsBuilderX86::VisitX86FPNeg(HX86FPNeg* neg) {
2145 LocationSummary* locations =
2146 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2147 DCHECK(Primitive::IsFloatingPointType(neg->GetType()));
2148 locations->SetInAt(0, Location::RequiresFpuRegister());
2149 locations->SetInAt(1, Location::RequiresRegister());
2150 locations->SetOut(Location::SameAsFirstInput());
2151 locations->AddTemp(Location::RequiresFpuRegister());
2152}
2153
2154void InstructionCodeGeneratorX86::VisitX86FPNeg(HX86FPNeg* neg) {
2155 LocationSummary* locations = neg->GetLocations();
2156 Location out = locations->Out();
2157 DCHECK(locations->InAt(0).Equals(out));
2158
2159 Register constant_area = locations->InAt(1).AsRegister<Register>();
2160 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
2161 if (neg->GetType() == Primitive::kPrimFloat) {
2162 __ movss(mask, codegen_->LiteralInt32Address(INT32_C(0x80000000), constant_area));
2163 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
2164 } else {
2165 __ movsd(mask, codegen_->LiteralInt64Address(INT64_C(0x8000000000000000), constant_area));
2166 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
2167 }
2168}
2169
Roland Levillaindff1f282014-11-05 14:15:05 +00002170void LocationsBuilderX86::VisitTypeConversion(HTypeConversion* conversion) {
Roland Levillaindff1f282014-11-05 14:15:05 +00002171 Primitive::Type result_type = conversion->GetResultType();
2172 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002173 DCHECK_NE(result_type, input_type);
Roland Levillain624279f2014-12-04 11:54:28 +00002174
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002175 // The float-to-long and double-to-long type conversions rely on a
2176 // call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00002177 LocationSummary::CallKind call_kind =
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002178 ((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
2179 && result_type == Primitive::kPrimLong)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01002180 ? LocationSummary::kCallOnMainOnly
Roland Levillain624279f2014-12-04 11:54:28 +00002181 : LocationSummary::kNoCall;
2182 LocationSummary* locations =
2183 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
2184
David Brazdilb2bd1c52015-03-25 11:17:37 +00002185 // The Java language does not allow treating boolean as an integral type but
2186 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00002187
Roland Levillaindff1f282014-11-05 14:15:05 +00002188 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002189 case Primitive::kPrimByte:
2190 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002191 case Primitive::kPrimLong: {
2192 // Type conversion from long to byte is a result of code transformations.
2193 HInstruction* input = conversion->InputAt(0);
2194 Location input_location = input->IsConstant()
2195 ? Location::ConstantLocation(input->AsConstant())
2196 : Location::RegisterPairLocation(EAX, EDX);
2197 locations->SetInAt(0, input_location);
2198 // Make the output overlap to please the register allocator. This greatly simplifies
2199 // the validation of the linear scan implementation
2200 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2201 break;
2202 }
David Brazdil46e2a392015-03-16 17:31:52 +00002203 case Primitive::kPrimBoolean:
2204 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002205 case Primitive::kPrimShort:
2206 case Primitive::kPrimInt:
2207 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002208 // Processing a Dex `int-to-byte' instruction.
Mark Mendell5f874182015-03-04 15:42:45 -05002209 locations->SetInAt(0, Location::ByteRegisterOrConstant(ECX, conversion->InputAt(0)));
2210 // Make the output overlap to please the register allocator. This greatly simplifies
2211 // the validation of the linear scan implementation
2212 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Roland Levillain51d3fc42014-11-13 14:11:42 +00002213 break;
2214
2215 default:
2216 LOG(FATAL) << "Unexpected type conversion from " << input_type
2217 << " to " << result_type;
2218 }
2219 break;
2220
Roland Levillain01a8d712014-11-14 16:27:39 +00002221 case Primitive::kPrimShort:
2222 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002223 case Primitive::kPrimLong:
2224 // Type conversion from long to short is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002225 case Primitive::kPrimBoolean:
2226 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002227 case Primitive::kPrimByte:
2228 case Primitive::kPrimInt:
2229 case Primitive::kPrimChar:
2230 // Processing a Dex `int-to-short' instruction.
2231 locations->SetInAt(0, Location::Any());
2232 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2233 break;
2234
2235 default:
2236 LOG(FATAL) << "Unexpected type conversion from " << input_type
2237 << " to " << result_type;
2238 }
2239 break;
2240
Roland Levillain946e1432014-11-11 17:35:19 +00002241 case Primitive::kPrimInt:
2242 switch (input_type) {
2243 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002244 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002245 locations->SetInAt(0, Location::Any());
2246 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2247 break;
2248
2249 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00002250 // Processing a Dex `float-to-int' instruction.
2251 locations->SetInAt(0, Location::RequiresFpuRegister());
2252 locations->SetOut(Location::RequiresRegister());
2253 locations->AddTemp(Location::RequiresFpuRegister());
2254 break;
2255
Roland Levillain946e1432014-11-11 17:35:19 +00002256 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002257 // Processing a Dex `double-to-int' instruction.
2258 locations->SetInAt(0, Location::RequiresFpuRegister());
2259 locations->SetOut(Location::RequiresRegister());
2260 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00002261 break;
2262
2263 default:
2264 LOG(FATAL) << "Unexpected type conversion from " << input_type
2265 << " to " << result_type;
2266 }
2267 break;
2268
Roland Levillaindff1f282014-11-05 14:15:05 +00002269 case Primitive::kPrimLong:
2270 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002271 case Primitive::kPrimBoolean:
2272 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002273 case Primitive::kPrimByte:
2274 case Primitive::kPrimShort:
2275 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002276 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002277 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002278 locations->SetInAt(0, Location::RegisterLocation(EAX));
2279 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
2280 break;
2281
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002282 case Primitive::kPrimFloat:
Vladimir Marko949c91f2015-01-27 10:48:44 +00002283 case Primitive::kPrimDouble: {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002284 // Processing a Dex `float-to-long' or 'double-to-long' instruction.
Vladimir Marko949c91f2015-01-27 10:48:44 +00002285 InvokeRuntimeCallingConvention calling_convention;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002286 XmmRegister parameter = calling_convention.GetFpuRegisterAt(0);
2287 locations->SetInAt(0, Location::FpuRegisterLocation(parameter));
2288
Vladimir Marko949c91f2015-01-27 10:48:44 +00002289 // The runtime helper puts the result in EAX, EDX.
2290 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Vladimir Marko949c91f2015-01-27 10:48:44 +00002291 }
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002292 break;
Roland Levillaindff1f282014-11-05 14:15:05 +00002293
2294 default:
2295 LOG(FATAL) << "Unexpected type conversion from " << input_type
2296 << " to " << result_type;
2297 }
2298 break;
2299
Roland Levillain981e4542014-11-14 11:47:14 +00002300 case Primitive::kPrimChar:
2301 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002302 case Primitive::kPrimLong:
2303 // Type conversion from long to char is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002304 case Primitive::kPrimBoolean:
2305 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002306 case Primitive::kPrimByte:
2307 case Primitive::kPrimShort:
2308 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002309 // Processing a Dex `int-to-char' instruction.
2310 locations->SetInAt(0, Location::Any());
2311 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2312 break;
2313
2314 default:
2315 LOG(FATAL) << "Unexpected type conversion from " << input_type
2316 << " to " << result_type;
2317 }
2318 break;
2319
Roland Levillaindff1f282014-11-05 14:15:05 +00002320 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002321 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002322 case Primitive::kPrimBoolean:
2323 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002324 case Primitive::kPrimByte:
2325 case Primitive::kPrimShort:
2326 case Primitive::kPrimInt:
2327 case Primitive::kPrimChar:
2328 // Processing a Dex `int-to-float' instruction.
2329 locations->SetInAt(0, Location::RequiresRegister());
2330 locations->SetOut(Location::RequiresFpuRegister());
2331 break;
2332
2333 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002334 // Processing a Dex `long-to-float' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002335 locations->SetInAt(0, Location::Any());
2336 locations->SetOut(Location::Any());
Roland Levillain6d0e4832014-11-27 18:31:21 +00002337 break;
2338
Roland Levillaincff13742014-11-17 14:32:17 +00002339 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002340 // Processing a Dex `double-to-float' instruction.
2341 locations->SetInAt(0, Location::RequiresFpuRegister());
2342 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002343 break;
2344
2345 default:
2346 LOG(FATAL) << "Unexpected type conversion from " << input_type
2347 << " to " << result_type;
2348 };
2349 break;
2350
Roland Levillaindff1f282014-11-05 14:15:05 +00002351 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002352 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002353 case Primitive::kPrimBoolean:
2354 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002355 case Primitive::kPrimByte:
2356 case Primitive::kPrimShort:
2357 case Primitive::kPrimInt:
2358 case Primitive::kPrimChar:
2359 // Processing a Dex `int-to-double' instruction.
2360 locations->SetInAt(0, Location::RequiresRegister());
2361 locations->SetOut(Location::RequiresFpuRegister());
2362 break;
2363
2364 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002365 // Processing a Dex `long-to-double' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002366 locations->SetInAt(0, Location::Any());
2367 locations->SetOut(Location::Any());
Roland Levillain647b9ed2014-11-27 12:06:00 +00002368 break;
2369
Roland Levillaincff13742014-11-17 14:32:17 +00002370 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002371 // Processing a Dex `float-to-double' instruction.
2372 locations->SetInAt(0, Location::RequiresFpuRegister());
2373 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002374 break;
2375
2376 default:
2377 LOG(FATAL) << "Unexpected type conversion from " << input_type
2378 << " to " << result_type;
2379 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002380 break;
2381
2382 default:
2383 LOG(FATAL) << "Unexpected type conversion from " << input_type
2384 << " to " << result_type;
2385 }
2386}
2387
2388void InstructionCodeGeneratorX86::VisitTypeConversion(HTypeConversion* conversion) {
2389 LocationSummary* locations = conversion->GetLocations();
2390 Location out = locations->Out();
2391 Location in = locations->InAt(0);
2392 Primitive::Type result_type = conversion->GetResultType();
2393 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002394 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00002395 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002396 case Primitive::kPrimByte:
2397 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002398 case Primitive::kPrimLong:
2399 // Type conversion from long to byte is a result of code transformations.
2400 if (in.IsRegisterPair()) {
2401 __ movsxb(out.AsRegister<Register>(), in.AsRegisterPairLow<ByteRegister>());
2402 } else {
2403 DCHECK(in.GetConstant()->IsLongConstant());
2404 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2405 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
2406 }
2407 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002408 case Primitive::kPrimBoolean:
2409 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002410 case Primitive::kPrimShort:
2411 case Primitive::kPrimInt:
2412 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002413 // Processing a Dex `int-to-byte' instruction.
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002414 if (in.IsRegister()) {
2415 __ movsxb(out.AsRegister<Register>(), in.AsRegister<ByteRegister>());
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002416 } else {
2417 DCHECK(in.GetConstant()->IsIntConstant());
2418 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
2419 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
2420 }
Roland Levillain51d3fc42014-11-13 14:11:42 +00002421 break;
2422
2423 default:
2424 LOG(FATAL) << "Unexpected type conversion from " << input_type
2425 << " to " << result_type;
2426 }
2427 break;
2428
Roland Levillain01a8d712014-11-14 16:27:39 +00002429 case Primitive::kPrimShort:
2430 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002431 case Primitive::kPrimLong:
2432 // Type conversion from long to short is a result of code transformations.
2433 if (in.IsRegisterPair()) {
2434 __ movsxw(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
2435 } else if (in.IsDoubleStackSlot()) {
2436 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
2437 } else {
2438 DCHECK(in.GetConstant()->IsLongConstant());
2439 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2440 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
2441 }
2442 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002443 case Primitive::kPrimBoolean:
2444 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002445 case Primitive::kPrimByte:
2446 case Primitive::kPrimInt:
2447 case Primitive::kPrimChar:
2448 // Processing a Dex `int-to-short' instruction.
2449 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002450 __ movsxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain01a8d712014-11-14 16:27:39 +00002451 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002452 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain01a8d712014-11-14 16:27:39 +00002453 } else {
2454 DCHECK(in.GetConstant()->IsIntConstant());
2455 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002456 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
Roland Levillain01a8d712014-11-14 16:27:39 +00002457 }
2458 break;
2459
2460 default:
2461 LOG(FATAL) << "Unexpected type conversion from " << input_type
2462 << " to " << result_type;
2463 }
2464 break;
2465
Roland Levillain946e1432014-11-11 17:35:19 +00002466 case Primitive::kPrimInt:
2467 switch (input_type) {
2468 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002469 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002470 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002471 __ movl(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00002472 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002473 __ movl(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain946e1432014-11-11 17:35:19 +00002474 } else {
2475 DCHECK(in.IsConstant());
2476 DCHECK(in.GetConstant()->IsLongConstant());
2477 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002478 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00002479 }
2480 break;
2481
Roland Levillain3f8f9362014-12-02 17:45:01 +00002482 case Primitive::kPrimFloat: {
2483 // Processing a Dex `float-to-int' instruction.
2484 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2485 Register output = out.AsRegister<Register>();
2486 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002487 NearLabel done, nan;
Roland Levillain3f8f9362014-12-02 17:45:01 +00002488
2489 __ movl(output, Immediate(kPrimIntMax));
2490 // temp = int-to-float(output)
2491 __ cvtsi2ss(temp, output);
2492 // if input >= temp goto done
2493 __ comiss(input, temp);
2494 __ j(kAboveEqual, &done);
2495 // if input == NaN goto nan
2496 __ j(kUnordered, &nan);
2497 // output = float-to-int-truncate(input)
2498 __ cvttss2si(output, input);
2499 __ jmp(&done);
2500 __ Bind(&nan);
2501 // output = 0
2502 __ xorl(output, output);
2503 __ Bind(&done);
2504 break;
2505 }
2506
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002507 case Primitive::kPrimDouble: {
2508 // Processing a Dex `double-to-int' instruction.
2509 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2510 Register output = out.AsRegister<Register>();
2511 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002512 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002513
2514 __ movl(output, Immediate(kPrimIntMax));
2515 // temp = int-to-double(output)
2516 __ cvtsi2sd(temp, output);
2517 // if input >= temp goto done
2518 __ comisd(input, temp);
2519 __ j(kAboveEqual, &done);
2520 // if input == NaN goto nan
2521 __ j(kUnordered, &nan);
2522 // output = double-to-int-truncate(input)
2523 __ cvttsd2si(output, input);
2524 __ jmp(&done);
2525 __ Bind(&nan);
2526 // output = 0
2527 __ xorl(output, output);
2528 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00002529 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002530 }
Roland Levillain946e1432014-11-11 17:35:19 +00002531
2532 default:
2533 LOG(FATAL) << "Unexpected type conversion from " << input_type
2534 << " to " << result_type;
2535 }
2536 break;
2537
Roland Levillaindff1f282014-11-05 14:15:05 +00002538 case Primitive::kPrimLong:
2539 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002540 case Primitive::kPrimBoolean:
2541 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002542 case Primitive::kPrimByte:
2543 case Primitive::kPrimShort:
2544 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002545 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002546 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002547 DCHECK_EQ(out.AsRegisterPairLow<Register>(), EAX);
2548 DCHECK_EQ(out.AsRegisterPairHigh<Register>(), EDX);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002549 DCHECK_EQ(in.AsRegister<Register>(), EAX);
Roland Levillaindff1f282014-11-05 14:15:05 +00002550 __ cdq();
2551 break;
2552
2553 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00002554 // Processing a Dex `float-to-long' instruction.
Serban Constantinescuba45db02016-07-12 22:53:02 +01002555 codegen_->InvokeRuntime(kQuickF2l, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00002556 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
Roland Levillain624279f2014-12-04 11:54:28 +00002557 break;
2558
Roland Levillaindff1f282014-11-05 14:15:05 +00002559 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002560 // Processing a Dex `double-to-long' instruction.
Serban Constantinescuba45db02016-07-12 22:53:02 +01002561 codegen_->InvokeRuntime(kQuickD2l, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00002562 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
Roland Levillaindff1f282014-11-05 14:15:05 +00002563 break;
2564
2565 default:
2566 LOG(FATAL) << "Unexpected type conversion from " << input_type
2567 << " to " << result_type;
2568 }
2569 break;
2570
Roland Levillain981e4542014-11-14 11:47:14 +00002571 case Primitive::kPrimChar:
2572 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002573 case Primitive::kPrimLong:
2574 // Type conversion from long to short is a result of code transformations.
2575 if (in.IsRegisterPair()) {
2576 __ movzxw(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
2577 } else if (in.IsDoubleStackSlot()) {
2578 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
2579 } else {
2580 DCHECK(in.GetConstant()->IsLongConstant());
2581 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2582 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
2583 }
2584 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002585 case Primitive::kPrimBoolean:
2586 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002587 case Primitive::kPrimByte:
2588 case Primitive::kPrimShort:
2589 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002590 // Processing a Dex `Process a Dex `int-to-char'' instruction.
2591 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002592 __ movzxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain981e4542014-11-14 11:47:14 +00002593 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002594 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain981e4542014-11-14 11:47:14 +00002595 } else {
2596 DCHECK(in.GetConstant()->IsIntConstant());
2597 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002598 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
Roland Levillain981e4542014-11-14 11:47:14 +00002599 }
2600 break;
2601
2602 default:
2603 LOG(FATAL) << "Unexpected type conversion from " << input_type
2604 << " to " << result_type;
2605 }
2606 break;
2607
Roland Levillaindff1f282014-11-05 14:15:05 +00002608 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002609 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002610 case Primitive::kPrimBoolean:
2611 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002612 case Primitive::kPrimByte:
2613 case Primitive::kPrimShort:
2614 case Primitive::kPrimInt:
2615 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002616 // Processing a Dex `int-to-float' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002617 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002618 break;
2619
Roland Levillain6d0e4832014-11-27 18:31:21 +00002620 case Primitive::kPrimLong: {
2621 // Processing a Dex `long-to-float' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002622 size_t adjustment = 0;
Roland Levillain6d0e4832014-11-27 18:31:21 +00002623
Roland Levillain232ade02015-04-20 15:14:36 +01002624 // Create stack space for the call to
2625 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstps below.
2626 // TODO: enhance register allocator to ask for stack temporaries.
2627 if (!in.IsDoubleStackSlot() || !out.IsStackSlot()) {
2628 adjustment = Primitive::ComponentSize(Primitive::kPrimLong);
2629 __ subl(ESP, Immediate(adjustment));
2630 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002631
Roland Levillain232ade02015-04-20 15:14:36 +01002632 // Load the value to the FP stack, using temporaries if needed.
2633 PushOntoFPStack(in, 0, adjustment, false, true);
2634
2635 if (out.IsStackSlot()) {
2636 __ fstps(Address(ESP, out.GetStackIndex() + adjustment));
2637 } else {
2638 __ fstps(Address(ESP, 0));
2639 Location stack_temp = Location::StackSlot(0);
2640 codegen_->Move32(out, stack_temp);
2641 }
2642
2643 // Remove the temporary stack space we allocated.
2644 if (adjustment != 0) {
2645 __ addl(ESP, Immediate(adjustment));
2646 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002647 break;
2648 }
2649
Roland Levillaincff13742014-11-17 14:32:17 +00002650 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002651 // Processing a Dex `double-to-float' instruction.
2652 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002653 break;
2654
2655 default:
2656 LOG(FATAL) << "Unexpected type conversion from " << input_type
2657 << " to " << result_type;
2658 };
2659 break;
2660
Roland Levillaindff1f282014-11-05 14:15:05 +00002661 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002662 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002663 case Primitive::kPrimBoolean:
2664 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002665 case Primitive::kPrimByte:
2666 case Primitive::kPrimShort:
2667 case Primitive::kPrimInt:
2668 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002669 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002670 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002671 break;
2672
Roland Levillain647b9ed2014-11-27 12:06:00 +00002673 case Primitive::kPrimLong: {
2674 // Processing a Dex `long-to-double' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002675 size_t adjustment = 0;
Roland Levillain647b9ed2014-11-27 12:06:00 +00002676
Roland Levillain232ade02015-04-20 15:14:36 +01002677 // Create stack space for the call to
2678 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstpl below.
2679 // TODO: enhance register allocator to ask for stack temporaries.
2680 if (!in.IsDoubleStackSlot() || !out.IsDoubleStackSlot()) {
2681 adjustment = Primitive::ComponentSize(Primitive::kPrimLong);
2682 __ subl(ESP, Immediate(adjustment));
2683 }
2684
2685 // Load the value to the FP stack, using temporaries if needed.
2686 PushOntoFPStack(in, 0, adjustment, false, true);
2687
2688 if (out.IsDoubleStackSlot()) {
2689 __ fstpl(Address(ESP, out.GetStackIndex() + adjustment));
2690 } else {
2691 __ fstpl(Address(ESP, 0));
2692 Location stack_temp = Location::DoubleStackSlot(0);
2693 codegen_->Move64(out, stack_temp);
2694 }
2695
2696 // Remove the temporary stack space we allocated.
2697 if (adjustment != 0) {
2698 __ addl(ESP, Immediate(adjustment));
2699 }
Roland Levillain647b9ed2014-11-27 12:06:00 +00002700 break;
2701 }
2702
Roland Levillaincff13742014-11-17 14:32:17 +00002703 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002704 // Processing a Dex `float-to-double' instruction.
2705 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002706 break;
2707
2708 default:
2709 LOG(FATAL) << "Unexpected type conversion from " << input_type
2710 << " to " << result_type;
2711 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002712 break;
2713
2714 default:
2715 LOG(FATAL) << "Unexpected type conversion from " << input_type
2716 << " to " << result_type;
2717 }
2718}
2719
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002720void LocationsBuilderX86::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002721 LocationSummary* locations =
2722 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002723 switch (add->GetResultType()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002724 case Primitive::kPrimInt: {
2725 locations->SetInAt(0, Location::RequiresRegister());
2726 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
2727 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2728 break;
2729 }
2730
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002731 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002732 locations->SetInAt(0, Location::RequiresRegister());
2733 locations->SetInAt(1, Location::Any());
2734 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002735 break;
2736 }
2737
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002738 case Primitive::kPrimFloat:
2739 case Primitive::kPrimDouble: {
2740 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002741 if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
2742 DCHECK(add->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00002743 } else if (add->InputAt(1)->IsConstant()) {
2744 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002745 } else {
2746 locations->SetInAt(1, Location::Any());
2747 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002748 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002749 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002750 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002751
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002752 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002753 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
2754 break;
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002755 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002756}
2757
2758void InstructionCodeGeneratorX86::VisitAdd(HAdd* add) {
2759 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002760 Location first = locations->InAt(0);
2761 Location second = locations->InAt(1);
Mark Mendell09b84632015-02-13 17:48:38 -05002762 Location out = locations->Out();
2763
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002764 switch (add->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002765 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002766 if (second.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002767 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2768 __ addl(out.AsRegister<Register>(), second.AsRegister<Register>());
Mark Mendell33bf2452015-05-27 10:08:24 -04002769 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
2770 __ addl(out.AsRegister<Register>(), first.AsRegister<Register>());
Mark Mendell09b84632015-02-13 17:48:38 -05002771 } else {
2772 __ leal(out.AsRegister<Register>(), Address(
2773 first.AsRegister<Register>(), second.AsRegister<Register>(), TIMES_1, 0));
2774 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002775 } else if (second.IsConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002776 int32_t value = second.GetConstant()->AsIntConstant()->GetValue();
2777 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2778 __ addl(out.AsRegister<Register>(), Immediate(value));
2779 } else {
2780 __ leal(out.AsRegister<Register>(), Address(first.AsRegister<Register>(), value));
2781 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002782 } else {
Mark Mendell09b84632015-02-13 17:48:38 -05002783 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002784 __ addl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002785 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002786 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002787 }
2788
2789 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002790 if (second.IsRegisterPair()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002791 __ addl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
2792 __ adcl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002793 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002794 __ addl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
2795 __ adcl(first.AsRegisterPairHigh<Register>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002796 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002797 } else {
2798 DCHECK(second.IsConstant()) << second;
2799 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2800 __ addl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
2801 __ adcl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002802 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002803 break;
2804 }
2805
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002806 case Primitive::kPrimFloat: {
2807 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002808 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04002809 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
2810 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00002811 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04002812 __ addss(first.AsFpuRegister<XmmRegister>(),
2813 codegen_->LiteralFloatAddress(
2814 const_area->GetConstant()->AsFloatConstant()->GetValue(),
2815 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2816 } else {
2817 DCHECK(second.IsStackSlot());
2818 __ addss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002819 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002820 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002821 }
2822
2823 case Primitive::kPrimDouble: {
2824 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002825 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04002826 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
2827 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00002828 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04002829 __ addsd(first.AsFpuRegister<XmmRegister>(),
2830 codegen_->LiteralDoubleAddress(
2831 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
2832 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2833 } else {
2834 DCHECK(second.IsDoubleStackSlot());
2835 __ addsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002836 }
2837 break;
2838 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002839
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002840 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002841 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002842 }
2843}
2844
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002845void LocationsBuilderX86::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002846 LocationSummary* locations =
2847 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002848 switch (sub->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002849 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002850 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002851 locations->SetInAt(0, Location::RequiresRegister());
2852 locations->SetInAt(1, Location::Any());
2853 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002854 break;
2855 }
Calin Juravle11351682014-10-23 15:38:15 +01002856 case Primitive::kPrimFloat:
2857 case Primitive::kPrimDouble: {
2858 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002859 if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
2860 DCHECK(sub->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00002861 } else if (sub->InputAt(1)->IsConstant()) {
2862 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002863 } else {
2864 locations->SetInAt(1, Location::Any());
2865 }
Calin Juravle11351682014-10-23 15:38:15 +01002866 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002867 break;
Calin Juravle11351682014-10-23 15:38:15 +01002868 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002869
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002870 default:
Calin Juravle11351682014-10-23 15:38:15 +01002871 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002872 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002873}
2874
2875void InstructionCodeGeneratorX86::VisitSub(HSub* sub) {
2876 LocationSummary* locations = sub->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002877 Location first = locations->InAt(0);
2878 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01002879 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002880 switch (sub->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002881 case Primitive::kPrimInt: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002882 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002883 __ subl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002884 } else if (second.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002885 __ subl(first.AsRegister<Register>(),
2886 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002887 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002888 __ subl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002889 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002890 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002891 }
2892
2893 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002894 if (second.IsRegisterPair()) {
Calin Juravle11351682014-10-23 15:38:15 +01002895 __ subl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
2896 __ sbbl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002897 } else if (second.IsDoubleStackSlot()) {
Calin Juravle11351682014-10-23 15:38:15 +01002898 __ subl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002899 __ sbbl(first.AsRegisterPairHigh<Register>(),
2900 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002901 } else {
2902 DCHECK(second.IsConstant()) << second;
2903 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2904 __ subl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
2905 __ sbbl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002906 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002907 break;
2908 }
2909
Calin Juravle11351682014-10-23 15:38:15 +01002910 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04002911 if (second.IsFpuRegister()) {
2912 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2913 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
2914 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00002915 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04002916 __ subss(first.AsFpuRegister<XmmRegister>(),
2917 codegen_->LiteralFloatAddress(
2918 const_area->GetConstant()->AsFloatConstant()->GetValue(),
2919 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2920 } else {
2921 DCHECK(second.IsStackSlot());
2922 __ subss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
2923 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002924 break;
Calin Juravle11351682014-10-23 15:38:15 +01002925 }
2926
2927 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04002928 if (second.IsFpuRegister()) {
2929 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2930 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
2931 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00002932 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04002933 __ subsd(first.AsFpuRegister<XmmRegister>(),
2934 codegen_->LiteralDoubleAddress(
2935 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
2936 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2937 } else {
2938 DCHECK(second.IsDoubleStackSlot());
2939 __ subsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
2940 }
Calin Juravle11351682014-10-23 15:38:15 +01002941 break;
2942 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002943
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002944 default:
Calin Juravle11351682014-10-23 15:38:15 +01002945 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002946 }
2947}
2948
Calin Juravle34bacdf2014-10-07 20:23:36 +01002949void LocationsBuilderX86::VisitMul(HMul* mul) {
2950 LocationSummary* locations =
2951 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
2952 switch (mul->GetResultType()) {
2953 case Primitive::kPrimInt:
2954 locations->SetInAt(0, Location::RequiresRegister());
2955 locations->SetInAt(1, Location::Any());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002956 if (mul->InputAt(1)->IsIntConstant()) {
2957 // Can use 3 operand multiply.
2958 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2959 } else {
2960 locations->SetOut(Location::SameAsFirstInput());
2961 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002962 break;
2963 case Primitive::kPrimLong: {
2964 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002965 locations->SetInAt(1, Location::Any());
2966 locations->SetOut(Location::SameAsFirstInput());
2967 // Needed for imul on 32bits with 64bits output.
2968 locations->AddTemp(Location::RegisterLocation(EAX));
2969 locations->AddTemp(Location::RegisterLocation(EDX));
2970 break;
2971 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002972 case Primitive::kPrimFloat:
2973 case Primitive::kPrimDouble: {
2974 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002975 if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
2976 DCHECK(mul->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00002977 } else if (mul->InputAt(1)->IsConstant()) {
2978 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002979 } else {
2980 locations->SetInAt(1, Location::Any());
2981 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002982 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002983 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002984 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002985
2986 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002987 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002988 }
2989}
2990
2991void InstructionCodeGeneratorX86::VisitMul(HMul* mul) {
2992 LocationSummary* locations = mul->GetLocations();
2993 Location first = locations->InAt(0);
2994 Location second = locations->InAt(1);
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002995 Location out = locations->Out();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002996
2997 switch (mul->GetResultType()) {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002998 case Primitive::kPrimInt:
2999 // The constant may have ended up in a register, so test explicitly to avoid
3000 // problems where the output may not be the same as the first operand.
3001 if (mul->InputAt(1)->IsIntConstant()) {
3002 Immediate imm(mul->InputAt(1)->AsIntConstant()->GetValue());
3003 __ imull(out.AsRegister<Register>(), first.AsRegister<Register>(), imm);
3004 } else if (second.IsRegister()) {
3005 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003006 __ imull(first.AsRegister<Register>(), second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003007 } else {
3008 DCHECK(second.IsStackSlot());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003009 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003010 __ imull(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01003011 }
3012 break;
Calin Juravle34bacdf2014-10-07 20:23:36 +01003013
3014 case Primitive::kPrimLong: {
Calin Juravle34bacdf2014-10-07 20:23:36 +01003015 Register in1_hi = first.AsRegisterPairHigh<Register>();
3016 Register in1_lo = first.AsRegisterPairLow<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003017 Register eax = locations->GetTemp(0).AsRegister<Register>();
3018 Register edx = locations->GetTemp(1).AsRegister<Register>();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003019
3020 DCHECK_EQ(EAX, eax);
3021 DCHECK_EQ(EDX, edx);
3022
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003023 // input: in1 - 64 bits, in2 - 64 bits.
Calin Juravle34bacdf2014-10-07 20:23:36 +01003024 // output: in1
3025 // formula: in1.hi : in1.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
3026 // parts: in1.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
3027 // parts: in1.lo = (in1.lo * in2.lo)[31:0]
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003028 if (second.IsConstant()) {
3029 DCHECK(second.GetConstant()->IsLongConstant());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003030
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003031 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3032 int32_t low_value = Low32Bits(value);
3033 int32_t high_value = High32Bits(value);
3034 Immediate low(low_value);
3035 Immediate high(high_value);
3036
3037 __ movl(eax, high);
3038 // eax <- in1.lo * in2.hi
3039 __ imull(eax, in1_lo);
3040 // in1.hi <- in1.hi * in2.lo
3041 __ imull(in1_hi, low);
3042 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3043 __ addl(in1_hi, eax);
3044 // move in2_lo to eax to prepare for double precision
3045 __ movl(eax, low);
3046 // edx:eax <- in1.lo * in2.lo
3047 __ mull(in1_lo);
3048 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3049 __ addl(in1_hi, edx);
3050 // in1.lo <- (in1.lo * in2.lo)[31:0];
3051 __ movl(in1_lo, eax);
3052 } else if (second.IsRegisterPair()) {
3053 Register in2_hi = second.AsRegisterPairHigh<Register>();
3054 Register in2_lo = second.AsRegisterPairLow<Register>();
3055
3056 __ movl(eax, in2_hi);
3057 // eax <- in1.lo * in2.hi
3058 __ imull(eax, in1_lo);
3059 // in1.hi <- in1.hi * in2.lo
3060 __ imull(in1_hi, in2_lo);
3061 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3062 __ addl(in1_hi, eax);
3063 // move in1_lo to eax to prepare for double precision
3064 __ movl(eax, in1_lo);
3065 // edx:eax <- in1.lo * in2.lo
3066 __ mull(in2_lo);
3067 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3068 __ addl(in1_hi, edx);
3069 // in1.lo <- (in1.lo * in2.lo)[31:0];
3070 __ movl(in1_lo, eax);
3071 } else {
3072 DCHECK(second.IsDoubleStackSlot()) << second;
3073 Address in2_hi(ESP, second.GetHighStackIndex(kX86WordSize));
3074 Address in2_lo(ESP, second.GetStackIndex());
3075
3076 __ movl(eax, in2_hi);
3077 // eax <- in1.lo * in2.hi
3078 __ imull(eax, in1_lo);
3079 // in1.hi <- in1.hi * in2.lo
3080 __ imull(in1_hi, in2_lo);
3081 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3082 __ addl(in1_hi, eax);
3083 // move in1_lo to eax to prepare for double precision
3084 __ movl(eax, in1_lo);
3085 // edx:eax <- in1.lo * in2.lo
3086 __ mull(in2_lo);
3087 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3088 __ addl(in1_hi, edx);
3089 // in1.lo <- (in1.lo * in2.lo)[31:0];
3090 __ movl(in1_lo, eax);
3091 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003092
3093 break;
3094 }
3095
Calin Juravleb5bfa962014-10-21 18:02:24 +01003096 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003097 DCHECK(first.Equals(locations->Out()));
3098 if (second.IsFpuRegister()) {
3099 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3100 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3101 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003102 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003103 __ mulss(first.AsFpuRegister<XmmRegister>(),
3104 codegen_->LiteralFloatAddress(
3105 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3106 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3107 } else {
3108 DCHECK(second.IsStackSlot());
3109 __ mulss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3110 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003111 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003112 }
3113
3114 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003115 DCHECK(first.Equals(locations->Out()));
3116 if (second.IsFpuRegister()) {
3117 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3118 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3119 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003120 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003121 __ mulsd(first.AsFpuRegister<XmmRegister>(),
3122 codegen_->LiteralDoubleAddress(
3123 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3124 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3125 } else {
3126 DCHECK(second.IsDoubleStackSlot());
3127 __ mulsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3128 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003129 break;
3130 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003131
3132 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003133 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003134 }
3135}
3136
Roland Levillain232ade02015-04-20 15:14:36 +01003137void InstructionCodeGeneratorX86::PushOntoFPStack(Location source,
3138 uint32_t temp_offset,
3139 uint32_t stack_adjustment,
3140 bool is_fp,
3141 bool is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003142 if (source.IsStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01003143 DCHECK(!is_wide);
3144 if (is_fp) {
3145 __ flds(Address(ESP, source.GetStackIndex() + stack_adjustment));
3146 } else {
3147 __ filds(Address(ESP, source.GetStackIndex() + stack_adjustment));
3148 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003149 } else if (source.IsDoubleStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01003150 DCHECK(is_wide);
3151 if (is_fp) {
3152 __ fldl(Address(ESP, source.GetStackIndex() + stack_adjustment));
3153 } else {
3154 __ fildl(Address(ESP, source.GetStackIndex() + stack_adjustment));
3155 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003156 } else {
3157 // Write the value to the temporary location on the stack and load to FP stack.
Roland Levillain232ade02015-04-20 15:14:36 +01003158 if (!is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003159 Location stack_temp = Location::StackSlot(temp_offset);
3160 codegen_->Move32(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01003161 if (is_fp) {
3162 __ flds(Address(ESP, temp_offset));
3163 } else {
3164 __ filds(Address(ESP, temp_offset));
3165 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003166 } else {
3167 Location stack_temp = Location::DoubleStackSlot(temp_offset);
3168 codegen_->Move64(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01003169 if (is_fp) {
3170 __ fldl(Address(ESP, temp_offset));
3171 } else {
3172 __ fildl(Address(ESP, temp_offset));
3173 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003174 }
3175 }
3176}
3177
3178void InstructionCodeGeneratorX86::GenerateRemFP(HRem *rem) {
3179 Primitive::Type type = rem->GetResultType();
3180 bool is_float = type == Primitive::kPrimFloat;
3181 size_t elem_size = Primitive::ComponentSize(type);
3182 LocationSummary* locations = rem->GetLocations();
3183 Location first = locations->InAt(0);
3184 Location second = locations->InAt(1);
3185 Location out = locations->Out();
3186
3187 // Create stack space for 2 elements.
3188 // TODO: enhance register allocator to ask for stack temporaries.
3189 __ subl(ESP, Immediate(2 * elem_size));
3190
3191 // Load the values to the FP stack in reverse order, using temporaries if needed.
Roland Levillain232ade02015-04-20 15:14:36 +01003192 const bool is_wide = !is_float;
3193 PushOntoFPStack(second, elem_size, 2 * elem_size, /* is_fp */ true, is_wide);
3194 PushOntoFPStack(first, 0, 2 * elem_size, /* is_fp */ true, is_wide);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003195
3196 // Loop doing FPREM until we stabilize.
Mark Mendell0c9497d2015-08-21 09:30:05 -04003197 NearLabel retry;
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003198 __ Bind(&retry);
3199 __ fprem();
3200
3201 // Move FP status to AX.
3202 __ fstsw();
3203
3204 // And see if the argument reduction is complete. This is signaled by the
3205 // C2 FPU flag bit set to 0.
3206 __ andl(EAX, Immediate(kC2ConditionMask));
3207 __ j(kNotEqual, &retry);
3208
3209 // We have settled on the final value. Retrieve it into an XMM register.
3210 // Store FP top of stack to real stack.
3211 if (is_float) {
3212 __ fsts(Address(ESP, 0));
3213 } else {
3214 __ fstl(Address(ESP, 0));
3215 }
3216
3217 // Pop the 2 items from the FP stack.
3218 __ fucompp();
3219
3220 // Load the value from the stack into an XMM register.
3221 DCHECK(out.IsFpuRegister()) << out;
3222 if (is_float) {
3223 __ movss(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
3224 } else {
3225 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
3226 }
3227
3228 // And remove the temporary stack space we allocated.
3229 __ addl(ESP, Immediate(2 * elem_size));
3230}
3231
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003232
3233void InstructionCodeGeneratorX86::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3234 DCHECK(instruction->IsDiv() || instruction->IsRem());
3235
3236 LocationSummary* locations = instruction->GetLocations();
3237 DCHECK(locations->InAt(1).IsConstant());
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003238 DCHECK(locations->InAt(1).GetConstant()->IsIntConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003239
3240 Register out_register = locations->Out().AsRegister<Register>();
3241 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003242 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003243
3244 DCHECK(imm == 1 || imm == -1);
3245
3246 if (instruction->IsRem()) {
3247 __ xorl(out_register, out_register);
3248 } else {
3249 __ movl(out_register, input_register);
3250 if (imm == -1) {
3251 __ negl(out_register);
3252 }
3253 }
3254}
3255
3256
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003257void InstructionCodeGeneratorX86::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003258 LocationSummary* locations = instruction->GetLocations();
3259
3260 Register out_register = locations->Out().AsRegister<Register>();
3261 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003262 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003263 DCHECK(IsPowerOfTwo(AbsOrMin(imm)));
3264 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003265
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003266 Register num = locations->GetTemp(0).AsRegister<Register>();
3267
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003268 __ leal(num, Address(input_register, abs_imm - 1));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003269 __ testl(input_register, input_register);
3270 __ cmovl(kGreaterEqual, num, input_register);
3271 int shift = CTZ(imm);
3272 __ sarl(num, Immediate(shift));
3273
3274 if (imm < 0) {
3275 __ negl(num);
3276 }
3277
3278 __ movl(out_register, num);
3279}
3280
3281void InstructionCodeGeneratorX86::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3282 DCHECK(instruction->IsDiv() || instruction->IsRem());
3283
3284 LocationSummary* locations = instruction->GetLocations();
3285 int imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
3286
3287 Register eax = locations->InAt(0).AsRegister<Register>();
3288 Register out = locations->Out().AsRegister<Register>();
3289 Register num;
3290 Register edx;
3291
3292 if (instruction->IsDiv()) {
3293 edx = locations->GetTemp(0).AsRegister<Register>();
3294 num = locations->GetTemp(1).AsRegister<Register>();
3295 } else {
3296 edx = locations->Out().AsRegister<Register>();
3297 num = locations->GetTemp(0).AsRegister<Register>();
3298 }
3299
3300 DCHECK_EQ(EAX, eax);
3301 DCHECK_EQ(EDX, edx);
3302 if (instruction->IsDiv()) {
3303 DCHECK_EQ(EAX, out);
3304 } else {
3305 DCHECK_EQ(EDX, out);
3306 }
3307
3308 int64_t magic;
3309 int shift;
3310 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
3311
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003312 // Save the numerator.
3313 __ movl(num, eax);
3314
3315 // EAX = magic
3316 __ movl(eax, Immediate(magic));
3317
3318 // EDX:EAX = magic * numerator
3319 __ imull(num);
3320
3321 if (imm > 0 && magic < 0) {
3322 // EDX += num
3323 __ addl(edx, num);
3324 } else if (imm < 0 && magic > 0) {
3325 __ subl(edx, num);
3326 }
3327
3328 // Shift if needed.
3329 if (shift != 0) {
3330 __ sarl(edx, Immediate(shift));
3331 }
3332
3333 // EDX += 1 if EDX < 0
3334 __ movl(eax, edx);
3335 __ shrl(edx, Immediate(31));
3336 __ addl(edx, eax);
3337
3338 if (instruction->IsRem()) {
3339 __ movl(eax, num);
3340 __ imull(edx, Immediate(imm));
3341 __ subl(eax, edx);
3342 __ movl(edx, eax);
3343 } else {
3344 __ movl(eax, edx);
3345 }
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003346}
3347
Calin Juravlebacfec32014-11-14 15:54:36 +00003348void InstructionCodeGeneratorX86::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3349 DCHECK(instruction->IsDiv() || instruction->IsRem());
3350
3351 LocationSummary* locations = instruction->GetLocations();
3352 Location out = locations->Out();
3353 Location first = locations->InAt(0);
3354 Location second = locations->InAt(1);
3355 bool is_div = instruction->IsDiv();
3356
3357 switch (instruction->GetResultType()) {
3358 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003359 DCHECK_EQ(EAX, first.AsRegister<Register>());
3360 DCHECK_EQ(is_div ? EAX : EDX, out.AsRegister<Register>());
Calin Juravlebacfec32014-11-14 15:54:36 +00003361
Vladimir Marko13c86fd2015-11-11 12:37:46 +00003362 if (second.IsConstant()) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003363 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003364
3365 if (imm == 0) {
3366 // Do not generate anything for 0. DivZeroCheck would forbid any generated code.
3367 } else if (imm == 1 || imm == -1) {
3368 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003369 } else if (is_div && IsPowerOfTwo(AbsOrMin(imm))) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003370 DivByPowerOfTwo(instruction->AsDiv());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003371 } else {
3372 DCHECK(imm <= -2 || imm >= 2);
3373 GenerateDivRemWithAnyConstant(instruction);
3374 }
3375 } else {
David Srbecky9cd6d372016-02-09 15:24:47 +00003376 SlowPathCode* slow_path = new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86(
3377 instruction, out.AsRegister<Register>(), is_div);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003378 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00003379
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003380 Register second_reg = second.AsRegister<Register>();
3381 // 0x80000000/-1 triggers an arithmetic exception!
3382 // Dividing by -1 is actually negation and -0x800000000 = 0x80000000 so
3383 // it's safe to just use negl instead of more complex comparisons.
Calin Juravlebacfec32014-11-14 15:54:36 +00003384
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003385 __ cmpl(second_reg, Immediate(-1));
3386 __ j(kEqual, slow_path->GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +00003387
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003388 // edx:eax <- sign-extended of eax
3389 __ cdq();
3390 // eax = quotient, edx = remainder
3391 __ idivl(second_reg);
3392 __ Bind(slow_path->GetExitLabel());
3393 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003394 break;
3395 }
3396
3397 case Primitive::kPrimLong: {
3398 InvokeRuntimeCallingConvention calling_convention;
3399 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
3400 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
3401 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
3402 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
3403 DCHECK_EQ(EAX, out.AsRegisterPairLow<Register>());
3404 DCHECK_EQ(EDX, out.AsRegisterPairHigh<Register>());
3405
3406 if (is_div) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01003407 codegen_->InvokeRuntime(kQuickLdiv, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003408 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003409 } else {
Serban Constantinescuba45db02016-07-12 22:53:02 +01003410 codegen_->InvokeRuntime(kQuickLmod, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003411 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003412 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003413 break;
3414 }
3415
3416 default:
3417 LOG(FATAL) << "Unexpected type for GenerateDivRemIntegral " << instruction->GetResultType();
3418 }
3419}
3420
Calin Juravle7c4954d2014-10-28 16:57:40 +00003421void LocationsBuilderX86::VisitDiv(HDiv* div) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003422 LocationSummary::CallKind call_kind = (div->GetResultType() == Primitive::kPrimLong)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003423 ? LocationSummary::kCallOnMainOnly
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003424 : LocationSummary::kNoCall;
3425 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
3426
Calin Juravle7c4954d2014-10-28 16:57:40 +00003427 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00003428 case Primitive::kPrimInt: {
3429 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003430 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00003431 locations->SetOut(Location::SameAsFirstInput());
3432 // Intel uses edx:eax as the dividend.
3433 locations->AddTemp(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003434 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3435 // which enforces results to be in EAX and EDX, things are simpler if we use EAX also as
3436 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003437 if (div->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003438 locations->AddTemp(Location::RequiresRegister());
3439 }
Calin Juravled0d48522014-11-04 16:40:20 +00003440 break;
3441 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003442 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003443 InvokeRuntimeCallingConvention calling_convention;
3444 locations->SetInAt(0, Location::RegisterPairLocation(
3445 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3446 locations->SetInAt(1, Location::RegisterPairLocation(
3447 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3448 // Runtime helper puts the result in EAX, EDX.
3449 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Calin Juravle7c4954d2014-10-28 16:57:40 +00003450 break;
3451 }
3452 case Primitive::kPrimFloat:
3453 case Primitive::kPrimDouble: {
3454 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003455 if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3456 DCHECK(div->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003457 } else if (div->InputAt(1)->IsConstant()) {
3458 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003459 } else {
3460 locations->SetInAt(1, Location::Any());
3461 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003462 locations->SetOut(Location::SameAsFirstInput());
3463 break;
3464 }
3465
3466 default:
3467 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3468 }
3469}
3470
3471void InstructionCodeGeneratorX86::VisitDiv(HDiv* div) {
3472 LocationSummary* locations = div->GetLocations();
3473 Location first = locations->InAt(0);
3474 Location second = locations->InAt(1);
Calin Juravle7c4954d2014-10-28 16:57:40 +00003475
3476 switch (div->GetResultType()) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003477 case Primitive::kPrimInt:
Calin Juravle7c4954d2014-10-28 16:57:40 +00003478 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003479 GenerateDivRemIntegral(div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00003480 break;
3481 }
3482
3483 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003484 if (second.IsFpuRegister()) {
3485 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3486 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3487 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003488 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003489 __ divss(first.AsFpuRegister<XmmRegister>(),
3490 codegen_->LiteralFloatAddress(
3491 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3492 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3493 } else {
3494 DCHECK(second.IsStackSlot());
3495 __ divss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3496 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003497 break;
3498 }
3499
3500 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003501 if (second.IsFpuRegister()) {
3502 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3503 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3504 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003505 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003506 __ divsd(first.AsFpuRegister<XmmRegister>(),
3507 codegen_->LiteralDoubleAddress(
3508 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3509 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3510 } else {
3511 DCHECK(second.IsDoubleStackSlot());
3512 __ divsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3513 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003514 break;
3515 }
3516
3517 default:
3518 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3519 }
3520}
3521
Calin Juravlebacfec32014-11-14 15:54:36 +00003522void LocationsBuilderX86::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00003523 Primitive::Type type = rem->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003524
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003525 LocationSummary::CallKind call_kind = (rem->GetResultType() == Primitive::kPrimLong)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003526 ? LocationSummary::kCallOnMainOnly
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003527 : LocationSummary::kNoCall;
3528 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
Calin Juravlebacfec32014-11-14 15:54:36 +00003529
Calin Juravled2ec87d2014-12-08 14:24:46 +00003530 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003531 case Primitive::kPrimInt: {
3532 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003533 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00003534 locations->SetOut(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003535 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3536 // which enforces results to be in EAX and EDX, things are simpler if we use EDX also as
3537 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003538 if (rem->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003539 locations->AddTemp(Location::RequiresRegister());
3540 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003541 break;
3542 }
3543 case Primitive::kPrimLong: {
3544 InvokeRuntimeCallingConvention calling_convention;
3545 locations->SetInAt(0, Location::RegisterPairLocation(
3546 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3547 locations->SetInAt(1, Location::RegisterPairLocation(
3548 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3549 // Runtime helper puts the result in EAX, EDX.
3550 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
3551 break;
3552 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003553 case Primitive::kPrimDouble:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003554 case Primitive::kPrimFloat: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003555 locations->SetInAt(0, Location::Any());
3556 locations->SetInAt(1, Location::Any());
3557 locations->SetOut(Location::RequiresFpuRegister());
3558 locations->AddTemp(Location::RegisterLocation(EAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00003559 break;
3560 }
3561
3562 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003563 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003564 }
3565}
3566
3567void InstructionCodeGeneratorX86::VisitRem(HRem* rem) {
3568 Primitive::Type type = rem->GetResultType();
3569 switch (type) {
3570 case Primitive::kPrimInt:
3571 case Primitive::kPrimLong: {
3572 GenerateDivRemIntegral(rem);
3573 break;
3574 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003575 case Primitive::kPrimFloat:
Calin Juravlebacfec32014-11-14 15:54:36 +00003576 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003577 GenerateRemFP(rem);
Calin Juravlebacfec32014-11-14 15:54:36 +00003578 break;
3579 }
3580 default:
3581 LOG(FATAL) << "Unexpected rem type " << type;
3582 }
3583}
3584
Calin Juravled0d48522014-11-04 16:40:20 +00003585void LocationsBuilderX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003586 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003587 switch (instruction->GetType()) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003588 case Primitive::kPrimBoolean:
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003589 case Primitive::kPrimByte:
3590 case Primitive::kPrimChar:
3591 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003592 case Primitive::kPrimInt: {
3593 locations->SetInAt(0, Location::Any());
3594 break;
3595 }
3596 case Primitive::kPrimLong: {
3597 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
3598 if (!instruction->IsConstant()) {
3599 locations->AddTemp(Location::RequiresRegister());
3600 }
3601 break;
3602 }
3603 default:
3604 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
3605 }
Calin Juravled0d48522014-11-04 16:40:20 +00003606}
3607
3608void InstructionCodeGeneratorX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003609 SlowPathCode* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86(instruction);
Calin Juravled0d48522014-11-04 16:40:20 +00003610 codegen_->AddSlowPath(slow_path);
3611
3612 LocationSummary* locations = instruction->GetLocations();
3613 Location value = locations->InAt(0);
3614
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003615 switch (instruction->GetType()) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003616 case Primitive::kPrimBoolean:
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003617 case Primitive::kPrimByte:
3618 case Primitive::kPrimChar:
3619 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003620 case Primitive::kPrimInt: {
3621 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003622 __ testl(value.AsRegister<Register>(), value.AsRegister<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003623 __ j(kEqual, slow_path->GetEntryLabel());
3624 } else if (value.IsStackSlot()) {
3625 __ cmpl(Address(ESP, value.GetStackIndex()), Immediate(0));
3626 __ j(kEqual, slow_path->GetEntryLabel());
3627 } else {
3628 DCHECK(value.IsConstant()) << value;
3629 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01003630 __ jmp(slow_path->GetEntryLabel());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003631 }
3632 }
3633 break;
Calin Juravled0d48522014-11-04 16:40:20 +00003634 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003635 case Primitive::kPrimLong: {
3636 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003637 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003638 __ movl(temp, value.AsRegisterPairLow<Register>());
3639 __ orl(temp, value.AsRegisterPairHigh<Register>());
3640 __ j(kEqual, slow_path->GetEntryLabel());
3641 } else {
3642 DCHECK(value.IsConstant()) << value;
3643 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
3644 __ jmp(slow_path->GetEntryLabel());
3645 }
3646 }
3647 break;
3648 }
3649 default:
3650 LOG(FATAL) << "Unexpected type for HDivZeroCheck" << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00003651 }
Calin Juravled0d48522014-11-04 16:40:20 +00003652}
3653
Calin Juravle9aec02f2014-11-18 23:06:35 +00003654void LocationsBuilderX86::HandleShift(HBinaryOperation* op) {
3655 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3656
3657 LocationSummary* locations =
3658 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
3659
3660 switch (op->GetResultType()) {
Mark P Mendell73945692015-04-29 14:56:17 +00003661 case Primitive::kPrimInt:
Calin Juravle9aec02f2014-11-18 23:06:35 +00003662 case Primitive::kPrimLong: {
Mark P Mendell73945692015-04-29 14:56:17 +00003663 // Can't have Location::Any() and output SameAsFirstInput()
Calin Juravle9aec02f2014-11-18 23:06:35 +00003664 locations->SetInAt(0, Location::RequiresRegister());
Mark P Mendell73945692015-04-29 14:56:17 +00003665 // The shift count needs to be in CL or a constant.
3666 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, op->InputAt(1)));
Calin Juravle9aec02f2014-11-18 23:06:35 +00003667 locations->SetOut(Location::SameAsFirstInput());
3668 break;
3669 }
3670 default:
3671 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
3672 }
3673}
3674
3675void InstructionCodeGeneratorX86::HandleShift(HBinaryOperation* op) {
3676 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3677
3678 LocationSummary* locations = op->GetLocations();
3679 Location first = locations->InAt(0);
3680 Location second = locations->InAt(1);
3681 DCHECK(first.Equals(locations->Out()));
3682
3683 switch (op->GetResultType()) {
3684 case Primitive::kPrimInt: {
Mark P Mendell73945692015-04-29 14:56:17 +00003685 DCHECK(first.IsRegister());
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003686 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003687 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003688 Register second_reg = second.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003689 DCHECK_EQ(ECX, second_reg);
3690 if (op->IsShl()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003691 __ shll(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003692 } else if (op->IsShr()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003693 __ sarl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003694 } else {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003695 __ shrl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003696 }
3697 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003698 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance;
Mark P Mendell73945692015-04-29 14:56:17 +00003699 if (shift == 0) {
3700 return;
3701 }
3702 Immediate imm(shift);
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003703 if (op->IsShl()) {
3704 __ shll(first_reg, imm);
3705 } else if (op->IsShr()) {
3706 __ sarl(first_reg, imm);
3707 } else {
3708 __ shrl(first_reg, imm);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003709 }
3710 }
3711 break;
3712 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003713 case Primitive::kPrimLong: {
Mark P Mendell73945692015-04-29 14:56:17 +00003714 if (second.IsRegister()) {
3715 Register second_reg = second.AsRegister<Register>();
3716 DCHECK_EQ(ECX, second_reg);
3717 if (op->IsShl()) {
3718 GenerateShlLong(first, second_reg);
3719 } else if (op->IsShr()) {
3720 GenerateShrLong(first, second_reg);
3721 } else {
3722 GenerateUShrLong(first, second_reg);
3723 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003724 } else {
Mark P Mendell73945692015-04-29 14:56:17 +00003725 // Shift by a constant.
Roland Levillain5b5b9312016-03-22 14:57:31 +00003726 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance;
Mark P Mendell73945692015-04-29 14:56:17 +00003727 // Nothing to do if the shift is 0, as the input is already the output.
3728 if (shift != 0) {
3729 if (op->IsShl()) {
3730 GenerateShlLong(first, shift);
3731 } else if (op->IsShr()) {
3732 GenerateShrLong(first, shift);
3733 } else {
3734 GenerateUShrLong(first, shift);
3735 }
3736 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003737 }
3738 break;
3739 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003740 default:
3741 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
3742 }
3743}
3744
Mark P Mendell73945692015-04-29 14:56:17 +00003745void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, int shift) {
3746 Register low = loc.AsRegisterPairLow<Register>();
3747 Register high = loc.AsRegisterPairHigh<Register>();
Mark Mendellba56d062015-05-05 21:34:03 -04003748 if (shift == 1) {
3749 // This is just an addition.
3750 __ addl(low, low);
3751 __ adcl(high, high);
3752 } else if (shift == 32) {
Mark P Mendell73945692015-04-29 14:56:17 +00003753 // Shift by 32 is easy. High gets low, and low gets 0.
3754 codegen_->EmitParallelMoves(
3755 loc.ToLow(),
3756 loc.ToHigh(),
3757 Primitive::kPrimInt,
3758 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
3759 loc.ToLow(),
3760 Primitive::kPrimInt);
3761 } else if (shift > 32) {
3762 // Low part becomes 0. High part is low part << (shift-32).
3763 __ movl(high, low);
3764 __ shll(high, Immediate(shift - 32));
3765 __ xorl(low, low);
3766 } else {
3767 // Between 1 and 31.
3768 __ shld(high, low, Immediate(shift));
3769 __ shll(low, Immediate(shift));
3770 }
3771}
3772
Calin Juravle9aec02f2014-11-18 23:06:35 +00003773void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04003774 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00003775 __ shld(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>(), shifter);
3776 __ shll(loc.AsRegisterPairLow<Register>(), shifter);
3777 __ testl(shifter, Immediate(32));
3778 __ j(kEqual, &done);
3779 __ movl(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>());
3780 __ movl(loc.AsRegisterPairLow<Register>(), Immediate(0));
3781 __ Bind(&done);
3782}
3783
Mark P Mendell73945692015-04-29 14:56:17 +00003784void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, int shift) {
3785 Register low = loc.AsRegisterPairLow<Register>();
3786 Register high = loc.AsRegisterPairHigh<Register>();
3787 if (shift == 32) {
3788 // Need to copy the sign.
3789 DCHECK_NE(low, high);
3790 __ movl(low, high);
3791 __ sarl(high, Immediate(31));
3792 } else if (shift > 32) {
3793 DCHECK_NE(low, high);
3794 // High part becomes sign. Low part is shifted by shift - 32.
3795 __ movl(low, high);
3796 __ sarl(high, Immediate(31));
3797 __ sarl(low, Immediate(shift - 32));
3798 } else {
3799 // Between 1 and 31.
3800 __ shrd(low, high, Immediate(shift));
3801 __ sarl(high, Immediate(shift));
3802 }
3803}
3804
Calin Juravle9aec02f2014-11-18 23:06:35 +00003805void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04003806 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00003807 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
3808 __ sarl(loc.AsRegisterPairHigh<Register>(), shifter);
3809 __ testl(shifter, Immediate(32));
3810 __ j(kEqual, &done);
3811 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
3812 __ sarl(loc.AsRegisterPairHigh<Register>(), Immediate(31));
3813 __ Bind(&done);
3814}
3815
Mark P Mendell73945692015-04-29 14:56:17 +00003816void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, int shift) {
3817 Register low = loc.AsRegisterPairLow<Register>();
3818 Register high = loc.AsRegisterPairHigh<Register>();
3819 if (shift == 32) {
3820 // Shift by 32 is easy. Low gets high, and high gets 0.
3821 codegen_->EmitParallelMoves(
3822 loc.ToHigh(),
3823 loc.ToLow(),
3824 Primitive::kPrimInt,
3825 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
3826 loc.ToHigh(),
3827 Primitive::kPrimInt);
3828 } else if (shift > 32) {
3829 // Low part is high >> (shift - 32). High part becomes 0.
3830 __ movl(low, high);
3831 __ shrl(low, Immediate(shift - 32));
3832 __ xorl(high, high);
3833 } else {
3834 // Between 1 and 31.
3835 __ shrd(low, high, Immediate(shift));
3836 __ shrl(high, Immediate(shift));
3837 }
3838}
3839
Calin Juravle9aec02f2014-11-18 23:06:35 +00003840void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04003841 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00003842 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
3843 __ shrl(loc.AsRegisterPairHigh<Register>(), shifter);
3844 __ testl(shifter, Immediate(32));
3845 __ j(kEqual, &done);
3846 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
3847 __ movl(loc.AsRegisterPairHigh<Register>(), Immediate(0));
3848 __ Bind(&done);
3849}
3850
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003851void LocationsBuilderX86::VisitRor(HRor* ror) {
3852 LocationSummary* locations =
3853 new (GetGraph()->GetArena()) LocationSummary(ror, LocationSummary::kNoCall);
3854
3855 switch (ror->GetResultType()) {
3856 case Primitive::kPrimLong:
3857 // Add the temporary needed.
3858 locations->AddTemp(Location::RequiresRegister());
3859 FALLTHROUGH_INTENDED;
3860 case Primitive::kPrimInt:
3861 locations->SetInAt(0, Location::RequiresRegister());
3862 // The shift count needs to be in CL (unless it is a constant).
3863 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, ror->InputAt(1)));
3864 locations->SetOut(Location::SameAsFirstInput());
3865 break;
3866 default:
3867 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
3868 UNREACHABLE();
3869 }
3870}
3871
3872void InstructionCodeGeneratorX86::VisitRor(HRor* ror) {
3873 LocationSummary* locations = ror->GetLocations();
3874 Location first = locations->InAt(0);
3875 Location second = locations->InAt(1);
3876
3877 if (ror->GetResultType() == Primitive::kPrimInt) {
3878 Register first_reg = first.AsRegister<Register>();
3879 if (second.IsRegister()) {
3880 Register second_reg = second.AsRegister<Register>();
3881 __ rorl(first_reg, second_reg);
3882 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003883 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003884 __ rorl(first_reg, imm);
3885 }
3886 return;
3887 }
3888
3889 DCHECK_EQ(ror->GetResultType(), Primitive::kPrimLong);
3890 Register first_reg_lo = first.AsRegisterPairLow<Register>();
3891 Register first_reg_hi = first.AsRegisterPairHigh<Register>();
3892 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
3893 if (second.IsRegister()) {
3894 Register second_reg = second.AsRegister<Register>();
3895 DCHECK_EQ(second_reg, ECX);
3896 __ movl(temp_reg, first_reg_hi);
3897 __ shrd(first_reg_hi, first_reg_lo, second_reg);
3898 __ shrd(first_reg_lo, temp_reg, second_reg);
3899 __ movl(temp_reg, first_reg_hi);
3900 __ testl(second_reg, Immediate(32));
3901 __ cmovl(kNotEqual, first_reg_hi, first_reg_lo);
3902 __ cmovl(kNotEqual, first_reg_lo, temp_reg);
3903 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003904 int32_t shift_amt = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance;
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003905 if (shift_amt == 0) {
3906 // Already fine.
3907 return;
3908 }
3909 if (shift_amt == 32) {
3910 // Just swap.
3911 __ movl(temp_reg, first_reg_lo);
3912 __ movl(first_reg_lo, first_reg_hi);
3913 __ movl(first_reg_hi, temp_reg);
3914 return;
3915 }
3916
3917 Immediate imm(shift_amt);
3918 // Save the constents of the low value.
3919 __ movl(temp_reg, first_reg_lo);
3920
3921 // Shift right into low, feeding bits from high.
3922 __ shrd(first_reg_lo, first_reg_hi, imm);
3923
3924 // Shift right into high, feeding bits from the original low.
3925 __ shrd(first_reg_hi, temp_reg, imm);
3926
3927 // Swap if needed.
3928 if (shift_amt > 32) {
3929 __ movl(temp_reg, first_reg_lo);
3930 __ movl(first_reg_lo, first_reg_hi);
3931 __ movl(first_reg_hi, temp_reg);
3932 }
3933 }
3934}
3935
Calin Juravle9aec02f2014-11-18 23:06:35 +00003936void LocationsBuilderX86::VisitShl(HShl* shl) {
3937 HandleShift(shl);
3938}
3939
3940void InstructionCodeGeneratorX86::VisitShl(HShl* shl) {
3941 HandleShift(shl);
3942}
3943
3944void LocationsBuilderX86::VisitShr(HShr* shr) {
3945 HandleShift(shr);
3946}
3947
3948void InstructionCodeGeneratorX86::VisitShr(HShr* shr) {
3949 HandleShift(shr);
3950}
3951
3952void LocationsBuilderX86::VisitUShr(HUShr* ushr) {
3953 HandleShift(ushr);
3954}
3955
3956void InstructionCodeGeneratorX86::VisitUShr(HUShr* ushr) {
3957 HandleShift(ushr);
3958}
3959
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003960void LocationsBuilderX86::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003961 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003962 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003963 locations->SetOut(Location::RegisterLocation(EAX));
David Brazdil6de19382016-01-08 17:37:10 +00003964 if (instruction->IsStringAlloc()) {
3965 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
3966 } else {
3967 InvokeRuntimeCallingConvention calling_convention;
3968 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3969 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3970 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003971}
3972
3973void InstructionCodeGeneratorX86::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01003974 // Note: if heap poisoning is enabled, the entry point takes cares
3975 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00003976 if (instruction->IsStringAlloc()) {
3977 // String is allocated through StringFactory. Call NewEmptyString entry point.
3978 Register temp = instruction->GetLocations()->GetTemp(0).AsRegister<Register>();
Andreas Gampe542451c2016-07-26 09:02:02 -07003979 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86PointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00003980 __ fs()->movl(temp, Address::Absolute(QUICK_ENTRY_POINT(pNewEmptyString)));
3981 __ call(Address(temp, code_offset.Int32Value()));
3982 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3983 } else {
Serban Constantinescuba45db02016-07-12 22:53:02 +01003984 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
David Brazdil6de19382016-01-08 17:37:10 +00003985 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
3986 DCHECK(!codegen_->IsLeafMethod());
3987 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003988}
3989
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003990void LocationsBuilderX86::VisitNewArray(HNewArray* instruction) {
3991 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003992 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003993 locations->SetOut(Location::RegisterLocation(EAX));
3994 InvokeRuntimeCallingConvention calling_convention;
3995 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003996 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003997 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003998}
3999
4000void InstructionCodeGeneratorX86::VisitNewArray(HNewArray* instruction) {
4001 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004002 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
Roland Levillain4d027112015-07-01 15:41:14 +01004003 // Note: if heap poisoning is enabled, the entry point takes cares
4004 // of poisoning the reference.
Serban Constantinescuba45db02016-07-12 22:53:02 +01004005 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00004006 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004007 DCHECK(!codegen_->IsLeafMethod());
4008}
4009
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004010void LocationsBuilderX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004011 LocationSummary* locations =
4012 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01004013 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
4014 if (location.IsStackSlot()) {
4015 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4016 } else if (location.IsDoubleStackSlot()) {
4017 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004018 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01004019 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004020}
4021
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004022void InstructionCodeGeneratorX86::VisitParameterValue(
4023 HParameterValue* instruction ATTRIBUTE_UNUSED) {
4024}
4025
4026void LocationsBuilderX86::VisitCurrentMethod(HCurrentMethod* instruction) {
4027 LocationSummary* locations =
4028 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4029 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
4030}
4031
4032void InstructionCodeGeneratorX86::VisitCurrentMethod(HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004033}
4034
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004035void LocationsBuilderX86::VisitClassTableGet(HClassTableGet* instruction) {
4036 LocationSummary* locations =
4037 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4038 locations->SetInAt(0, Location::RequiresRegister());
4039 locations->SetOut(Location::RequiresRegister());
4040}
4041
4042void InstructionCodeGeneratorX86::VisitClassTableGet(HClassTableGet* instruction) {
4043 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004044 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004045 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004046 instruction->GetIndex(), kX86PointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004047 __ movl(locations->Out().AsRegister<Register>(),
4048 Address(locations->InAt(0).AsRegister<Register>(), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004049 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004050 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00004051 instruction->GetIndex(), kX86PointerSize));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004052 __ movl(locations->Out().AsRegister<Register>(),
4053 Address(locations->InAt(0).AsRegister<Register>(),
4054 mirror::Class::ImtPtrOffset(kX86PointerSize).Uint32Value()));
4055 // temp = temp->GetImtEntryAt(method_offset);
4056 __ movl(locations->Out().AsRegister<Register>(),
4057 Address(locations->Out().AsRegister<Register>(), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004058 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004059}
4060
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004061void LocationsBuilderX86::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004062 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004063 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01004064 locations->SetInAt(0, Location::RequiresRegister());
4065 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004066}
4067
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004068void InstructionCodeGeneratorX86::VisitNot(HNot* not_) {
4069 LocationSummary* locations = not_->GetLocations();
Roland Levillain70566432014-10-24 16:20:17 +01004070 Location in = locations->InAt(0);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01004071 Location out = locations->Out();
Roland Levillain70566432014-10-24 16:20:17 +01004072 DCHECK(in.Equals(out));
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00004073 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004074 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00004075 __ notl(out.AsRegister<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004076 break;
4077
4078 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01004079 __ notl(out.AsRegisterPairLow<Register>());
4080 __ notl(out.AsRegisterPairHigh<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004081 break;
4082
4083 default:
4084 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
4085 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004086}
4087
David Brazdil66d126e2015-04-03 16:02:44 +01004088void LocationsBuilderX86::VisitBooleanNot(HBooleanNot* bool_not) {
4089 LocationSummary* locations =
4090 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
4091 locations->SetInAt(0, Location::RequiresRegister());
4092 locations->SetOut(Location::SameAsFirstInput());
4093}
4094
4095void InstructionCodeGeneratorX86::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01004096 LocationSummary* locations = bool_not->GetLocations();
4097 Location in = locations->InAt(0);
4098 Location out = locations->Out();
4099 DCHECK(in.Equals(out));
4100 __ xorl(out.AsRegister<Register>(), Immediate(1));
4101}
4102
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004103void LocationsBuilderX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004104 LocationSummary* locations =
4105 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00004106 switch (compare->InputAt(0)->GetType()) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00004107 case Primitive::kPrimBoolean:
4108 case Primitive::kPrimByte:
4109 case Primitive::kPrimShort:
4110 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08004111 case Primitive::kPrimInt:
Calin Juravleddb7df22014-11-25 20:56:51 +00004112 case Primitive::kPrimLong: {
4113 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravleddb7df22014-11-25 20:56:51 +00004114 locations->SetInAt(1, Location::Any());
4115 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4116 break;
4117 }
4118 case Primitive::kPrimFloat:
4119 case Primitive::kPrimDouble: {
4120 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004121 if (compare->InputAt(1)->IsX86LoadFromConstantTable()) {
4122 DCHECK(compare->InputAt(1)->IsEmittedAtUseSite());
4123 } else if (compare->InputAt(1)->IsConstant()) {
4124 locations->SetInAt(1, Location::RequiresFpuRegister());
4125 } else {
4126 locations->SetInAt(1, Location::Any());
4127 }
Calin Juravleddb7df22014-11-25 20:56:51 +00004128 locations->SetOut(Location::RequiresRegister());
4129 break;
4130 }
4131 default:
4132 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
4133 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004134}
4135
4136void InstructionCodeGeneratorX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004137 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004138 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00004139 Location left = locations->InAt(0);
4140 Location right = locations->InAt(1);
4141
Mark Mendell0c9497d2015-08-21 09:30:05 -04004142 NearLabel less, greater, done;
Aart Bika19616e2016-02-01 18:57:58 -08004143 Condition less_cond = kLess;
4144
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004145 switch (compare->InputAt(0)->GetType()) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00004146 case Primitive::kPrimBoolean:
4147 case Primitive::kPrimByte:
4148 case Primitive::kPrimShort:
4149 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08004150 case Primitive::kPrimInt: {
Roland Levillain0b671c02016-08-19 12:02:34 +01004151 codegen_->GenerateIntCompare(left, right);
Aart Bika19616e2016-02-01 18:57:58 -08004152 break;
4153 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004154 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004155 Register left_low = left.AsRegisterPairLow<Register>();
4156 Register left_high = left.AsRegisterPairHigh<Register>();
4157 int32_t val_low = 0;
4158 int32_t val_high = 0;
4159 bool right_is_const = false;
4160
4161 if (right.IsConstant()) {
4162 DCHECK(right.GetConstant()->IsLongConstant());
4163 right_is_const = true;
4164 int64_t val = right.GetConstant()->AsLongConstant()->GetValue();
4165 val_low = Low32Bits(val);
4166 val_high = High32Bits(val);
4167 }
4168
Calin Juravleddb7df22014-11-25 20:56:51 +00004169 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004170 __ cmpl(left_high, right.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004171 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004172 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004173 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004174 DCHECK(right_is_const) << right;
Aart Bika19616e2016-02-01 18:57:58 -08004175 codegen_->Compare32BitValue(left_high, val_high);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004176 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004177 __ j(kLess, &less); // Signed compare.
4178 __ j(kGreater, &greater); // Signed compare.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004179 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004180 __ cmpl(left_low, right.AsRegisterPairLow<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004181 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004182 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004183 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004184 DCHECK(right_is_const) << right;
Aart Bika19616e2016-02-01 18:57:58 -08004185 codegen_->Compare32BitValue(left_low, val_low);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004186 }
Aart Bika19616e2016-02-01 18:57:58 -08004187 less_cond = kBelow; // for CF (unsigned).
Calin Juravleddb7df22014-11-25 20:56:51 +00004188 break;
4189 }
4190 case Primitive::kPrimFloat: {
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004191 GenerateFPCompare(left, right, compare, false);
Calin Juravleddb7df22014-11-25 20:56:51 +00004192 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08004193 less_cond = kBelow; // for CF (floats).
Calin Juravleddb7df22014-11-25 20:56:51 +00004194 break;
4195 }
4196 case Primitive::kPrimDouble: {
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004197 GenerateFPCompare(left, right, compare, true);
Calin Juravleddb7df22014-11-25 20:56:51 +00004198 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08004199 less_cond = kBelow; // for CF (floats).
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004200 break;
4201 }
4202 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00004203 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004204 }
Aart Bika19616e2016-02-01 18:57:58 -08004205
Calin Juravleddb7df22014-11-25 20:56:51 +00004206 __ movl(out, Immediate(0));
4207 __ j(kEqual, &done);
Aart Bika19616e2016-02-01 18:57:58 -08004208 __ j(less_cond, &less);
Calin Juravleddb7df22014-11-25 20:56:51 +00004209
4210 __ Bind(&greater);
4211 __ movl(out, Immediate(1));
4212 __ jmp(&done);
4213
4214 __ Bind(&less);
4215 __ movl(out, Immediate(-1));
4216
4217 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004218}
4219
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004220void LocationsBuilderX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004221 LocationSummary* locations =
4222 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko372f10e2016-05-17 16:30:10 +01004223 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01004224 locations->SetInAt(i, Location::Any());
4225 }
4226 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004227}
4228
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004229void InstructionCodeGeneratorX86::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004230 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004231}
4232
Roland Levillain7c1559a2015-12-15 10:55:36 +00004233void CodeGeneratorX86::GenerateMemoryBarrier(MemBarrierKind kind) {
Calin Juravle52c48962014-12-16 17:02:57 +00004234 /*
4235 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
4236 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
4237 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
4238 */
4239 switch (kind) {
4240 case MemBarrierKind::kAnyAny: {
Mark P Mendell17077d82015-12-16 19:15:59 +00004241 MemoryFence();
Calin Juravle52c48962014-12-16 17:02:57 +00004242 break;
4243 }
4244 case MemBarrierKind::kAnyStore:
4245 case MemBarrierKind::kLoadAny:
4246 case MemBarrierKind::kStoreStore: {
4247 // nop
4248 break;
4249 }
Mark Mendell7aa04a12016-01-27 22:39:07 -05004250 case MemBarrierKind::kNTStoreStore:
4251 // Non-Temporal Store/Store needs an explicit fence.
4252 MemoryFence(/* non-temporal */ true);
4253 break;
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004254 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004255}
4256
Vladimir Markodc151b22015-10-15 18:02:30 +01004257HInvokeStaticOrDirect::DispatchInfo CodeGeneratorX86::GetSupportedInvokeStaticOrDirectDispatch(
4258 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004259 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00004260 HInvokeStaticOrDirect::DispatchInfo dispatch_info = desired_dispatch_info;
4261
4262 // We disable pc-relative load when there is an irreducible loop, as the optimization
4263 // is incompatible with it.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004264 // TODO: Create as many X86ComputeBaseMethodAddress instructions
4265 // as needed for methods with irreducible loops.
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00004266 if (GetGraph()->HasIrreducibleLoops() &&
4267 (dispatch_info.method_load_kind ==
4268 HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative)) {
4269 dispatch_info.method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod;
4270 }
4271 switch (dispatch_info.code_ptr_location) {
Vladimir Markodc151b22015-10-15 18:02:30 +01004272 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
4273 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
4274 // For direct code, we actually prefer to call via the code pointer from ArtMethod*.
4275 // (Though the direct CALL ptr16:32 is available for consideration).
4276 return HInvokeStaticOrDirect::DispatchInfo {
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00004277 dispatch_info.method_load_kind,
Vladimir Markodc151b22015-10-15 18:02:30 +01004278 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00004279 dispatch_info.method_load_data,
Vladimir Markodc151b22015-10-15 18:02:30 +01004280 0u
4281 };
4282 default:
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00004283 return dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01004284 }
4285}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004286
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004287Register CodeGeneratorX86::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
4288 Register temp) {
4289 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
Vladimir Markoc53c0792015-11-19 15:48:33 +00004290 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004291 if (!invoke->GetLocations()->Intrinsified()) {
4292 return location.AsRegister<Register>();
4293 }
4294 // For intrinsics we allow any location, so it may be on the stack.
4295 if (!location.IsRegister()) {
4296 __ movl(temp, Address(ESP, location.GetStackIndex()));
4297 return temp;
4298 }
4299 // For register locations, check if the register was saved. If so, get it from the stack.
4300 // Note: There is a chance that the register was saved but not overwritten, so we could
4301 // save one load. However, since this is just an intrinsic slow path we prefer this
4302 // simple and more robust approach rather that trying to determine if that's the case.
4303 SlowPathCode* slow_path = GetCurrentSlowPath();
Serguei Katkov288c7a82016-05-16 11:53:15 +06004304 if (slow_path != nullptr) {
4305 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
4306 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
4307 __ movl(temp, Address(ESP, stack_offset));
4308 return temp;
4309 }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004310 }
4311 return location.AsRegister<Register>();
4312}
4313
Serguei Katkov288c7a82016-05-16 11:53:15 +06004314Location CodeGeneratorX86::GenerateCalleeMethodStaticOrDirectCall(HInvokeStaticOrDirect* invoke,
4315 Location temp) {
Vladimir Marko58155012015-08-19 12:49:41 +00004316 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
4317 switch (invoke->GetMethodLoadKind()) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004318 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
Vladimir Marko58155012015-08-19 12:49:41 +00004319 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004320 uint32_t offset =
4321 GetThreadOffset<kX86PointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
4322 __ fs()->movl(temp.AsRegister<Register>(), Address::Absolute(offset));
Vladimir Marko58155012015-08-19 12:49:41 +00004323 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004324 }
Vladimir Marko58155012015-08-19 12:49:41 +00004325 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00004326 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004327 break;
4328 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
4329 __ movl(temp.AsRegister<Register>(), Immediate(invoke->GetMethodAddress()));
4330 break;
4331 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004332 __ movl(temp.AsRegister<Register>(), Immediate(/* placeholder */ 0));
Vladimir Markoaad75c62016-10-03 08:46:48 +00004333 method_patches_.emplace_back(*invoke->GetTargetMethod().dex_file,
4334 invoke->GetTargetMethod().dex_method_index);
Vladimir Marko58155012015-08-19 12:49:41 +00004335 __ Bind(&method_patches_.back().label); // Bind the label at the end of the "movl" insn.
4336 break;
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004337 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
4338 Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
4339 temp.AsRegister<Register>());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004340 __ movl(temp.AsRegister<Register>(), Address(base_reg, kDummy32BitOffset));
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004341 // Bind a new fixup label at the end of the "movl" insn.
4342 uint32_t offset = invoke->GetDexCacheArrayOffset();
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004343 __ Bind(NewPcRelativeDexCacheArrayPatch(invoke->GetDexFile(), offset));
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004344 break;
4345 }
Vladimir Marko58155012015-08-19 12:49:41 +00004346 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004347 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004348 Register method_reg;
4349 Register reg = temp.AsRegister<Register>();
4350 if (current_method.IsRegister()) {
4351 method_reg = current_method.AsRegister<Register>();
4352 } else {
David Brazdil58282f42016-01-14 12:45:10 +00004353 DCHECK(invoke->GetLocations()->Intrinsified());
Vladimir Marko58155012015-08-19 12:49:41 +00004354 DCHECK(!current_method.IsValid());
4355 method_reg = reg;
4356 __ movl(reg, Address(ESP, kCurrentMethodStackOffset));
4357 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00004358 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +01004359 __ movl(reg, Address(method_reg,
4360 ArtMethod::DexCacheResolvedMethodsOffset(kX86PointerSize).Int32Value()));
Vladimir Marko40ecb122016-04-06 17:33:41 +01004361 // temp = temp[index_in_cache];
4362 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
4363 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Vladimir Marko58155012015-08-19 12:49:41 +00004364 __ movl(reg, Address(reg, CodeGenerator::GetCachePointerOffset(index_in_cache)));
4365 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +01004366 }
Vladimir Marko58155012015-08-19 12:49:41 +00004367 }
Serguei Katkov288c7a82016-05-16 11:53:15 +06004368 return callee_method;
4369}
4370
4371void CodeGeneratorX86::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
4372 Location callee_method = GenerateCalleeMethodStaticOrDirectCall(invoke, temp);
Vladimir Marko58155012015-08-19 12:49:41 +00004373
4374 switch (invoke->GetCodePtrLocation()) {
4375 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
4376 __ call(GetFrameEntryLabel());
4377 break;
4378 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative: {
Vladimir Markoaad75c62016-10-03 08:46:48 +00004379 relative_call_patches_.emplace_back(*invoke->GetTargetMethod().dex_file,
4380 invoke->GetTargetMethod().dex_method_index);
Vladimir Marko58155012015-08-19 12:49:41 +00004381 Label* label = &relative_call_patches_.back().label;
4382 __ call(label); // Bind to the patch label, override at link time.
4383 __ Bind(label); // Bind the label at the end of the "call" insn.
4384 break;
4385 }
4386 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
4387 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
Vladimir Markodc151b22015-10-15 18:02:30 +01004388 // Filtered out by GetSupportedInvokeStaticOrDirectDispatch().
4389 LOG(FATAL) << "Unsupported";
4390 UNREACHABLE();
Vladimir Marko58155012015-08-19 12:49:41 +00004391 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
4392 // (callee_method + offset_of_quick_compiled_code)()
4393 __ call(Address(callee_method.AsRegister<Register>(),
4394 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07004395 kX86PointerSize).Int32Value()));
Vladimir Marko58155012015-08-19 12:49:41 +00004396 break;
Mark Mendell09ed1a32015-03-25 08:30:06 -04004397 }
4398
4399 DCHECK(!IsLeafMethod());
Mark Mendell09ed1a32015-03-25 08:30:06 -04004400}
4401
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004402void CodeGeneratorX86::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_in) {
4403 Register temp = temp_in.AsRegister<Register>();
4404 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
4405 invoke->GetVTableIndex(), kX86PointerSize).Uint32Value();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004406
4407 // Use the calling convention instead of the location of the receiver, as
4408 // intrinsics may have put the receiver in a different register. In the intrinsics
4409 // slow path, the arguments have been moved to the right place, so here we are
4410 // guaranteed that the receiver is the first register of the calling convention.
4411 InvokeDexCallingConvention calling_convention;
4412 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004413 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004414 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004415 __ movl(temp, Address(receiver, class_offset));
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004416 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00004417 // Instead of simply (possibly) unpoisoning `temp` here, we should
4418 // emit a read barrier for the previous class reference load.
4419 // However this is not required in practice, as this is an
4420 // intermediate/temporary reference and because the current
4421 // concurrent copying collector keeps the from-space memory
4422 // intact/accessible until the end of the marking phase (the
4423 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004424 __ MaybeUnpoisonHeapReference(temp);
4425 // temp = temp->GetMethodAt(method_offset);
4426 __ movl(temp, Address(temp, method_offset));
4427 // call temp->GetEntryPoint();
4428 __ call(Address(
Andreas Gampe542451c2016-07-26 09:02:02 -07004429 temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86PointerSize).Int32Value()));
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004430}
4431
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004432void CodeGeneratorX86::RecordSimplePatch() {
4433 if (GetCompilerOptions().GetIncludePatchInformation()) {
4434 simple_patches_.emplace_back();
4435 __ Bind(&simple_patches_.back());
4436 }
4437}
4438
Vladimir Markoaad75c62016-10-03 08:46:48 +00004439void CodeGeneratorX86::RecordBootStringPatch(HLoadString* load_string) {
4440 DCHECK(GetCompilerOptions().IsBootImage());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004441 string_patches_.emplace_back(load_string->GetDexFile(), load_string->GetStringIndex());
4442 __ Bind(&string_patches_.back().label);
4443}
4444
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004445void CodeGeneratorX86::RecordTypePatch(HLoadClass* load_class) {
4446 type_patches_.emplace_back(load_class->GetDexFile(), load_class->GetTypeIndex());
4447 __ Bind(&type_patches_.back().label);
4448}
4449
Vladimir Markoaad75c62016-10-03 08:46:48 +00004450Label* CodeGeneratorX86::NewStringBssEntryPatch(HLoadString* load_string) {
4451 DCHECK(!GetCompilerOptions().IsBootImage());
4452 string_patches_.emplace_back(load_string->GetDexFile(), load_string->GetStringIndex());
4453 return &string_patches_.back().label;
4454}
4455
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004456Label* CodeGeneratorX86::NewPcRelativeDexCacheArrayPatch(const DexFile& dex_file,
4457 uint32_t element_offset) {
4458 // Add the patch entry and bind its label at the end of the instruction.
4459 pc_relative_dex_cache_patches_.emplace_back(dex_file, element_offset);
4460 return &pc_relative_dex_cache_patches_.back().label;
4461}
4462
Vladimir Markoaad75c62016-10-03 08:46:48 +00004463// The label points to the end of the "movl" or another instruction but the literal offset
4464// for method patch needs to point to the embedded constant which occupies the last 4 bytes.
4465constexpr uint32_t kLabelPositionToLiteralOffsetAdjustment = 4u;
4466
4467template <LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
4468inline void CodeGeneratorX86::EmitPcRelativeLinkerPatches(
4469 const ArenaDeque<PatchInfo<Label>>& infos,
4470 ArenaVector<LinkerPatch>* linker_patches) {
4471 for (const PatchInfo<Label>& info : infos) {
4472 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
4473 linker_patches->push_back(
4474 Factory(literal_offset, &info.dex_file, GetMethodAddressOffset(), info.index));
4475 }
4476}
4477
Vladimir Marko58155012015-08-19 12:49:41 +00004478void CodeGeneratorX86::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
4479 DCHECK(linker_patches->empty());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004480 size_t size =
4481 method_patches_.size() +
4482 relative_call_patches_.size() +
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004483 pc_relative_dex_cache_patches_.size() +
4484 simple_patches_.size() +
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004485 string_patches_.size() +
4486 type_patches_.size();
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004487 linker_patches->reserve(size);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004488 for (const PatchInfo<Label>& info : method_patches_) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004489 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Markoaad75c62016-10-03 08:46:48 +00004490 linker_patches->push_back(LinkerPatch::MethodPatch(literal_offset, &info.dex_file, info.index));
Vladimir Marko58155012015-08-19 12:49:41 +00004491 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00004492 for (const PatchInfo<Label>& info : relative_call_patches_) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004493 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Markoaad75c62016-10-03 08:46:48 +00004494 linker_patches->push_back(
4495 LinkerPatch::RelativeCodePatch(literal_offset, &info.dex_file, info.index));
Vladimir Marko58155012015-08-19 12:49:41 +00004496 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00004497 EmitPcRelativeLinkerPatches<LinkerPatch::DexCacheArrayPatch>(pc_relative_dex_cache_patches_,
4498 linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004499 for (const Label& label : simple_patches_) {
4500 uint32_t literal_offset = label.Position() - kLabelPositionToLiteralOffsetAdjustment;
4501 linker_patches->push_back(LinkerPatch::RecordPosition(literal_offset));
4502 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00004503 if (!GetCompilerOptions().IsBootImage()) {
4504 EmitPcRelativeLinkerPatches<LinkerPatch::StringBssEntryPatch>(string_patches_, linker_patches);
4505 } else if (GetCompilerOptions().GetCompilePic()) {
4506 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeStringPatch>(string_patches_, linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004507 } else {
Vladimir Markoaad75c62016-10-03 08:46:48 +00004508 for (const PatchInfo<Label>& info : string_patches_) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004509 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Markoaad75c62016-10-03 08:46:48 +00004510 linker_patches->push_back(
4511 LinkerPatch::StringPatch(literal_offset, &info.dex_file, info.index));
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004512 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00004513 }
4514 if (GetCompilerOptions().GetCompilePic()) {
4515 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeTypePatch>(type_patches_, linker_patches);
4516 } else {
4517 for (const PatchInfo<Label>& info : type_patches_) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004518 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Markoaad75c62016-10-03 08:46:48 +00004519 linker_patches->push_back(LinkerPatch::TypePatch(literal_offset, &info.dex_file, info.index));
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004520 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004521 }
Vladimir Marko58155012015-08-19 12:49:41 +00004522}
4523
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004524void CodeGeneratorX86::MarkGCCard(Register temp,
4525 Register card,
4526 Register object,
4527 Register value,
4528 bool value_can_be_null) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004529 NearLabel is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004530 if (value_can_be_null) {
4531 __ testl(value, value);
4532 __ j(kEqual, &is_null);
4533 }
Andreas Gampe542451c2016-07-26 09:02:02 -07004534 __ fs()->movl(card, Address::Absolute(Thread::CardTableOffset<kX86PointerSize>().Int32Value()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004535 __ movl(temp, object);
4536 __ shrl(temp, Immediate(gc::accounting::CardTable::kCardShift));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00004537 __ movb(Address(temp, card, TIMES_1, 0),
4538 X86ManagedRegister::FromCpuRegister(card).AsByteRegister());
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004539 if (value_can_be_null) {
4540 __ Bind(&is_null);
4541 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004542}
4543
Calin Juravle52c48962014-12-16 17:02:57 +00004544void LocationsBuilderX86::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
4545 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain0d5a2812015-11-13 10:07:31 +00004546
4547 bool object_field_get_with_read_barrier =
4548 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004549 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00004550 new (GetGraph()->GetArena()) LocationSummary(instruction,
4551 kEmitCompilerReadBarrier ?
4552 LocationSummary::kCallOnSlowPath :
4553 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01004554 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004555 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01004556 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004557 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004558
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004559 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4560 locations->SetOut(Location::RequiresFpuRegister());
4561 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004562 // The output overlaps in case of long: we don't want the low move
4563 // to overwrite the object's location. Likewise, in the case of
4564 // an object field get with read barriers enabled, we do not want
4565 // the move to overwrite the object's location, as we need it to emit
4566 // the read barrier.
4567 locations->SetOut(
4568 Location::RequiresRegister(),
4569 (object_field_get_with_read_barrier || instruction->GetType() == Primitive::kPrimLong) ?
4570 Location::kOutputOverlap :
4571 Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004572 }
Calin Juravle52c48962014-12-16 17:02:57 +00004573
4574 if (field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong)) {
4575 // Long values can be loaded atomically into an XMM using movsd.
Roland Levillain7c1559a2015-12-15 10:55:36 +00004576 // So we use an XMM register as a temp to achieve atomicity (first
4577 // load the temp into the XMM and then copy the XMM into the
4578 // output, 32 bits at a time).
Calin Juravle52c48962014-12-16 17:02:57 +00004579 locations->AddTemp(Location::RequiresFpuRegister());
4580 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004581}
4582
Calin Juravle52c48962014-12-16 17:02:57 +00004583void InstructionCodeGeneratorX86::HandleFieldGet(HInstruction* instruction,
4584 const FieldInfo& field_info) {
4585 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004586
Calin Juravle52c48962014-12-16 17:02:57 +00004587 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004588 Location base_loc = locations->InAt(0);
4589 Register base = base_loc.AsRegister<Register>();
Calin Juravle52c48962014-12-16 17:02:57 +00004590 Location out = locations->Out();
4591 bool is_volatile = field_info.IsVolatile();
4592 Primitive::Type field_type = field_info.GetFieldType();
4593 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4594
4595 switch (field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004596 case Primitive::kPrimBoolean: {
Calin Juravle52c48962014-12-16 17:02:57 +00004597 __ movzxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004598 break;
4599 }
4600
4601 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00004602 __ movsxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004603 break;
4604 }
4605
4606 case Primitive::kPrimShort: {
Calin Juravle52c48962014-12-16 17:02:57 +00004607 __ movsxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004608 break;
4609 }
4610
4611 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00004612 __ movzxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004613 break;
4614 }
4615
4616 case Primitive::kPrimInt:
Calin Juravle52c48962014-12-16 17:02:57 +00004617 __ movl(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004618 break;
Roland Levillain7c1559a2015-12-15 10:55:36 +00004619
4620 case Primitive::kPrimNot: {
4621 // /* HeapReference<Object> */ out = *(base + offset)
4622 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004623 // Note that a potential implicit null check is handled in this
4624 // CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier call.
4625 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00004626 instruction, out, base, offset, /* needs_null_check */ true);
Roland Levillain7c1559a2015-12-15 10:55:36 +00004627 if (is_volatile) {
4628 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4629 }
4630 } else {
4631 __ movl(out.AsRegister<Register>(), Address(base, offset));
4632 codegen_->MaybeRecordImplicitNullCheck(instruction);
4633 if (is_volatile) {
4634 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4635 }
4636 // If read barriers are enabled, emit read barriers other than
4637 // Baker's using a slow path (and also unpoison the loaded
4638 // reference, if heap poisoning is enabled).
4639 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
4640 }
4641 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004642 }
4643
4644 case Primitive::kPrimLong: {
Calin Juravle52c48962014-12-16 17:02:57 +00004645 if (is_volatile) {
4646 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4647 __ movsd(temp, Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004648 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004649 __ movd(out.AsRegisterPairLow<Register>(), temp);
4650 __ psrlq(temp, Immediate(32));
4651 __ movd(out.AsRegisterPairHigh<Register>(), temp);
4652 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004653 DCHECK_NE(base, out.AsRegisterPairLow<Register>());
Calin Juravle52c48962014-12-16 17:02:57 +00004654 __ movl(out.AsRegisterPairLow<Register>(), Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004655 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004656 __ movl(out.AsRegisterPairHigh<Register>(), Address(base, kX86WordSize + offset));
4657 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004658 break;
4659 }
4660
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004661 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00004662 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004663 break;
4664 }
4665
4666 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00004667 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004668 break;
4669 }
4670
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004671 case Primitive::kPrimVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00004672 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004673 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004674 }
Calin Juravle52c48962014-12-16 17:02:57 +00004675
Roland Levillain7c1559a2015-12-15 10:55:36 +00004676 if (field_type == Primitive::kPrimNot || field_type == Primitive::kPrimLong) {
4677 // Potential implicit null checks, in the case of reference or
4678 // long fields, are handled in the previous switch statement.
4679 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00004680 codegen_->MaybeRecordImplicitNullCheck(instruction);
4681 }
4682
Calin Juravle52c48962014-12-16 17:02:57 +00004683 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004684 if (field_type == Primitive::kPrimNot) {
4685 // Memory barriers, in the case of references, are also handled
4686 // in the previous switch statement.
4687 } else {
4688 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4689 }
Roland Levillain4d027112015-07-01 15:41:14 +01004690 }
Calin Juravle52c48962014-12-16 17:02:57 +00004691}
4692
4693void LocationsBuilderX86::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
4694 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4695
4696 LocationSummary* locations =
4697 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4698 locations->SetInAt(0, Location::RequiresRegister());
4699 bool is_volatile = field_info.IsVolatile();
4700 Primitive::Type field_type = field_info.GetFieldType();
4701 bool is_byte_type = (field_type == Primitive::kPrimBoolean)
4702 || (field_type == Primitive::kPrimByte);
4703
4704 // The register allocator does not support multiple
4705 // inputs that die at entry with one in a specific register.
4706 if (is_byte_type) {
4707 // Ensure the value is in a byte register.
4708 locations->SetInAt(1, Location::RegisterLocation(EAX));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004709 } else if (Primitive::IsFloatingPointType(field_type)) {
Mark Mendell81489372015-11-04 11:30:41 -05004710 if (is_volatile && field_type == Primitive::kPrimDouble) {
4711 // In order to satisfy the semantics of volatile, this must be a single instruction store.
4712 locations->SetInAt(1, Location::RequiresFpuRegister());
4713 } else {
4714 locations->SetInAt(1, Location::FpuRegisterOrConstant(instruction->InputAt(1)));
4715 }
4716 } else if (is_volatile && field_type == Primitive::kPrimLong) {
4717 // In order to satisfy the semantics of volatile, this must be a single instruction store.
Calin Juravle52c48962014-12-16 17:02:57 +00004718 locations->SetInAt(1, Location::RequiresRegister());
Mark Mendell81489372015-11-04 11:30:41 -05004719
Calin Juravle52c48962014-12-16 17:02:57 +00004720 // 64bits value can be atomically written to an address with movsd and an XMM register.
4721 // We need two XMM registers because there's no easier way to (bit) copy a register pair
4722 // into a single XMM register (we copy each pair part into the XMMs and then interleave them).
4723 // NB: We could make the register allocator understand fp_reg <-> core_reg moves but given the
4724 // isolated cases when we need this it isn't worth adding the extra complexity.
4725 locations->AddTemp(Location::RequiresFpuRegister());
4726 locations->AddTemp(Location::RequiresFpuRegister());
Mark Mendell81489372015-11-04 11:30:41 -05004727 } else {
4728 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4729
4730 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
4731 // Temporary registers for the write barrier.
4732 locations->AddTemp(Location::RequiresRegister()); // May be used for reference poisoning too.
4733 // Ensure the card is in a byte register.
4734 locations->AddTemp(Location::RegisterLocation(ECX));
4735 }
Calin Juravle52c48962014-12-16 17:02:57 +00004736 }
4737}
4738
4739void InstructionCodeGeneratorX86::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004740 const FieldInfo& field_info,
4741 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00004742 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4743
4744 LocationSummary* locations = instruction->GetLocations();
4745 Register base = locations->InAt(0).AsRegister<Register>();
4746 Location value = locations->InAt(1);
4747 bool is_volatile = field_info.IsVolatile();
4748 Primitive::Type field_type = field_info.GetFieldType();
4749 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01004750 bool needs_write_barrier =
4751 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00004752
4753 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004754 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
Calin Juravle52c48962014-12-16 17:02:57 +00004755 }
4756
Mark Mendell81489372015-11-04 11:30:41 -05004757 bool maybe_record_implicit_null_check_done = false;
4758
Calin Juravle52c48962014-12-16 17:02:57 +00004759 switch (field_type) {
4760 case Primitive::kPrimBoolean:
4761 case Primitive::kPrimByte: {
4762 __ movb(Address(base, offset), value.AsRegister<ByteRegister>());
4763 break;
4764 }
4765
4766 case Primitive::kPrimShort:
4767 case Primitive::kPrimChar: {
Mark Mendell81489372015-11-04 11:30:41 -05004768 if (value.IsConstant()) {
4769 int16_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4770 __ movw(Address(base, offset), Immediate(v));
4771 } else {
4772 __ movw(Address(base, offset), value.AsRegister<Register>());
4773 }
Calin Juravle52c48962014-12-16 17:02:57 +00004774 break;
4775 }
4776
4777 case Primitive::kPrimInt:
4778 case Primitive::kPrimNot: {
Roland Levillain4d027112015-07-01 15:41:14 +01004779 if (kPoisonHeapReferences && needs_write_barrier) {
4780 // Note that in the case where `value` is a null reference,
4781 // we do not enter this block, as the reference does not
4782 // need poisoning.
4783 DCHECK_EQ(field_type, Primitive::kPrimNot);
4784 Register temp = locations->GetTemp(0).AsRegister<Register>();
4785 __ movl(temp, value.AsRegister<Register>());
4786 __ PoisonHeapReference(temp);
4787 __ movl(Address(base, offset), temp);
Mark Mendell81489372015-11-04 11:30:41 -05004788 } else if (value.IsConstant()) {
4789 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4790 __ movl(Address(base, offset), Immediate(v));
Roland Levillain4d027112015-07-01 15:41:14 +01004791 } else {
Nicolas Geoffray03971632016-03-17 10:44:24 +00004792 DCHECK(value.IsRegister()) << value;
Roland Levillain4d027112015-07-01 15:41:14 +01004793 __ movl(Address(base, offset), value.AsRegister<Register>());
4794 }
Calin Juravle52c48962014-12-16 17:02:57 +00004795 break;
4796 }
4797
4798 case Primitive::kPrimLong: {
4799 if (is_volatile) {
4800 XmmRegister temp1 = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4801 XmmRegister temp2 = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
4802 __ movd(temp1, value.AsRegisterPairLow<Register>());
4803 __ movd(temp2, value.AsRegisterPairHigh<Register>());
4804 __ punpckldq(temp1, temp2);
4805 __ movsd(Address(base, offset), temp1);
Calin Juravle77520bc2015-01-12 18:45:46 +00004806 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell81489372015-11-04 11:30:41 -05004807 } else if (value.IsConstant()) {
4808 int64_t v = CodeGenerator::GetInt64ValueOf(value.GetConstant());
4809 __ movl(Address(base, offset), Immediate(Low32Bits(v)));
4810 codegen_->MaybeRecordImplicitNullCheck(instruction);
4811 __ movl(Address(base, kX86WordSize + offset), Immediate(High32Bits(v)));
Calin Juravle52c48962014-12-16 17:02:57 +00004812 } else {
4813 __ movl(Address(base, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00004814 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004815 __ movl(Address(base, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
4816 }
Mark Mendell81489372015-11-04 11:30:41 -05004817 maybe_record_implicit_null_check_done = true;
Calin Juravle52c48962014-12-16 17:02:57 +00004818 break;
4819 }
4820
4821 case Primitive::kPrimFloat: {
Mark Mendell81489372015-11-04 11:30:41 -05004822 if (value.IsConstant()) {
4823 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4824 __ movl(Address(base, offset), Immediate(v));
4825 } else {
4826 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4827 }
Calin Juravle52c48962014-12-16 17:02:57 +00004828 break;
4829 }
4830
4831 case Primitive::kPrimDouble: {
Mark Mendell81489372015-11-04 11:30:41 -05004832 if (value.IsConstant()) {
4833 int64_t v = CodeGenerator::GetInt64ValueOf(value.GetConstant());
4834 __ movl(Address(base, offset), Immediate(Low32Bits(v)));
4835 codegen_->MaybeRecordImplicitNullCheck(instruction);
4836 __ movl(Address(base, kX86WordSize + offset), Immediate(High32Bits(v)));
4837 maybe_record_implicit_null_check_done = true;
4838 } else {
4839 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4840 }
Calin Juravle52c48962014-12-16 17:02:57 +00004841 break;
4842 }
4843
4844 case Primitive::kPrimVoid:
4845 LOG(FATAL) << "Unreachable type " << field_type;
4846 UNREACHABLE();
4847 }
4848
Mark Mendell81489372015-11-04 11:30:41 -05004849 if (!maybe_record_implicit_null_check_done) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004850 codegen_->MaybeRecordImplicitNullCheck(instruction);
4851 }
4852
Roland Levillain4d027112015-07-01 15:41:14 +01004853 if (needs_write_barrier) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004854 Register temp = locations->GetTemp(0).AsRegister<Register>();
4855 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004856 codegen_->MarkGCCard(temp, card, base, value.AsRegister<Register>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00004857 }
4858
Calin Juravle52c48962014-12-16 17:02:57 +00004859 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004860 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
Calin Juravle52c48962014-12-16 17:02:57 +00004861 }
4862}
4863
4864void LocationsBuilderX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4865 HandleFieldGet(instruction, instruction->GetFieldInfo());
4866}
4867
4868void InstructionCodeGeneratorX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4869 HandleFieldGet(instruction, instruction->GetFieldInfo());
4870}
4871
4872void LocationsBuilderX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
4873 HandleFieldSet(instruction, instruction->GetFieldInfo());
4874}
4875
4876void InstructionCodeGeneratorX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004877 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00004878}
4879
4880void LocationsBuilderX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
4881 HandleFieldSet(instruction, instruction->GetFieldInfo());
4882}
4883
4884void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004885 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00004886}
4887
4888void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4889 HandleFieldGet(instruction, instruction->GetFieldInfo());
4890}
4891
4892void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4893 HandleFieldGet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004894}
4895
Calin Juravlee460d1d2015-09-29 04:52:17 +01004896void LocationsBuilderX86::VisitUnresolvedInstanceFieldGet(
4897 HUnresolvedInstanceFieldGet* instruction) {
4898 FieldAccessCallingConventionX86 calling_convention;
4899 codegen_->CreateUnresolvedFieldLocationSummary(
4900 instruction, instruction->GetFieldType(), calling_convention);
4901}
4902
4903void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldGet(
4904 HUnresolvedInstanceFieldGet* instruction) {
4905 FieldAccessCallingConventionX86 calling_convention;
4906 codegen_->GenerateUnresolvedFieldAccess(instruction,
4907 instruction->GetFieldType(),
4908 instruction->GetFieldIndex(),
4909 instruction->GetDexPc(),
4910 calling_convention);
4911}
4912
4913void LocationsBuilderX86::VisitUnresolvedInstanceFieldSet(
4914 HUnresolvedInstanceFieldSet* instruction) {
4915 FieldAccessCallingConventionX86 calling_convention;
4916 codegen_->CreateUnresolvedFieldLocationSummary(
4917 instruction, instruction->GetFieldType(), calling_convention);
4918}
4919
4920void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldSet(
4921 HUnresolvedInstanceFieldSet* instruction) {
4922 FieldAccessCallingConventionX86 calling_convention;
4923 codegen_->GenerateUnresolvedFieldAccess(instruction,
4924 instruction->GetFieldType(),
4925 instruction->GetFieldIndex(),
4926 instruction->GetDexPc(),
4927 calling_convention);
4928}
4929
4930void LocationsBuilderX86::VisitUnresolvedStaticFieldGet(
4931 HUnresolvedStaticFieldGet* instruction) {
4932 FieldAccessCallingConventionX86 calling_convention;
4933 codegen_->CreateUnresolvedFieldLocationSummary(
4934 instruction, instruction->GetFieldType(), calling_convention);
4935}
4936
4937void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldGet(
4938 HUnresolvedStaticFieldGet* instruction) {
4939 FieldAccessCallingConventionX86 calling_convention;
4940 codegen_->GenerateUnresolvedFieldAccess(instruction,
4941 instruction->GetFieldType(),
4942 instruction->GetFieldIndex(),
4943 instruction->GetDexPc(),
4944 calling_convention);
4945}
4946
4947void LocationsBuilderX86::VisitUnresolvedStaticFieldSet(
4948 HUnresolvedStaticFieldSet* instruction) {
4949 FieldAccessCallingConventionX86 calling_convention;
4950 codegen_->CreateUnresolvedFieldLocationSummary(
4951 instruction, instruction->GetFieldType(), calling_convention);
4952}
4953
4954void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldSet(
4955 HUnresolvedStaticFieldSet* instruction) {
4956 FieldAccessCallingConventionX86 calling_convention;
4957 codegen_->GenerateUnresolvedFieldAccess(instruction,
4958 instruction->GetFieldType(),
4959 instruction->GetFieldIndex(),
4960 instruction->GetDexPc(),
4961 calling_convention);
4962}
4963
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004964void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004965 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
4966 Location loc = codegen_->GetCompilerOptions().GetImplicitNullChecks()
4967 ? Location::RequiresRegister()
4968 : Location::Any();
4969 locations->SetInAt(0, loc);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004970}
4971
Calin Juravle2ae48182016-03-16 14:05:09 +00004972void CodeGeneratorX86::GenerateImplicitNullCheck(HNullCheck* instruction) {
4973 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004974 return;
4975 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004976 LocationSummary* locations = instruction->GetLocations();
4977 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00004978
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004979 __ testl(EAX, Address(obj.AsRegister<Register>(), 0));
Calin Juravle2ae48182016-03-16 14:05:09 +00004980 RecordPcInfo(instruction, instruction->GetDexPc());
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004981}
4982
Calin Juravle2ae48182016-03-16 14:05:09 +00004983void CodeGeneratorX86::GenerateExplicitNullCheck(HNullCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07004984 SlowPathCode* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00004985 AddSlowPath(slow_path);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004986
4987 LocationSummary* locations = instruction->GetLocations();
4988 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004989
4990 if (obj.IsRegister()) {
Mark Mendell42514f62015-03-31 11:34:22 -04004991 __ testl(obj.AsRegister<Register>(), obj.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004992 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004993 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004994 } else {
4995 DCHECK(obj.IsConstant()) << obj;
David Brazdil77a48ae2015-09-15 12:34:04 +00004996 DCHECK(obj.GetConstant()->IsNullConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004997 __ jmp(slow_path->GetEntryLabel());
4998 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004999 }
5000 __ j(kEqual, slow_path->GetEntryLabel());
5001}
5002
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005003void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00005004 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005005}
5006
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005007void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005008 bool object_array_get_with_read_barrier =
5009 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005010 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00005011 new (GetGraph()->GetArena()) LocationSummary(instruction,
5012 object_array_get_with_read_barrier ?
5013 LocationSummary::kCallOnSlowPath :
5014 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01005015 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005016 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01005017 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005018 locations->SetInAt(0, Location::RequiresRegister());
5019 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005020 if (Primitive::IsFloatingPointType(instruction->GetType())) {
5021 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5022 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005023 // The output overlaps in case of long: we don't want the low move
5024 // to overwrite the array's location. Likewise, in the case of an
5025 // object array get with read barriers enabled, we do not want the
5026 // move to overwrite the array's location, as we need it to emit
5027 // the read barrier.
5028 locations->SetOut(
5029 Location::RequiresRegister(),
5030 (instruction->GetType() == Primitive::kPrimLong || object_array_get_with_read_barrier) ?
5031 Location::kOutputOverlap :
5032 Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005033 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005034}
5035
5036void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
5037 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005038 Location obj_loc = locations->InAt(0);
5039 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005040 Location index = locations->InAt(1);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005041 Location out_loc = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005042 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005043
Calin Juravle77520bc2015-01-12 18:45:46 +00005044 Primitive::Type type = instruction->GetType();
5045 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005046 case Primitive::kPrimBoolean: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005047 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005048 __ movzxb(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005049 break;
5050 }
5051
5052 case Primitive::kPrimByte: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005053 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005054 __ movsxb(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005055 break;
5056 }
5057
5058 case Primitive::kPrimShort: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005059 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005060 __ movsxw(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005061 break;
5062 }
5063
5064 case Primitive::kPrimChar: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005065 Register out = out_loc.AsRegister<Register>();
jessicahandojo4877b792016-09-08 19:49:13 -07005066 if (mirror::kUseStringCompression && instruction->IsStringCharAt()) {
5067 // Branch cases into compressed and uncompressed for each index's type.
5068 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
5069 NearLabel done, not_compressed;
5070 __ cmpl(Address(obj, count_offset), Immediate(0));
5071 codegen_->MaybeRecordImplicitNullCheck(instruction);
5072 __ j(kGreaterEqual, &not_compressed);
5073 __ movzxb(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_1, data_offset));
5074 __ jmp(&done);
5075 __ Bind(&not_compressed);
5076 __ movzxw(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_2, data_offset));
5077 __ Bind(&done);
5078 } else {
5079 // Common case for charAt of array of char or when string compression's
5080 // feature is turned off.
5081 __ movzxw(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_2, data_offset));
5082 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005083 break;
5084 }
5085
Roland Levillain7c1559a2015-12-15 10:55:36 +00005086 case Primitive::kPrimInt: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005087 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005088 __ movl(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005089 break;
5090 }
5091
Roland Levillain7c1559a2015-12-15 10:55:36 +00005092 case Primitive::kPrimNot: {
5093 static_assert(
5094 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
5095 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain7c1559a2015-12-15 10:55:36 +00005096 // /* HeapReference<Object> */ out =
5097 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
5098 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005099 // Note that a potential implicit null check is handled in this
5100 // CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier call.
5101 codegen_->GenerateArrayLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00005102 instruction, out_loc, obj, data_offset, index, /* needs_null_check */ true);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005103 } else {
5104 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005105 __ movl(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset));
5106 codegen_->MaybeRecordImplicitNullCheck(instruction);
5107 // If read barriers are enabled, emit read barriers other than
5108 // Baker's using a slow path (and also unpoison the loaded
5109 // reference, if heap poisoning is enabled).
Roland Levillain7c1559a2015-12-15 10:55:36 +00005110 if (index.IsConstant()) {
5111 uint32_t offset =
5112 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Roland Levillain7c1559a2015-12-15 10:55:36 +00005113 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
5114 } else {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005115 codegen_->MaybeGenerateReadBarrierSlow(
5116 instruction, out_loc, out_loc, obj_loc, data_offset, index);
5117 }
5118 }
5119 break;
5120 }
5121
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005122 case Primitive::kPrimLong: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005123 DCHECK_NE(obj, out_loc.AsRegisterPairLow<Register>());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005124 __ movl(out_loc.AsRegisterPairLow<Register>(),
5125 CodeGeneratorX86::ArrayAddress(obj, index, TIMES_8, data_offset));
5126 codegen_->MaybeRecordImplicitNullCheck(instruction);
5127 __ movl(out_loc.AsRegisterPairHigh<Register>(),
5128 CodeGeneratorX86::ArrayAddress(obj, index, TIMES_8, data_offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005129 break;
5130 }
5131
Mark Mendell7c8d0092015-01-26 11:21:33 -05005132 case Primitive::kPrimFloat: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005133 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005134 __ movss(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005135 break;
5136 }
5137
5138 case Primitive::kPrimDouble: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005139 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005140 __ movsd(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_8, data_offset));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005141 break;
5142 }
5143
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005144 case Primitive::kPrimVoid:
Calin Juravle77520bc2015-01-12 18:45:46 +00005145 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07005146 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005147 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005148
Roland Levillain7c1559a2015-12-15 10:55:36 +00005149 if (type == Primitive::kPrimNot || type == Primitive::kPrimLong) {
5150 // Potential implicit null checks, in the case of reference or
5151 // long arrays, are handled in the previous switch statement.
5152 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00005153 codegen_->MaybeRecordImplicitNullCheck(instruction);
5154 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005155}
5156
5157void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01005158 Primitive::Type value_type = instruction->GetComponentType();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005159
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005160 bool needs_write_barrier =
5161 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Roland Levillain0d5a2812015-11-13 10:07:31 +00005162 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005163
Nicolas Geoffray39468442014-09-02 15:17:15 +01005164 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
5165 instruction,
Vladimir Marko8d49fd72016-08-25 15:20:47 +01005166 may_need_runtime_call_for_type_check ?
Roland Levillain0d5a2812015-11-13 10:07:31 +00005167 LocationSummary::kCallOnSlowPath :
5168 LocationSummary::kNoCall);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005169
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005170 bool is_byte_type = (value_type == Primitive::kPrimBoolean)
5171 || (value_type == Primitive::kPrimByte);
5172 // We need the inputs to be different than the output in case of long operation.
5173 // In case of a byte operation, the register allocator does not support multiple
5174 // inputs that die at entry with one in a specific register.
5175 locations->SetInAt(0, Location::RequiresRegister());
5176 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
5177 if (is_byte_type) {
5178 // Ensure the value is in a byte register.
5179 locations->SetInAt(2, Location::ByteRegisterOrConstant(EAX, instruction->InputAt(2)));
5180 } else if (Primitive::IsFloatingPointType(value_type)) {
Mark Mendell81489372015-11-04 11:30:41 -05005181 locations->SetInAt(2, Location::FpuRegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005182 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005183 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
5184 }
5185 if (needs_write_barrier) {
5186 // Temporary registers for the write barrier.
5187 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
5188 // Ensure the card is in a byte register.
Roland Levillain4f6b0b52015-11-23 19:29:22 +00005189 locations->AddTemp(Location::RegisterLocation(ECX));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005190 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005191}
5192
5193void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
5194 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005195 Location array_loc = locations->InAt(0);
5196 Register array = array_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005197 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005198 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005199 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005200 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
5201 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5202 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005203 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005204 bool needs_write_barrier =
5205 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005206
5207 switch (value_type) {
5208 case Primitive::kPrimBoolean:
5209 case Primitive::kPrimByte: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005210 uint32_t offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005211 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_1, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005212 if (value.IsRegister()) {
5213 __ movb(address, value.AsRegister<ByteRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005214 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005215 __ movb(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005216 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005217 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005218 break;
5219 }
5220
5221 case Primitive::kPrimShort:
5222 case Primitive::kPrimChar: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005223 uint32_t offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005224 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_2, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005225 if (value.IsRegister()) {
5226 __ movw(address, value.AsRegister<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005227 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005228 __ movw(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005229 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005230 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005231 break;
5232 }
5233
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005234 case Primitive::kPrimNot: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005235 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005236 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_4, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005237
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005238 if (!value.IsRegister()) {
5239 // Just setting null.
5240 DCHECK(instruction->InputAt(2)->IsNullConstant());
5241 DCHECK(value.IsConstant()) << value;
5242 __ movl(address, Immediate(0));
Calin Juravle77520bc2015-01-12 18:45:46 +00005243 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005244 DCHECK(!needs_write_barrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005245 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005246 break;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005247 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005248
5249 DCHECK(needs_write_barrier);
5250 Register register_value = value.AsRegister<Register>();
Roland Levillain16d9f942016-08-25 17:27:56 +01005251 // We cannot use a NearLabel for `done`, as its range may be too
5252 // short when Baker read barriers are enabled.
5253 Label done;
5254 NearLabel not_null, do_put;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005255 SlowPathCode* slow_path = nullptr;
Roland Levillain16d9f942016-08-25 17:27:56 +01005256 Location temp_loc = locations->GetTemp(0);
5257 Register temp = temp_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005258 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005259 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathX86(instruction);
5260 codegen_->AddSlowPath(slow_path);
5261 if (instruction->GetValueCanBeNull()) {
5262 __ testl(register_value, register_value);
5263 __ j(kNotEqual, &not_null);
5264 __ movl(address, Immediate(0));
5265 codegen_->MaybeRecordImplicitNullCheck(instruction);
5266 __ jmp(&done);
5267 __ Bind(&not_null);
5268 }
5269
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005270 // Note that when Baker read barriers are enabled, the type
5271 // checks are performed without read barriers. This is fine,
5272 // even in the case where a class object is in the from-space
5273 // after the flip, as a comparison involving such a type would
5274 // not produce a false positive; it may of course produce a
5275 // false negative, in which case we would take the ArraySet
5276 // slow path.
Roland Levillain16d9f942016-08-25 17:27:56 +01005277
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005278 // /* HeapReference<Class> */ temp = array->klass_
5279 __ movl(temp, Address(array, class_offset));
5280 codegen_->MaybeRecordImplicitNullCheck(instruction);
5281 __ MaybeUnpoisonHeapReference(temp);
Roland Levillain16d9f942016-08-25 17:27:56 +01005282
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005283 // /* HeapReference<Class> */ temp = temp->component_type_
5284 __ movl(temp, Address(temp, component_offset));
5285 // If heap poisoning is enabled, no need to unpoison `temp`
5286 // nor the object reference in `register_value->klass`, as
5287 // we are comparing two poisoned references.
5288 __ cmpl(temp, Address(register_value, class_offset));
Roland Levillain16d9f942016-08-25 17:27:56 +01005289
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005290 if (instruction->StaticTypeOfArrayIsObjectArray()) {
5291 __ j(kEqual, &do_put);
5292 // If heap poisoning is enabled, the `temp` reference has
5293 // not been unpoisoned yet; unpoison it now.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005294 __ MaybeUnpoisonHeapReference(temp);
5295
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005296 // If heap poisoning is enabled, no need to unpoison the
5297 // heap reference loaded below, as it is only used for a
5298 // comparison with null.
5299 __ cmpl(Address(temp, super_offset), Immediate(0));
5300 __ j(kNotEqual, slow_path->GetEntryLabel());
5301 __ Bind(&do_put);
5302 } else {
5303 __ j(kNotEqual, slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005304 }
5305 }
5306
5307 if (kPoisonHeapReferences) {
5308 __ movl(temp, register_value);
5309 __ PoisonHeapReference(temp);
5310 __ movl(address, temp);
5311 } else {
5312 __ movl(address, register_value);
5313 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005314 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005315 codegen_->MaybeRecordImplicitNullCheck(instruction);
5316 }
5317
5318 Register card = locations->GetTemp(1).AsRegister<Register>();
5319 codegen_->MarkGCCard(
5320 temp, card, array, value.AsRegister<Register>(), instruction->GetValueCanBeNull());
5321 __ Bind(&done);
5322
5323 if (slow_path != nullptr) {
5324 __ Bind(slow_path->GetExitLabel());
5325 }
5326
5327 break;
5328 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005329
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005330 case Primitive::kPrimInt: {
5331 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005332 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_4, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005333 if (value.IsRegister()) {
5334 __ movl(address, value.AsRegister<Register>());
5335 } else {
5336 DCHECK(value.IsConstant()) << value;
5337 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
5338 __ movl(address, Immediate(v));
5339 }
5340 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005341 break;
5342 }
5343
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005344 case Primitive::kPrimLong: {
5345 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005346 if (value.IsRegisterPair()) {
5347 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset),
5348 value.AsRegisterPairLow<Register>());
5349 codegen_->MaybeRecordImplicitNullCheck(instruction);
5350 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset + kX86WordSize),
5351 value.AsRegisterPairHigh<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005352 } else {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005353 DCHECK(value.IsConstant());
5354 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
5355 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset),
5356 Immediate(Low32Bits(val)));
5357 codegen_->MaybeRecordImplicitNullCheck(instruction);
5358 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset + kX86WordSize),
5359 Immediate(High32Bits(val)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005360 }
5361 break;
5362 }
5363
Mark Mendell7c8d0092015-01-26 11:21:33 -05005364 case Primitive::kPrimFloat: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005365 uint32_t offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005366 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_4, offset);
Mark Mendell81489372015-11-04 11:30:41 -05005367 if (value.IsFpuRegister()) {
5368 __ movss(address, value.AsFpuRegister<XmmRegister>());
5369 } else {
5370 DCHECK(value.IsConstant());
5371 int32_t v = bit_cast<int32_t, float>(value.GetConstant()->AsFloatConstant()->GetValue());
5372 __ movl(address, Immediate(v));
5373 }
5374 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005375 break;
5376 }
5377
5378 case Primitive::kPrimDouble: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005379 uint32_t offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005380 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, offset);
Mark Mendell81489372015-11-04 11:30:41 -05005381 if (value.IsFpuRegister()) {
5382 __ movsd(address, value.AsFpuRegister<XmmRegister>());
5383 } else {
5384 DCHECK(value.IsConstant());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005385 Address address_hi =
5386 CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, offset + kX86WordSize);
Mark Mendell81489372015-11-04 11:30:41 -05005387 int64_t v = bit_cast<int64_t, double>(value.GetConstant()->AsDoubleConstant()->GetValue());
5388 __ movl(address, Immediate(Low32Bits(v)));
5389 codegen_->MaybeRecordImplicitNullCheck(instruction);
5390 __ movl(address_hi, Immediate(High32Bits(v)));
5391 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005392 break;
5393 }
5394
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005395 case Primitive::kPrimVoid:
5396 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07005397 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005398 }
5399}
5400
5401void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
5402 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005403 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellee8d9712016-07-12 11:13:15 -04005404 if (!instruction->IsEmittedAtUseSite()) {
5405 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5406 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005407}
5408
5409void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
Mark Mendellee8d9712016-07-12 11:13:15 -04005410 if (instruction->IsEmittedAtUseSite()) {
5411 return;
5412 }
5413
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005414 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01005415 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00005416 Register obj = locations->InAt(0).AsRegister<Register>();
5417 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005418 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005419 codegen_->MaybeRecordImplicitNullCheck(instruction);
jessicahandojo4877b792016-09-08 19:49:13 -07005420 // Mask out most significant bit in case the array is String's array of char.
5421 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
5422 __ andl(out, Immediate(INT32_MAX));
5423 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005424}
5425
5426void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005427 RegisterSet caller_saves = RegisterSet::Empty();
5428 InvokeRuntimeCallingConvention calling_convention;
5429 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5430 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
5431 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Mark Mendellf60c90b2015-03-04 15:12:59 -05005432 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendellee8d9712016-07-12 11:13:15 -04005433 HInstruction* length = instruction->InputAt(1);
5434 if (!length->IsEmittedAtUseSite()) {
5435 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
5436 }
jessicahandojo4877b792016-09-08 19:49:13 -07005437 // Need register to see array's length.
5438 if (mirror::kUseStringCompression && instruction->IsStringCharAt()) {
5439 locations->AddTemp(Location::RequiresRegister());
5440 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005441}
5442
5443void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
jessicahandojo4877b792016-09-08 19:49:13 -07005444 const bool is_string_compressed_char_at =
5445 mirror::kUseStringCompression && instruction->IsStringCharAt();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005446 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05005447 Location index_loc = locations->InAt(0);
5448 Location length_loc = locations->InAt(1);
Andreas Gampe85b62f22015-09-09 13:15:38 -07005449 SlowPathCode* slow_path =
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01005450 new (GetGraph()->GetArena()) BoundsCheckSlowPathX86(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005451
Mark Mendell99dbd682015-04-22 16:18:52 -04005452 if (length_loc.IsConstant()) {
5453 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
5454 if (index_loc.IsConstant()) {
5455 // BCE will remove the bounds check if we are guarenteed to pass.
5456 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5457 if (index < 0 || index >= length) {
5458 codegen_->AddSlowPath(slow_path);
5459 __ jmp(slow_path->GetEntryLabel());
5460 } else {
5461 // Some optimization after BCE may have generated this, and we should not
5462 // generate a bounds check if it is a valid range.
5463 }
5464 return;
5465 }
5466
5467 // We have to reverse the jump condition because the length is the constant.
5468 Register index_reg = index_loc.AsRegister<Register>();
5469 __ cmpl(index_reg, Immediate(length));
5470 codegen_->AddSlowPath(slow_path);
5471 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005472 } else {
Mark Mendellee8d9712016-07-12 11:13:15 -04005473 HInstruction* array_length = instruction->InputAt(1);
5474 if (array_length->IsEmittedAtUseSite()) {
5475 // Address the length field in the array.
5476 DCHECK(array_length->IsArrayLength());
5477 uint32_t len_offset = CodeGenerator::GetArrayLengthOffset(array_length->AsArrayLength());
5478 Location array_loc = array_length->GetLocations()->InAt(0);
5479 Address array_len(array_loc.AsRegister<Register>(), len_offset);
jessicahandojo4877b792016-09-08 19:49:13 -07005480 if (is_string_compressed_char_at) {
5481 Register length_reg = locations->GetTemp(0).AsRegister<Register>();
5482 __ movl(length_reg, array_len);
5483 codegen_->MaybeRecordImplicitNullCheck(array_length);
5484 __ andl(length_reg, Immediate(INT32_MAX));
5485 codegen_->GenerateIntCompare(length_reg, index_loc);
Mark Mendellee8d9712016-07-12 11:13:15 -04005486 } else {
jessicahandojo4877b792016-09-08 19:49:13 -07005487 // Checking bounds for general case:
5488 // Array of char or string's array with feature compression off.
5489 if (index_loc.IsConstant()) {
5490 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5491 __ cmpl(array_len, Immediate(value));
5492 } else {
5493 __ cmpl(array_len, index_loc.AsRegister<Register>());
5494 }
5495 codegen_->MaybeRecordImplicitNullCheck(array_length);
Mark Mendellee8d9712016-07-12 11:13:15 -04005496 }
Mark Mendell99dbd682015-04-22 16:18:52 -04005497 } else {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005498 codegen_->GenerateIntCompare(length_loc, index_loc);
Mark Mendell99dbd682015-04-22 16:18:52 -04005499 }
5500 codegen_->AddSlowPath(slow_path);
5501 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005502 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005503}
5504
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005505void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005506 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005507}
5508
5509void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005510 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5511}
5512
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005513void LocationsBuilderX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Marko70e97462016-08-09 11:04:26 +01005514 LocationSummary* locations =
5515 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01005516 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005517}
5518
5519void InstructionCodeGeneratorX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005520 HBasicBlock* block = instruction->GetBlock();
5521 if (block->GetLoopInformation() != nullptr) {
5522 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5523 // The back edge will generate the suspend check.
5524 return;
5525 }
5526 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5527 // The goto will generate the suspend check.
5528 return;
5529 }
5530 GenerateSuspendCheck(instruction, nullptr);
5531}
5532
5533void InstructionCodeGeneratorX86::GenerateSuspendCheck(HSuspendCheck* instruction,
5534 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005535 SuspendCheckSlowPathX86* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005536 down_cast<SuspendCheckSlowPathX86*>(instruction->GetSlowPath());
5537 if (slow_path == nullptr) {
5538 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathX86(instruction, successor);
5539 instruction->SetSlowPath(slow_path);
5540 codegen_->AddSlowPath(slow_path);
5541 if (successor != nullptr) {
5542 DCHECK(successor->IsLoopHeader());
5543 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
5544 }
5545 } else {
5546 DCHECK_EQ(slow_path->GetSuccessor(), successor);
5547 }
5548
Andreas Gampe542451c2016-07-26 09:02:02 -07005549 __ fs()->cmpw(Address::Absolute(Thread::ThreadFlagsOffset<kX86PointerSize>().Int32Value()),
Roland Levillain7c1559a2015-12-15 10:55:36 +00005550 Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005551 if (successor == nullptr) {
5552 __ j(kNotEqual, slow_path->GetEntryLabel());
5553 __ Bind(slow_path->GetReturnLabel());
5554 } else {
5555 __ j(kEqual, codegen_->GetLabelOf(successor));
5556 __ jmp(slow_path->GetEntryLabel());
5557 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005558}
5559
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005560X86Assembler* ParallelMoveResolverX86::GetAssembler() const {
5561 return codegen_->GetAssembler();
5562}
5563
Mark Mendell7c8d0092015-01-26 11:21:33 -05005564void ParallelMoveResolverX86::MoveMemoryToMemory32(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005565 ScratchRegisterScope ensure_scratch(
5566 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5567 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5568 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5569 __ movl(temp_reg, Address(ESP, src + stack_offset));
5570 __ movl(Address(ESP, dst + stack_offset), temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005571}
5572
5573void ParallelMoveResolverX86::MoveMemoryToMemory64(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005574 ScratchRegisterScope ensure_scratch(
5575 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5576 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5577 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5578 __ movl(temp_reg, Address(ESP, src + stack_offset));
5579 __ movl(Address(ESP, dst + stack_offset), temp_reg);
5580 __ movl(temp_reg, Address(ESP, src + stack_offset + kX86WordSize));
5581 __ movl(Address(ESP, dst + stack_offset + kX86WordSize), temp_reg);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005582}
5583
5584void ParallelMoveResolverX86::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005585 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005586 Location source = move->GetSource();
5587 Location destination = move->GetDestination();
5588
5589 if (source.IsRegister()) {
5590 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005591 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
David Brazdil74eb1b22015-12-14 11:44:01 +00005592 } else if (destination.IsFpuRegister()) {
5593 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005594 } else {
5595 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00005596 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005597 }
David Brazdil74eb1b22015-12-14 11:44:01 +00005598 } else if (source.IsRegisterPair()) {
5599 size_t elem_size = Primitive::ComponentSize(Primitive::kPrimInt);
5600 // Create stack space for 2 elements.
5601 __ subl(ESP, Immediate(2 * elem_size));
5602 __ movl(Address(ESP, 0), source.AsRegisterPairLow<Register>());
5603 __ movl(Address(ESP, elem_size), source.AsRegisterPairHigh<Register>());
5604 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
5605 // And remove the temporary stack space we allocated.
5606 __ addl(ESP, Immediate(2 * elem_size));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005607 } else if (source.IsFpuRegister()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00005608 if (destination.IsRegister()) {
5609 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
5610 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005611 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
David Brazdil74eb1b22015-12-14 11:44:01 +00005612 } else if (destination.IsRegisterPair()) {
5613 XmmRegister src_reg = source.AsFpuRegister<XmmRegister>();
5614 __ movd(destination.AsRegisterPairLow<Register>(), src_reg);
5615 __ psrlq(src_reg, Immediate(32));
5616 __ movd(destination.AsRegisterPairHigh<Register>(), src_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005617 } else if (destination.IsStackSlot()) {
5618 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
5619 } else {
5620 DCHECK(destination.IsDoubleStackSlot());
5621 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
5622 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005623 } else if (source.IsStackSlot()) {
5624 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005625 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005626 } else if (destination.IsFpuRegister()) {
5627 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005628 } else {
5629 DCHECK(destination.IsStackSlot());
Mark Mendell7c8d0092015-01-26 11:21:33 -05005630 MoveMemoryToMemory32(destination.GetStackIndex(), source.GetStackIndex());
5631 }
5632 } else if (source.IsDoubleStackSlot()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00005633 if (destination.IsRegisterPair()) {
5634 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
5635 __ movl(destination.AsRegisterPairHigh<Register>(),
5636 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
5637 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005638 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
5639 } else {
5640 DCHECK(destination.IsDoubleStackSlot()) << destination;
5641 MoveMemoryToMemory64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005642 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005643 } else if (source.IsConstant()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005644 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00005645 if (constant->IsIntConstant() || constant->IsNullConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05005646 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005647 if (destination.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05005648 if (value == 0) {
5649 __ xorl(destination.AsRegister<Register>(), destination.AsRegister<Register>());
5650 } else {
5651 __ movl(destination.AsRegister<Register>(), Immediate(value));
5652 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005653 } else {
5654 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell09b84632015-02-13 17:48:38 -05005655 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005656 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005657 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005658 float fp_value = constant->AsFloatConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00005659 int32_t value = bit_cast<int32_t, float>(fp_value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005660 Immediate imm(value);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005661 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005662 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
5663 if (value == 0) {
5664 // Easy handling of 0.0.
5665 __ xorps(dest, dest);
5666 } else {
5667 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005668 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5669 Register temp = static_cast<Register>(ensure_scratch.GetRegister());
5670 __ movl(temp, Immediate(value));
5671 __ movd(dest, temp);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005672 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005673 } else {
5674 DCHECK(destination.IsStackSlot()) << destination;
5675 __ movl(Address(ESP, destination.GetStackIndex()), imm);
5676 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005677 } else if (constant->IsLongConstant()) {
5678 int64_t value = constant->AsLongConstant()->GetValue();
5679 int32_t low_value = Low32Bits(value);
5680 int32_t high_value = High32Bits(value);
5681 Immediate low(low_value);
5682 Immediate high(high_value);
5683 if (destination.IsDoubleStackSlot()) {
5684 __ movl(Address(ESP, destination.GetStackIndex()), low);
5685 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
5686 } else {
5687 __ movl(destination.AsRegisterPairLow<Register>(), low);
5688 __ movl(destination.AsRegisterPairHigh<Register>(), high);
5689 }
5690 } else {
5691 DCHECK(constant->IsDoubleConstant());
5692 double dbl_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00005693 int64_t value = bit_cast<int64_t, double>(dbl_value);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005694 int32_t low_value = Low32Bits(value);
5695 int32_t high_value = High32Bits(value);
5696 Immediate low(low_value);
5697 Immediate high(high_value);
5698 if (destination.IsFpuRegister()) {
5699 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
5700 if (value == 0) {
5701 // Easy handling of 0.0.
5702 __ xorpd(dest, dest);
5703 } else {
5704 __ pushl(high);
5705 __ pushl(low);
5706 __ movsd(dest, Address(ESP, 0));
5707 __ addl(ESP, Immediate(8));
5708 }
5709 } else {
5710 DCHECK(destination.IsDoubleStackSlot()) << destination;
5711 __ movl(Address(ESP, destination.GetStackIndex()), low);
5712 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
5713 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005714 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005715 } else {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00005716 LOG(FATAL) << "Unimplemented move: " << destination << " <- " << source;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005717 }
5718}
5719
Mark Mendella5c19ce2015-04-01 12:51:05 -04005720void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005721 Register suggested_scratch = reg == EAX ? EBX : EAX;
5722 ScratchRegisterScope ensure_scratch(
5723 this, reg, suggested_scratch, codegen_->GetNumberOfCoreRegisters());
5724
5725 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5726 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset));
5727 __ movl(Address(ESP, mem + stack_offset), reg);
5728 __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005729}
5730
Mark Mendell7c8d0092015-01-26 11:21:33 -05005731void ParallelMoveResolverX86::Exchange32(XmmRegister reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005732 ScratchRegisterScope ensure_scratch(
5733 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5734
5735 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5736 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5737 __ movl(temp_reg, Address(ESP, mem + stack_offset));
5738 __ movss(Address(ESP, mem + stack_offset), reg);
5739 __ movd(reg, temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005740}
5741
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005742void ParallelMoveResolverX86::Exchange(int mem1, int mem2) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005743 ScratchRegisterScope ensure_scratch1(
5744 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005745
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005746 Register suggested_scratch = ensure_scratch1.GetRegister() == EAX ? EBX : EAX;
5747 ScratchRegisterScope ensure_scratch2(
5748 this, ensure_scratch1.GetRegister(), suggested_scratch, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005749
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005750 int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0;
5751 stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0;
5752 __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset));
5753 __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset));
5754 __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister()));
5755 __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005756}
5757
5758void ParallelMoveResolverX86::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005759 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005760 Location source = move->GetSource();
5761 Location destination = move->GetDestination();
5762
5763 if (source.IsRegister() && destination.IsRegister()) {
Mark Mendell90979812015-07-28 16:41:21 -04005764 // Use XOR swap algorithm to avoid serializing XCHG instruction or using a temporary.
5765 DCHECK_NE(destination.AsRegister<Register>(), source.AsRegister<Register>());
5766 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
5767 __ xorl(source.AsRegister<Register>(), destination.AsRegister<Register>());
5768 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005769 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005770 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005771 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005772 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005773 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
5774 Exchange(destination.GetStackIndex(), source.GetStackIndex());
Mark Mendell7c8d0092015-01-26 11:21:33 -05005775 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
5776 // Use XOR Swap algorithm to avoid a temporary.
5777 DCHECK_NE(source.reg(), destination.reg());
5778 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
5779 __ xorpd(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
5780 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
5781 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
5782 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
5783 } else if (destination.IsFpuRegister() && source.IsStackSlot()) {
5784 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005785 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
5786 // Take advantage of the 16 bytes in the XMM register.
5787 XmmRegister reg = source.AsFpuRegister<XmmRegister>();
5788 Address stack(ESP, destination.GetStackIndex());
5789 // Load the double into the high doubleword.
5790 __ movhpd(reg, stack);
5791
5792 // Store the low double into the destination.
5793 __ movsd(stack, reg);
5794
5795 // Move the high double to the low double.
5796 __ psrldq(reg, Immediate(8));
5797 } else if (destination.IsFpuRegister() && source.IsDoubleStackSlot()) {
5798 // Take advantage of the 16 bytes in the XMM register.
5799 XmmRegister reg = destination.AsFpuRegister<XmmRegister>();
5800 Address stack(ESP, source.GetStackIndex());
5801 // Load the double into the high doubleword.
5802 __ movhpd(reg, stack);
5803
5804 // Store the low double into the destination.
5805 __ movsd(stack, reg);
5806
5807 // Move the high double to the low double.
5808 __ psrldq(reg, Immediate(8));
5809 } else if (destination.IsDoubleStackSlot() && source.IsDoubleStackSlot()) {
5810 Exchange(destination.GetStackIndex(), source.GetStackIndex());
5811 Exchange(destination.GetHighStackIndex(kX86WordSize), source.GetHighStackIndex(kX86WordSize));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005812 } else {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005813 LOG(FATAL) << "Unimplemented: source: " << source << ", destination: " << destination;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005814 }
5815}
5816
5817void ParallelMoveResolverX86::SpillScratch(int reg) {
5818 __ pushl(static_cast<Register>(reg));
5819}
5820
5821void ParallelMoveResolverX86::RestoreScratch(int reg) {
5822 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005823}
5824
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005825HLoadClass::LoadKind CodeGeneratorX86::GetSupportedLoadClassKind(
5826 HLoadClass::LoadKind desired_class_load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005827 switch (desired_class_load_kind) {
5828 case HLoadClass::LoadKind::kReferrersClass:
5829 break;
5830 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
5831 DCHECK(!GetCompilerOptions().GetCompilePic());
5832 break;
5833 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
5834 DCHECK(GetCompilerOptions().GetCompilePic());
5835 FALLTHROUGH_INTENDED;
5836 case HLoadClass::LoadKind::kDexCachePcRelative:
5837 DCHECK(!Runtime::Current()->UseJitCompilation()); // Note: boot image is also non-JIT.
5838 // We disable pc-relative load when there is an irreducible loop, as the optimization
5839 // is incompatible with it.
5840 // TODO: Create as many X86ComputeBaseMethodAddress instructions as needed for methods
5841 // with irreducible loops.
5842 if (GetGraph()->HasIrreducibleLoops()) {
5843 return HLoadClass::LoadKind::kDexCacheViaMethod;
5844 }
5845 break;
5846 case HLoadClass::LoadKind::kBootImageAddress:
5847 break;
5848 case HLoadClass::LoadKind::kDexCacheAddress:
5849 DCHECK(Runtime::Current()->UseJitCompilation());
5850 break;
5851 case HLoadClass::LoadKind::kDexCacheViaMethod:
5852 break;
5853 }
5854 return desired_class_load_kind;
5855}
5856
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005857void LocationsBuilderX86::VisitLoadClass(HLoadClass* cls) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005858 if (cls->NeedsAccessCheck()) {
5859 InvokeRuntimeCallingConvention calling_convention;
5860 CodeGenerator::CreateLoadClassLocationSummary(
5861 cls,
5862 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
5863 Location::RegisterLocation(EAX),
5864 /* code_generator_supports_read_barrier */ true);
5865 return;
5866 }
5867
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005868 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
5869 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005870 ? LocationSummary::kCallOnSlowPath
5871 : LocationSummary::kNoCall;
5872 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005873 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005874 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01005875 }
5876
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005877 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
5878 if (load_kind == HLoadClass::LoadKind::kReferrersClass ||
5879 load_kind == HLoadClass::LoadKind::kDexCacheViaMethod ||
5880 load_kind == HLoadClass::LoadKind::kBootImageLinkTimePcRelative ||
5881 load_kind == HLoadClass::LoadKind::kDexCachePcRelative) {
5882 locations->SetInAt(0, Location::RequiresRegister());
5883 }
5884 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005885}
5886
5887void InstructionCodeGeneratorX86::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005888 LocationSummary* locations = cls->GetLocations();
Calin Juravle98893e12015-10-02 21:05:03 +01005889 if (cls->NeedsAccessCheck()) {
5890 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex());
Serban Constantinescuba45db02016-07-12 22:53:02 +01005891 codegen_->InvokeRuntime(kQuickInitializeTypeAndVerifyAccess, cls, cls->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00005892 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Calin Juravle580b6092015-10-06 17:35:58 +01005893 return;
5894 }
5895
Roland Levillain0d5a2812015-11-13 10:07:31 +00005896 Location out_loc = locations->Out();
5897 Register out = out_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005898
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005899 bool generate_null_check = false;
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005900 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005901 switch (cls->GetLoadKind()) {
5902 case HLoadClass::LoadKind::kReferrersClass: {
5903 DCHECK(!cls->CanCallRuntime());
5904 DCHECK(!cls->MustGenerateClinitCheck());
5905 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5906 Register current_method = locations->InAt(0).AsRegister<Register>();
5907 GenerateGcRootFieldLoad(
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005908 cls,
5909 out_loc,
5910 Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()),
5911 /*fixup_label*/ nullptr,
5912 requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005913 break;
5914 }
5915 case HLoadClass::LoadKind::kBootImageLinkTimeAddress: {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005916 DCHECK(!requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005917 __ movl(out, Immediate(/* placeholder */ 0));
5918 codegen_->RecordTypePatch(cls);
5919 break;
5920 }
5921 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005922 DCHECK(!requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005923 Register method_address = locations->InAt(0).AsRegister<Register>();
5924 __ leal(out, Address(method_address, CodeGeneratorX86::kDummy32BitOffset));
5925 codegen_->RecordTypePatch(cls);
5926 break;
5927 }
5928 case HLoadClass::LoadKind::kBootImageAddress: {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005929 DCHECK(!requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005930 DCHECK_NE(cls->GetAddress(), 0u);
5931 uint32_t address = dchecked_integral_cast<uint32_t>(cls->GetAddress());
5932 __ movl(out, Immediate(address));
5933 codegen_->RecordSimplePatch();
5934 break;
5935 }
5936 case HLoadClass::LoadKind::kDexCacheAddress: {
5937 DCHECK_NE(cls->GetAddress(), 0u);
5938 uint32_t address = dchecked_integral_cast<uint32_t>(cls->GetAddress());
5939 // /* GcRoot<mirror::Class> */ out = *address
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005940 GenerateGcRootFieldLoad(cls,
5941 out_loc,
5942 Address::Absolute(address),
5943 /*fixup_label*/ nullptr,
5944 requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005945 generate_null_check = !cls->IsInDexCache();
5946 break;
5947 }
5948 case HLoadClass::LoadKind::kDexCachePcRelative: {
5949 Register base_reg = locations->InAt(0).AsRegister<Register>();
5950 uint32_t offset = cls->GetDexCacheElementOffset();
5951 Label* fixup_label = codegen_->NewPcRelativeDexCacheArrayPatch(cls->GetDexFile(), offset);
5952 // /* GcRoot<mirror::Class> */ out = *(base + offset) /* PC-relative */
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005953 GenerateGcRootFieldLoad(cls,
5954 out_loc,
5955 Address(base_reg, CodeGeneratorX86::kDummy32BitOffset),
5956 fixup_label,
5957 requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005958 generate_null_check = !cls->IsInDexCache();
5959 break;
5960 }
5961 case HLoadClass::LoadKind::kDexCacheViaMethod: {
5962 // /* GcRoot<mirror::Class>[] */ out =
5963 // current_method.ptr_sized_fields_->dex_cache_resolved_types_
5964 Register current_method = locations->InAt(0).AsRegister<Register>();
5965 __ movl(out, Address(current_method,
5966 ArtMethod::DexCacheResolvedTypesOffset(kX86PointerSize).Int32Value()));
5967 // /* GcRoot<mirror::Class> */ out = out[type_index]
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005968 GenerateGcRootFieldLoad(cls,
5969 out_loc,
5970 Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())),
5971 /*fixup_label*/ nullptr,
5972 requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005973 generate_null_check = !cls->IsInDexCache();
5974 break;
5975 }
5976 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005977
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005978 if (generate_null_check || cls->MustGenerateClinitCheck()) {
5979 DCHECK(cls->CanCallRuntime());
5980 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
5981 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
5982 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005983
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005984 if (generate_null_check) {
5985 __ testl(out, out);
5986 __ j(kEqual, slow_path->GetEntryLabel());
5987 }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005988
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005989 if (cls->MustGenerateClinitCheck()) {
5990 GenerateClassInitializationCheck(slow_path, out);
5991 } else {
5992 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005993 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005994 }
5995}
5996
5997void LocationsBuilderX86::VisitClinitCheck(HClinitCheck* check) {
5998 LocationSummary* locations =
5999 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
6000 locations->SetInAt(0, Location::RequiresRegister());
6001 if (check->HasUses()) {
6002 locations->SetOut(Location::SameAsFirstInput());
6003 }
6004}
6005
6006void InstructionCodeGeneratorX86::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006007 // We assume the class to not be null.
Andreas Gampe85b62f22015-09-09 13:15:38 -07006008 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006009 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006010 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00006011 GenerateClassInitializationCheck(slow_path,
6012 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006013}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006014
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006015void InstructionCodeGeneratorX86::GenerateClassInitializationCheck(
Andreas Gampe85b62f22015-09-09 13:15:38 -07006016 SlowPathCode* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006017 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
6018 Immediate(mirror::Class::kStatusInitialized));
6019 __ j(kLess, slow_path->GetEntryLabel());
6020 __ Bind(slow_path->GetExitLabel());
6021 // No need for memory fence, thanks to the X86 memory model.
6022}
6023
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006024HLoadString::LoadKind CodeGeneratorX86::GetSupportedLoadStringKind(
6025 HLoadString::LoadKind desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006026 switch (desired_string_load_kind) {
6027 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
6028 DCHECK(!GetCompilerOptions().GetCompilePic());
6029 break;
6030 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
6031 DCHECK(GetCompilerOptions().GetCompilePic());
6032 FALLTHROUGH_INTENDED;
Vladimir Markoaad75c62016-10-03 08:46:48 +00006033 case HLoadString::LoadKind::kBssEntry:
Calin Juravleffc87072016-04-20 14:22:09 +01006034 DCHECK(!Runtime::Current()->UseJitCompilation()); // Note: boot image is also non-JIT.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006035 // We disable pc-relative load when there is an irreducible loop, as the optimization
6036 // is incompatible with it.
6037 // TODO: Create as many X86ComputeBaseMethodAddress instructions as needed for methods
6038 // with irreducible loops.
6039 if (GetGraph()->HasIrreducibleLoops()) {
6040 return HLoadString::LoadKind::kDexCacheViaMethod;
6041 }
6042 break;
6043 case HLoadString::LoadKind::kBootImageAddress:
6044 break;
6045 case HLoadString::LoadKind::kDexCacheAddress:
Calin Juravleffc87072016-04-20 14:22:09 +01006046 DCHECK(Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006047 break;
6048 case HLoadString::LoadKind::kDexCacheViaMethod:
6049 break;
6050 }
6051 return desired_string_load_kind;
6052}
6053
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006054void LocationsBuilderX86::VisitLoadString(HLoadString* load) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006055 LocationSummary::CallKind call_kind = (load->NeedsEnvironment() || kEmitCompilerReadBarrier)
Vladimir Markoaad75c62016-10-03 08:46:48 +00006056 ? ((load->GetLoadKind() == HLoadString::LoadKind::kDexCacheViaMethod)
6057 ? LocationSummary::kCallOnMainOnly
6058 : LocationSummary::kCallOnSlowPath)
Nicolas Geoffray917d0162015-11-24 18:25:35 +00006059 : LocationSummary::kNoCall;
6060 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006061 HLoadString::LoadKind load_kind = load->GetLoadKind();
6062 if (load_kind == HLoadString::LoadKind::kDexCacheViaMethod ||
6063 load_kind == HLoadString::LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markoaad75c62016-10-03 08:46:48 +00006064 load_kind == HLoadString::LoadKind::kBssEntry) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006065 locations->SetInAt(0, Location::RequiresRegister());
6066 }
Christina Wadsworth175d09b2016-08-31 16:26:01 -07006067 if (load_kind == HLoadString::LoadKind::kDexCacheViaMethod) {
6068 locations->SetOut(Location::RegisterLocation(EAX));
6069 } else {
6070 locations->SetOut(Location::RequiresRegister());
6071 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006072}
6073
6074void InstructionCodeGeneratorX86::VisitLoadString(HLoadString* load) {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01006075 LocationSummary* locations = load->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006076 Location out_loc = locations->Out();
6077 Register out = out_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006078
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006079 switch (load->GetLoadKind()) {
6080 case HLoadString::LoadKind::kBootImageLinkTimeAddress: {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006081 __ movl(out, Immediate(/* placeholder */ 0));
Vladimir Markoaad75c62016-10-03 08:46:48 +00006082 codegen_->RecordBootStringPatch(load);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006083 return; // No dex cache slow path.
6084 }
6085 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006086 Register method_address = locations->InAt(0).AsRegister<Register>();
6087 __ leal(out, Address(method_address, CodeGeneratorX86::kDummy32BitOffset));
Vladimir Markoaad75c62016-10-03 08:46:48 +00006088 codegen_->RecordBootStringPatch(load);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006089 return; // No dex cache slow path.
6090 }
6091 case HLoadString::LoadKind::kBootImageAddress: {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006092 DCHECK_NE(load->GetAddress(), 0u);
6093 uint32_t address = dchecked_integral_cast<uint32_t>(load->GetAddress());
6094 __ movl(out, Immediate(address));
6095 codegen_->RecordSimplePatch();
6096 return; // No dex cache slow path.
6097 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00006098 case HLoadString::LoadKind::kBssEntry: {
6099 Register method_address = locations->InAt(0).AsRegister<Register>();
6100 Address address = Address(method_address, CodeGeneratorX86::kDummy32BitOffset);
6101 Label* fixup_label = codegen_->NewStringBssEntryPatch(load);
6102 // /* GcRoot<mirror::Class> */ out = *address /* PC-relative */
6103 GenerateGcRootFieldLoad(load, out_loc, address, fixup_label);
6104 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86(load);
6105 codegen_->AddSlowPath(slow_path);
6106 __ testl(out, out);
6107 __ j(kEqual, slow_path->GetEntryLabel());
6108 __ Bind(slow_path->GetExitLabel());
6109 return;
6110 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006111 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07006112 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006113 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006114
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07006115 // TODO: Re-add the compiler code to do string dex cache lookup again.
Christina Wadsworth175d09b2016-08-31 16:26:01 -07006116 InvokeRuntimeCallingConvention calling_convention;
6117 __ movl(calling_convention.GetRegisterAt(0), Immediate(load->GetStringIndex()));
6118 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
6119 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006120}
6121
David Brazdilcb1c0552015-08-04 16:22:25 +01006122static Address GetExceptionTlsAddress() {
Andreas Gampe542451c2016-07-26 09:02:02 -07006123 return Address::Absolute(Thread::ExceptionOffset<kX86PointerSize>().Int32Value());
David Brazdilcb1c0552015-08-04 16:22:25 +01006124}
6125
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006126void LocationsBuilderX86::VisitLoadException(HLoadException* load) {
6127 LocationSummary* locations =
6128 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
6129 locations->SetOut(Location::RequiresRegister());
6130}
6131
6132void InstructionCodeGeneratorX86::VisitLoadException(HLoadException* load) {
David Brazdilcb1c0552015-08-04 16:22:25 +01006133 __ fs()->movl(load->GetLocations()->Out().AsRegister<Register>(), GetExceptionTlsAddress());
6134}
6135
6136void LocationsBuilderX86::VisitClearException(HClearException* clear) {
6137 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
6138}
6139
6140void InstructionCodeGeneratorX86::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
6141 __ fs()->movl(GetExceptionTlsAddress(), Immediate(0));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006142}
6143
6144void LocationsBuilderX86::VisitThrow(HThrow* instruction) {
6145 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006146 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006147 InvokeRuntimeCallingConvention calling_convention;
6148 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6149}
6150
6151void InstructionCodeGeneratorX86::VisitThrow(HThrow* instruction) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01006152 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006153 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006154}
6155
Roland Levillain7c1559a2015-12-15 10:55:36 +00006156static bool TypeCheckNeedsATemporary(TypeCheckKind type_check_kind) {
6157 return kEmitCompilerReadBarrier &&
Vladimir Marko953437b2016-08-24 08:30:46 +00006158 !kUseBakerReadBarrier &&
6159 (type_check_kind == TypeCheckKind::kAbstractClassCheck ||
Roland Levillain7c1559a2015-12-15 10:55:36 +00006160 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
6161 type_check_kind == TypeCheckKind::kArrayObjectCheck);
6162}
6163
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006164void LocationsBuilderX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006165 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain0d5a2812015-11-13 10:07:31 +00006166 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01006167 bool baker_read_barrier_slow_path = false;
Roland Levillain0d5a2812015-11-13 10:07:31 +00006168 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006169 case TypeCheckKind::kExactCheck:
6170 case TypeCheckKind::kAbstractClassCheck:
6171 case TypeCheckKind::kClassHierarchyCheck:
6172 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006173 call_kind =
6174 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Vladimir Marko70e97462016-08-09 11:04:26 +01006175 baker_read_barrier_slow_path = kUseBakerReadBarrier;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006176 break;
6177 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006178 case TypeCheckKind::kUnresolvedCheck:
6179 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006180 call_kind = LocationSummary::kCallOnSlowPath;
6181 break;
6182 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006183
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006184 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01006185 if (baker_read_barrier_slow_path) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006186 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01006187 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006188 locations->SetInAt(0, Location::RequiresRegister());
6189 locations->SetInAt(1, Location::Any());
6190 // Note that TypeCheckSlowPathX86 uses this "out" register too.
6191 locations->SetOut(Location::RequiresRegister());
6192 // When read barriers are enabled, we need a temporary register for
6193 // some cases.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006194 if (TypeCheckNeedsATemporary(type_check_kind)) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006195 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006196 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006197}
6198
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006199void InstructionCodeGeneratorX86::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006200 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006201 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006202 Location obj_loc = locations->InAt(0);
6203 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006204 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006205 Location out_loc = locations->Out();
6206 Register out = out_loc.AsRegister<Register>();
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006207 Location maybe_temp_loc = TypeCheckNeedsATemporary(type_check_kind) ?
Roland Levillain7c1559a2015-12-15 10:55:36 +00006208 locations->GetTemp(0) :
6209 Location::NoLocation();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006210 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006211 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6212 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6213 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Andreas Gampe85b62f22015-09-09 13:15:38 -07006214 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006215 NearLabel done, zero;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006216
6217 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006218 // Avoid null check if we know obj is not null.
6219 if (instruction->MustDoNullCheck()) {
6220 __ testl(obj, obj);
6221 __ j(kEqual, &zero);
6222 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006223
Roland Levillain0d5a2812015-11-13 10:07:31 +00006224 // /* HeapReference<Class> */ out = obj->klass_
Vladimir Marko953437b2016-08-24 08:30:46 +00006225 GenerateReferenceLoadTwoRegisters(instruction, out_loc, obj_loc, class_offset);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006226
Roland Levillain7c1559a2015-12-15 10:55:36 +00006227 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006228 case TypeCheckKind::kExactCheck: {
6229 if (cls.IsRegister()) {
6230 __ cmpl(out, cls.AsRegister<Register>());
6231 } else {
6232 DCHECK(cls.IsStackSlot()) << cls;
6233 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6234 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006235
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006236 // Classes must be equal for the instanceof to succeed.
6237 __ j(kNotEqual, &zero);
6238 __ movl(out, Immediate(1));
6239 __ jmp(&done);
6240 break;
6241 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006242
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006243 case TypeCheckKind::kAbstractClassCheck: {
6244 // If the class is abstract, we eagerly fetch the super class of the
6245 // object to avoid doing a comparison we know will fail.
6246 NearLabel loop;
6247 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006248 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006249 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006250 __ testl(out, out);
6251 // If `out` is null, we use it for the result, and jump to `done`.
6252 __ j(kEqual, &done);
6253 if (cls.IsRegister()) {
6254 __ cmpl(out, cls.AsRegister<Register>());
6255 } else {
6256 DCHECK(cls.IsStackSlot()) << cls;
6257 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6258 }
6259 __ j(kNotEqual, &loop);
6260 __ movl(out, Immediate(1));
6261 if (zero.IsLinked()) {
6262 __ jmp(&done);
6263 }
6264 break;
6265 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006266
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006267 case TypeCheckKind::kClassHierarchyCheck: {
6268 // Walk over the class hierarchy to find a match.
6269 NearLabel loop, success;
6270 __ Bind(&loop);
6271 if (cls.IsRegister()) {
6272 __ cmpl(out, cls.AsRegister<Register>());
6273 } else {
6274 DCHECK(cls.IsStackSlot()) << cls;
6275 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6276 }
6277 __ j(kEqual, &success);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006278 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006279 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006280 __ testl(out, out);
6281 __ j(kNotEqual, &loop);
6282 // If `out` is null, we use it for the result, and jump to `done`.
6283 __ jmp(&done);
6284 __ Bind(&success);
6285 __ movl(out, Immediate(1));
6286 if (zero.IsLinked()) {
6287 __ jmp(&done);
6288 }
6289 break;
6290 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006291
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006292 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006293 // Do an exact check.
6294 NearLabel exact_check;
6295 if (cls.IsRegister()) {
6296 __ cmpl(out, cls.AsRegister<Register>());
6297 } else {
6298 DCHECK(cls.IsStackSlot()) << cls;
6299 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6300 }
6301 __ j(kEqual, &exact_check);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006302 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006303 // /* HeapReference<Class> */ out = out->component_type_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006304 GenerateReferenceLoadOneRegister(instruction, out_loc, component_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006305 __ testl(out, out);
6306 // If `out` is null, we use it for the result, and jump to `done`.
6307 __ j(kEqual, &done);
6308 __ cmpw(Address(out, primitive_offset), Immediate(Primitive::kPrimNot));
6309 __ j(kNotEqual, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006310 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006311 __ movl(out, Immediate(1));
6312 __ jmp(&done);
6313 break;
6314 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006315
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006316 case TypeCheckKind::kArrayCheck: {
6317 if (cls.IsRegister()) {
6318 __ cmpl(out, cls.AsRegister<Register>());
6319 } else {
6320 DCHECK(cls.IsStackSlot()) << cls;
6321 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6322 }
6323 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain0d5a2812015-11-13 10:07:31 +00006324 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
6325 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006326 codegen_->AddSlowPath(slow_path);
6327 __ j(kNotEqual, slow_path->GetEntryLabel());
6328 __ movl(out, Immediate(1));
6329 if (zero.IsLinked()) {
6330 __ jmp(&done);
6331 }
6332 break;
6333 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006334
Calin Juravle98893e12015-10-02 21:05:03 +01006335 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006336 case TypeCheckKind::kInterfaceCheck: {
6337 // Note that we indeed only call on slow path, but we always go
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006338 // into the slow path for the unresolved and interface check
Roland Levillain0d5a2812015-11-13 10:07:31 +00006339 // cases.
6340 //
6341 // We cannot directly call the InstanceofNonTrivial runtime
6342 // entry point without resorting to a type checking slow path
6343 // here (i.e. by calling InvokeRuntime directly), as it would
6344 // require to assign fixed registers for the inputs of this
6345 // HInstanceOf instruction (following the runtime calling
6346 // convention), which might be cluttered by the potential first
6347 // read barrier emission at the beginning of this method.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006348 //
6349 // TODO: Introduce a new runtime entry point taking the object
6350 // to test (instead of its class) as argument, and let it deal
6351 // with the read barrier issues. This will let us refactor this
6352 // case of the `switch` code as it was previously (with a direct
6353 // call to the runtime not using a type checking slow path).
6354 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006355 DCHECK(locations->OnlyCallsOnSlowPath());
6356 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
6357 /* is_fatal */ false);
6358 codegen_->AddSlowPath(slow_path);
6359 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006360 if (zero.IsLinked()) {
6361 __ jmp(&done);
6362 }
6363 break;
6364 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006365 }
6366
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006367 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006368 __ Bind(&zero);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006369 __ xorl(out, out);
6370 }
6371
6372 if (done.IsLinked()) {
6373 __ Bind(&done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006374 }
6375
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006376 if (slow_path != nullptr) {
6377 __ Bind(slow_path->GetExitLabel());
6378 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006379}
6380
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006381void LocationsBuilderX86::VisitCheckCast(HCheckCast* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006382 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
6383 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006384 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
6385 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006386 case TypeCheckKind::kExactCheck:
6387 case TypeCheckKind::kAbstractClassCheck:
6388 case TypeCheckKind::kClassHierarchyCheck:
6389 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006390 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
6391 LocationSummary::kCallOnSlowPath :
6392 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006393 break;
6394 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006395 case TypeCheckKind::kUnresolvedCheck:
6396 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006397 call_kind = LocationSummary::kCallOnSlowPath;
6398 break;
6399 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006400 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
6401 locations->SetInAt(0, Location::RequiresRegister());
6402 locations->SetInAt(1, Location::Any());
6403 // Note that TypeCheckSlowPathX86 uses this "temp" register too.
6404 locations->AddTemp(Location::RequiresRegister());
6405 // When read barriers are enabled, we need an additional temporary
6406 // register for some cases.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006407 if (TypeCheckNeedsATemporary(type_check_kind)) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006408 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006409 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006410}
6411
6412void InstructionCodeGeneratorX86::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006413 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006414 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006415 Location obj_loc = locations->InAt(0);
6416 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006417 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006418 Location temp_loc = locations->GetTemp(0);
6419 Register temp = temp_loc.AsRegister<Register>();
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006420 Location maybe_temp2_loc = TypeCheckNeedsATemporary(type_check_kind) ?
Roland Levillain7c1559a2015-12-15 10:55:36 +00006421 locations->GetTemp(1) :
6422 Location::NoLocation();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006423 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
6424 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6425 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6426 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006427
Roland Levillain0d5a2812015-11-13 10:07:31 +00006428 bool is_type_check_slow_path_fatal =
6429 (type_check_kind == TypeCheckKind::kExactCheck ||
6430 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
6431 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
6432 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
6433 !instruction->CanThrowIntoCatchBlock();
6434 SlowPathCode* type_check_slow_path =
6435 new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
6436 is_type_check_slow_path_fatal);
6437 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006438
Roland Levillain0d5a2812015-11-13 10:07:31 +00006439 NearLabel done;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006440 // Avoid null check if we know obj is not null.
6441 if (instruction->MustDoNullCheck()) {
6442 __ testl(obj, obj);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006443 __ j(kEqual, &done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006444 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006445
Roland Levillain0d5a2812015-11-13 10:07:31 +00006446 // /* HeapReference<Class> */ temp = obj->klass_
Vladimir Marko953437b2016-08-24 08:30:46 +00006447 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006448
Roland Levillain0d5a2812015-11-13 10:07:31 +00006449 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006450 case TypeCheckKind::kExactCheck:
6451 case TypeCheckKind::kArrayCheck: {
6452 if (cls.IsRegister()) {
6453 __ cmpl(temp, cls.AsRegister<Register>());
6454 } else {
6455 DCHECK(cls.IsStackSlot()) << cls;
6456 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6457 }
6458 // Jump to slow path for throwing the exception or doing a
6459 // more involved array check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006460 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006461 break;
6462 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006463
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006464 case TypeCheckKind::kAbstractClassCheck: {
6465 // If the class is abstract, we eagerly fetch the super class of the
6466 // object to avoid doing a comparison we know will fail.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006467 NearLabel loop, compare_classes;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006468 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006469 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006470 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006471
6472 // If the class reference currently in `temp` is not null, jump
6473 // to the `compare_classes` label to compare it with the checked
6474 // class.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006475 __ testl(temp, temp);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006476 __ j(kNotEqual, &compare_classes);
6477 // Otherwise, jump to the slow path to throw the exception.
6478 //
6479 // But before, move back the object's class into `temp` before
6480 // going into the slow path, as it has been overwritten in the
6481 // meantime.
6482 // /* HeapReference<Class> */ temp = obj->klass_
Vladimir Marko953437b2016-08-24 08:30:46 +00006483 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006484 __ jmp(type_check_slow_path->GetEntryLabel());
6485
6486 __ Bind(&compare_classes);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006487 if (cls.IsRegister()) {
6488 __ cmpl(temp, cls.AsRegister<Register>());
6489 } else {
6490 DCHECK(cls.IsStackSlot()) << cls;
6491 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6492 }
6493 __ j(kNotEqual, &loop);
6494 break;
6495 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006496
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006497 case TypeCheckKind::kClassHierarchyCheck: {
6498 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006499 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006500 __ Bind(&loop);
6501 if (cls.IsRegister()) {
6502 __ cmpl(temp, cls.AsRegister<Register>());
6503 } else {
6504 DCHECK(cls.IsStackSlot()) << cls;
6505 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6506 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006507 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006508
Roland Levillain0d5a2812015-11-13 10:07:31 +00006509 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006510 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006511
6512 // If the class reference currently in `temp` is not null, jump
6513 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006514 __ testl(temp, temp);
6515 __ j(kNotEqual, &loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006516 // Otherwise, jump to the slow path to throw the exception.
6517 //
6518 // But before, move back the object's class into `temp` before
6519 // going into the slow path, as it has been overwritten in the
6520 // meantime.
6521 // /* HeapReference<Class> */ temp = obj->klass_
Vladimir Marko953437b2016-08-24 08:30:46 +00006522 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006523 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006524 break;
6525 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006526
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006527 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006528 // Do an exact check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006529 NearLabel check_non_primitive_component_type;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006530 if (cls.IsRegister()) {
6531 __ cmpl(temp, cls.AsRegister<Register>());
6532 } else {
6533 DCHECK(cls.IsStackSlot()) << cls;
6534 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6535 }
6536 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006537
6538 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006539 // /* HeapReference<Class> */ temp = temp->component_type_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006540 GenerateReferenceLoadOneRegister(instruction, temp_loc, component_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006541
6542 // If the component type is not null (i.e. the object is indeed
6543 // an array), jump to label `check_non_primitive_component_type`
6544 // to further check that this component type is not a primitive
6545 // type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006546 __ testl(temp, temp);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006547 __ j(kNotEqual, &check_non_primitive_component_type);
6548 // Otherwise, jump to the slow path to throw the exception.
6549 //
6550 // But before, move back the object's class into `temp` before
6551 // going into the slow path, as it has been overwritten in the
6552 // meantime.
6553 // /* HeapReference<Class> */ temp = obj->klass_
Vladimir Marko953437b2016-08-24 08:30:46 +00006554 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006555 __ jmp(type_check_slow_path->GetEntryLabel());
6556
6557 __ Bind(&check_non_primitive_component_type);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006558 __ cmpw(Address(temp, primitive_offset), Immediate(Primitive::kPrimNot));
Roland Levillain0d5a2812015-11-13 10:07:31 +00006559 __ j(kEqual, &done);
6560 // Same comment as above regarding `temp` and the slow path.
6561 // /* HeapReference<Class> */ temp = obj->klass_
Vladimir Marko953437b2016-08-24 08:30:46 +00006562 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006563 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006564 break;
6565 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006566
Calin Juravle98893e12015-10-02 21:05:03 +01006567 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006568 case TypeCheckKind::kInterfaceCheck:
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006569 // We always go into the type check slow path for the unresolved
6570 // and interface check cases.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006571 //
6572 // We cannot directly call the CheckCast runtime entry point
6573 // without resorting to a type checking slow path here (i.e. by
6574 // calling InvokeRuntime directly), as it would require to
6575 // assign fixed registers for the inputs of this HInstanceOf
6576 // instruction (following the runtime calling convention), which
6577 // might be cluttered by the potential first read barrier
6578 // emission at the beginning of this method.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006579 //
6580 // TODO: Introduce a new runtime entry point taking the object
6581 // to test (instead of its class) as argument, and let it deal
6582 // with the read barrier issues. This will let us refactor this
6583 // case of the `switch` code as it was previously (with a direct
6584 // call to the runtime not using a type checking slow path).
6585 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006586 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006587 break;
6588 }
6589 __ Bind(&done);
6590
Roland Levillain0d5a2812015-11-13 10:07:31 +00006591 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006592}
6593
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006594void LocationsBuilderX86::VisitMonitorOperation(HMonitorOperation* instruction) {
6595 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006596 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006597 InvokeRuntimeCallingConvention calling_convention;
6598 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6599}
6600
6601void InstructionCodeGeneratorX86::VisitMonitorOperation(HMonitorOperation* instruction) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01006602 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject
6603 : kQuickUnlockObject,
Alexandre Rames8158f282015-08-07 10:26:17 +01006604 instruction,
Serban Constantinescuba45db02016-07-12 22:53:02 +01006605 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006606 if (instruction->IsEnter()) {
6607 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
6608 } else {
6609 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
6610 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006611}
6612
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006613void LocationsBuilderX86::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
6614void LocationsBuilderX86::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
6615void LocationsBuilderX86::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
6616
6617void LocationsBuilderX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
6618 LocationSummary* locations =
6619 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6620 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
6621 || instruction->GetResultType() == Primitive::kPrimLong);
6622 locations->SetInAt(0, Location::RequiresRegister());
6623 locations->SetInAt(1, Location::Any());
6624 locations->SetOut(Location::SameAsFirstInput());
6625}
6626
6627void InstructionCodeGeneratorX86::VisitAnd(HAnd* instruction) {
6628 HandleBitwiseOperation(instruction);
6629}
6630
6631void InstructionCodeGeneratorX86::VisitOr(HOr* instruction) {
6632 HandleBitwiseOperation(instruction);
6633}
6634
6635void InstructionCodeGeneratorX86::VisitXor(HXor* instruction) {
6636 HandleBitwiseOperation(instruction);
6637}
6638
6639void InstructionCodeGeneratorX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
6640 LocationSummary* locations = instruction->GetLocations();
6641 Location first = locations->InAt(0);
6642 Location second = locations->InAt(1);
6643 DCHECK(first.Equals(locations->Out()));
6644
6645 if (instruction->GetResultType() == Primitive::kPrimInt) {
6646 if (second.IsRegister()) {
6647 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006648 __ andl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006649 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006650 __ orl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006651 } else {
6652 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006653 __ xorl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006654 }
6655 } else if (second.IsConstant()) {
6656 if (instruction->IsAnd()) {
Roland Levillain199f3362014-11-27 17:15:16 +00006657 __ andl(first.AsRegister<Register>(),
6658 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006659 } else if (instruction->IsOr()) {
Roland Levillain199f3362014-11-27 17:15:16 +00006660 __ orl(first.AsRegister<Register>(),
6661 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006662 } else {
6663 DCHECK(instruction->IsXor());
Roland Levillain199f3362014-11-27 17:15:16 +00006664 __ xorl(first.AsRegister<Register>(),
6665 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006666 }
6667 } else {
6668 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006669 __ andl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006670 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006671 __ orl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006672 } else {
6673 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006674 __ xorl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006675 }
6676 }
6677 } else {
6678 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
6679 if (second.IsRegisterPair()) {
6680 if (instruction->IsAnd()) {
6681 __ andl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
6682 __ andl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
6683 } else if (instruction->IsOr()) {
6684 __ orl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
6685 __ orl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
6686 } else {
6687 DCHECK(instruction->IsXor());
6688 __ xorl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
6689 __ xorl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
6690 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006691 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006692 if (instruction->IsAnd()) {
6693 __ andl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
6694 __ andl(first.AsRegisterPairHigh<Register>(),
6695 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
6696 } else if (instruction->IsOr()) {
6697 __ orl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
6698 __ orl(first.AsRegisterPairHigh<Register>(),
6699 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
6700 } else {
6701 DCHECK(instruction->IsXor());
6702 __ xorl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
6703 __ xorl(first.AsRegisterPairHigh<Register>(),
6704 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
6705 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006706 } else {
6707 DCHECK(second.IsConstant()) << second;
6708 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006709 int32_t low_value = Low32Bits(value);
6710 int32_t high_value = High32Bits(value);
6711 Immediate low(low_value);
6712 Immediate high(high_value);
6713 Register first_low = first.AsRegisterPairLow<Register>();
6714 Register first_high = first.AsRegisterPairHigh<Register>();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006715 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006716 if (low_value == 0) {
6717 __ xorl(first_low, first_low);
6718 } else if (low_value != -1) {
6719 __ andl(first_low, low);
6720 }
6721 if (high_value == 0) {
6722 __ xorl(first_high, first_high);
6723 } else if (high_value != -1) {
6724 __ andl(first_high, high);
6725 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006726 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006727 if (low_value != 0) {
6728 __ orl(first_low, low);
6729 }
6730 if (high_value != 0) {
6731 __ orl(first_high, high);
6732 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006733 } else {
6734 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006735 if (low_value != 0) {
6736 __ xorl(first_low, low);
6737 }
6738 if (high_value != 0) {
6739 __ xorl(first_high, high);
6740 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006741 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006742 }
6743 }
6744}
6745
Roland Levillain7c1559a2015-12-15 10:55:36 +00006746void InstructionCodeGeneratorX86::GenerateReferenceLoadOneRegister(HInstruction* instruction,
6747 Location out,
6748 uint32_t offset,
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006749 Location maybe_temp) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006750 Register out_reg = out.AsRegister<Register>();
6751 if (kEmitCompilerReadBarrier) {
6752 if (kUseBakerReadBarrier) {
6753 // Load with fast path based Baker's read barrier.
6754 // /* HeapReference<Object> */ out = *(out + offset)
6755 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00006756 instruction, out, out_reg, offset, /* needs_null_check */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006757 } else {
6758 // Load with slow path based read barrier.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006759 // Save the value of `out` into `maybe_temp` before overwriting it
Roland Levillain7c1559a2015-12-15 10:55:36 +00006760 // in the following move operation, as we will need it for the
6761 // read barrier below.
Vladimir Marko953437b2016-08-24 08:30:46 +00006762 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006763 __ movl(maybe_temp.AsRegister<Register>(), out_reg);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006764 // /* HeapReference<Object> */ out = *(out + offset)
6765 __ movl(out_reg, Address(out_reg, offset));
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006766 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006767 }
6768 } else {
6769 // Plain load with no read barrier.
6770 // /* HeapReference<Object> */ out = *(out + offset)
6771 __ movl(out_reg, Address(out_reg, offset));
6772 __ MaybeUnpoisonHeapReference(out_reg);
6773 }
6774}
6775
6776void InstructionCodeGeneratorX86::GenerateReferenceLoadTwoRegisters(HInstruction* instruction,
6777 Location out,
6778 Location obj,
Vladimir Marko953437b2016-08-24 08:30:46 +00006779 uint32_t offset) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006780 Register out_reg = out.AsRegister<Register>();
6781 Register obj_reg = obj.AsRegister<Register>();
6782 if (kEmitCompilerReadBarrier) {
6783 if (kUseBakerReadBarrier) {
6784 // Load with fast path based Baker's read barrier.
6785 // /* HeapReference<Object> */ out = *(obj + offset)
6786 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00006787 instruction, out, obj_reg, offset, /* needs_null_check */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006788 } else {
6789 // Load with slow path based read barrier.
6790 // /* HeapReference<Object> */ out = *(obj + offset)
6791 __ movl(out_reg, Address(obj_reg, offset));
6792 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
6793 }
6794 } else {
6795 // Plain load with no read barrier.
6796 // /* HeapReference<Object> */ out = *(obj + offset)
6797 __ movl(out_reg, Address(obj_reg, offset));
6798 __ MaybeUnpoisonHeapReference(out_reg);
6799 }
6800}
6801
6802void InstructionCodeGeneratorX86::GenerateGcRootFieldLoad(HInstruction* instruction,
6803 Location root,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006804 const Address& address,
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006805 Label* fixup_label,
6806 bool requires_read_barrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006807 Register root_reg = root.AsRegister<Register>();
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006808 if (requires_read_barrier) {
6809 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006810 if (kUseBakerReadBarrier) {
6811 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
6812 // Baker's read barrier are used:
6813 //
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006814 // root = *address;
Roland Levillain7c1559a2015-12-15 10:55:36 +00006815 // if (Thread::Current()->GetIsGcMarking()) {
6816 // root = ReadBarrier::Mark(root)
6817 // }
6818
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006819 // /* GcRoot<mirror::Object> */ root = *address
6820 __ movl(root_reg, address);
6821 if (fixup_label != nullptr) {
6822 __ Bind(fixup_label);
6823 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00006824 static_assert(
6825 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
6826 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
6827 "have different sizes.");
6828 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
6829 "art::mirror::CompressedReference<mirror::Object> and int32_t "
6830 "have different sizes.");
6831
Vladimir Marko953437b2016-08-24 08:30:46 +00006832 // Slow path marking the GC root `root`.
6833 SlowPathCode* slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathX86(
6834 instruction, root, /* unpoison */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006835 codegen_->AddSlowPath(slow_path);
6836
Andreas Gampe542451c2016-07-26 09:02:02 -07006837 __ fs()->cmpl(Address::Absolute(Thread::IsGcMarkingOffset<kX86PointerSize>().Int32Value()),
Roland Levillain7c1559a2015-12-15 10:55:36 +00006838 Immediate(0));
6839 __ j(kNotEqual, slow_path->GetEntryLabel());
6840 __ Bind(slow_path->GetExitLabel());
6841 } else {
6842 // GC root loaded through a slow path for read barriers other
6843 // than Baker's.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006844 // /* GcRoot<mirror::Object>* */ root = address
6845 __ leal(root_reg, address);
6846 if (fixup_label != nullptr) {
6847 __ Bind(fixup_label);
6848 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00006849 // /* mirror::Object* */ root = root->Read()
6850 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
6851 }
6852 } else {
6853 // Plain GC root load with no read barrier.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006854 // /* GcRoot<mirror::Object> */ root = *address
6855 __ movl(root_reg, address);
6856 if (fixup_label != nullptr) {
6857 __ Bind(fixup_label);
6858 }
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006859 // Note that GC roots are not affected by heap poisoning, thus we
6860 // do not have to unpoison `root_reg` here.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006861 }
6862}
6863
6864void CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
6865 Location ref,
6866 Register obj,
6867 uint32_t offset,
Roland Levillain7c1559a2015-12-15 10:55:36 +00006868 bool needs_null_check) {
6869 DCHECK(kEmitCompilerReadBarrier);
6870 DCHECK(kUseBakerReadBarrier);
6871
6872 // /* HeapReference<Object> */ ref = *(obj + offset)
6873 Address src(obj, offset);
Vladimir Marko953437b2016-08-24 08:30:46 +00006874 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, needs_null_check);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006875}
6876
6877void CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
6878 Location ref,
6879 Register obj,
6880 uint32_t data_offset,
6881 Location index,
Roland Levillain7c1559a2015-12-15 10:55:36 +00006882 bool needs_null_check) {
6883 DCHECK(kEmitCompilerReadBarrier);
6884 DCHECK(kUseBakerReadBarrier);
6885
Roland Levillain3d312422016-06-23 13:53:42 +01006886 static_assert(
6887 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
6888 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain7c1559a2015-12-15 10:55:36 +00006889 // /* HeapReference<Object> */ ref =
6890 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006891 Address src = CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset);
Vladimir Marko953437b2016-08-24 08:30:46 +00006892 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, needs_null_check);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006893}
6894
6895void CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
6896 Location ref,
6897 Register obj,
6898 const Address& src,
Roland Levillain7c1559a2015-12-15 10:55:36 +00006899 bool needs_null_check) {
6900 DCHECK(kEmitCompilerReadBarrier);
6901 DCHECK(kUseBakerReadBarrier);
6902
6903 // In slow path based read barriers, the read barrier call is
6904 // inserted after the original load. However, in fast path based
6905 // Baker's read barriers, we need to perform the load of
6906 // mirror::Object::monitor_ *before* the original reference load.
6907 // This load-load ordering is required by the read barrier.
6908 // The fast path/slow path (for Baker's algorithm) should look like:
6909 //
6910 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
6911 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
6912 // HeapReference<Object> ref = *src; // Original reference load.
6913 // bool is_gray = (rb_state == ReadBarrier::gray_ptr_);
6914 // if (is_gray) {
6915 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
6916 // }
6917 //
6918 // Note: the original implementation in ReadBarrier::Barrier is
6919 // slightly more complex as:
6920 // - it implements the load-load fence using a data dependency on
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006921 // the high-bits of rb_state, which are expected to be all zeroes
6922 // (we use CodeGeneratorX86::GenerateMemoryBarrier instead here,
6923 // which is a no-op thanks to the x86 memory model);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006924 // - it performs additional checks that we do not do here for
6925 // performance reasons.
6926
6927 Register ref_reg = ref.AsRegister<Register>();
Roland Levillain7c1559a2015-12-15 10:55:36 +00006928 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
6929
Vladimir Marko953437b2016-08-24 08:30:46 +00006930 // Given the numeric representation, it's enough to check the low bit of the rb_state.
6931 static_assert(ReadBarrier::white_ptr_ == 0, "Expecting white to have value 0");
6932 static_assert(ReadBarrier::gray_ptr_ == 1, "Expecting gray to have value 1");
6933 static_assert(ReadBarrier::black_ptr_ == 2, "Expecting black to have value 2");
6934 constexpr uint32_t gray_byte_position = LockWord::kReadBarrierStateShift / kBitsPerByte;
6935 constexpr uint32_t gray_bit_position = LockWord::kReadBarrierStateShift % kBitsPerByte;
6936 constexpr int32_t test_value = static_cast<int8_t>(1 << gray_bit_position);
6937
6938 // if (rb_state == ReadBarrier::gray_ptr_)
6939 // ref = ReadBarrier::Mark(ref);
6940 // At this point, just do the "if" and make sure that flags are preserved until the branch.
6941 __ testb(Address(obj, monitor_offset + gray_byte_position), Immediate(test_value));
Roland Levillain7c1559a2015-12-15 10:55:36 +00006942 if (needs_null_check) {
6943 MaybeRecordImplicitNullCheck(instruction);
6944 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00006945
6946 // Load fence to prevent load-load reordering.
6947 // Note that this is a no-op, thanks to the x86 memory model.
6948 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
6949
6950 // The actual reference load.
6951 // /* HeapReference<Object> */ ref = *src
Vladimir Marko953437b2016-08-24 08:30:46 +00006952 __ movl(ref_reg, src); // Flags are unaffected.
6953
6954 // Note: Reference unpoisoning modifies the flags, so we need to delay it after the branch.
6955 // Slow path marking the object `ref` when it is gray.
6956 SlowPathCode* slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathX86(
6957 instruction, ref, /* unpoison */ true);
6958 AddSlowPath(slow_path);
6959
6960 // We have done the "if" of the gray bit check above, now branch based on the flags.
6961 __ j(kNotZero, slow_path->GetEntryLabel());
Roland Levillain7c1559a2015-12-15 10:55:36 +00006962
6963 // Object* ref = ref_addr->AsMirrorPtr()
6964 __ MaybeUnpoisonHeapReference(ref_reg);
6965
Roland Levillain7c1559a2015-12-15 10:55:36 +00006966 __ Bind(slow_path->GetExitLabel());
6967}
6968
6969void CodeGeneratorX86::GenerateReadBarrierSlow(HInstruction* instruction,
6970 Location out,
6971 Location ref,
6972 Location obj,
6973 uint32_t offset,
6974 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006975 DCHECK(kEmitCompilerReadBarrier);
6976
Roland Levillain7c1559a2015-12-15 10:55:36 +00006977 // Insert a slow path based read barrier *after* the reference load.
6978 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00006979 // If heap poisoning is enabled, the unpoisoning of the loaded
6980 // reference will be carried out by the runtime within the slow
6981 // path.
6982 //
6983 // Note that `ref` currently does not get unpoisoned (when heap
6984 // poisoning is enabled), which is alright as the `ref` argument is
6985 // not used by the artReadBarrierSlow entry point.
6986 //
6987 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
6988 SlowPathCode* slow_path = new (GetGraph()->GetArena())
6989 ReadBarrierForHeapReferenceSlowPathX86(instruction, out, ref, obj, offset, index);
6990 AddSlowPath(slow_path);
6991
Roland Levillain0d5a2812015-11-13 10:07:31 +00006992 __ jmp(slow_path->GetEntryLabel());
6993 __ Bind(slow_path->GetExitLabel());
6994}
6995
Roland Levillain7c1559a2015-12-15 10:55:36 +00006996void CodeGeneratorX86::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
6997 Location out,
6998 Location ref,
6999 Location obj,
7000 uint32_t offset,
7001 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007002 if (kEmitCompilerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007003 // Baker's read barriers shall be handled by the fast path
7004 // (CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier).
7005 DCHECK(!kUseBakerReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007006 // If heap poisoning is enabled, unpoisoning will be taken care of
7007 // by the runtime within the slow path.
Roland Levillain7c1559a2015-12-15 10:55:36 +00007008 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007009 } else if (kPoisonHeapReferences) {
7010 __ UnpoisonHeapReference(out.AsRegister<Register>());
7011 }
7012}
7013
Roland Levillain7c1559a2015-12-15 10:55:36 +00007014void CodeGeneratorX86::GenerateReadBarrierForRootSlow(HInstruction* instruction,
7015 Location out,
7016 Location root) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007017 DCHECK(kEmitCompilerReadBarrier);
7018
Roland Levillain7c1559a2015-12-15 10:55:36 +00007019 // Insert a slow path based read barrier *after* the GC root load.
7020 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00007021 // Note that GC roots are not affected by heap poisoning, so we do
7022 // not need to do anything special for this here.
7023 SlowPathCode* slow_path =
7024 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathX86(instruction, out, root);
7025 AddSlowPath(slow_path);
7026
Roland Levillain0d5a2812015-11-13 10:07:31 +00007027 __ jmp(slow_path->GetEntryLabel());
7028 __ Bind(slow_path->GetExitLabel());
7029}
7030
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007031void LocationsBuilderX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00007032 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00007033 LOG(FATAL) << "Unreachable";
7034}
7035
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007036void InstructionCodeGeneratorX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00007037 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00007038 LOG(FATAL) << "Unreachable";
7039}
7040
Mark Mendellfe57faa2015-09-18 09:26:15 -04007041// Simple implementation of packed switch - generate cascaded compare/jumps.
7042void LocationsBuilderX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7043 LocationSummary* locations =
7044 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
7045 locations->SetInAt(0, Location::RequiresRegister());
7046}
7047
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007048void InstructionCodeGeneratorX86::GenPackedSwitchWithCompares(Register value_reg,
7049 int32_t lower_bound,
7050 uint32_t num_entries,
7051 HBasicBlock* switch_block,
7052 HBasicBlock* default_block) {
7053 // Figure out the correct compare values and jump conditions.
7054 // Handle the first compare/branch as a special case because it might
7055 // jump to the default case.
7056 DCHECK_GT(num_entries, 2u);
7057 Condition first_condition;
7058 uint32_t index;
7059 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
7060 if (lower_bound != 0) {
7061 first_condition = kLess;
7062 __ cmpl(value_reg, Immediate(lower_bound));
7063 __ j(first_condition, codegen_->GetLabelOf(default_block));
7064 __ j(kEqual, codegen_->GetLabelOf(successors[0]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007065
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007066 index = 1;
7067 } else {
7068 // Handle all the compare/jumps below.
7069 first_condition = kBelow;
7070 index = 0;
7071 }
7072
7073 // Handle the rest of the compare/jumps.
7074 for (; index + 1 < num_entries; index += 2) {
7075 int32_t compare_to_value = lower_bound + index + 1;
7076 __ cmpl(value_reg, Immediate(compare_to_value));
7077 // Jump to successors[index] if value < case_value[index].
7078 __ j(first_condition, codegen_->GetLabelOf(successors[index]));
7079 // Jump to successors[index + 1] if value == case_value[index + 1].
7080 __ j(kEqual, codegen_->GetLabelOf(successors[index + 1]));
7081 }
7082
7083 if (index != num_entries) {
7084 // There are an odd number of entries. Handle the last one.
7085 DCHECK_EQ(index + 1, num_entries);
7086 __ cmpl(value_reg, Immediate(lower_bound + index));
7087 __ j(kEqual, codegen_->GetLabelOf(successors[index]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007088 }
7089
7090 // And the default for any other value.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007091 if (!codegen_->GoesToNextBlock(switch_block, default_block)) {
7092 __ jmp(codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007093 }
7094}
7095
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007096void InstructionCodeGeneratorX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7097 int32_t lower_bound = switch_instr->GetStartValue();
7098 uint32_t num_entries = switch_instr->GetNumEntries();
7099 LocationSummary* locations = switch_instr->GetLocations();
7100 Register value_reg = locations->InAt(0).AsRegister<Register>();
7101
7102 GenPackedSwitchWithCompares(value_reg,
7103 lower_bound,
7104 num_entries,
7105 switch_instr->GetBlock(),
7106 switch_instr->GetDefaultBlock());
7107}
7108
Mark Mendell805b3b52015-09-18 14:10:29 -04007109void LocationsBuilderX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
7110 LocationSummary* locations =
7111 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
7112 locations->SetInAt(0, Location::RequiresRegister());
7113
7114 // Constant area pointer.
7115 locations->SetInAt(1, Location::RequiresRegister());
7116
7117 // And the temporary we need.
7118 locations->AddTemp(Location::RequiresRegister());
7119}
7120
7121void InstructionCodeGeneratorX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
7122 int32_t lower_bound = switch_instr->GetStartValue();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007123 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendell805b3b52015-09-18 14:10:29 -04007124 LocationSummary* locations = switch_instr->GetLocations();
7125 Register value_reg = locations->InAt(0).AsRegister<Register>();
7126 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
7127
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007128 if (num_entries <= kPackedSwitchJumpTableThreshold) {
7129 GenPackedSwitchWithCompares(value_reg,
7130 lower_bound,
7131 num_entries,
7132 switch_instr->GetBlock(),
7133 default_block);
7134 return;
7135 }
7136
Mark Mendell805b3b52015-09-18 14:10:29 -04007137 // Optimizing has a jump area.
7138 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
7139 Register constant_area = locations->InAt(1).AsRegister<Register>();
7140
7141 // Remove the bias, if needed.
7142 if (lower_bound != 0) {
7143 __ leal(temp_reg, Address(value_reg, -lower_bound));
7144 value_reg = temp_reg;
7145 }
7146
7147 // Is the value in range?
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007148 DCHECK_GE(num_entries, 1u);
Mark Mendell805b3b52015-09-18 14:10:29 -04007149 __ cmpl(value_reg, Immediate(num_entries - 1));
7150 __ j(kAbove, codegen_->GetLabelOf(default_block));
7151
7152 // We are in the range of the table.
7153 // Load (target-constant_area) from the jump table, indexing by the value.
7154 __ movl(temp_reg, codegen_->LiteralCaseTable(switch_instr, constant_area, value_reg));
7155
7156 // Compute the actual target address by adding in constant_area.
7157 __ addl(temp_reg, constant_area);
7158
7159 // And jump.
7160 __ jmp(temp_reg);
7161}
7162
Mark Mendell0616ae02015-04-17 12:49:27 -04007163void LocationsBuilderX86::VisitX86ComputeBaseMethodAddress(
7164 HX86ComputeBaseMethodAddress* insn) {
7165 LocationSummary* locations =
7166 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
7167 locations->SetOut(Location::RequiresRegister());
7168}
7169
7170void InstructionCodeGeneratorX86::VisitX86ComputeBaseMethodAddress(
7171 HX86ComputeBaseMethodAddress* insn) {
7172 LocationSummary* locations = insn->GetLocations();
7173 Register reg = locations->Out().AsRegister<Register>();
7174
7175 // Generate call to next instruction.
7176 Label next_instruction;
7177 __ call(&next_instruction);
7178 __ Bind(&next_instruction);
7179
7180 // Remember this offset for later use with constant area.
7181 codegen_->SetMethodAddressOffset(GetAssembler()->CodeSize());
7182
7183 // Grab the return address off the stack.
7184 __ popl(reg);
7185}
7186
7187void LocationsBuilderX86::VisitX86LoadFromConstantTable(
7188 HX86LoadFromConstantTable* insn) {
7189 LocationSummary* locations =
7190 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
7191
7192 locations->SetInAt(0, Location::RequiresRegister());
7193 locations->SetInAt(1, Location::ConstantLocation(insn->GetConstant()));
7194
7195 // If we don't need to be materialized, we only need the inputs to be set.
David Brazdilb3e773e2016-01-26 11:28:37 +00007196 if (insn->IsEmittedAtUseSite()) {
Mark Mendell0616ae02015-04-17 12:49:27 -04007197 return;
7198 }
7199
7200 switch (insn->GetType()) {
7201 case Primitive::kPrimFloat:
7202 case Primitive::kPrimDouble:
7203 locations->SetOut(Location::RequiresFpuRegister());
7204 break;
7205
7206 case Primitive::kPrimInt:
7207 locations->SetOut(Location::RequiresRegister());
7208 break;
7209
7210 default:
7211 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
7212 }
7213}
7214
7215void InstructionCodeGeneratorX86::VisitX86LoadFromConstantTable(HX86LoadFromConstantTable* insn) {
David Brazdilb3e773e2016-01-26 11:28:37 +00007216 if (insn->IsEmittedAtUseSite()) {
Mark Mendell0616ae02015-04-17 12:49:27 -04007217 return;
7218 }
7219
7220 LocationSummary* locations = insn->GetLocations();
7221 Location out = locations->Out();
7222 Register const_area = locations->InAt(0).AsRegister<Register>();
7223 HConstant *value = insn->GetConstant();
7224
7225 switch (insn->GetType()) {
7226 case Primitive::kPrimFloat:
7227 __ movss(out.AsFpuRegister<XmmRegister>(),
7228 codegen_->LiteralFloatAddress(value->AsFloatConstant()->GetValue(), const_area));
7229 break;
7230
7231 case Primitive::kPrimDouble:
7232 __ movsd(out.AsFpuRegister<XmmRegister>(),
7233 codegen_->LiteralDoubleAddress(value->AsDoubleConstant()->GetValue(), const_area));
7234 break;
7235
7236 case Primitive::kPrimInt:
7237 __ movl(out.AsRegister<Register>(),
7238 codegen_->LiteralInt32Address(value->AsIntConstant()->GetValue(), const_area));
7239 break;
7240
7241 default:
7242 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
7243 }
7244}
7245
Mark Mendell0616ae02015-04-17 12:49:27 -04007246/**
7247 * Class to handle late fixup of offsets into constant area.
7248 */
Vladimir Marko5233f932015-09-29 19:01:15 +01007249class RIPFixup : public AssemblerFixup, public ArenaObject<kArenaAllocCodeGenerator> {
Mark Mendell0616ae02015-04-17 12:49:27 -04007250 public:
Mark Mendell805b3b52015-09-18 14:10:29 -04007251 RIPFixup(CodeGeneratorX86& codegen, size_t offset)
7252 : codegen_(&codegen), offset_into_constant_area_(offset) {}
7253
7254 protected:
7255 void SetOffset(size_t offset) { offset_into_constant_area_ = offset; }
7256
7257 CodeGeneratorX86* codegen_;
Mark Mendell0616ae02015-04-17 12:49:27 -04007258
7259 private:
7260 void Process(const MemoryRegion& region, int pos) OVERRIDE {
7261 // Patch the correct offset for the instruction. The place to patch is the
7262 // last 4 bytes of the instruction.
7263 // The value to patch is the distance from the offset in the constant area
7264 // from the address computed by the HX86ComputeBaseMethodAddress instruction.
Mark Mendell805b3b52015-09-18 14:10:29 -04007265 int32_t constant_offset = codegen_->ConstantAreaStart() + offset_into_constant_area_;
7266 int32_t relative_position = constant_offset - codegen_->GetMethodAddressOffset();;
Mark Mendell0616ae02015-04-17 12:49:27 -04007267
7268 // Patch in the right value.
7269 region.StoreUnaligned<int32_t>(pos - 4, relative_position);
7270 }
7271
Mark Mendell0616ae02015-04-17 12:49:27 -04007272 // Location in constant area that the fixup refers to.
Mark Mendell805b3b52015-09-18 14:10:29 -04007273 int32_t offset_into_constant_area_;
Mark Mendell0616ae02015-04-17 12:49:27 -04007274};
7275
Mark Mendell805b3b52015-09-18 14:10:29 -04007276/**
7277 * Class to handle late fixup of offsets to a jump table that will be created in the
7278 * constant area.
7279 */
7280class JumpTableRIPFixup : public RIPFixup {
7281 public:
7282 JumpTableRIPFixup(CodeGeneratorX86& codegen, HX86PackedSwitch* switch_instr)
7283 : RIPFixup(codegen, static_cast<size_t>(-1)), switch_instr_(switch_instr) {}
7284
7285 void CreateJumpTable() {
7286 X86Assembler* assembler = codegen_->GetAssembler();
7287
7288 // Ensure that the reference to the jump table has the correct offset.
7289 const int32_t offset_in_constant_table = assembler->ConstantAreaSize();
7290 SetOffset(offset_in_constant_table);
7291
7292 // The label values in the jump table are computed relative to the
7293 // instruction addressing the constant area.
7294 const int32_t relative_offset = codegen_->GetMethodAddressOffset();
7295
7296 // Populate the jump table with the correct values for the jump table.
7297 int32_t num_entries = switch_instr_->GetNumEntries();
7298 HBasicBlock* block = switch_instr_->GetBlock();
7299 const ArenaVector<HBasicBlock*>& successors = block->GetSuccessors();
7300 // The value that we want is the target offset - the position of the table.
7301 for (int32_t i = 0; i < num_entries; i++) {
7302 HBasicBlock* b = successors[i];
7303 Label* l = codegen_->GetLabelOf(b);
7304 DCHECK(l->IsBound());
7305 int32_t offset_to_block = l->Position() - relative_offset;
7306 assembler->AppendInt32(offset_to_block);
7307 }
7308 }
7309
7310 private:
7311 const HX86PackedSwitch* switch_instr_;
7312};
7313
7314void CodeGeneratorX86::Finalize(CodeAllocator* allocator) {
7315 // Generate the constant area if needed.
7316 X86Assembler* assembler = GetAssembler();
7317 if (!assembler->IsConstantAreaEmpty() || !fixups_to_jump_tables_.empty()) {
7318 // Align to 4 byte boundary to reduce cache misses, as the data is 4 and 8
7319 // byte values.
7320 assembler->Align(4, 0);
7321 constant_area_start_ = assembler->CodeSize();
7322
7323 // Populate any jump tables.
7324 for (auto jump_table : fixups_to_jump_tables_) {
7325 jump_table->CreateJumpTable();
7326 }
7327
7328 // And now add the constant area to the generated code.
7329 assembler->AddConstantArea();
7330 }
7331
7332 // And finish up.
7333 CodeGenerator::Finalize(allocator);
7334}
7335
Mark Mendell0616ae02015-04-17 12:49:27 -04007336Address CodeGeneratorX86::LiteralDoubleAddress(double v, Register reg) {
7337 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddDouble(v));
7338 return Address(reg, kDummy32BitOffset, fixup);
7339}
7340
7341Address CodeGeneratorX86::LiteralFloatAddress(float v, Register reg) {
7342 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddFloat(v));
7343 return Address(reg, kDummy32BitOffset, fixup);
7344}
7345
7346Address CodeGeneratorX86::LiteralInt32Address(int32_t v, Register reg) {
7347 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt32(v));
7348 return Address(reg, kDummy32BitOffset, fixup);
7349}
7350
7351Address CodeGeneratorX86::LiteralInt64Address(int64_t v, Register reg) {
7352 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt64(v));
7353 return Address(reg, kDummy32BitOffset, fixup);
7354}
7355
Aart Bika19616e2016-02-01 18:57:58 -08007356void CodeGeneratorX86::Load32BitValue(Register dest, int32_t value) {
7357 if (value == 0) {
7358 __ xorl(dest, dest);
7359 } else {
7360 __ movl(dest, Immediate(value));
7361 }
7362}
7363
7364void CodeGeneratorX86::Compare32BitValue(Register dest, int32_t value) {
7365 if (value == 0) {
7366 __ testl(dest, dest);
7367 } else {
7368 __ cmpl(dest, Immediate(value));
7369 }
7370}
7371
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007372void CodeGeneratorX86::GenerateIntCompare(Location lhs, Location rhs) {
7373 Register lhs_reg = lhs.AsRegister<Register>();
jessicahandojo4877b792016-09-08 19:49:13 -07007374 GenerateIntCompare(lhs_reg, rhs);
7375}
7376
7377void CodeGeneratorX86::GenerateIntCompare(Register lhs, Location rhs) {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007378 if (rhs.IsConstant()) {
7379 int32_t value = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
jessicahandojo4877b792016-09-08 19:49:13 -07007380 Compare32BitValue(lhs, value);
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007381 } else if (rhs.IsStackSlot()) {
jessicahandojo4877b792016-09-08 19:49:13 -07007382 __ cmpl(lhs, Address(ESP, rhs.GetStackIndex()));
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007383 } else {
jessicahandojo4877b792016-09-08 19:49:13 -07007384 __ cmpl(lhs, rhs.AsRegister<Register>());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007385 }
7386}
7387
7388Address CodeGeneratorX86::ArrayAddress(Register obj,
7389 Location index,
7390 ScaleFactor scale,
7391 uint32_t data_offset) {
7392 return index.IsConstant() ?
7393 Address(obj, (index.GetConstant()->AsIntConstant()->GetValue() << scale) + data_offset) :
7394 Address(obj, index.AsRegister<Register>(), scale, data_offset);
7395}
7396
Mark Mendell805b3b52015-09-18 14:10:29 -04007397Address CodeGeneratorX86::LiteralCaseTable(HX86PackedSwitch* switch_instr,
7398 Register reg,
7399 Register value) {
7400 // Create a fixup to be used to create and address the jump table.
7401 JumpTableRIPFixup* table_fixup =
7402 new (GetGraph()->GetArena()) JumpTableRIPFixup(*this, switch_instr);
7403
7404 // We have to populate the jump tables.
7405 fixups_to_jump_tables_.push_back(table_fixup);
7406
7407 // We want a scaled address, as we are extracting the correct offset from the table.
7408 return Address(reg, value, TIMES_4, kDummy32BitOffset, table_fixup);
7409}
7410
Andreas Gampe85b62f22015-09-09 13:15:38 -07007411// TODO: target as memory.
7412void CodeGeneratorX86::MoveFromReturnRegister(Location target, Primitive::Type type) {
7413 if (!target.IsValid()) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007414 DCHECK_EQ(type, Primitive::kPrimVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07007415 return;
7416 }
7417
7418 DCHECK_NE(type, Primitive::kPrimVoid);
7419
7420 Location return_loc = InvokeDexCallingConventionVisitorX86().GetReturnLocation(type);
7421 if (target.Equals(return_loc)) {
7422 return;
7423 }
7424
7425 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
7426 // with the else branch.
7427 if (type == Primitive::kPrimLong) {
7428 HParallelMove parallel_move(GetGraph()->GetArena());
7429 parallel_move.AddMove(return_loc.ToLow(), target.ToLow(), Primitive::kPrimInt, nullptr);
7430 parallel_move.AddMove(return_loc.ToHigh(), target.ToHigh(), Primitive::kPrimInt, nullptr);
7431 GetMoveResolver()->EmitNativeCode(&parallel_move);
7432 } else {
7433 // Let the parallel move resolver take care of all of this.
7434 HParallelMove parallel_move(GetGraph()->GetArena());
7435 parallel_move.AddMove(return_loc, target, type, nullptr);
7436 GetMoveResolver()->EmitNativeCode(&parallel_move);
7437 }
7438}
7439
Roland Levillain4d027112015-07-01 15:41:14 +01007440#undef __
7441
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00007442} // namespace x86
7443} // namespace art