blob: 960f01ce9dbcbc576938f3a9f2c0266d3e8456c4 [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 Geoffray96eeb4e2016-10-12 22:03:31 +0100901 // Save the current method if we need it. Note that we do not
902 // do this in HCurrentMethod, as the instruction might have been removed
903 // in the SSA graph.
904 if (RequiresCurrentMethod()) {
905 __ movl(Address(ESP, kCurrentMethodStackOffset), kMethodRegisterArgument);
906 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000907}
908
909void CodeGeneratorX86::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +0100910 __ cfi().RememberState();
911 if (!HasEmptyFrame()) {
912 int adjust = GetFrameSize() - FrameEntrySpillSize();
913 __ addl(ESP, Immediate(adjust));
914 __ cfi().AdjustCFAOffset(-adjust);
Mark Mendell5f874182015-03-04 15:42:45 -0500915
David Srbeckyc34dc932015-04-12 09:27:43 +0100916 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
917 Register reg = kCoreCalleeSaves[i];
918 if (allocated_registers_.ContainsCoreRegister(reg)) {
919 __ popl(reg);
920 __ cfi().AdjustCFAOffset(-static_cast<int>(kX86WordSize));
921 __ cfi().Restore(DWARFReg(reg));
922 }
Mark Mendell5f874182015-03-04 15:42:45 -0500923 }
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000924 }
David Srbeckyc34dc932015-04-12 09:27:43 +0100925 __ ret();
926 __ cfi().RestoreState();
927 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000928}
929
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100930void CodeGeneratorX86::Bind(HBasicBlock* block) {
931 __ Bind(GetLabelOf(block));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000932}
933
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100934Location InvokeDexCallingConventionVisitorX86::GetReturnLocation(Primitive::Type type) const {
935 switch (type) {
936 case Primitive::kPrimBoolean:
937 case Primitive::kPrimByte:
938 case Primitive::kPrimChar:
939 case Primitive::kPrimShort:
940 case Primitive::kPrimInt:
941 case Primitive::kPrimNot:
942 return Location::RegisterLocation(EAX);
943
944 case Primitive::kPrimLong:
945 return Location::RegisterPairLocation(EAX, EDX);
946
947 case Primitive::kPrimVoid:
948 return Location::NoLocation();
949
950 case Primitive::kPrimDouble:
951 case Primitive::kPrimFloat:
952 return Location::FpuRegisterLocation(XMM0);
953 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +0100954
955 UNREACHABLE();
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100956}
957
958Location InvokeDexCallingConventionVisitorX86::GetMethodLocation() const {
959 return Location::RegisterLocation(kMethodRegisterArgument);
960}
961
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100962Location InvokeDexCallingConventionVisitorX86::GetNextLocation(Primitive::Type type) {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100963 switch (type) {
964 case Primitive::kPrimBoolean:
965 case Primitive::kPrimByte:
966 case Primitive::kPrimChar:
967 case Primitive::kPrimShort:
968 case Primitive::kPrimInt:
969 case Primitive::kPrimNot: {
970 uint32_t index = gp_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000971 stack_index_++;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100972 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100973 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100974 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000975 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100976 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100977 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100978
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000979 case Primitive::kPrimLong: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100980 uint32_t index = gp_index_;
981 gp_index_ += 2;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000982 stack_index_ += 2;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100983 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100984 X86ManagedRegister pair = X86ManagedRegister::FromRegisterPair(
985 calling_convention.GetRegisterPairAt(index));
986 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100987 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000988 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
989 }
990 }
991
992 case Primitive::kPrimFloat: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100993 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000994 stack_index_++;
995 if (index < calling_convention.GetNumberOfFpuRegisters()) {
996 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
997 } else {
998 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
999 }
1000 }
1001
1002 case Primitive::kPrimDouble: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001003 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001004 stack_index_ += 2;
1005 if (index < calling_convention.GetNumberOfFpuRegisters()) {
1006 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
1007 } else {
1008 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001009 }
1010 }
1011
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001012 case Primitive::kPrimVoid:
1013 LOG(FATAL) << "Unexpected parameter type " << type;
1014 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001015 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00001016 return Location::NoLocation();
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001017}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001018
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001019void CodeGeneratorX86::Move32(Location destination, Location source) {
1020 if (source.Equals(destination)) {
1021 return;
1022 }
1023 if (destination.IsRegister()) {
1024 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001025 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001026 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001027 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001028 } else {
1029 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001030 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001031 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001032 } else if (destination.IsFpuRegister()) {
1033 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001034 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001035 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001036 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001037 } else {
1038 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001039 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001040 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001041 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001042 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001043 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001044 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001045 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001046 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Mark Mendell7c8d0092015-01-26 11:21:33 -05001047 } else if (source.IsConstant()) {
1048 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001049 int32_t value = GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05001050 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001051 } else {
1052 DCHECK(source.IsStackSlot());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001053 __ pushl(Address(ESP, source.GetStackIndex()));
1054 __ popl(Address(ESP, destination.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001055 }
1056 }
1057}
1058
1059void CodeGeneratorX86::Move64(Location destination, Location source) {
1060 if (source.Equals(destination)) {
1061 return;
1062 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001063 if (destination.IsRegisterPair()) {
1064 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001065 EmitParallelMoves(
1066 Location::RegisterLocation(source.AsRegisterPairHigh<Register>()),
1067 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001068 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001069 Location::RegisterLocation(source.AsRegisterPairLow<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001070 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()),
1071 Primitive::kPrimInt);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001072 } else if (source.IsFpuRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001073 XmmRegister src_reg = source.AsFpuRegister<XmmRegister>();
1074 __ movd(destination.AsRegisterPairLow<Register>(), src_reg);
1075 __ psrlq(src_reg, Immediate(32));
1076 __ movd(destination.AsRegisterPairHigh<Register>(), src_reg);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001077 } else {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001078 // No conflict possible, so just do the moves.
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001079 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001080 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
1081 __ movl(destination.AsRegisterPairHigh<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001082 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
1083 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001084 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05001085 if (source.IsFpuRegister()) {
1086 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
1087 } else if (source.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001088 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01001089 } else if (source.IsRegisterPair()) {
1090 size_t elem_size = Primitive::ComponentSize(Primitive::kPrimInt);
1091 // Create stack space for 2 elements.
1092 __ subl(ESP, Immediate(2 * elem_size));
1093 __ movl(Address(ESP, 0), source.AsRegisterPairLow<Register>());
1094 __ movl(Address(ESP, elem_size), source.AsRegisterPairHigh<Register>());
1095 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
1096 // And remove the temporary stack space we allocated.
1097 __ addl(ESP, Immediate(2 * elem_size));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001098 } else {
1099 LOG(FATAL) << "Unimplemented";
1100 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001101 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001102 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001103 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001104 // No conflict possible, so just do the moves.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001105 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegisterPairLow<Register>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001106 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001107 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001108 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001109 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001110 } else if (source.IsConstant()) {
1111 HConstant* constant = source.GetConstant();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001112 DCHECK(constant->IsLongConstant() || constant->IsDoubleConstant());
1113 int64_t value = GetInt64ValueOf(constant);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001114 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(Low32Bits(value)));
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001115 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
1116 Immediate(High32Bits(value)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001117 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001118 DCHECK(source.IsDoubleStackSlot()) << source;
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001119 EmitParallelMoves(
1120 Location::StackSlot(source.GetStackIndex()),
1121 Location::StackSlot(destination.GetStackIndex()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001122 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001123 Location::StackSlot(source.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001124 Location::StackSlot(destination.GetHighStackIndex(kX86WordSize)),
1125 Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001126 }
1127 }
1128}
1129
Calin Juravle175dc732015-08-25 15:42:32 +01001130void CodeGeneratorX86::MoveConstant(Location location, int32_t value) {
1131 DCHECK(location.IsRegister());
1132 __ movl(location.AsRegister<Register>(), Immediate(value));
1133}
1134
Calin Juravlee460d1d2015-09-29 04:52:17 +01001135void CodeGeneratorX86::MoveLocation(Location dst, Location src, Primitive::Type dst_type) {
David Brazdil74eb1b22015-12-14 11:44:01 +00001136 HParallelMove move(GetGraph()->GetArena());
1137 if (dst_type == Primitive::kPrimLong && !src.IsConstant() && !src.IsFpuRegister()) {
1138 move.AddMove(src.ToLow(), dst.ToLow(), Primitive::kPrimInt, nullptr);
1139 move.AddMove(src.ToHigh(), dst.ToHigh(), Primitive::kPrimInt, nullptr);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001140 } else {
David Brazdil74eb1b22015-12-14 11:44:01 +00001141 move.AddMove(src, dst, dst_type, nullptr);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001142 }
David Brazdil74eb1b22015-12-14 11:44:01 +00001143 GetMoveResolver()->EmitNativeCode(&move);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001144}
1145
1146void CodeGeneratorX86::AddLocationAsTemp(Location location, LocationSummary* locations) {
1147 if (location.IsRegister()) {
1148 locations->AddTemp(location);
1149 } else if (location.IsRegisterPair()) {
1150 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
1151 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
1152 } else {
1153 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1154 }
1155}
1156
David Brazdilfc6a86a2015-06-26 10:33:45 +00001157void InstructionCodeGeneratorX86::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001158 DCHECK(!successor->IsExitBlock());
1159
1160 HBasicBlock* block = got->GetBlock();
1161 HInstruction* previous = got->GetPrevious();
1162
1163 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +00001164 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001165 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1166 return;
1167 }
1168
1169 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1170 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1171 }
1172 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001173 __ jmp(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001174 }
1175}
1176
David Brazdilfc6a86a2015-06-26 10:33:45 +00001177void LocationsBuilderX86::VisitGoto(HGoto* got) {
1178 got->SetLocations(nullptr);
1179}
1180
1181void InstructionCodeGeneratorX86::VisitGoto(HGoto* got) {
1182 HandleGoto(got, got->GetSuccessor());
1183}
1184
1185void LocationsBuilderX86::VisitTryBoundary(HTryBoundary* try_boundary) {
1186 try_boundary->SetLocations(nullptr);
1187}
1188
1189void InstructionCodeGeneratorX86::VisitTryBoundary(HTryBoundary* try_boundary) {
1190 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
1191 if (!successor->IsExitBlock()) {
1192 HandleGoto(try_boundary, successor);
1193 }
1194}
1195
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001196void LocationsBuilderX86::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001197 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001198}
1199
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001200void InstructionCodeGeneratorX86::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001201}
1202
Mark Mendell152408f2015-12-31 12:28:50 -05001203template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001204void InstructionCodeGeneratorX86::GenerateFPJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001205 LabelType* true_label,
1206 LabelType* false_label) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001207 if (cond->IsFPConditionTrueIfNaN()) {
1208 __ j(kUnordered, true_label);
1209 } else if (cond->IsFPConditionFalseIfNaN()) {
1210 __ j(kUnordered, false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001211 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001212 __ j(X86UnsignedOrFPCondition(cond->GetCondition()), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001213}
1214
Mark Mendell152408f2015-12-31 12:28:50 -05001215template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001216void InstructionCodeGeneratorX86::GenerateLongComparesAndJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001217 LabelType* true_label,
1218 LabelType* false_label) {
Mark Mendellc4701932015-04-10 13:18:51 -04001219 LocationSummary* locations = cond->GetLocations();
1220 Location left = locations->InAt(0);
1221 Location right = locations->InAt(1);
1222 IfCondition if_cond = cond->GetCondition();
1223
Mark Mendellc4701932015-04-10 13:18:51 -04001224 Register left_high = left.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001225 Register left_low = left.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001226 IfCondition true_high_cond = if_cond;
1227 IfCondition false_high_cond = cond->GetOppositeCondition();
Aart Bike9f37602015-10-09 11:15:55 -07001228 Condition final_condition = X86UnsignedOrFPCondition(if_cond); // unsigned on lower part
Mark Mendellc4701932015-04-10 13:18:51 -04001229
1230 // Set the conditions for the test, remembering that == needs to be
1231 // decided using the low words.
1232 switch (if_cond) {
1233 case kCondEQ:
Mark Mendellc4701932015-04-10 13:18:51 -04001234 case kCondNE:
Roland Levillain4fa13f62015-07-06 18:11:54 +01001235 // Nothing to do.
Mark Mendellc4701932015-04-10 13:18:51 -04001236 break;
1237 case kCondLT:
1238 false_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -04001239 break;
1240 case kCondLE:
1241 true_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -04001242 break;
1243 case kCondGT:
1244 false_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -04001245 break;
1246 case kCondGE:
1247 true_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -04001248 break;
Aart Bike9f37602015-10-09 11:15:55 -07001249 case kCondB:
1250 false_high_cond = kCondA;
1251 break;
1252 case kCondBE:
1253 true_high_cond = kCondB;
1254 break;
1255 case kCondA:
1256 false_high_cond = kCondB;
1257 break;
1258 case kCondAE:
1259 true_high_cond = kCondA;
1260 break;
Mark Mendellc4701932015-04-10 13:18:51 -04001261 }
1262
1263 if (right.IsConstant()) {
1264 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
Mark Mendellc4701932015-04-10 13:18:51 -04001265 int32_t val_high = High32Bits(value);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001266 int32_t val_low = Low32Bits(value);
Mark Mendellc4701932015-04-10 13:18:51 -04001267
Aart Bika19616e2016-02-01 18:57:58 -08001268 codegen_->Compare32BitValue(left_high, val_high);
Mark Mendellc4701932015-04-10 13:18:51 -04001269 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001270 __ j(X86Condition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001271 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001272 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001273 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001274 __ j(X86Condition(true_high_cond), true_label);
1275 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001276 }
1277 // Must be equal high, so compare the lows.
Aart Bika19616e2016-02-01 18:57:58 -08001278 codegen_->Compare32BitValue(left_low, val_low);
Mark Mendell8659e842016-02-16 10:41:46 -05001279 } else if (right.IsRegisterPair()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001280 Register right_high = right.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001281 Register right_low = right.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001282
1283 __ cmpl(left_high, right_high);
1284 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001285 __ j(X86Condition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001286 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001287 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001288 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001289 __ j(X86Condition(true_high_cond), true_label);
1290 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001291 }
1292 // Must be equal high, so compare the lows.
1293 __ cmpl(left_low, right_low);
Mark Mendell8659e842016-02-16 10:41:46 -05001294 } else {
1295 DCHECK(right.IsDoubleStackSlot());
1296 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
1297 if (if_cond == kCondNE) {
1298 __ j(X86Condition(true_high_cond), true_label);
1299 } else if (if_cond == kCondEQ) {
1300 __ j(X86Condition(false_high_cond), false_label);
1301 } else {
1302 __ j(X86Condition(true_high_cond), true_label);
1303 __ j(X86Condition(false_high_cond), false_label);
1304 }
1305 // Must be equal high, so compare the lows.
1306 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Mark Mendellc4701932015-04-10 13:18:51 -04001307 }
1308 // The last comparison might be unsigned.
1309 __ j(final_condition, true_label);
1310}
1311
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001312void InstructionCodeGeneratorX86::GenerateFPCompare(Location lhs,
1313 Location rhs,
1314 HInstruction* insn,
1315 bool is_double) {
1316 HX86LoadFromConstantTable* const_area = insn->InputAt(1)->AsX86LoadFromConstantTable();
1317 if (is_double) {
1318 if (rhs.IsFpuRegister()) {
1319 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1320 } else if (const_area != nullptr) {
1321 DCHECK(const_area->IsEmittedAtUseSite());
1322 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(),
1323 codegen_->LiteralDoubleAddress(
1324 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
1325 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
1326 } else {
1327 DCHECK(rhs.IsDoubleStackSlot());
1328 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(), Address(ESP, rhs.GetStackIndex()));
1329 }
1330 } else {
1331 if (rhs.IsFpuRegister()) {
1332 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1333 } else if (const_area != nullptr) {
1334 DCHECK(const_area->IsEmittedAtUseSite());
1335 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(),
1336 codegen_->LiteralFloatAddress(
1337 const_area->GetConstant()->AsFloatConstant()->GetValue(),
1338 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
1339 } else {
1340 DCHECK(rhs.IsStackSlot());
1341 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(), Address(ESP, rhs.GetStackIndex()));
1342 }
1343 }
1344}
1345
Mark Mendell152408f2015-12-31 12:28:50 -05001346template<class LabelType>
David Brazdil0debae72015-11-12 18:37:00 +00001347void InstructionCodeGeneratorX86::GenerateCompareTestAndBranch(HCondition* condition,
Mark Mendell152408f2015-12-31 12:28:50 -05001348 LabelType* true_target_in,
1349 LabelType* false_target_in) {
David Brazdil0debae72015-11-12 18:37:00 +00001350 // Generated branching requires both targets to be explicit. If either of the
1351 // targets is nullptr (fallthrough) use and bind `fallthrough_target` instead.
Mark Mendell152408f2015-12-31 12:28:50 -05001352 LabelType fallthrough_target;
1353 LabelType* true_target = true_target_in == nullptr ? &fallthrough_target : true_target_in;
1354 LabelType* false_target = false_target_in == nullptr ? &fallthrough_target : false_target_in;
David Brazdil0debae72015-11-12 18:37:00 +00001355
Mark Mendellc4701932015-04-10 13:18:51 -04001356 LocationSummary* locations = condition->GetLocations();
1357 Location left = locations->InAt(0);
1358 Location right = locations->InAt(1);
1359
Mark Mendellc4701932015-04-10 13:18:51 -04001360 Primitive::Type type = condition->InputAt(0)->GetType();
1361 switch (type) {
1362 case Primitive::kPrimLong:
1363 GenerateLongComparesAndJumps(condition, true_target, false_target);
1364 break;
1365 case Primitive::kPrimFloat:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001366 GenerateFPCompare(left, right, condition, false);
Mark Mendellc4701932015-04-10 13:18:51 -04001367 GenerateFPJumps(condition, true_target, false_target);
1368 break;
1369 case Primitive::kPrimDouble:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001370 GenerateFPCompare(left, right, condition, true);
Mark Mendellc4701932015-04-10 13:18:51 -04001371 GenerateFPJumps(condition, true_target, false_target);
1372 break;
1373 default:
1374 LOG(FATAL) << "Unexpected compare type " << type;
1375 }
1376
David Brazdil0debae72015-11-12 18:37:00 +00001377 if (false_target != &fallthrough_target) {
Mark Mendellc4701932015-04-10 13:18:51 -04001378 __ jmp(false_target);
1379 }
David Brazdil0debae72015-11-12 18:37:00 +00001380
1381 if (fallthrough_target.IsLinked()) {
1382 __ Bind(&fallthrough_target);
1383 }
Mark Mendellc4701932015-04-10 13:18:51 -04001384}
1385
David Brazdil0debae72015-11-12 18:37:00 +00001386static bool AreEflagsSetFrom(HInstruction* cond, HInstruction* branch) {
1387 // Moves may affect the eflags register (move zero uses xorl), so the EFLAGS
1388 // are set only strictly before `branch`. We can't use the eflags on long/FP
1389 // conditions if they are materialized due to the complex branching.
1390 return cond->IsCondition() &&
1391 cond->GetNext() == branch &&
1392 cond->InputAt(0)->GetType() != Primitive::kPrimLong &&
1393 !Primitive::IsFloatingPointType(cond->InputAt(0)->GetType());
1394}
1395
Mark Mendell152408f2015-12-31 12:28:50 -05001396template<class LabelType>
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001397void InstructionCodeGeneratorX86::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00001398 size_t condition_input_index,
Mark Mendell152408f2015-12-31 12:28:50 -05001399 LabelType* true_target,
1400 LabelType* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00001401 HInstruction* cond = instruction->InputAt(condition_input_index);
1402
1403 if (true_target == nullptr && false_target == nullptr) {
1404 // Nothing to do. The code always falls through.
1405 return;
1406 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00001407 // Constant condition, statically compared against "true" (integer value 1).
1408 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00001409 if (true_target != nullptr) {
1410 __ jmp(true_target);
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001411 }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001412 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00001413 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00001414 if (false_target != nullptr) {
1415 __ jmp(false_target);
1416 }
1417 }
1418 return;
1419 }
1420
1421 // The following code generates these patterns:
1422 // (1) true_target == nullptr && false_target != nullptr
1423 // - opposite condition true => branch to false_target
1424 // (2) true_target != nullptr && false_target == nullptr
1425 // - condition true => branch to true_target
1426 // (3) true_target != nullptr && false_target != nullptr
1427 // - condition true => branch to true_target
1428 // - branch to false_target
1429 if (IsBooleanValueOrMaterializedCondition(cond)) {
1430 if (AreEflagsSetFrom(cond, instruction)) {
1431 if (true_target == nullptr) {
1432 __ j(X86Condition(cond->AsCondition()->GetOppositeCondition()), false_target);
1433 } else {
1434 __ j(X86Condition(cond->AsCondition()->GetCondition()), true_target);
1435 }
1436 } else {
1437 // Materialized condition, compare against 0.
1438 Location lhs = instruction->GetLocations()->InAt(condition_input_index);
1439 if (lhs.IsRegister()) {
1440 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
1441 } else {
1442 __ cmpl(Address(ESP, lhs.GetStackIndex()), Immediate(0));
1443 }
1444 if (true_target == nullptr) {
1445 __ j(kEqual, false_target);
1446 } else {
1447 __ j(kNotEqual, true_target);
1448 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001449 }
1450 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001451 // Condition has not been materialized, use its inputs as the comparison and
1452 // its condition as the branch condition.
Mark Mendellb8b97692015-05-22 16:58:19 -04001453 HCondition* condition = cond->AsCondition();
David Brazdil0debae72015-11-12 18:37:00 +00001454
1455 // If this is a long or FP comparison that has been folded into
1456 // the HCondition, generate the comparison directly.
1457 Primitive::Type type = condition->InputAt(0)->GetType();
1458 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
1459 GenerateCompareTestAndBranch(condition, true_target, false_target);
1460 return;
1461 }
1462
1463 Location lhs = condition->GetLocations()->InAt(0);
1464 Location rhs = condition->GetLocations()->InAt(1);
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001465 // LHS is guaranteed to be in a register (see LocationsBuilderX86::HandleCondition).
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001466 codegen_->GenerateIntCompare(lhs, rhs);
David Brazdil0debae72015-11-12 18:37:00 +00001467 if (true_target == nullptr) {
1468 __ j(X86Condition(condition->GetOppositeCondition()), false_target);
1469 } else {
Mark Mendellb8b97692015-05-22 16:58:19 -04001470 __ j(X86Condition(condition->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -07001471 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001472 }
David Brazdil0debae72015-11-12 18:37:00 +00001473
1474 // If neither branch falls through (case 3), the conditional branch to `true_target`
1475 // was already emitted (case 2) and we need to emit a jump to `false_target`.
1476 if (true_target != nullptr && false_target != nullptr) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001477 __ jmp(false_target);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001478 }
1479}
1480
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001481void LocationsBuilderX86::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001482 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
1483 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001484 locations->SetInAt(0, Location::Any());
1485 }
1486}
1487
1488void InstructionCodeGeneratorX86::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001489 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
1490 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
1491 Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
1492 nullptr : codegen_->GetLabelOf(true_successor);
1493 Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
1494 nullptr : codegen_->GetLabelOf(false_successor);
1495 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001496}
1497
1498void LocationsBuilderX86::VisitDeoptimize(HDeoptimize* deoptimize) {
1499 LocationSummary* locations = new (GetGraph()->GetArena())
1500 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01001501 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
David Brazdil0debae72015-11-12 18:37:00 +00001502 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001503 locations->SetInAt(0, Location::Any());
1504 }
1505}
1506
1507void InstructionCodeGeneratorX86::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08001508 SlowPathCode* slow_path = deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathX86>(deoptimize);
David Brazdil74eb1b22015-12-14 11:44:01 +00001509 GenerateTestAndBranch<Label>(deoptimize,
1510 /* condition_input_index */ 0,
1511 slow_path->GetEntryLabel(),
1512 /* false_target */ nullptr);
1513}
1514
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001515static bool SelectCanUseCMOV(HSelect* select) {
1516 // There are no conditional move instructions for XMMs.
1517 if (Primitive::IsFloatingPointType(select->GetType())) {
1518 return false;
1519 }
1520
1521 // A FP condition doesn't generate the single CC that we need.
1522 // In 32 bit mode, a long condition doesn't generate a single CC either.
1523 HInstruction* condition = select->GetCondition();
1524 if (condition->IsCondition()) {
1525 Primitive::Type compare_type = condition->InputAt(0)->GetType();
1526 if (compare_type == Primitive::kPrimLong ||
1527 Primitive::IsFloatingPointType(compare_type)) {
1528 return false;
1529 }
1530 }
1531
1532 // We can generate a CMOV for this Select.
1533 return true;
1534}
1535
David Brazdil74eb1b22015-12-14 11:44:01 +00001536void LocationsBuilderX86::VisitSelect(HSelect* select) {
1537 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001538 if (Primitive::IsFloatingPointType(select->GetType())) {
David Brazdil74eb1b22015-12-14 11:44:01 +00001539 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001540 locations->SetInAt(1, Location::Any());
David Brazdil74eb1b22015-12-14 11:44:01 +00001541 } else {
1542 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001543 if (SelectCanUseCMOV(select)) {
1544 if (select->InputAt(1)->IsConstant()) {
1545 // Cmov can't handle a constant value.
1546 locations->SetInAt(1, Location::RequiresRegister());
1547 } else {
1548 locations->SetInAt(1, Location::Any());
1549 }
1550 } else {
1551 locations->SetInAt(1, Location::Any());
1552 }
David Brazdil74eb1b22015-12-14 11:44:01 +00001553 }
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001554 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
1555 locations->SetInAt(2, Location::RequiresRegister());
David Brazdil74eb1b22015-12-14 11:44:01 +00001556 }
1557 locations->SetOut(Location::SameAsFirstInput());
1558}
1559
1560void InstructionCodeGeneratorX86::VisitSelect(HSelect* select) {
1561 LocationSummary* locations = select->GetLocations();
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001562 DCHECK(locations->InAt(0).Equals(locations->Out()));
1563 if (SelectCanUseCMOV(select)) {
1564 // If both the condition and the source types are integer, we can generate
1565 // a CMOV to implement Select.
1566
1567 HInstruction* select_condition = select->GetCondition();
1568 Condition cond = kNotEqual;
1569
1570 // Figure out how to test the 'condition'.
1571 if (select_condition->IsCondition()) {
1572 HCondition* condition = select_condition->AsCondition();
1573 if (!condition->IsEmittedAtUseSite()) {
1574 // This was a previously materialized condition.
1575 // Can we use the existing condition code?
1576 if (AreEflagsSetFrom(condition, select)) {
1577 // Materialization was the previous instruction. Condition codes are right.
1578 cond = X86Condition(condition->GetCondition());
1579 } else {
1580 // No, we have to recreate the condition code.
1581 Register cond_reg = locations->InAt(2).AsRegister<Register>();
1582 __ testl(cond_reg, cond_reg);
1583 }
1584 } else {
1585 // We can't handle FP or long here.
1586 DCHECK_NE(condition->InputAt(0)->GetType(), Primitive::kPrimLong);
1587 DCHECK(!Primitive::IsFloatingPointType(condition->InputAt(0)->GetType()));
1588 LocationSummary* cond_locations = condition->GetLocations();
Roland Levillain0b671c02016-08-19 12:02:34 +01001589 codegen_->GenerateIntCompare(cond_locations->InAt(0), cond_locations->InAt(1));
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001590 cond = X86Condition(condition->GetCondition());
1591 }
1592 } else {
1593 // Must be a boolean condition, which needs to be compared to 0.
1594 Register cond_reg = locations->InAt(2).AsRegister<Register>();
1595 __ testl(cond_reg, cond_reg);
1596 }
1597
1598 // If the condition is true, overwrite the output, which already contains false.
1599 Location false_loc = locations->InAt(0);
1600 Location true_loc = locations->InAt(1);
1601 if (select->GetType() == Primitive::kPrimLong) {
1602 // 64 bit conditional move.
1603 Register false_high = false_loc.AsRegisterPairHigh<Register>();
1604 Register false_low = false_loc.AsRegisterPairLow<Register>();
1605 if (true_loc.IsRegisterPair()) {
1606 __ cmovl(cond, false_high, true_loc.AsRegisterPairHigh<Register>());
1607 __ cmovl(cond, false_low, true_loc.AsRegisterPairLow<Register>());
1608 } else {
1609 __ cmovl(cond, false_high, Address(ESP, true_loc.GetHighStackIndex(kX86WordSize)));
1610 __ cmovl(cond, false_low, Address(ESP, true_loc.GetStackIndex()));
1611 }
1612 } else {
1613 // 32 bit conditional move.
1614 Register false_reg = false_loc.AsRegister<Register>();
1615 if (true_loc.IsRegister()) {
1616 __ cmovl(cond, false_reg, true_loc.AsRegister<Register>());
1617 } else {
1618 __ cmovl(cond, false_reg, Address(ESP, true_loc.GetStackIndex()));
1619 }
1620 }
1621 } else {
1622 NearLabel false_target;
1623 GenerateTestAndBranch<NearLabel>(
1624 select, /* condition_input_index */ 2, /* true_target */ nullptr, &false_target);
1625 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
1626 __ Bind(&false_target);
1627 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001628}
1629
David Srbecky0cf44932015-12-09 14:09:59 +00001630void LocationsBuilderX86::VisitNativeDebugInfo(HNativeDebugInfo* info) {
1631 new (GetGraph()->GetArena()) LocationSummary(info);
1632}
1633
David Srbeckyd28f4a02016-03-14 17:14:24 +00001634void InstructionCodeGeneratorX86::VisitNativeDebugInfo(HNativeDebugInfo*) {
1635 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00001636}
1637
1638void CodeGeneratorX86::GenerateNop() {
1639 __ nop();
David Srbecky0cf44932015-12-09 14:09:59 +00001640}
1641
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001642void LocationsBuilderX86::HandleCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001643 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +01001644 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Mark Mendellc4701932015-04-10 13:18:51 -04001645 // Handle the long/FP comparisons made in instruction simplification.
1646 switch (cond->InputAt(0)->GetType()) {
1647 case Primitive::kPrimLong: {
1648 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell8659e842016-02-16 10:41:46 -05001649 locations->SetInAt(1, Location::Any());
David Brazdilb3e773e2016-01-26 11:28:37 +00001650 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001651 locations->SetOut(Location::RequiresRegister());
1652 }
1653 break;
1654 }
1655 case Primitive::kPrimFloat:
1656 case Primitive::kPrimDouble: {
1657 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001658 if (cond->InputAt(1)->IsX86LoadFromConstantTable()) {
1659 DCHECK(cond->InputAt(1)->IsEmittedAtUseSite());
1660 } else if (cond->InputAt(1)->IsConstant()) {
1661 locations->SetInAt(1, Location::RequiresFpuRegister());
1662 } else {
1663 locations->SetInAt(1, Location::Any());
1664 }
David Brazdilb3e773e2016-01-26 11:28:37 +00001665 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001666 locations->SetOut(Location::RequiresRegister());
1667 }
1668 break;
1669 }
1670 default:
1671 locations->SetInAt(0, Location::RequiresRegister());
1672 locations->SetInAt(1, Location::Any());
David Brazdilb3e773e2016-01-26 11:28:37 +00001673 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001674 // We need a byte register.
1675 locations->SetOut(Location::RegisterLocation(ECX));
1676 }
1677 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001678 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001679}
1680
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001681void InstructionCodeGeneratorX86::HandleCondition(HCondition* cond) {
David Brazdilb3e773e2016-01-26 11:28:37 +00001682 if (cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001683 return;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001684 }
Mark Mendellc4701932015-04-10 13:18:51 -04001685
1686 LocationSummary* locations = cond->GetLocations();
1687 Location lhs = locations->InAt(0);
1688 Location rhs = locations->InAt(1);
1689 Register reg = locations->Out().AsRegister<Register>();
Mark Mendell152408f2015-12-31 12:28:50 -05001690 NearLabel true_label, false_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001691
1692 switch (cond->InputAt(0)->GetType()) {
1693 default: {
1694 // Integer case.
1695
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01001696 // Clear output register: setb only sets the low byte.
Mark Mendellc4701932015-04-10 13:18:51 -04001697 __ xorl(reg, reg);
Roland Levillain0b671c02016-08-19 12:02:34 +01001698 codegen_->GenerateIntCompare(lhs, rhs);
Aart Bike9f37602015-10-09 11:15:55 -07001699 __ setb(X86Condition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001700 return;
1701 }
1702 case Primitive::kPrimLong:
1703 GenerateLongComparesAndJumps(cond, &true_label, &false_label);
1704 break;
1705 case Primitive::kPrimFloat:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001706 GenerateFPCompare(lhs, rhs, cond, false);
Mark Mendellc4701932015-04-10 13:18:51 -04001707 GenerateFPJumps(cond, &true_label, &false_label);
1708 break;
1709 case Primitive::kPrimDouble:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001710 GenerateFPCompare(lhs, rhs, cond, true);
Mark Mendellc4701932015-04-10 13:18:51 -04001711 GenerateFPJumps(cond, &true_label, &false_label);
1712 break;
1713 }
1714
1715 // Convert the jumps into the result.
Mark Mendell0c9497d2015-08-21 09:30:05 -04001716 NearLabel done_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001717
Roland Levillain4fa13f62015-07-06 18:11:54 +01001718 // False case: result = 0.
Mark Mendellc4701932015-04-10 13:18:51 -04001719 __ Bind(&false_label);
1720 __ xorl(reg, reg);
1721 __ jmp(&done_label);
1722
Roland Levillain4fa13f62015-07-06 18:11:54 +01001723 // True case: result = 1.
Mark Mendellc4701932015-04-10 13:18:51 -04001724 __ Bind(&true_label);
1725 __ movl(reg, Immediate(1));
1726 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07001727}
1728
1729void LocationsBuilderX86::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001730 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001731}
1732
1733void InstructionCodeGeneratorX86::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001734 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001735}
1736
1737void LocationsBuilderX86::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001738 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001739}
1740
1741void InstructionCodeGeneratorX86::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001742 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001743}
1744
1745void LocationsBuilderX86::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001746 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001747}
1748
1749void InstructionCodeGeneratorX86::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001750 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001751}
1752
1753void LocationsBuilderX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001754 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001755}
1756
1757void InstructionCodeGeneratorX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001758 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001759}
1760
1761void LocationsBuilderX86::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001762 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001763}
1764
1765void InstructionCodeGeneratorX86::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001766 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001767}
1768
1769void LocationsBuilderX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001770 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001771}
1772
1773void InstructionCodeGeneratorX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001774 HandleCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001775}
1776
Aart Bike9f37602015-10-09 11:15:55 -07001777void LocationsBuilderX86::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001778 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001779}
1780
1781void InstructionCodeGeneratorX86::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001782 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001783}
1784
1785void LocationsBuilderX86::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001786 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001787}
1788
1789void InstructionCodeGeneratorX86::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001790 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001791}
1792
1793void LocationsBuilderX86::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001794 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001795}
1796
1797void InstructionCodeGeneratorX86::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001798 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001799}
1800
1801void LocationsBuilderX86::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001802 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001803}
1804
1805void InstructionCodeGeneratorX86::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001806 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001807}
1808
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001809void LocationsBuilderX86::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001810 LocationSummary* locations =
1811 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001812 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001813}
1814
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001815void InstructionCodeGeneratorX86::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001816 // Will be generated at use site.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001817}
1818
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001819void LocationsBuilderX86::VisitNullConstant(HNullConstant* constant) {
1820 LocationSummary* locations =
1821 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1822 locations->SetOut(Location::ConstantLocation(constant));
1823}
1824
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001825void InstructionCodeGeneratorX86::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001826 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001827}
1828
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001829void LocationsBuilderX86::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001830 LocationSummary* locations =
1831 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001832 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001833}
1834
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001835void InstructionCodeGeneratorX86::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001836 // Will be generated at use site.
1837}
1838
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001839void LocationsBuilderX86::VisitFloatConstant(HFloatConstant* constant) {
1840 LocationSummary* locations =
1841 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1842 locations->SetOut(Location::ConstantLocation(constant));
1843}
1844
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001845void InstructionCodeGeneratorX86::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001846 // Will be generated at use site.
1847}
1848
1849void LocationsBuilderX86::VisitDoubleConstant(HDoubleConstant* constant) {
1850 LocationSummary* locations =
1851 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1852 locations->SetOut(Location::ConstantLocation(constant));
1853}
1854
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001855void InstructionCodeGeneratorX86::VisitDoubleConstant(HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001856 // Will be generated at use site.
1857}
1858
Calin Juravle27df7582015-04-17 19:12:31 +01001859void LocationsBuilderX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1860 memory_barrier->SetLocations(nullptr);
1861}
1862
1863void InstructionCodeGeneratorX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00001864 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01001865}
1866
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001867void LocationsBuilderX86::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001868 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001869}
1870
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001871void InstructionCodeGeneratorX86::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001872 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001873}
1874
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001875void LocationsBuilderX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001876 LocationSummary* locations =
1877 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001878 switch (ret->InputAt(0)->GetType()) {
1879 case Primitive::kPrimBoolean:
1880 case Primitive::kPrimByte:
1881 case Primitive::kPrimChar:
1882 case Primitive::kPrimShort:
1883 case Primitive::kPrimInt:
1884 case Primitive::kPrimNot:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001885 locations->SetInAt(0, Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001886 break;
1887
1888 case Primitive::kPrimLong:
1889 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001890 0, Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001891 break;
1892
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001893 case Primitive::kPrimFloat:
1894 case Primitive::kPrimDouble:
1895 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001896 0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001897 break;
1898
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001899 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001900 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001901 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001902}
1903
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001904void InstructionCodeGeneratorX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001905 if (kIsDebugBuild) {
1906 switch (ret->InputAt(0)->GetType()) {
1907 case Primitive::kPrimBoolean:
1908 case Primitive::kPrimByte:
1909 case Primitive::kPrimChar:
1910 case Primitive::kPrimShort:
1911 case Primitive::kPrimInt:
1912 case Primitive::kPrimNot:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001913 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<Register>(), EAX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001914 break;
1915
1916 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001917 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairLow<Register>(), EAX);
1918 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairHigh<Register>(), EDX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001919 break;
1920
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001921 case Primitive::kPrimFloat:
1922 case Primitive::kPrimDouble:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001923 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>(), XMM0);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001924 break;
1925
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001926 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001927 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001928 }
1929 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001930 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001931}
1932
Calin Juravle175dc732015-08-25 15:42:32 +01001933void LocationsBuilderX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
1934 // The trampoline uses the same calling convention as dex calling conventions,
1935 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
1936 // the method_idx.
1937 HandleInvoke(invoke);
1938}
1939
1940void InstructionCodeGeneratorX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
1941 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
1942}
1943
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001944void LocationsBuilderX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00001945 // Explicit clinit checks triggered by static invokes must have been pruned by
1946 // art::PrepareForRegisterAllocation.
1947 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001948
Mark Mendellfb8d2792015-03-31 22:16:59 -04001949 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001950 if (intrinsic.TryDispatch(invoke)) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00001951 if (invoke->GetLocations()->CanCall() && invoke->HasPcRelativeDexCache()) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00001952 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00001953 }
Mark Mendell09ed1a32015-03-25 08:30:06 -04001954 return;
1955 }
1956
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001957 HandleInvoke(invoke);
Nicolas Geoffray94015b92015-06-04 18:21:04 +01001958
Vladimir Marko0f7dca42015-11-02 14:36:43 +00001959 // For PC-relative dex cache the invoke has an extra input, the PC-relative address base.
1960 if (invoke->HasPcRelativeDexCache()) {
Vladimir Markob4536b72015-11-24 13:45:23 +00001961 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00001962 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001963}
1964
Mark Mendell09ed1a32015-03-25 08:30:06 -04001965static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86* codegen) {
1966 if (invoke->GetLocations()->Intrinsified()) {
1967 IntrinsicCodeGeneratorX86 intrinsic(codegen);
1968 intrinsic.Dispatch(invoke);
1969 return true;
1970 }
1971 return false;
1972}
1973
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001974void InstructionCodeGeneratorX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00001975 // Explicit clinit checks triggered by static invokes must have been pruned by
1976 // art::PrepareForRegisterAllocation.
1977 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001978
Mark Mendell09ed1a32015-03-25 08:30:06 -04001979 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1980 return;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001981 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001982
Nicolas Geoffray94015b92015-06-04 18:21:04 +01001983 LocationSummary* locations = invoke->GetLocations();
Mark Mendell09ed1a32015-03-25 08:30:06 -04001984 codegen_->GenerateStaticOrDirectCall(
Nicolas Geoffray94015b92015-06-04 18:21:04 +01001985 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Mingyao Yang8693fe12015-04-17 16:51:08 -07001986 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001987}
1988
1989void LocationsBuilderX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00001990 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
1991 if (intrinsic.TryDispatch(invoke)) {
1992 return;
1993 }
1994
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001995 HandleInvoke(invoke);
1996}
1997
1998void LocationsBuilderX86::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001999 InvokeDexCallingConventionVisitorX86 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002000 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002001}
2002
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002003void InstructionCodeGeneratorX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002004 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2005 return;
2006 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002007
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002008 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002009 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01002010 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002011}
2012
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002013void LocationsBuilderX86::VisitInvokeInterface(HInvokeInterface* invoke) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002014 // This call to HandleInvoke allocates a temporary (core) register
2015 // which is also used to transfer the hidden argument from FP to
2016 // core register.
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002017 HandleInvoke(invoke);
2018 // Add the hidden argument.
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002019 invoke->GetLocations()->AddTemp(Location::FpuRegisterLocation(XMM7));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002020}
2021
2022void InstructionCodeGeneratorX86::VisitInvokeInterface(HInvokeInterface* invoke) {
2023 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain0d5a2812015-11-13 10:07:31 +00002024 LocationSummary* locations = invoke->GetLocations();
2025 Register temp = locations->GetTemp(0).AsRegister<Register>();
2026 XmmRegister hidden_reg = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002027 Location receiver = locations->InAt(0);
2028 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2029
Roland Levillain0d5a2812015-11-13 10:07:31 +00002030 // Set the hidden argument. This is safe to do this here, as XMM7
2031 // won't be modified thereafter, before the `call` instruction.
2032 DCHECK_EQ(XMM7, hidden_reg);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002033 __ movl(temp, Immediate(invoke->GetDexMethodIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002034 __ movd(hidden_reg, temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002035
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002036 if (receiver.IsStackSlot()) {
2037 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002038 // /* HeapReference<Class> */ temp = temp->klass_
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002039 __ movl(temp, Address(temp, class_offset));
2040 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002041 // /* HeapReference<Class> */ temp = receiver->klass_
Roland Levillain271ab9c2014-11-27 15:23:57 +00002042 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002043 }
Roland Levillain4d027112015-07-01 15:41:14 +01002044 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00002045 // Instead of simply (possibly) unpoisoning `temp` here, we should
2046 // emit a read barrier for the previous class reference load.
2047 // However this is not required in practice, as this is an
2048 // intermediate/temporary reference and because the current
2049 // concurrent copying collector keeps the from-space memory
2050 // intact/accessible until the end of the marking phase (the
2051 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01002052 __ MaybeUnpoisonHeapReference(temp);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002053 // temp = temp->GetAddressOfIMT()
2054 __ movl(temp,
2055 Address(temp, mirror::Class::ImtPtrOffset(kX86PointerSize).Uint32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002056 // temp = temp->GetImtEntryAt(method_offset);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002057 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00002058 invoke->GetImtIndex(), kX86PointerSize));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002059 __ movl(temp, Address(temp, method_offset));
2060 // call temp->GetEntryPoint();
Roland Levillain0d5a2812015-11-13 10:07:31 +00002061 __ call(Address(temp,
Andreas Gampe542451c2016-07-26 09:02:02 -07002062 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86PointerSize).Int32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002063
2064 DCHECK(!codegen_->IsLeafMethod());
2065 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2066}
2067
Roland Levillain88cb1752014-10-20 16:36:47 +01002068void LocationsBuilderX86::VisitNeg(HNeg* neg) {
2069 LocationSummary* locations =
2070 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2071 switch (neg->GetResultType()) {
2072 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002073 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01002074 locations->SetInAt(0, Location::RequiresRegister());
2075 locations->SetOut(Location::SameAsFirstInput());
2076 break;
2077
Roland Levillain88cb1752014-10-20 16:36:47 +01002078 case Primitive::kPrimFloat:
Roland Levillain5368c212014-11-27 15:03:41 +00002079 locations->SetInAt(0, Location::RequiresFpuRegister());
2080 locations->SetOut(Location::SameAsFirstInput());
2081 locations->AddTemp(Location::RequiresRegister());
2082 locations->AddTemp(Location::RequiresFpuRegister());
2083 break;
2084
Roland Levillain88cb1752014-10-20 16:36:47 +01002085 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002086 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00002087 locations->SetOut(Location::SameAsFirstInput());
2088 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01002089 break;
2090
2091 default:
2092 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2093 }
2094}
2095
2096void InstructionCodeGeneratorX86::VisitNeg(HNeg* neg) {
2097 LocationSummary* locations = neg->GetLocations();
2098 Location out = locations->Out();
2099 Location in = locations->InAt(0);
2100 switch (neg->GetResultType()) {
2101 case Primitive::kPrimInt:
2102 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002103 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002104 __ negl(out.AsRegister<Register>());
Roland Levillain88cb1752014-10-20 16:36:47 +01002105 break;
2106
2107 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002108 DCHECK(in.IsRegisterPair());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002109 DCHECK(in.Equals(out));
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002110 __ negl(out.AsRegisterPairLow<Register>());
2111 // Negation is similar to subtraction from zero. The least
2112 // significant byte triggers a borrow when it is different from
2113 // zero; to take it into account, add 1 to the most significant
2114 // byte if the carry flag (CF) is set to 1 after the first NEGL
2115 // operation.
2116 __ adcl(out.AsRegisterPairHigh<Register>(), Immediate(0));
2117 __ negl(out.AsRegisterPairHigh<Register>());
2118 break;
2119
Roland Levillain5368c212014-11-27 15:03:41 +00002120 case Primitive::kPrimFloat: {
2121 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002122 Register constant = locations->GetTemp(0).AsRegister<Register>();
2123 XmmRegister mask = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002124 // Implement float negation with an exclusive or with value
2125 // 0x80000000 (mask for bit 31, representing the sign of a
2126 // single-precision floating-point number).
2127 __ movl(constant, Immediate(INT32_C(0x80000000)));
2128 __ movd(mask, constant);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002129 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00002130 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002131 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00002132
Roland Levillain5368c212014-11-27 15:03:41 +00002133 case Primitive::kPrimDouble: {
2134 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002135 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002136 // Implement double negation with an exclusive or with value
2137 // 0x8000000000000000 (mask for bit 63, representing the sign of
2138 // a double-precision floating-point number).
2139 __ LoadLongConstant(mask, INT64_C(0x8000000000000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002140 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01002141 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002142 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002143
2144 default:
2145 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2146 }
2147}
2148
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002149void LocationsBuilderX86::VisitX86FPNeg(HX86FPNeg* neg) {
2150 LocationSummary* locations =
2151 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2152 DCHECK(Primitive::IsFloatingPointType(neg->GetType()));
2153 locations->SetInAt(0, Location::RequiresFpuRegister());
2154 locations->SetInAt(1, Location::RequiresRegister());
2155 locations->SetOut(Location::SameAsFirstInput());
2156 locations->AddTemp(Location::RequiresFpuRegister());
2157}
2158
2159void InstructionCodeGeneratorX86::VisitX86FPNeg(HX86FPNeg* neg) {
2160 LocationSummary* locations = neg->GetLocations();
2161 Location out = locations->Out();
2162 DCHECK(locations->InAt(0).Equals(out));
2163
2164 Register constant_area = locations->InAt(1).AsRegister<Register>();
2165 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
2166 if (neg->GetType() == Primitive::kPrimFloat) {
2167 __ movss(mask, codegen_->LiteralInt32Address(INT32_C(0x80000000), constant_area));
2168 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
2169 } else {
2170 __ movsd(mask, codegen_->LiteralInt64Address(INT64_C(0x8000000000000000), constant_area));
2171 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
2172 }
2173}
2174
Roland Levillaindff1f282014-11-05 14:15:05 +00002175void LocationsBuilderX86::VisitTypeConversion(HTypeConversion* conversion) {
Roland Levillaindff1f282014-11-05 14:15:05 +00002176 Primitive::Type result_type = conversion->GetResultType();
2177 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002178 DCHECK_NE(result_type, input_type);
Roland Levillain624279f2014-12-04 11:54:28 +00002179
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002180 // The float-to-long and double-to-long type conversions rely on a
2181 // call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00002182 LocationSummary::CallKind call_kind =
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002183 ((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
2184 && result_type == Primitive::kPrimLong)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01002185 ? LocationSummary::kCallOnMainOnly
Roland Levillain624279f2014-12-04 11:54:28 +00002186 : LocationSummary::kNoCall;
2187 LocationSummary* locations =
2188 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
2189
David Brazdilb2bd1c52015-03-25 11:17:37 +00002190 // The Java language does not allow treating boolean as an integral type but
2191 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00002192
Roland Levillaindff1f282014-11-05 14:15:05 +00002193 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002194 case Primitive::kPrimByte:
2195 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002196 case Primitive::kPrimLong: {
2197 // Type conversion from long to byte is a result of code transformations.
2198 HInstruction* input = conversion->InputAt(0);
2199 Location input_location = input->IsConstant()
2200 ? Location::ConstantLocation(input->AsConstant())
2201 : Location::RegisterPairLocation(EAX, EDX);
2202 locations->SetInAt(0, input_location);
2203 // Make the output overlap to please the register allocator. This greatly simplifies
2204 // the validation of the linear scan implementation
2205 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2206 break;
2207 }
David Brazdil46e2a392015-03-16 17:31:52 +00002208 case Primitive::kPrimBoolean:
2209 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002210 case Primitive::kPrimShort:
2211 case Primitive::kPrimInt:
2212 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002213 // Processing a Dex `int-to-byte' instruction.
Mark Mendell5f874182015-03-04 15:42:45 -05002214 locations->SetInAt(0, Location::ByteRegisterOrConstant(ECX, conversion->InputAt(0)));
2215 // Make the output overlap to please the register allocator. This greatly simplifies
2216 // the validation of the linear scan implementation
2217 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Roland Levillain51d3fc42014-11-13 14:11:42 +00002218 break;
2219
2220 default:
2221 LOG(FATAL) << "Unexpected type conversion from " << input_type
2222 << " to " << result_type;
2223 }
2224 break;
2225
Roland Levillain01a8d712014-11-14 16:27:39 +00002226 case Primitive::kPrimShort:
2227 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002228 case Primitive::kPrimLong:
2229 // Type conversion from long to short is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002230 case Primitive::kPrimBoolean:
2231 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002232 case Primitive::kPrimByte:
2233 case Primitive::kPrimInt:
2234 case Primitive::kPrimChar:
2235 // Processing a Dex `int-to-short' instruction.
2236 locations->SetInAt(0, Location::Any());
2237 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2238 break;
2239
2240 default:
2241 LOG(FATAL) << "Unexpected type conversion from " << input_type
2242 << " to " << result_type;
2243 }
2244 break;
2245
Roland Levillain946e1432014-11-11 17:35:19 +00002246 case Primitive::kPrimInt:
2247 switch (input_type) {
2248 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002249 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002250 locations->SetInAt(0, Location::Any());
2251 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2252 break;
2253
2254 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00002255 // Processing a Dex `float-to-int' instruction.
2256 locations->SetInAt(0, Location::RequiresFpuRegister());
2257 locations->SetOut(Location::RequiresRegister());
2258 locations->AddTemp(Location::RequiresFpuRegister());
2259 break;
2260
Roland Levillain946e1432014-11-11 17:35:19 +00002261 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002262 // Processing a Dex `double-to-int' instruction.
2263 locations->SetInAt(0, Location::RequiresFpuRegister());
2264 locations->SetOut(Location::RequiresRegister());
2265 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00002266 break;
2267
2268 default:
2269 LOG(FATAL) << "Unexpected type conversion from " << input_type
2270 << " to " << result_type;
2271 }
2272 break;
2273
Roland Levillaindff1f282014-11-05 14:15:05 +00002274 case Primitive::kPrimLong:
2275 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002276 case Primitive::kPrimBoolean:
2277 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002278 case Primitive::kPrimByte:
2279 case Primitive::kPrimShort:
2280 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002281 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002282 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002283 locations->SetInAt(0, Location::RegisterLocation(EAX));
2284 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
2285 break;
2286
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002287 case Primitive::kPrimFloat:
Vladimir Marko949c91f2015-01-27 10:48:44 +00002288 case Primitive::kPrimDouble: {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002289 // Processing a Dex `float-to-long' or 'double-to-long' instruction.
Vladimir Marko949c91f2015-01-27 10:48:44 +00002290 InvokeRuntimeCallingConvention calling_convention;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002291 XmmRegister parameter = calling_convention.GetFpuRegisterAt(0);
2292 locations->SetInAt(0, Location::FpuRegisterLocation(parameter));
2293
Vladimir Marko949c91f2015-01-27 10:48:44 +00002294 // The runtime helper puts the result in EAX, EDX.
2295 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Vladimir Marko949c91f2015-01-27 10:48:44 +00002296 }
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002297 break;
Roland Levillaindff1f282014-11-05 14:15:05 +00002298
2299 default:
2300 LOG(FATAL) << "Unexpected type conversion from " << input_type
2301 << " to " << result_type;
2302 }
2303 break;
2304
Roland Levillain981e4542014-11-14 11:47:14 +00002305 case Primitive::kPrimChar:
2306 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002307 case Primitive::kPrimLong:
2308 // Type conversion from long to char is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002309 case Primitive::kPrimBoolean:
2310 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002311 case Primitive::kPrimByte:
2312 case Primitive::kPrimShort:
2313 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002314 // Processing a Dex `int-to-char' instruction.
2315 locations->SetInAt(0, Location::Any());
2316 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2317 break;
2318
2319 default:
2320 LOG(FATAL) << "Unexpected type conversion from " << input_type
2321 << " to " << result_type;
2322 }
2323 break;
2324
Roland Levillaindff1f282014-11-05 14:15:05 +00002325 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002326 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002327 case Primitive::kPrimBoolean:
2328 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002329 case Primitive::kPrimByte:
2330 case Primitive::kPrimShort:
2331 case Primitive::kPrimInt:
2332 case Primitive::kPrimChar:
2333 // Processing a Dex `int-to-float' instruction.
2334 locations->SetInAt(0, Location::RequiresRegister());
2335 locations->SetOut(Location::RequiresFpuRegister());
2336 break;
2337
2338 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002339 // Processing a Dex `long-to-float' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002340 locations->SetInAt(0, Location::Any());
2341 locations->SetOut(Location::Any());
Roland Levillain6d0e4832014-11-27 18:31:21 +00002342 break;
2343
Roland Levillaincff13742014-11-17 14:32:17 +00002344 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002345 // Processing a Dex `double-to-float' instruction.
2346 locations->SetInAt(0, Location::RequiresFpuRegister());
2347 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002348 break;
2349
2350 default:
2351 LOG(FATAL) << "Unexpected type conversion from " << input_type
2352 << " to " << result_type;
2353 };
2354 break;
2355
Roland Levillaindff1f282014-11-05 14:15:05 +00002356 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002357 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002358 case Primitive::kPrimBoolean:
2359 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002360 case Primitive::kPrimByte:
2361 case Primitive::kPrimShort:
2362 case Primitive::kPrimInt:
2363 case Primitive::kPrimChar:
2364 // Processing a Dex `int-to-double' instruction.
2365 locations->SetInAt(0, Location::RequiresRegister());
2366 locations->SetOut(Location::RequiresFpuRegister());
2367 break;
2368
2369 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002370 // Processing a Dex `long-to-double' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002371 locations->SetInAt(0, Location::Any());
2372 locations->SetOut(Location::Any());
Roland Levillain647b9ed2014-11-27 12:06:00 +00002373 break;
2374
Roland Levillaincff13742014-11-17 14:32:17 +00002375 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002376 // Processing a Dex `float-to-double' instruction.
2377 locations->SetInAt(0, Location::RequiresFpuRegister());
2378 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002379 break;
2380
2381 default:
2382 LOG(FATAL) << "Unexpected type conversion from " << input_type
2383 << " to " << result_type;
2384 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002385 break;
2386
2387 default:
2388 LOG(FATAL) << "Unexpected type conversion from " << input_type
2389 << " to " << result_type;
2390 }
2391}
2392
2393void InstructionCodeGeneratorX86::VisitTypeConversion(HTypeConversion* conversion) {
2394 LocationSummary* locations = conversion->GetLocations();
2395 Location out = locations->Out();
2396 Location in = locations->InAt(0);
2397 Primitive::Type result_type = conversion->GetResultType();
2398 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002399 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00002400 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002401 case Primitive::kPrimByte:
2402 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002403 case Primitive::kPrimLong:
2404 // Type conversion from long to byte is a result of code transformations.
2405 if (in.IsRegisterPair()) {
2406 __ movsxb(out.AsRegister<Register>(), in.AsRegisterPairLow<ByteRegister>());
2407 } else {
2408 DCHECK(in.GetConstant()->IsLongConstant());
2409 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2410 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
2411 }
2412 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002413 case Primitive::kPrimBoolean:
2414 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002415 case Primitive::kPrimShort:
2416 case Primitive::kPrimInt:
2417 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002418 // Processing a Dex `int-to-byte' instruction.
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002419 if (in.IsRegister()) {
2420 __ movsxb(out.AsRegister<Register>(), in.AsRegister<ByteRegister>());
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002421 } else {
2422 DCHECK(in.GetConstant()->IsIntConstant());
2423 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
2424 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
2425 }
Roland Levillain51d3fc42014-11-13 14:11:42 +00002426 break;
2427
2428 default:
2429 LOG(FATAL) << "Unexpected type conversion from " << input_type
2430 << " to " << result_type;
2431 }
2432 break;
2433
Roland Levillain01a8d712014-11-14 16:27:39 +00002434 case Primitive::kPrimShort:
2435 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002436 case Primitive::kPrimLong:
2437 // Type conversion from long to short is a result of code transformations.
2438 if (in.IsRegisterPair()) {
2439 __ movsxw(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
2440 } else if (in.IsDoubleStackSlot()) {
2441 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
2442 } else {
2443 DCHECK(in.GetConstant()->IsLongConstant());
2444 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2445 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
2446 }
2447 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002448 case Primitive::kPrimBoolean:
2449 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002450 case Primitive::kPrimByte:
2451 case Primitive::kPrimInt:
2452 case Primitive::kPrimChar:
2453 // Processing a Dex `int-to-short' instruction.
2454 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002455 __ movsxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain01a8d712014-11-14 16:27:39 +00002456 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002457 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain01a8d712014-11-14 16:27:39 +00002458 } else {
2459 DCHECK(in.GetConstant()->IsIntConstant());
2460 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002461 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
Roland Levillain01a8d712014-11-14 16:27:39 +00002462 }
2463 break;
2464
2465 default:
2466 LOG(FATAL) << "Unexpected type conversion from " << input_type
2467 << " to " << result_type;
2468 }
2469 break;
2470
Roland Levillain946e1432014-11-11 17:35:19 +00002471 case Primitive::kPrimInt:
2472 switch (input_type) {
2473 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002474 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002475 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002476 __ movl(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00002477 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002478 __ movl(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain946e1432014-11-11 17:35:19 +00002479 } else {
2480 DCHECK(in.IsConstant());
2481 DCHECK(in.GetConstant()->IsLongConstant());
2482 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002483 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00002484 }
2485 break;
2486
Roland Levillain3f8f9362014-12-02 17:45:01 +00002487 case Primitive::kPrimFloat: {
2488 // Processing a Dex `float-to-int' instruction.
2489 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2490 Register output = out.AsRegister<Register>();
2491 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002492 NearLabel done, nan;
Roland Levillain3f8f9362014-12-02 17:45:01 +00002493
2494 __ movl(output, Immediate(kPrimIntMax));
2495 // temp = int-to-float(output)
2496 __ cvtsi2ss(temp, output);
2497 // if input >= temp goto done
2498 __ comiss(input, temp);
2499 __ j(kAboveEqual, &done);
2500 // if input == NaN goto nan
2501 __ j(kUnordered, &nan);
2502 // output = float-to-int-truncate(input)
2503 __ cvttss2si(output, input);
2504 __ jmp(&done);
2505 __ Bind(&nan);
2506 // output = 0
2507 __ xorl(output, output);
2508 __ Bind(&done);
2509 break;
2510 }
2511
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002512 case Primitive::kPrimDouble: {
2513 // Processing a Dex `double-to-int' instruction.
2514 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2515 Register output = out.AsRegister<Register>();
2516 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002517 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002518
2519 __ movl(output, Immediate(kPrimIntMax));
2520 // temp = int-to-double(output)
2521 __ cvtsi2sd(temp, output);
2522 // if input >= temp goto done
2523 __ comisd(input, temp);
2524 __ j(kAboveEqual, &done);
2525 // if input == NaN goto nan
2526 __ j(kUnordered, &nan);
2527 // output = double-to-int-truncate(input)
2528 __ cvttsd2si(output, input);
2529 __ jmp(&done);
2530 __ Bind(&nan);
2531 // output = 0
2532 __ xorl(output, output);
2533 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00002534 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002535 }
Roland Levillain946e1432014-11-11 17:35:19 +00002536
2537 default:
2538 LOG(FATAL) << "Unexpected type conversion from " << input_type
2539 << " to " << result_type;
2540 }
2541 break;
2542
Roland Levillaindff1f282014-11-05 14:15:05 +00002543 case Primitive::kPrimLong:
2544 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002545 case Primitive::kPrimBoolean:
2546 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002547 case Primitive::kPrimByte:
2548 case Primitive::kPrimShort:
2549 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002550 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002551 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002552 DCHECK_EQ(out.AsRegisterPairLow<Register>(), EAX);
2553 DCHECK_EQ(out.AsRegisterPairHigh<Register>(), EDX);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002554 DCHECK_EQ(in.AsRegister<Register>(), EAX);
Roland Levillaindff1f282014-11-05 14:15:05 +00002555 __ cdq();
2556 break;
2557
2558 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00002559 // Processing a Dex `float-to-long' instruction.
Serban Constantinescuba45db02016-07-12 22:53:02 +01002560 codegen_->InvokeRuntime(kQuickF2l, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00002561 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
Roland Levillain624279f2014-12-04 11:54:28 +00002562 break;
2563
Roland Levillaindff1f282014-11-05 14:15:05 +00002564 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002565 // Processing a Dex `double-to-long' instruction.
Serban Constantinescuba45db02016-07-12 22:53:02 +01002566 codegen_->InvokeRuntime(kQuickD2l, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00002567 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
Roland Levillaindff1f282014-11-05 14:15:05 +00002568 break;
2569
2570 default:
2571 LOG(FATAL) << "Unexpected type conversion from " << input_type
2572 << " to " << result_type;
2573 }
2574 break;
2575
Roland Levillain981e4542014-11-14 11:47:14 +00002576 case Primitive::kPrimChar:
2577 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002578 case Primitive::kPrimLong:
2579 // Type conversion from long to short is a result of code transformations.
2580 if (in.IsRegisterPair()) {
2581 __ movzxw(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
2582 } else if (in.IsDoubleStackSlot()) {
2583 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
2584 } else {
2585 DCHECK(in.GetConstant()->IsLongConstant());
2586 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2587 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
2588 }
2589 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002590 case Primitive::kPrimBoolean:
2591 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002592 case Primitive::kPrimByte:
2593 case Primitive::kPrimShort:
2594 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002595 // Processing a Dex `Process a Dex `int-to-char'' instruction.
2596 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002597 __ movzxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain981e4542014-11-14 11:47:14 +00002598 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002599 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain981e4542014-11-14 11:47:14 +00002600 } else {
2601 DCHECK(in.GetConstant()->IsIntConstant());
2602 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002603 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
Roland Levillain981e4542014-11-14 11:47:14 +00002604 }
2605 break;
2606
2607 default:
2608 LOG(FATAL) << "Unexpected type conversion from " << input_type
2609 << " to " << result_type;
2610 }
2611 break;
2612
Roland Levillaindff1f282014-11-05 14:15:05 +00002613 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002614 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002615 case Primitive::kPrimBoolean:
2616 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002617 case Primitive::kPrimByte:
2618 case Primitive::kPrimShort:
2619 case Primitive::kPrimInt:
2620 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002621 // Processing a Dex `int-to-float' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002622 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002623 break;
2624
Roland Levillain6d0e4832014-11-27 18:31:21 +00002625 case Primitive::kPrimLong: {
2626 // Processing a Dex `long-to-float' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002627 size_t adjustment = 0;
Roland Levillain6d0e4832014-11-27 18:31:21 +00002628
Roland Levillain232ade02015-04-20 15:14:36 +01002629 // Create stack space for the call to
2630 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstps below.
2631 // TODO: enhance register allocator to ask for stack temporaries.
2632 if (!in.IsDoubleStackSlot() || !out.IsStackSlot()) {
2633 adjustment = Primitive::ComponentSize(Primitive::kPrimLong);
2634 __ subl(ESP, Immediate(adjustment));
2635 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002636
Roland Levillain232ade02015-04-20 15:14:36 +01002637 // Load the value to the FP stack, using temporaries if needed.
2638 PushOntoFPStack(in, 0, adjustment, false, true);
2639
2640 if (out.IsStackSlot()) {
2641 __ fstps(Address(ESP, out.GetStackIndex() + adjustment));
2642 } else {
2643 __ fstps(Address(ESP, 0));
2644 Location stack_temp = Location::StackSlot(0);
2645 codegen_->Move32(out, stack_temp);
2646 }
2647
2648 // Remove the temporary stack space we allocated.
2649 if (adjustment != 0) {
2650 __ addl(ESP, Immediate(adjustment));
2651 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002652 break;
2653 }
2654
Roland Levillaincff13742014-11-17 14:32:17 +00002655 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002656 // Processing a Dex `double-to-float' instruction.
2657 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002658 break;
2659
2660 default:
2661 LOG(FATAL) << "Unexpected type conversion from " << input_type
2662 << " to " << result_type;
2663 };
2664 break;
2665
Roland Levillaindff1f282014-11-05 14:15:05 +00002666 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002667 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002668 case Primitive::kPrimBoolean:
2669 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002670 case Primitive::kPrimByte:
2671 case Primitive::kPrimShort:
2672 case Primitive::kPrimInt:
2673 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002674 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002675 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002676 break;
2677
Roland Levillain647b9ed2014-11-27 12:06:00 +00002678 case Primitive::kPrimLong: {
2679 // Processing a Dex `long-to-double' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002680 size_t adjustment = 0;
Roland Levillain647b9ed2014-11-27 12:06:00 +00002681
Roland Levillain232ade02015-04-20 15:14:36 +01002682 // Create stack space for the call to
2683 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstpl below.
2684 // TODO: enhance register allocator to ask for stack temporaries.
2685 if (!in.IsDoubleStackSlot() || !out.IsDoubleStackSlot()) {
2686 adjustment = Primitive::ComponentSize(Primitive::kPrimLong);
2687 __ subl(ESP, Immediate(adjustment));
2688 }
2689
2690 // Load the value to the FP stack, using temporaries if needed.
2691 PushOntoFPStack(in, 0, adjustment, false, true);
2692
2693 if (out.IsDoubleStackSlot()) {
2694 __ fstpl(Address(ESP, out.GetStackIndex() + adjustment));
2695 } else {
2696 __ fstpl(Address(ESP, 0));
2697 Location stack_temp = Location::DoubleStackSlot(0);
2698 codegen_->Move64(out, stack_temp);
2699 }
2700
2701 // Remove the temporary stack space we allocated.
2702 if (adjustment != 0) {
2703 __ addl(ESP, Immediate(adjustment));
2704 }
Roland Levillain647b9ed2014-11-27 12:06:00 +00002705 break;
2706 }
2707
Roland Levillaincff13742014-11-17 14:32:17 +00002708 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002709 // Processing a Dex `float-to-double' instruction.
2710 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002711 break;
2712
2713 default:
2714 LOG(FATAL) << "Unexpected type conversion from " << input_type
2715 << " to " << result_type;
2716 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002717 break;
2718
2719 default:
2720 LOG(FATAL) << "Unexpected type conversion from " << input_type
2721 << " to " << result_type;
2722 }
2723}
2724
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002725void LocationsBuilderX86::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002726 LocationSummary* locations =
2727 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002728 switch (add->GetResultType()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002729 case Primitive::kPrimInt: {
2730 locations->SetInAt(0, Location::RequiresRegister());
2731 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
2732 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2733 break;
2734 }
2735
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002736 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002737 locations->SetInAt(0, Location::RequiresRegister());
2738 locations->SetInAt(1, Location::Any());
2739 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002740 break;
2741 }
2742
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002743 case Primitive::kPrimFloat:
2744 case Primitive::kPrimDouble: {
2745 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002746 if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
2747 DCHECK(add->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00002748 } else if (add->InputAt(1)->IsConstant()) {
2749 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002750 } else {
2751 locations->SetInAt(1, Location::Any());
2752 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002753 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002754 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002755 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002756
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002757 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002758 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
2759 break;
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002760 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002761}
2762
2763void InstructionCodeGeneratorX86::VisitAdd(HAdd* add) {
2764 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002765 Location first = locations->InAt(0);
2766 Location second = locations->InAt(1);
Mark Mendell09b84632015-02-13 17:48:38 -05002767 Location out = locations->Out();
2768
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002769 switch (add->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002770 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002771 if (second.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002772 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2773 __ addl(out.AsRegister<Register>(), second.AsRegister<Register>());
Mark Mendell33bf2452015-05-27 10:08:24 -04002774 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
2775 __ addl(out.AsRegister<Register>(), first.AsRegister<Register>());
Mark Mendell09b84632015-02-13 17:48:38 -05002776 } else {
2777 __ leal(out.AsRegister<Register>(), Address(
2778 first.AsRegister<Register>(), second.AsRegister<Register>(), TIMES_1, 0));
2779 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002780 } else if (second.IsConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002781 int32_t value = second.GetConstant()->AsIntConstant()->GetValue();
2782 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2783 __ addl(out.AsRegister<Register>(), Immediate(value));
2784 } else {
2785 __ leal(out.AsRegister<Register>(), Address(first.AsRegister<Register>(), value));
2786 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002787 } else {
Mark Mendell09b84632015-02-13 17:48:38 -05002788 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002789 __ addl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002790 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002791 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002792 }
2793
2794 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002795 if (second.IsRegisterPair()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002796 __ addl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
2797 __ adcl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002798 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002799 __ addl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
2800 __ adcl(first.AsRegisterPairHigh<Register>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002801 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002802 } else {
2803 DCHECK(second.IsConstant()) << second;
2804 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2805 __ addl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
2806 __ adcl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002807 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002808 break;
2809 }
2810
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002811 case Primitive::kPrimFloat: {
2812 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002813 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04002814 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
2815 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00002816 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04002817 __ addss(first.AsFpuRegister<XmmRegister>(),
2818 codegen_->LiteralFloatAddress(
2819 const_area->GetConstant()->AsFloatConstant()->GetValue(),
2820 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2821 } else {
2822 DCHECK(second.IsStackSlot());
2823 __ addss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002824 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002825 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002826 }
2827
2828 case Primitive::kPrimDouble: {
2829 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002830 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04002831 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
2832 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00002833 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04002834 __ addsd(first.AsFpuRegister<XmmRegister>(),
2835 codegen_->LiteralDoubleAddress(
2836 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
2837 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2838 } else {
2839 DCHECK(second.IsDoubleStackSlot());
2840 __ addsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002841 }
2842 break;
2843 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002844
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002845 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002846 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002847 }
2848}
2849
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002850void LocationsBuilderX86::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002851 LocationSummary* locations =
2852 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002853 switch (sub->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002854 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002855 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002856 locations->SetInAt(0, Location::RequiresRegister());
2857 locations->SetInAt(1, Location::Any());
2858 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002859 break;
2860 }
Calin Juravle11351682014-10-23 15:38:15 +01002861 case Primitive::kPrimFloat:
2862 case Primitive::kPrimDouble: {
2863 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002864 if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
2865 DCHECK(sub->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00002866 } else if (sub->InputAt(1)->IsConstant()) {
2867 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002868 } else {
2869 locations->SetInAt(1, Location::Any());
2870 }
Calin Juravle11351682014-10-23 15:38:15 +01002871 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002872 break;
Calin Juravle11351682014-10-23 15:38:15 +01002873 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002874
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002875 default:
Calin Juravle11351682014-10-23 15:38:15 +01002876 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002877 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002878}
2879
2880void InstructionCodeGeneratorX86::VisitSub(HSub* sub) {
2881 LocationSummary* locations = sub->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002882 Location first = locations->InAt(0);
2883 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01002884 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002885 switch (sub->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002886 case Primitive::kPrimInt: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002887 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002888 __ subl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002889 } else if (second.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002890 __ subl(first.AsRegister<Register>(),
2891 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002892 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002893 __ subl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002894 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002895 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002896 }
2897
2898 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002899 if (second.IsRegisterPair()) {
Calin Juravle11351682014-10-23 15:38:15 +01002900 __ subl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
2901 __ sbbl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002902 } else if (second.IsDoubleStackSlot()) {
Calin Juravle11351682014-10-23 15:38:15 +01002903 __ subl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002904 __ sbbl(first.AsRegisterPairHigh<Register>(),
2905 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002906 } else {
2907 DCHECK(second.IsConstant()) << second;
2908 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2909 __ subl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
2910 __ sbbl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002911 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002912 break;
2913 }
2914
Calin Juravle11351682014-10-23 15:38:15 +01002915 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04002916 if (second.IsFpuRegister()) {
2917 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2918 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
2919 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00002920 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04002921 __ subss(first.AsFpuRegister<XmmRegister>(),
2922 codegen_->LiteralFloatAddress(
2923 const_area->GetConstant()->AsFloatConstant()->GetValue(),
2924 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2925 } else {
2926 DCHECK(second.IsStackSlot());
2927 __ subss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
2928 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002929 break;
Calin Juravle11351682014-10-23 15:38:15 +01002930 }
2931
2932 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04002933 if (second.IsFpuRegister()) {
2934 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2935 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
2936 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00002937 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04002938 __ subsd(first.AsFpuRegister<XmmRegister>(),
2939 codegen_->LiteralDoubleAddress(
2940 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
2941 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2942 } else {
2943 DCHECK(second.IsDoubleStackSlot());
2944 __ subsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
2945 }
Calin Juravle11351682014-10-23 15:38:15 +01002946 break;
2947 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002948
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002949 default:
Calin Juravle11351682014-10-23 15:38:15 +01002950 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002951 }
2952}
2953
Calin Juravle34bacdf2014-10-07 20:23:36 +01002954void LocationsBuilderX86::VisitMul(HMul* mul) {
2955 LocationSummary* locations =
2956 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
2957 switch (mul->GetResultType()) {
2958 case Primitive::kPrimInt:
2959 locations->SetInAt(0, Location::RequiresRegister());
2960 locations->SetInAt(1, Location::Any());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002961 if (mul->InputAt(1)->IsIntConstant()) {
2962 // Can use 3 operand multiply.
2963 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2964 } else {
2965 locations->SetOut(Location::SameAsFirstInput());
2966 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002967 break;
2968 case Primitive::kPrimLong: {
2969 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002970 locations->SetInAt(1, Location::Any());
2971 locations->SetOut(Location::SameAsFirstInput());
2972 // Needed for imul on 32bits with 64bits output.
2973 locations->AddTemp(Location::RegisterLocation(EAX));
2974 locations->AddTemp(Location::RegisterLocation(EDX));
2975 break;
2976 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002977 case Primitive::kPrimFloat:
2978 case Primitive::kPrimDouble: {
2979 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002980 if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
2981 DCHECK(mul->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00002982 } else if (mul->InputAt(1)->IsConstant()) {
2983 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002984 } else {
2985 locations->SetInAt(1, Location::Any());
2986 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002987 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002988 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002989 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002990
2991 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002992 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002993 }
2994}
2995
2996void InstructionCodeGeneratorX86::VisitMul(HMul* mul) {
2997 LocationSummary* locations = mul->GetLocations();
2998 Location first = locations->InAt(0);
2999 Location second = locations->InAt(1);
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003000 Location out = locations->Out();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003001
3002 switch (mul->GetResultType()) {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003003 case Primitive::kPrimInt:
3004 // The constant may have ended up in a register, so test explicitly to avoid
3005 // problems where the output may not be the same as the first operand.
3006 if (mul->InputAt(1)->IsIntConstant()) {
3007 Immediate imm(mul->InputAt(1)->AsIntConstant()->GetValue());
3008 __ imull(out.AsRegister<Register>(), first.AsRegister<Register>(), imm);
3009 } else if (second.IsRegister()) {
3010 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003011 __ imull(first.AsRegister<Register>(), second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003012 } else {
3013 DCHECK(second.IsStackSlot());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003014 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003015 __ imull(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01003016 }
3017 break;
Calin Juravle34bacdf2014-10-07 20:23:36 +01003018
3019 case Primitive::kPrimLong: {
Calin Juravle34bacdf2014-10-07 20:23:36 +01003020 Register in1_hi = first.AsRegisterPairHigh<Register>();
3021 Register in1_lo = first.AsRegisterPairLow<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003022 Register eax = locations->GetTemp(0).AsRegister<Register>();
3023 Register edx = locations->GetTemp(1).AsRegister<Register>();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003024
3025 DCHECK_EQ(EAX, eax);
3026 DCHECK_EQ(EDX, edx);
3027
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003028 // input: in1 - 64 bits, in2 - 64 bits.
Calin Juravle34bacdf2014-10-07 20:23:36 +01003029 // output: in1
3030 // formula: in1.hi : in1.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
3031 // parts: in1.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
3032 // parts: in1.lo = (in1.lo * in2.lo)[31:0]
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003033 if (second.IsConstant()) {
3034 DCHECK(second.GetConstant()->IsLongConstant());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003035
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003036 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3037 int32_t low_value = Low32Bits(value);
3038 int32_t high_value = High32Bits(value);
3039 Immediate low(low_value);
3040 Immediate high(high_value);
3041
3042 __ movl(eax, high);
3043 // eax <- in1.lo * in2.hi
3044 __ imull(eax, in1_lo);
3045 // in1.hi <- in1.hi * in2.lo
3046 __ imull(in1_hi, low);
3047 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3048 __ addl(in1_hi, eax);
3049 // move in2_lo to eax to prepare for double precision
3050 __ movl(eax, low);
3051 // edx:eax <- in1.lo * in2.lo
3052 __ mull(in1_lo);
3053 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3054 __ addl(in1_hi, edx);
3055 // in1.lo <- (in1.lo * in2.lo)[31:0];
3056 __ movl(in1_lo, eax);
3057 } else if (second.IsRegisterPair()) {
3058 Register in2_hi = second.AsRegisterPairHigh<Register>();
3059 Register in2_lo = second.AsRegisterPairLow<Register>();
3060
3061 __ movl(eax, in2_hi);
3062 // eax <- in1.lo * in2.hi
3063 __ imull(eax, in1_lo);
3064 // in1.hi <- in1.hi * in2.lo
3065 __ imull(in1_hi, in2_lo);
3066 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3067 __ addl(in1_hi, eax);
3068 // move in1_lo to eax to prepare for double precision
3069 __ movl(eax, in1_lo);
3070 // edx:eax <- in1.lo * in2.lo
3071 __ mull(in2_lo);
3072 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3073 __ addl(in1_hi, edx);
3074 // in1.lo <- (in1.lo * in2.lo)[31:0];
3075 __ movl(in1_lo, eax);
3076 } else {
3077 DCHECK(second.IsDoubleStackSlot()) << second;
3078 Address in2_hi(ESP, second.GetHighStackIndex(kX86WordSize));
3079 Address in2_lo(ESP, second.GetStackIndex());
3080
3081 __ movl(eax, in2_hi);
3082 // eax <- in1.lo * in2.hi
3083 __ imull(eax, in1_lo);
3084 // in1.hi <- in1.hi * in2.lo
3085 __ imull(in1_hi, in2_lo);
3086 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3087 __ addl(in1_hi, eax);
3088 // move in1_lo to eax to prepare for double precision
3089 __ movl(eax, in1_lo);
3090 // edx:eax <- in1.lo * in2.lo
3091 __ mull(in2_lo);
3092 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3093 __ addl(in1_hi, edx);
3094 // in1.lo <- (in1.lo * in2.lo)[31:0];
3095 __ movl(in1_lo, eax);
3096 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003097
3098 break;
3099 }
3100
Calin Juravleb5bfa962014-10-21 18:02:24 +01003101 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003102 DCHECK(first.Equals(locations->Out()));
3103 if (second.IsFpuRegister()) {
3104 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3105 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3106 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003107 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003108 __ mulss(first.AsFpuRegister<XmmRegister>(),
3109 codegen_->LiteralFloatAddress(
3110 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3111 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3112 } else {
3113 DCHECK(second.IsStackSlot());
3114 __ mulss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3115 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003116 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003117 }
3118
3119 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003120 DCHECK(first.Equals(locations->Out()));
3121 if (second.IsFpuRegister()) {
3122 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3123 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3124 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003125 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003126 __ mulsd(first.AsFpuRegister<XmmRegister>(),
3127 codegen_->LiteralDoubleAddress(
3128 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3129 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3130 } else {
3131 DCHECK(second.IsDoubleStackSlot());
3132 __ mulsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3133 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003134 break;
3135 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003136
3137 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003138 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003139 }
3140}
3141
Roland Levillain232ade02015-04-20 15:14:36 +01003142void InstructionCodeGeneratorX86::PushOntoFPStack(Location source,
3143 uint32_t temp_offset,
3144 uint32_t stack_adjustment,
3145 bool is_fp,
3146 bool is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003147 if (source.IsStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01003148 DCHECK(!is_wide);
3149 if (is_fp) {
3150 __ flds(Address(ESP, source.GetStackIndex() + stack_adjustment));
3151 } else {
3152 __ filds(Address(ESP, source.GetStackIndex() + stack_adjustment));
3153 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003154 } else if (source.IsDoubleStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01003155 DCHECK(is_wide);
3156 if (is_fp) {
3157 __ fldl(Address(ESP, source.GetStackIndex() + stack_adjustment));
3158 } else {
3159 __ fildl(Address(ESP, source.GetStackIndex() + stack_adjustment));
3160 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003161 } else {
3162 // Write the value to the temporary location on the stack and load to FP stack.
Roland Levillain232ade02015-04-20 15:14:36 +01003163 if (!is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003164 Location stack_temp = Location::StackSlot(temp_offset);
3165 codegen_->Move32(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01003166 if (is_fp) {
3167 __ flds(Address(ESP, temp_offset));
3168 } else {
3169 __ filds(Address(ESP, temp_offset));
3170 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003171 } else {
3172 Location stack_temp = Location::DoubleStackSlot(temp_offset);
3173 codegen_->Move64(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01003174 if (is_fp) {
3175 __ fldl(Address(ESP, temp_offset));
3176 } else {
3177 __ fildl(Address(ESP, temp_offset));
3178 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003179 }
3180 }
3181}
3182
3183void InstructionCodeGeneratorX86::GenerateRemFP(HRem *rem) {
3184 Primitive::Type type = rem->GetResultType();
3185 bool is_float = type == Primitive::kPrimFloat;
3186 size_t elem_size = Primitive::ComponentSize(type);
3187 LocationSummary* locations = rem->GetLocations();
3188 Location first = locations->InAt(0);
3189 Location second = locations->InAt(1);
3190 Location out = locations->Out();
3191
3192 // Create stack space for 2 elements.
3193 // TODO: enhance register allocator to ask for stack temporaries.
3194 __ subl(ESP, Immediate(2 * elem_size));
3195
3196 // Load the values to the FP stack in reverse order, using temporaries if needed.
Roland Levillain232ade02015-04-20 15:14:36 +01003197 const bool is_wide = !is_float;
3198 PushOntoFPStack(second, elem_size, 2 * elem_size, /* is_fp */ true, is_wide);
3199 PushOntoFPStack(first, 0, 2 * elem_size, /* is_fp */ true, is_wide);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003200
3201 // Loop doing FPREM until we stabilize.
Mark Mendell0c9497d2015-08-21 09:30:05 -04003202 NearLabel retry;
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003203 __ Bind(&retry);
3204 __ fprem();
3205
3206 // Move FP status to AX.
3207 __ fstsw();
3208
3209 // And see if the argument reduction is complete. This is signaled by the
3210 // C2 FPU flag bit set to 0.
3211 __ andl(EAX, Immediate(kC2ConditionMask));
3212 __ j(kNotEqual, &retry);
3213
3214 // We have settled on the final value. Retrieve it into an XMM register.
3215 // Store FP top of stack to real stack.
3216 if (is_float) {
3217 __ fsts(Address(ESP, 0));
3218 } else {
3219 __ fstl(Address(ESP, 0));
3220 }
3221
3222 // Pop the 2 items from the FP stack.
3223 __ fucompp();
3224
3225 // Load the value from the stack into an XMM register.
3226 DCHECK(out.IsFpuRegister()) << out;
3227 if (is_float) {
3228 __ movss(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
3229 } else {
3230 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
3231 }
3232
3233 // And remove the temporary stack space we allocated.
3234 __ addl(ESP, Immediate(2 * elem_size));
3235}
3236
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003237
3238void InstructionCodeGeneratorX86::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3239 DCHECK(instruction->IsDiv() || instruction->IsRem());
3240
3241 LocationSummary* locations = instruction->GetLocations();
3242 DCHECK(locations->InAt(1).IsConstant());
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003243 DCHECK(locations->InAt(1).GetConstant()->IsIntConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003244
3245 Register out_register = locations->Out().AsRegister<Register>();
3246 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003247 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003248
3249 DCHECK(imm == 1 || imm == -1);
3250
3251 if (instruction->IsRem()) {
3252 __ xorl(out_register, out_register);
3253 } else {
3254 __ movl(out_register, input_register);
3255 if (imm == -1) {
3256 __ negl(out_register);
3257 }
3258 }
3259}
3260
3261
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003262void InstructionCodeGeneratorX86::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003263 LocationSummary* locations = instruction->GetLocations();
3264
3265 Register out_register = locations->Out().AsRegister<Register>();
3266 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003267 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003268 DCHECK(IsPowerOfTwo(AbsOrMin(imm)));
3269 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003270
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003271 Register num = locations->GetTemp(0).AsRegister<Register>();
3272
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003273 __ leal(num, Address(input_register, abs_imm - 1));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003274 __ testl(input_register, input_register);
3275 __ cmovl(kGreaterEqual, num, input_register);
3276 int shift = CTZ(imm);
3277 __ sarl(num, Immediate(shift));
3278
3279 if (imm < 0) {
3280 __ negl(num);
3281 }
3282
3283 __ movl(out_register, num);
3284}
3285
3286void InstructionCodeGeneratorX86::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3287 DCHECK(instruction->IsDiv() || instruction->IsRem());
3288
3289 LocationSummary* locations = instruction->GetLocations();
3290 int imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
3291
3292 Register eax = locations->InAt(0).AsRegister<Register>();
3293 Register out = locations->Out().AsRegister<Register>();
3294 Register num;
3295 Register edx;
3296
3297 if (instruction->IsDiv()) {
3298 edx = locations->GetTemp(0).AsRegister<Register>();
3299 num = locations->GetTemp(1).AsRegister<Register>();
3300 } else {
3301 edx = locations->Out().AsRegister<Register>();
3302 num = locations->GetTemp(0).AsRegister<Register>();
3303 }
3304
3305 DCHECK_EQ(EAX, eax);
3306 DCHECK_EQ(EDX, edx);
3307 if (instruction->IsDiv()) {
3308 DCHECK_EQ(EAX, out);
3309 } else {
3310 DCHECK_EQ(EDX, out);
3311 }
3312
3313 int64_t magic;
3314 int shift;
3315 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
3316
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003317 // Save the numerator.
3318 __ movl(num, eax);
3319
3320 // EAX = magic
3321 __ movl(eax, Immediate(magic));
3322
3323 // EDX:EAX = magic * numerator
3324 __ imull(num);
3325
3326 if (imm > 0 && magic < 0) {
3327 // EDX += num
3328 __ addl(edx, num);
3329 } else if (imm < 0 && magic > 0) {
3330 __ subl(edx, num);
3331 }
3332
3333 // Shift if needed.
3334 if (shift != 0) {
3335 __ sarl(edx, Immediate(shift));
3336 }
3337
3338 // EDX += 1 if EDX < 0
3339 __ movl(eax, edx);
3340 __ shrl(edx, Immediate(31));
3341 __ addl(edx, eax);
3342
3343 if (instruction->IsRem()) {
3344 __ movl(eax, num);
3345 __ imull(edx, Immediate(imm));
3346 __ subl(eax, edx);
3347 __ movl(edx, eax);
3348 } else {
3349 __ movl(eax, edx);
3350 }
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003351}
3352
Calin Juravlebacfec32014-11-14 15:54:36 +00003353void InstructionCodeGeneratorX86::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3354 DCHECK(instruction->IsDiv() || instruction->IsRem());
3355
3356 LocationSummary* locations = instruction->GetLocations();
3357 Location out = locations->Out();
3358 Location first = locations->InAt(0);
3359 Location second = locations->InAt(1);
3360 bool is_div = instruction->IsDiv();
3361
3362 switch (instruction->GetResultType()) {
3363 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003364 DCHECK_EQ(EAX, first.AsRegister<Register>());
3365 DCHECK_EQ(is_div ? EAX : EDX, out.AsRegister<Register>());
Calin Juravlebacfec32014-11-14 15:54:36 +00003366
Vladimir Marko13c86fd2015-11-11 12:37:46 +00003367 if (second.IsConstant()) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003368 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003369
3370 if (imm == 0) {
3371 // Do not generate anything for 0. DivZeroCheck would forbid any generated code.
3372 } else if (imm == 1 || imm == -1) {
3373 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003374 } else if (is_div && IsPowerOfTwo(AbsOrMin(imm))) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003375 DivByPowerOfTwo(instruction->AsDiv());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003376 } else {
3377 DCHECK(imm <= -2 || imm >= 2);
3378 GenerateDivRemWithAnyConstant(instruction);
3379 }
3380 } else {
David Srbecky9cd6d372016-02-09 15:24:47 +00003381 SlowPathCode* slow_path = new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86(
3382 instruction, out.AsRegister<Register>(), is_div);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003383 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00003384
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003385 Register second_reg = second.AsRegister<Register>();
3386 // 0x80000000/-1 triggers an arithmetic exception!
3387 // Dividing by -1 is actually negation and -0x800000000 = 0x80000000 so
3388 // it's safe to just use negl instead of more complex comparisons.
Calin Juravlebacfec32014-11-14 15:54:36 +00003389
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003390 __ cmpl(second_reg, Immediate(-1));
3391 __ j(kEqual, slow_path->GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +00003392
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003393 // edx:eax <- sign-extended of eax
3394 __ cdq();
3395 // eax = quotient, edx = remainder
3396 __ idivl(second_reg);
3397 __ Bind(slow_path->GetExitLabel());
3398 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003399 break;
3400 }
3401
3402 case Primitive::kPrimLong: {
3403 InvokeRuntimeCallingConvention calling_convention;
3404 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
3405 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
3406 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
3407 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
3408 DCHECK_EQ(EAX, out.AsRegisterPairLow<Register>());
3409 DCHECK_EQ(EDX, out.AsRegisterPairHigh<Register>());
3410
3411 if (is_div) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01003412 codegen_->InvokeRuntime(kQuickLdiv, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003413 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003414 } else {
Serban Constantinescuba45db02016-07-12 22:53:02 +01003415 codegen_->InvokeRuntime(kQuickLmod, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003416 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003417 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003418 break;
3419 }
3420
3421 default:
3422 LOG(FATAL) << "Unexpected type for GenerateDivRemIntegral " << instruction->GetResultType();
3423 }
3424}
3425
Calin Juravle7c4954d2014-10-28 16:57:40 +00003426void LocationsBuilderX86::VisitDiv(HDiv* div) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003427 LocationSummary::CallKind call_kind = (div->GetResultType() == Primitive::kPrimLong)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003428 ? LocationSummary::kCallOnMainOnly
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003429 : LocationSummary::kNoCall;
3430 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
3431
Calin Juravle7c4954d2014-10-28 16:57:40 +00003432 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00003433 case Primitive::kPrimInt: {
3434 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003435 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00003436 locations->SetOut(Location::SameAsFirstInput());
3437 // Intel uses edx:eax as the dividend.
3438 locations->AddTemp(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003439 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3440 // which enforces results to be in EAX and EDX, things are simpler if we use EAX also as
3441 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003442 if (div->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003443 locations->AddTemp(Location::RequiresRegister());
3444 }
Calin Juravled0d48522014-11-04 16:40:20 +00003445 break;
3446 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003447 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003448 InvokeRuntimeCallingConvention calling_convention;
3449 locations->SetInAt(0, Location::RegisterPairLocation(
3450 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3451 locations->SetInAt(1, Location::RegisterPairLocation(
3452 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3453 // Runtime helper puts the result in EAX, EDX.
3454 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Calin Juravle7c4954d2014-10-28 16:57:40 +00003455 break;
3456 }
3457 case Primitive::kPrimFloat:
3458 case Primitive::kPrimDouble: {
3459 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003460 if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3461 DCHECK(div->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003462 } else if (div->InputAt(1)->IsConstant()) {
3463 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003464 } else {
3465 locations->SetInAt(1, Location::Any());
3466 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003467 locations->SetOut(Location::SameAsFirstInput());
3468 break;
3469 }
3470
3471 default:
3472 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3473 }
3474}
3475
3476void InstructionCodeGeneratorX86::VisitDiv(HDiv* div) {
3477 LocationSummary* locations = div->GetLocations();
3478 Location first = locations->InAt(0);
3479 Location second = locations->InAt(1);
Calin Juravle7c4954d2014-10-28 16:57:40 +00003480
3481 switch (div->GetResultType()) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003482 case Primitive::kPrimInt:
Calin Juravle7c4954d2014-10-28 16:57:40 +00003483 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003484 GenerateDivRemIntegral(div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00003485 break;
3486 }
3487
3488 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003489 if (second.IsFpuRegister()) {
3490 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3491 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3492 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003493 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003494 __ divss(first.AsFpuRegister<XmmRegister>(),
3495 codegen_->LiteralFloatAddress(
3496 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3497 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3498 } else {
3499 DCHECK(second.IsStackSlot());
3500 __ divss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3501 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003502 break;
3503 }
3504
3505 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003506 if (second.IsFpuRegister()) {
3507 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3508 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3509 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003510 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003511 __ divsd(first.AsFpuRegister<XmmRegister>(),
3512 codegen_->LiteralDoubleAddress(
3513 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3514 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3515 } else {
3516 DCHECK(second.IsDoubleStackSlot());
3517 __ divsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3518 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003519 break;
3520 }
3521
3522 default:
3523 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3524 }
3525}
3526
Calin Juravlebacfec32014-11-14 15:54:36 +00003527void LocationsBuilderX86::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00003528 Primitive::Type type = rem->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003529
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003530 LocationSummary::CallKind call_kind = (rem->GetResultType() == Primitive::kPrimLong)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003531 ? LocationSummary::kCallOnMainOnly
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003532 : LocationSummary::kNoCall;
3533 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
Calin Juravlebacfec32014-11-14 15:54:36 +00003534
Calin Juravled2ec87d2014-12-08 14:24:46 +00003535 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003536 case Primitive::kPrimInt: {
3537 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003538 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00003539 locations->SetOut(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003540 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3541 // which enforces results to be in EAX and EDX, things are simpler if we use EDX also as
3542 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003543 if (rem->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003544 locations->AddTemp(Location::RequiresRegister());
3545 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003546 break;
3547 }
3548 case Primitive::kPrimLong: {
3549 InvokeRuntimeCallingConvention calling_convention;
3550 locations->SetInAt(0, Location::RegisterPairLocation(
3551 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3552 locations->SetInAt(1, Location::RegisterPairLocation(
3553 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3554 // Runtime helper puts the result in EAX, EDX.
3555 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
3556 break;
3557 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003558 case Primitive::kPrimDouble:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003559 case Primitive::kPrimFloat: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003560 locations->SetInAt(0, Location::Any());
3561 locations->SetInAt(1, Location::Any());
3562 locations->SetOut(Location::RequiresFpuRegister());
3563 locations->AddTemp(Location::RegisterLocation(EAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00003564 break;
3565 }
3566
3567 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003568 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003569 }
3570}
3571
3572void InstructionCodeGeneratorX86::VisitRem(HRem* rem) {
3573 Primitive::Type type = rem->GetResultType();
3574 switch (type) {
3575 case Primitive::kPrimInt:
3576 case Primitive::kPrimLong: {
3577 GenerateDivRemIntegral(rem);
3578 break;
3579 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003580 case Primitive::kPrimFloat:
Calin Juravlebacfec32014-11-14 15:54:36 +00003581 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003582 GenerateRemFP(rem);
Calin Juravlebacfec32014-11-14 15:54:36 +00003583 break;
3584 }
3585 default:
3586 LOG(FATAL) << "Unexpected rem type " << type;
3587 }
3588}
3589
Calin Juravled0d48522014-11-04 16:40:20 +00003590void LocationsBuilderX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003591 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003592 switch (instruction->GetType()) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003593 case Primitive::kPrimBoolean:
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003594 case Primitive::kPrimByte:
3595 case Primitive::kPrimChar:
3596 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003597 case Primitive::kPrimInt: {
3598 locations->SetInAt(0, Location::Any());
3599 break;
3600 }
3601 case Primitive::kPrimLong: {
3602 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
3603 if (!instruction->IsConstant()) {
3604 locations->AddTemp(Location::RequiresRegister());
3605 }
3606 break;
3607 }
3608 default:
3609 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
3610 }
Calin Juravled0d48522014-11-04 16:40:20 +00003611}
3612
3613void InstructionCodeGeneratorX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003614 SlowPathCode* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86(instruction);
Calin Juravled0d48522014-11-04 16:40:20 +00003615 codegen_->AddSlowPath(slow_path);
3616
3617 LocationSummary* locations = instruction->GetLocations();
3618 Location value = locations->InAt(0);
3619
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003620 switch (instruction->GetType()) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003621 case Primitive::kPrimBoolean:
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003622 case Primitive::kPrimByte:
3623 case Primitive::kPrimChar:
3624 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003625 case Primitive::kPrimInt: {
3626 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003627 __ testl(value.AsRegister<Register>(), value.AsRegister<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003628 __ j(kEqual, slow_path->GetEntryLabel());
3629 } else if (value.IsStackSlot()) {
3630 __ cmpl(Address(ESP, value.GetStackIndex()), Immediate(0));
3631 __ j(kEqual, slow_path->GetEntryLabel());
3632 } else {
3633 DCHECK(value.IsConstant()) << value;
3634 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01003635 __ jmp(slow_path->GetEntryLabel());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003636 }
3637 }
3638 break;
Calin Juravled0d48522014-11-04 16:40:20 +00003639 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003640 case Primitive::kPrimLong: {
3641 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003642 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003643 __ movl(temp, value.AsRegisterPairLow<Register>());
3644 __ orl(temp, value.AsRegisterPairHigh<Register>());
3645 __ j(kEqual, slow_path->GetEntryLabel());
3646 } else {
3647 DCHECK(value.IsConstant()) << value;
3648 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
3649 __ jmp(slow_path->GetEntryLabel());
3650 }
3651 }
3652 break;
3653 }
3654 default:
3655 LOG(FATAL) << "Unexpected type for HDivZeroCheck" << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00003656 }
Calin Juravled0d48522014-11-04 16:40:20 +00003657}
3658
Calin Juravle9aec02f2014-11-18 23:06:35 +00003659void LocationsBuilderX86::HandleShift(HBinaryOperation* op) {
3660 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3661
3662 LocationSummary* locations =
3663 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
3664
3665 switch (op->GetResultType()) {
Mark P Mendell73945692015-04-29 14:56:17 +00003666 case Primitive::kPrimInt:
Calin Juravle9aec02f2014-11-18 23:06:35 +00003667 case Primitive::kPrimLong: {
Mark P Mendell73945692015-04-29 14:56:17 +00003668 // Can't have Location::Any() and output SameAsFirstInput()
Calin Juravle9aec02f2014-11-18 23:06:35 +00003669 locations->SetInAt(0, Location::RequiresRegister());
Mark P Mendell73945692015-04-29 14:56:17 +00003670 // The shift count needs to be in CL or a constant.
3671 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, op->InputAt(1)));
Calin Juravle9aec02f2014-11-18 23:06:35 +00003672 locations->SetOut(Location::SameAsFirstInput());
3673 break;
3674 }
3675 default:
3676 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
3677 }
3678}
3679
3680void InstructionCodeGeneratorX86::HandleShift(HBinaryOperation* op) {
3681 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3682
3683 LocationSummary* locations = op->GetLocations();
3684 Location first = locations->InAt(0);
3685 Location second = locations->InAt(1);
3686 DCHECK(first.Equals(locations->Out()));
3687
3688 switch (op->GetResultType()) {
3689 case Primitive::kPrimInt: {
Mark P Mendell73945692015-04-29 14:56:17 +00003690 DCHECK(first.IsRegister());
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003691 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003692 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003693 Register second_reg = second.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003694 DCHECK_EQ(ECX, second_reg);
3695 if (op->IsShl()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003696 __ shll(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003697 } else if (op->IsShr()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003698 __ sarl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003699 } else {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003700 __ shrl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003701 }
3702 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003703 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance;
Mark P Mendell73945692015-04-29 14:56:17 +00003704 if (shift == 0) {
3705 return;
3706 }
3707 Immediate imm(shift);
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003708 if (op->IsShl()) {
3709 __ shll(first_reg, imm);
3710 } else if (op->IsShr()) {
3711 __ sarl(first_reg, imm);
3712 } else {
3713 __ shrl(first_reg, imm);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003714 }
3715 }
3716 break;
3717 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003718 case Primitive::kPrimLong: {
Mark P Mendell73945692015-04-29 14:56:17 +00003719 if (second.IsRegister()) {
3720 Register second_reg = second.AsRegister<Register>();
3721 DCHECK_EQ(ECX, second_reg);
3722 if (op->IsShl()) {
3723 GenerateShlLong(first, second_reg);
3724 } else if (op->IsShr()) {
3725 GenerateShrLong(first, second_reg);
3726 } else {
3727 GenerateUShrLong(first, second_reg);
3728 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003729 } else {
Mark P Mendell73945692015-04-29 14:56:17 +00003730 // Shift by a constant.
Roland Levillain5b5b9312016-03-22 14:57:31 +00003731 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance;
Mark P Mendell73945692015-04-29 14:56:17 +00003732 // Nothing to do if the shift is 0, as the input is already the output.
3733 if (shift != 0) {
3734 if (op->IsShl()) {
3735 GenerateShlLong(first, shift);
3736 } else if (op->IsShr()) {
3737 GenerateShrLong(first, shift);
3738 } else {
3739 GenerateUShrLong(first, shift);
3740 }
3741 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003742 }
3743 break;
3744 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003745 default:
3746 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
3747 }
3748}
3749
Mark P Mendell73945692015-04-29 14:56:17 +00003750void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, int shift) {
3751 Register low = loc.AsRegisterPairLow<Register>();
3752 Register high = loc.AsRegisterPairHigh<Register>();
Mark Mendellba56d062015-05-05 21:34:03 -04003753 if (shift == 1) {
3754 // This is just an addition.
3755 __ addl(low, low);
3756 __ adcl(high, high);
3757 } else if (shift == 32) {
Mark P Mendell73945692015-04-29 14:56:17 +00003758 // Shift by 32 is easy. High gets low, and low gets 0.
3759 codegen_->EmitParallelMoves(
3760 loc.ToLow(),
3761 loc.ToHigh(),
3762 Primitive::kPrimInt,
3763 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
3764 loc.ToLow(),
3765 Primitive::kPrimInt);
3766 } else if (shift > 32) {
3767 // Low part becomes 0. High part is low part << (shift-32).
3768 __ movl(high, low);
3769 __ shll(high, Immediate(shift - 32));
3770 __ xorl(low, low);
3771 } else {
3772 // Between 1 and 31.
3773 __ shld(high, low, Immediate(shift));
3774 __ shll(low, Immediate(shift));
3775 }
3776}
3777
Calin Juravle9aec02f2014-11-18 23:06:35 +00003778void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04003779 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00003780 __ shld(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>(), shifter);
3781 __ shll(loc.AsRegisterPairLow<Register>(), shifter);
3782 __ testl(shifter, Immediate(32));
3783 __ j(kEqual, &done);
3784 __ movl(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>());
3785 __ movl(loc.AsRegisterPairLow<Register>(), Immediate(0));
3786 __ Bind(&done);
3787}
3788
Mark P Mendell73945692015-04-29 14:56:17 +00003789void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, int shift) {
3790 Register low = loc.AsRegisterPairLow<Register>();
3791 Register high = loc.AsRegisterPairHigh<Register>();
3792 if (shift == 32) {
3793 // Need to copy the sign.
3794 DCHECK_NE(low, high);
3795 __ movl(low, high);
3796 __ sarl(high, Immediate(31));
3797 } else if (shift > 32) {
3798 DCHECK_NE(low, high);
3799 // High part becomes sign. Low part is shifted by shift - 32.
3800 __ movl(low, high);
3801 __ sarl(high, Immediate(31));
3802 __ sarl(low, Immediate(shift - 32));
3803 } else {
3804 // Between 1 and 31.
3805 __ shrd(low, high, Immediate(shift));
3806 __ sarl(high, Immediate(shift));
3807 }
3808}
3809
Calin Juravle9aec02f2014-11-18 23:06:35 +00003810void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04003811 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00003812 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
3813 __ sarl(loc.AsRegisterPairHigh<Register>(), shifter);
3814 __ testl(shifter, Immediate(32));
3815 __ j(kEqual, &done);
3816 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
3817 __ sarl(loc.AsRegisterPairHigh<Register>(), Immediate(31));
3818 __ Bind(&done);
3819}
3820
Mark P Mendell73945692015-04-29 14:56:17 +00003821void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, int shift) {
3822 Register low = loc.AsRegisterPairLow<Register>();
3823 Register high = loc.AsRegisterPairHigh<Register>();
3824 if (shift == 32) {
3825 // Shift by 32 is easy. Low gets high, and high gets 0.
3826 codegen_->EmitParallelMoves(
3827 loc.ToHigh(),
3828 loc.ToLow(),
3829 Primitive::kPrimInt,
3830 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
3831 loc.ToHigh(),
3832 Primitive::kPrimInt);
3833 } else if (shift > 32) {
3834 // Low part is high >> (shift - 32). High part becomes 0.
3835 __ movl(low, high);
3836 __ shrl(low, Immediate(shift - 32));
3837 __ xorl(high, high);
3838 } else {
3839 // Between 1 and 31.
3840 __ shrd(low, high, Immediate(shift));
3841 __ shrl(high, Immediate(shift));
3842 }
3843}
3844
Calin Juravle9aec02f2014-11-18 23:06:35 +00003845void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04003846 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00003847 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
3848 __ shrl(loc.AsRegisterPairHigh<Register>(), shifter);
3849 __ testl(shifter, Immediate(32));
3850 __ j(kEqual, &done);
3851 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
3852 __ movl(loc.AsRegisterPairHigh<Register>(), Immediate(0));
3853 __ Bind(&done);
3854}
3855
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003856void LocationsBuilderX86::VisitRor(HRor* ror) {
3857 LocationSummary* locations =
3858 new (GetGraph()->GetArena()) LocationSummary(ror, LocationSummary::kNoCall);
3859
3860 switch (ror->GetResultType()) {
3861 case Primitive::kPrimLong:
3862 // Add the temporary needed.
3863 locations->AddTemp(Location::RequiresRegister());
3864 FALLTHROUGH_INTENDED;
3865 case Primitive::kPrimInt:
3866 locations->SetInAt(0, Location::RequiresRegister());
3867 // The shift count needs to be in CL (unless it is a constant).
3868 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, ror->InputAt(1)));
3869 locations->SetOut(Location::SameAsFirstInput());
3870 break;
3871 default:
3872 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
3873 UNREACHABLE();
3874 }
3875}
3876
3877void InstructionCodeGeneratorX86::VisitRor(HRor* ror) {
3878 LocationSummary* locations = ror->GetLocations();
3879 Location first = locations->InAt(0);
3880 Location second = locations->InAt(1);
3881
3882 if (ror->GetResultType() == Primitive::kPrimInt) {
3883 Register first_reg = first.AsRegister<Register>();
3884 if (second.IsRegister()) {
3885 Register second_reg = second.AsRegister<Register>();
3886 __ rorl(first_reg, second_reg);
3887 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003888 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003889 __ rorl(first_reg, imm);
3890 }
3891 return;
3892 }
3893
3894 DCHECK_EQ(ror->GetResultType(), Primitive::kPrimLong);
3895 Register first_reg_lo = first.AsRegisterPairLow<Register>();
3896 Register first_reg_hi = first.AsRegisterPairHigh<Register>();
3897 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
3898 if (second.IsRegister()) {
3899 Register second_reg = second.AsRegister<Register>();
3900 DCHECK_EQ(second_reg, ECX);
3901 __ movl(temp_reg, first_reg_hi);
3902 __ shrd(first_reg_hi, first_reg_lo, second_reg);
3903 __ shrd(first_reg_lo, temp_reg, second_reg);
3904 __ movl(temp_reg, first_reg_hi);
3905 __ testl(second_reg, Immediate(32));
3906 __ cmovl(kNotEqual, first_reg_hi, first_reg_lo);
3907 __ cmovl(kNotEqual, first_reg_lo, temp_reg);
3908 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003909 int32_t shift_amt = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance;
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003910 if (shift_amt == 0) {
3911 // Already fine.
3912 return;
3913 }
3914 if (shift_amt == 32) {
3915 // Just swap.
3916 __ movl(temp_reg, first_reg_lo);
3917 __ movl(first_reg_lo, first_reg_hi);
3918 __ movl(first_reg_hi, temp_reg);
3919 return;
3920 }
3921
3922 Immediate imm(shift_amt);
3923 // Save the constents of the low value.
3924 __ movl(temp_reg, first_reg_lo);
3925
3926 // Shift right into low, feeding bits from high.
3927 __ shrd(first_reg_lo, first_reg_hi, imm);
3928
3929 // Shift right into high, feeding bits from the original low.
3930 __ shrd(first_reg_hi, temp_reg, imm);
3931
3932 // Swap if needed.
3933 if (shift_amt > 32) {
3934 __ movl(temp_reg, first_reg_lo);
3935 __ movl(first_reg_lo, first_reg_hi);
3936 __ movl(first_reg_hi, temp_reg);
3937 }
3938 }
3939}
3940
Calin Juravle9aec02f2014-11-18 23:06:35 +00003941void LocationsBuilderX86::VisitShl(HShl* shl) {
3942 HandleShift(shl);
3943}
3944
3945void InstructionCodeGeneratorX86::VisitShl(HShl* shl) {
3946 HandleShift(shl);
3947}
3948
3949void LocationsBuilderX86::VisitShr(HShr* shr) {
3950 HandleShift(shr);
3951}
3952
3953void InstructionCodeGeneratorX86::VisitShr(HShr* shr) {
3954 HandleShift(shr);
3955}
3956
3957void LocationsBuilderX86::VisitUShr(HUShr* ushr) {
3958 HandleShift(ushr);
3959}
3960
3961void InstructionCodeGeneratorX86::VisitUShr(HUShr* ushr) {
3962 HandleShift(ushr);
3963}
3964
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003965void LocationsBuilderX86::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003966 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003967 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003968 locations->SetOut(Location::RegisterLocation(EAX));
David Brazdil6de19382016-01-08 17:37:10 +00003969 if (instruction->IsStringAlloc()) {
3970 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
3971 } else {
3972 InvokeRuntimeCallingConvention calling_convention;
3973 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3974 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3975 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003976}
3977
3978void InstructionCodeGeneratorX86::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01003979 // Note: if heap poisoning is enabled, the entry point takes cares
3980 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00003981 if (instruction->IsStringAlloc()) {
3982 // String is allocated through StringFactory. Call NewEmptyString entry point.
3983 Register temp = instruction->GetLocations()->GetTemp(0).AsRegister<Register>();
Andreas Gampe542451c2016-07-26 09:02:02 -07003984 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86PointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00003985 __ fs()->movl(temp, Address::Absolute(QUICK_ENTRY_POINT(pNewEmptyString)));
3986 __ call(Address(temp, code_offset.Int32Value()));
3987 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3988 } else {
Serban Constantinescuba45db02016-07-12 22:53:02 +01003989 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
David Brazdil6de19382016-01-08 17:37:10 +00003990 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
3991 DCHECK(!codegen_->IsLeafMethod());
3992 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003993}
3994
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003995void LocationsBuilderX86::VisitNewArray(HNewArray* instruction) {
3996 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003997 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003998 locations->SetOut(Location::RegisterLocation(EAX));
3999 InvokeRuntimeCallingConvention calling_convention;
4000 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08004001 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004002 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004003}
4004
4005void InstructionCodeGeneratorX86::VisitNewArray(HNewArray* instruction) {
4006 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004007 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
Roland Levillain4d027112015-07-01 15:41:14 +01004008 // Note: if heap poisoning is enabled, the entry point takes cares
4009 // of poisoning the reference.
Serban Constantinescuba45db02016-07-12 22:53:02 +01004010 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00004011 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004012 DCHECK(!codegen_->IsLeafMethod());
4013}
4014
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004015void LocationsBuilderX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004016 LocationSummary* locations =
4017 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01004018 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
4019 if (location.IsStackSlot()) {
4020 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4021 } else if (location.IsDoubleStackSlot()) {
4022 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004023 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01004024 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004025}
4026
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004027void InstructionCodeGeneratorX86::VisitParameterValue(
4028 HParameterValue* instruction ATTRIBUTE_UNUSED) {
4029}
4030
4031void LocationsBuilderX86::VisitCurrentMethod(HCurrentMethod* instruction) {
4032 LocationSummary* locations =
4033 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4034 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
4035}
4036
4037void InstructionCodeGeneratorX86::VisitCurrentMethod(HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004038}
4039
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004040void LocationsBuilderX86::VisitClassTableGet(HClassTableGet* instruction) {
4041 LocationSummary* locations =
4042 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4043 locations->SetInAt(0, Location::RequiresRegister());
4044 locations->SetOut(Location::RequiresRegister());
4045}
4046
4047void InstructionCodeGeneratorX86::VisitClassTableGet(HClassTableGet* instruction) {
4048 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004049 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004050 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004051 instruction->GetIndex(), kX86PointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004052 __ movl(locations->Out().AsRegister<Register>(),
4053 Address(locations->InAt(0).AsRegister<Register>(), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004054 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004055 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00004056 instruction->GetIndex(), kX86PointerSize));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004057 __ movl(locations->Out().AsRegister<Register>(),
4058 Address(locations->InAt(0).AsRegister<Register>(),
4059 mirror::Class::ImtPtrOffset(kX86PointerSize).Uint32Value()));
4060 // temp = temp->GetImtEntryAt(method_offset);
4061 __ movl(locations->Out().AsRegister<Register>(),
4062 Address(locations->Out().AsRegister<Register>(), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004063 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004064}
4065
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004066void LocationsBuilderX86::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004067 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004068 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01004069 locations->SetInAt(0, Location::RequiresRegister());
4070 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004071}
4072
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004073void InstructionCodeGeneratorX86::VisitNot(HNot* not_) {
4074 LocationSummary* locations = not_->GetLocations();
Roland Levillain70566432014-10-24 16:20:17 +01004075 Location in = locations->InAt(0);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01004076 Location out = locations->Out();
Roland Levillain70566432014-10-24 16:20:17 +01004077 DCHECK(in.Equals(out));
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00004078 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004079 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00004080 __ notl(out.AsRegister<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004081 break;
4082
4083 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01004084 __ notl(out.AsRegisterPairLow<Register>());
4085 __ notl(out.AsRegisterPairHigh<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004086 break;
4087
4088 default:
4089 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
4090 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004091}
4092
David Brazdil66d126e2015-04-03 16:02:44 +01004093void LocationsBuilderX86::VisitBooleanNot(HBooleanNot* bool_not) {
4094 LocationSummary* locations =
4095 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
4096 locations->SetInAt(0, Location::RequiresRegister());
4097 locations->SetOut(Location::SameAsFirstInput());
4098}
4099
4100void InstructionCodeGeneratorX86::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01004101 LocationSummary* locations = bool_not->GetLocations();
4102 Location in = locations->InAt(0);
4103 Location out = locations->Out();
4104 DCHECK(in.Equals(out));
4105 __ xorl(out.AsRegister<Register>(), Immediate(1));
4106}
4107
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004108void LocationsBuilderX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004109 LocationSummary* locations =
4110 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00004111 switch (compare->InputAt(0)->GetType()) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00004112 case Primitive::kPrimBoolean:
4113 case Primitive::kPrimByte:
4114 case Primitive::kPrimShort:
4115 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08004116 case Primitive::kPrimInt:
Calin Juravleddb7df22014-11-25 20:56:51 +00004117 case Primitive::kPrimLong: {
4118 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravleddb7df22014-11-25 20:56:51 +00004119 locations->SetInAt(1, Location::Any());
4120 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4121 break;
4122 }
4123 case Primitive::kPrimFloat:
4124 case Primitive::kPrimDouble: {
4125 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004126 if (compare->InputAt(1)->IsX86LoadFromConstantTable()) {
4127 DCHECK(compare->InputAt(1)->IsEmittedAtUseSite());
4128 } else if (compare->InputAt(1)->IsConstant()) {
4129 locations->SetInAt(1, Location::RequiresFpuRegister());
4130 } else {
4131 locations->SetInAt(1, Location::Any());
4132 }
Calin Juravleddb7df22014-11-25 20:56:51 +00004133 locations->SetOut(Location::RequiresRegister());
4134 break;
4135 }
4136 default:
4137 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
4138 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004139}
4140
4141void InstructionCodeGeneratorX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004142 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004143 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00004144 Location left = locations->InAt(0);
4145 Location right = locations->InAt(1);
4146
Mark Mendell0c9497d2015-08-21 09:30:05 -04004147 NearLabel less, greater, done;
Aart Bika19616e2016-02-01 18:57:58 -08004148 Condition less_cond = kLess;
4149
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004150 switch (compare->InputAt(0)->GetType()) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00004151 case Primitive::kPrimBoolean:
4152 case Primitive::kPrimByte:
4153 case Primitive::kPrimShort:
4154 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08004155 case Primitive::kPrimInt: {
Roland Levillain0b671c02016-08-19 12:02:34 +01004156 codegen_->GenerateIntCompare(left, right);
Aart Bika19616e2016-02-01 18:57:58 -08004157 break;
4158 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004159 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004160 Register left_low = left.AsRegisterPairLow<Register>();
4161 Register left_high = left.AsRegisterPairHigh<Register>();
4162 int32_t val_low = 0;
4163 int32_t val_high = 0;
4164 bool right_is_const = false;
4165
4166 if (right.IsConstant()) {
4167 DCHECK(right.GetConstant()->IsLongConstant());
4168 right_is_const = true;
4169 int64_t val = right.GetConstant()->AsLongConstant()->GetValue();
4170 val_low = Low32Bits(val);
4171 val_high = High32Bits(val);
4172 }
4173
Calin Juravleddb7df22014-11-25 20:56:51 +00004174 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004175 __ cmpl(left_high, right.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004176 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004177 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004178 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004179 DCHECK(right_is_const) << right;
Aart Bika19616e2016-02-01 18:57:58 -08004180 codegen_->Compare32BitValue(left_high, val_high);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004181 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004182 __ j(kLess, &less); // Signed compare.
4183 __ j(kGreater, &greater); // Signed compare.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004184 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004185 __ cmpl(left_low, right.AsRegisterPairLow<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004186 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004187 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004188 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004189 DCHECK(right_is_const) << right;
Aart Bika19616e2016-02-01 18:57:58 -08004190 codegen_->Compare32BitValue(left_low, val_low);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004191 }
Aart Bika19616e2016-02-01 18:57:58 -08004192 less_cond = kBelow; // for CF (unsigned).
Calin Juravleddb7df22014-11-25 20:56:51 +00004193 break;
4194 }
4195 case Primitive::kPrimFloat: {
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004196 GenerateFPCompare(left, right, compare, false);
Calin Juravleddb7df22014-11-25 20:56:51 +00004197 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08004198 less_cond = kBelow; // for CF (floats).
Calin Juravleddb7df22014-11-25 20:56:51 +00004199 break;
4200 }
4201 case Primitive::kPrimDouble: {
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004202 GenerateFPCompare(left, right, compare, true);
Calin Juravleddb7df22014-11-25 20:56:51 +00004203 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08004204 less_cond = kBelow; // for CF (floats).
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004205 break;
4206 }
4207 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00004208 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004209 }
Aart Bika19616e2016-02-01 18:57:58 -08004210
Calin Juravleddb7df22014-11-25 20:56:51 +00004211 __ movl(out, Immediate(0));
4212 __ j(kEqual, &done);
Aart Bika19616e2016-02-01 18:57:58 -08004213 __ j(less_cond, &less);
Calin Juravleddb7df22014-11-25 20:56:51 +00004214
4215 __ Bind(&greater);
4216 __ movl(out, Immediate(1));
4217 __ jmp(&done);
4218
4219 __ Bind(&less);
4220 __ movl(out, Immediate(-1));
4221
4222 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004223}
4224
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004225void LocationsBuilderX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004226 LocationSummary* locations =
4227 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko372f10e2016-05-17 16:30:10 +01004228 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01004229 locations->SetInAt(i, Location::Any());
4230 }
4231 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004232}
4233
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004234void InstructionCodeGeneratorX86::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004235 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004236}
4237
Roland Levillain7c1559a2015-12-15 10:55:36 +00004238void CodeGeneratorX86::GenerateMemoryBarrier(MemBarrierKind kind) {
Calin Juravle52c48962014-12-16 17:02:57 +00004239 /*
4240 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
4241 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
4242 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
4243 */
4244 switch (kind) {
4245 case MemBarrierKind::kAnyAny: {
Mark P Mendell17077d82015-12-16 19:15:59 +00004246 MemoryFence();
Calin Juravle52c48962014-12-16 17:02:57 +00004247 break;
4248 }
4249 case MemBarrierKind::kAnyStore:
4250 case MemBarrierKind::kLoadAny:
4251 case MemBarrierKind::kStoreStore: {
4252 // nop
4253 break;
4254 }
Mark Mendell7aa04a12016-01-27 22:39:07 -05004255 case MemBarrierKind::kNTStoreStore:
4256 // Non-Temporal Store/Store needs an explicit fence.
4257 MemoryFence(/* non-temporal */ true);
4258 break;
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004259 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004260}
4261
Vladimir Markodc151b22015-10-15 18:02:30 +01004262HInvokeStaticOrDirect::DispatchInfo CodeGeneratorX86::GetSupportedInvokeStaticOrDirectDispatch(
4263 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004264 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00004265 HInvokeStaticOrDirect::DispatchInfo dispatch_info = desired_dispatch_info;
4266
4267 // We disable pc-relative load when there is an irreducible loop, as the optimization
4268 // is incompatible with it.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004269 // TODO: Create as many X86ComputeBaseMethodAddress instructions
4270 // as needed for methods with irreducible loops.
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00004271 if (GetGraph()->HasIrreducibleLoops() &&
4272 (dispatch_info.method_load_kind ==
4273 HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative)) {
4274 dispatch_info.method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod;
4275 }
4276 switch (dispatch_info.code_ptr_location) {
Vladimir Markodc151b22015-10-15 18:02:30 +01004277 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
4278 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
4279 // For direct code, we actually prefer to call via the code pointer from ArtMethod*.
4280 // (Though the direct CALL ptr16:32 is available for consideration).
4281 return HInvokeStaticOrDirect::DispatchInfo {
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00004282 dispatch_info.method_load_kind,
Vladimir Markodc151b22015-10-15 18:02:30 +01004283 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00004284 dispatch_info.method_load_data,
Vladimir Markodc151b22015-10-15 18:02:30 +01004285 0u
4286 };
4287 default:
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00004288 return dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01004289 }
4290}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004291
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004292Register CodeGeneratorX86::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
4293 Register temp) {
4294 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
Vladimir Markoc53c0792015-11-19 15:48:33 +00004295 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004296 if (!invoke->GetLocations()->Intrinsified()) {
4297 return location.AsRegister<Register>();
4298 }
4299 // For intrinsics we allow any location, so it may be on the stack.
4300 if (!location.IsRegister()) {
4301 __ movl(temp, Address(ESP, location.GetStackIndex()));
4302 return temp;
4303 }
4304 // For register locations, check if the register was saved. If so, get it from the stack.
4305 // Note: There is a chance that the register was saved but not overwritten, so we could
4306 // save one load. However, since this is just an intrinsic slow path we prefer this
4307 // simple and more robust approach rather that trying to determine if that's the case.
4308 SlowPathCode* slow_path = GetCurrentSlowPath();
Serguei Katkov288c7a82016-05-16 11:53:15 +06004309 if (slow_path != nullptr) {
4310 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
4311 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
4312 __ movl(temp, Address(ESP, stack_offset));
4313 return temp;
4314 }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004315 }
4316 return location.AsRegister<Register>();
4317}
4318
Serguei Katkov288c7a82016-05-16 11:53:15 +06004319Location CodeGeneratorX86::GenerateCalleeMethodStaticOrDirectCall(HInvokeStaticOrDirect* invoke,
4320 Location temp) {
Vladimir Marko58155012015-08-19 12:49:41 +00004321 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
4322 switch (invoke->GetMethodLoadKind()) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004323 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
Vladimir Marko58155012015-08-19 12:49:41 +00004324 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004325 uint32_t offset =
4326 GetThreadOffset<kX86PointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
4327 __ fs()->movl(temp.AsRegister<Register>(), Address::Absolute(offset));
Vladimir Marko58155012015-08-19 12:49:41 +00004328 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004329 }
Vladimir Marko58155012015-08-19 12:49:41 +00004330 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00004331 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004332 break;
4333 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
4334 __ movl(temp.AsRegister<Register>(), Immediate(invoke->GetMethodAddress()));
4335 break;
4336 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004337 __ movl(temp.AsRegister<Register>(), Immediate(/* placeholder */ 0));
Vladimir Markoaad75c62016-10-03 08:46:48 +00004338 method_patches_.emplace_back(*invoke->GetTargetMethod().dex_file,
4339 invoke->GetTargetMethod().dex_method_index);
Vladimir Marko58155012015-08-19 12:49:41 +00004340 __ Bind(&method_patches_.back().label); // Bind the label at the end of the "movl" insn.
4341 break;
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004342 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
4343 Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
4344 temp.AsRegister<Register>());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004345 __ movl(temp.AsRegister<Register>(), Address(base_reg, kDummy32BitOffset));
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004346 // Bind a new fixup label at the end of the "movl" insn.
4347 uint32_t offset = invoke->GetDexCacheArrayOffset();
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004348 __ Bind(NewPcRelativeDexCacheArrayPatch(invoke->GetDexFile(), offset));
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004349 break;
4350 }
Vladimir Marko58155012015-08-19 12:49:41 +00004351 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004352 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004353 Register method_reg;
4354 Register reg = temp.AsRegister<Register>();
4355 if (current_method.IsRegister()) {
4356 method_reg = current_method.AsRegister<Register>();
4357 } else {
David Brazdil58282f42016-01-14 12:45:10 +00004358 DCHECK(invoke->GetLocations()->Intrinsified());
Vladimir Marko58155012015-08-19 12:49:41 +00004359 DCHECK(!current_method.IsValid());
4360 method_reg = reg;
4361 __ movl(reg, Address(ESP, kCurrentMethodStackOffset));
4362 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00004363 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +01004364 __ movl(reg, Address(method_reg,
4365 ArtMethod::DexCacheResolvedMethodsOffset(kX86PointerSize).Int32Value()));
Vladimir Marko40ecb122016-04-06 17:33:41 +01004366 // temp = temp[index_in_cache];
4367 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
4368 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Vladimir Marko58155012015-08-19 12:49:41 +00004369 __ movl(reg, Address(reg, CodeGenerator::GetCachePointerOffset(index_in_cache)));
4370 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +01004371 }
Vladimir Marko58155012015-08-19 12:49:41 +00004372 }
Serguei Katkov288c7a82016-05-16 11:53:15 +06004373 return callee_method;
4374}
4375
4376void CodeGeneratorX86::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
4377 Location callee_method = GenerateCalleeMethodStaticOrDirectCall(invoke, temp);
Vladimir Marko58155012015-08-19 12:49:41 +00004378
4379 switch (invoke->GetCodePtrLocation()) {
4380 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
4381 __ call(GetFrameEntryLabel());
4382 break;
4383 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative: {
Vladimir Markoaad75c62016-10-03 08:46:48 +00004384 relative_call_patches_.emplace_back(*invoke->GetTargetMethod().dex_file,
4385 invoke->GetTargetMethod().dex_method_index);
Vladimir Marko58155012015-08-19 12:49:41 +00004386 Label* label = &relative_call_patches_.back().label;
4387 __ call(label); // Bind to the patch label, override at link time.
4388 __ Bind(label); // Bind the label at the end of the "call" insn.
4389 break;
4390 }
4391 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
4392 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
Vladimir Markodc151b22015-10-15 18:02:30 +01004393 // Filtered out by GetSupportedInvokeStaticOrDirectDispatch().
4394 LOG(FATAL) << "Unsupported";
4395 UNREACHABLE();
Vladimir Marko58155012015-08-19 12:49:41 +00004396 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
4397 // (callee_method + offset_of_quick_compiled_code)()
4398 __ call(Address(callee_method.AsRegister<Register>(),
4399 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07004400 kX86PointerSize).Int32Value()));
Vladimir Marko58155012015-08-19 12:49:41 +00004401 break;
Mark Mendell09ed1a32015-03-25 08:30:06 -04004402 }
4403
4404 DCHECK(!IsLeafMethod());
Mark Mendell09ed1a32015-03-25 08:30:06 -04004405}
4406
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004407void CodeGeneratorX86::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_in) {
4408 Register temp = temp_in.AsRegister<Register>();
4409 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
4410 invoke->GetVTableIndex(), kX86PointerSize).Uint32Value();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004411
4412 // Use the calling convention instead of the location of the receiver, as
4413 // intrinsics may have put the receiver in a different register. In the intrinsics
4414 // slow path, the arguments have been moved to the right place, so here we are
4415 // guaranteed that the receiver is the first register of the calling convention.
4416 InvokeDexCallingConvention calling_convention;
4417 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004418 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004419 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004420 __ movl(temp, Address(receiver, class_offset));
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004421 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00004422 // Instead of simply (possibly) unpoisoning `temp` here, we should
4423 // emit a read barrier for the previous class reference load.
4424 // However this is not required in practice, as this is an
4425 // intermediate/temporary reference and because the current
4426 // concurrent copying collector keeps the from-space memory
4427 // intact/accessible until the end of the marking phase (the
4428 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004429 __ MaybeUnpoisonHeapReference(temp);
4430 // temp = temp->GetMethodAt(method_offset);
4431 __ movl(temp, Address(temp, method_offset));
4432 // call temp->GetEntryPoint();
4433 __ call(Address(
Andreas Gampe542451c2016-07-26 09:02:02 -07004434 temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86PointerSize).Int32Value()));
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004435}
4436
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004437void CodeGeneratorX86::RecordSimplePatch() {
4438 if (GetCompilerOptions().GetIncludePatchInformation()) {
4439 simple_patches_.emplace_back();
4440 __ Bind(&simple_patches_.back());
4441 }
4442}
4443
Vladimir Markoaad75c62016-10-03 08:46:48 +00004444void CodeGeneratorX86::RecordBootStringPatch(HLoadString* load_string) {
4445 DCHECK(GetCompilerOptions().IsBootImage());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004446 string_patches_.emplace_back(load_string->GetDexFile(), load_string->GetStringIndex());
4447 __ Bind(&string_patches_.back().label);
4448}
4449
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004450void CodeGeneratorX86::RecordTypePatch(HLoadClass* load_class) {
4451 type_patches_.emplace_back(load_class->GetDexFile(), load_class->GetTypeIndex());
4452 __ Bind(&type_patches_.back().label);
4453}
4454
Vladimir Markoaad75c62016-10-03 08:46:48 +00004455Label* CodeGeneratorX86::NewStringBssEntryPatch(HLoadString* load_string) {
4456 DCHECK(!GetCompilerOptions().IsBootImage());
4457 string_patches_.emplace_back(load_string->GetDexFile(), load_string->GetStringIndex());
4458 return &string_patches_.back().label;
4459}
4460
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004461Label* CodeGeneratorX86::NewPcRelativeDexCacheArrayPatch(const DexFile& dex_file,
4462 uint32_t element_offset) {
4463 // Add the patch entry and bind its label at the end of the instruction.
4464 pc_relative_dex_cache_patches_.emplace_back(dex_file, element_offset);
4465 return &pc_relative_dex_cache_patches_.back().label;
4466}
4467
Vladimir Markoaad75c62016-10-03 08:46:48 +00004468// The label points to the end of the "movl" or another instruction but the literal offset
4469// for method patch needs to point to the embedded constant which occupies the last 4 bytes.
4470constexpr uint32_t kLabelPositionToLiteralOffsetAdjustment = 4u;
4471
4472template <LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
4473inline void CodeGeneratorX86::EmitPcRelativeLinkerPatches(
4474 const ArenaDeque<PatchInfo<Label>>& infos,
4475 ArenaVector<LinkerPatch>* linker_patches) {
4476 for (const PatchInfo<Label>& info : infos) {
4477 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
4478 linker_patches->push_back(
4479 Factory(literal_offset, &info.dex_file, GetMethodAddressOffset(), info.index));
4480 }
4481}
4482
Vladimir Marko58155012015-08-19 12:49:41 +00004483void CodeGeneratorX86::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
4484 DCHECK(linker_patches->empty());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004485 size_t size =
4486 method_patches_.size() +
4487 relative_call_patches_.size() +
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004488 pc_relative_dex_cache_patches_.size() +
4489 simple_patches_.size() +
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004490 string_patches_.size() +
4491 type_patches_.size();
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004492 linker_patches->reserve(size);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004493 for (const PatchInfo<Label>& info : method_patches_) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004494 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Markoaad75c62016-10-03 08:46:48 +00004495 linker_patches->push_back(LinkerPatch::MethodPatch(literal_offset, &info.dex_file, info.index));
Vladimir Marko58155012015-08-19 12:49:41 +00004496 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00004497 for (const PatchInfo<Label>& info : relative_call_patches_) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004498 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Markoaad75c62016-10-03 08:46:48 +00004499 linker_patches->push_back(
4500 LinkerPatch::RelativeCodePatch(literal_offset, &info.dex_file, info.index));
Vladimir Marko58155012015-08-19 12:49:41 +00004501 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00004502 EmitPcRelativeLinkerPatches<LinkerPatch::DexCacheArrayPatch>(pc_relative_dex_cache_patches_,
4503 linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004504 for (const Label& label : simple_patches_) {
4505 uint32_t literal_offset = label.Position() - kLabelPositionToLiteralOffsetAdjustment;
4506 linker_patches->push_back(LinkerPatch::RecordPosition(literal_offset));
4507 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00004508 if (!GetCompilerOptions().IsBootImage()) {
4509 EmitPcRelativeLinkerPatches<LinkerPatch::StringBssEntryPatch>(string_patches_, linker_patches);
4510 } else if (GetCompilerOptions().GetCompilePic()) {
4511 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeStringPatch>(string_patches_, linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004512 } else {
Vladimir Markoaad75c62016-10-03 08:46:48 +00004513 for (const PatchInfo<Label>& info : string_patches_) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004514 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Markoaad75c62016-10-03 08:46:48 +00004515 linker_patches->push_back(
4516 LinkerPatch::StringPatch(literal_offset, &info.dex_file, info.index));
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004517 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00004518 }
4519 if (GetCompilerOptions().GetCompilePic()) {
4520 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeTypePatch>(type_patches_, linker_patches);
4521 } else {
4522 for (const PatchInfo<Label>& info : type_patches_) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004523 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Markoaad75c62016-10-03 08:46:48 +00004524 linker_patches->push_back(LinkerPatch::TypePatch(literal_offset, &info.dex_file, info.index));
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004525 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004526 }
Vladimir Marko58155012015-08-19 12:49:41 +00004527}
4528
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004529void CodeGeneratorX86::MarkGCCard(Register temp,
4530 Register card,
4531 Register object,
4532 Register value,
4533 bool value_can_be_null) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004534 NearLabel is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004535 if (value_can_be_null) {
4536 __ testl(value, value);
4537 __ j(kEqual, &is_null);
4538 }
Andreas Gampe542451c2016-07-26 09:02:02 -07004539 __ fs()->movl(card, Address::Absolute(Thread::CardTableOffset<kX86PointerSize>().Int32Value()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004540 __ movl(temp, object);
4541 __ shrl(temp, Immediate(gc::accounting::CardTable::kCardShift));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00004542 __ movb(Address(temp, card, TIMES_1, 0),
4543 X86ManagedRegister::FromCpuRegister(card).AsByteRegister());
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004544 if (value_can_be_null) {
4545 __ Bind(&is_null);
4546 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004547}
4548
Calin Juravle52c48962014-12-16 17:02:57 +00004549void LocationsBuilderX86::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
4550 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain0d5a2812015-11-13 10:07:31 +00004551
4552 bool object_field_get_with_read_barrier =
4553 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004554 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00004555 new (GetGraph()->GetArena()) LocationSummary(instruction,
4556 kEmitCompilerReadBarrier ?
4557 LocationSummary::kCallOnSlowPath :
4558 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01004559 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004560 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01004561 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004562 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004563
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004564 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4565 locations->SetOut(Location::RequiresFpuRegister());
4566 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004567 // The output overlaps in case of long: we don't want the low move
4568 // to overwrite the object's location. Likewise, in the case of
4569 // an object field get with read barriers enabled, we do not want
4570 // the move to overwrite the object's location, as we need it to emit
4571 // the read barrier.
4572 locations->SetOut(
4573 Location::RequiresRegister(),
4574 (object_field_get_with_read_barrier || instruction->GetType() == Primitive::kPrimLong) ?
4575 Location::kOutputOverlap :
4576 Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004577 }
Calin Juravle52c48962014-12-16 17:02:57 +00004578
4579 if (field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong)) {
4580 // Long values can be loaded atomically into an XMM using movsd.
Roland Levillain7c1559a2015-12-15 10:55:36 +00004581 // So we use an XMM register as a temp to achieve atomicity (first
4582 // load the temp into the XMM and then copy the XMM into the
4583 // output, 32 bits at a time).
Calin Juravle52c48962014-12-16 17:02:57 +00004584 locations->AddTemp(Location::RequiresFpuRegister());
4585 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004586}
4587
Calin Juravle52c48962014-12-16 17:02:57 +00004588void InstructionCodeGeneratorX86::HandleFieldGet(HInstruction* instruction,
4589 const FieldInfo& field_info) {
4590 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004591
Calin Juravle52c48962014-12-16 17:02:57 +00004592 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004593 Location base_loc = locations->InAt(0);
4594 Register base = base_loc.AsRegister<Register>();
Calin Juravle52c48962014-12-16 17:02:57 +00004595 Location out = locations->Out();
4596 bool is_volatile = field_info.IsVolatile();
4597 Primitive::Type field_type = field_info.GetFieldType();
4598 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4599
4600 switch (field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004601 case Primitive::kPrimBoolean: {
Calin Juravle52c48962014-12-16 17:02:57 +00004602 __ movzxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004603 break;
4604 }
4605
4606 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00004607 __ movsxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004608 break;
4609 }
4610
4611 case Primitive::kPrimShort: {
Calin Juravle52c48962014-12-16 17:02:57 +00004612 __ movsxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004613 break;
4614 }
4615
4616 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00004617 __ movzxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004618 break;
4619 }
4620
4621 case Primitive::kPrimInt:
Calin Juravle52c48962014-12-16 17:02:57 +00004622 __ movl(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004623 break;
Roland Levillain7c1559a2015-12-15 10:55:36 +00004624
4625 case Primitive::kPrimNot: {
4626 // /* HeapReference<Object> */ out = *(base + offset)
4627 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004628 // Note that a potential implicit null check is handled in this
4629 // CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier call.
4630 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00004631 instruction, out, base, offset, /* needs_null_check */ true);
Roland Levillain7c1559a2015-12-15 10:55:36 +00004632 if (is_volatile) {
4633 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4634 }
4635 } else {
4636 __ movl(out.AsRegister<Register>(), Address(base, offset));
4637 codegen_->MaybeRecordImplicitNullCheck(instruction);
4638 if (is_volatile) {
4639 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4640 }
4641 // If read barriers are enabled, emit read barriers other than
4642 // Baker's using a slow path (and also unpoison the loaded
4643 // reference, if heap poisoning is enabled).
4644 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
4645 }
4646 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004647 }
4648
4649 case Primitive::kPrimLong: {
Calin Juravle52c48962014-12-16 17:02:57 +00004650 if (is_volatile) {
4651 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4652 __ movsd(temp, Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004653 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004654 __ movd(out.AsRegisterPairLow<Register>(), temp);
4655 __ psrlq(temp, Immediate(32));
4656 __ movd(out.AsRegisterPairHigh<Register>(), temp);
4657 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004658 DCHECK_NE(base, out.AsRegisterPairLow<Register>());
Calin Juravle52c48962014-12-16 17:02:57 +00004659 __ movl(out.AsRegisterPairLow<Register>(), Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004660 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004661 __ movl(out.AsRegisterPairHigh<Register>(), Address(base, kX86WordSize + offset));
4662 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004663 break;
4664 }
4665
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004666 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00004667 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004668 break;
4669 }
4670
4671 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00004672 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004673 break;
4674 }
4675
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004676 case Primitive::kPrimVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00004677 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004678 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004679 }
Calin Juravle52c48962014-12-16 17:02:57 +00004680
Roland Levillain7c1559a2015-12-15 10:55:36 +00004681 if (field_type == Primitive::kPrimNot || field_type == Primitive::kPrimLong) {
4682 // Potential implicit null checks, in the case of reference or
4683 // long fields, are handled in the previous switch statement.
4684 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00004685 codegen_->MaybeRecordImplicitNullCheck(instruction);
4686 }
4687
Calin Juravle52c48962014-12-16 17:02:57 +00004688 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004689 if (field_type == Primitive::kPrimNot) {
4690 // Memory barriers, in the case of references, are also handled
4691 // in the previous switch statement.
4692 } else {
4693 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4694 }
Roland Levillain4d027112015-07-01 15:41:14 +01004695 }
Calin Juravle52c48962014-12-16 17:02:57 +00004696}
4697
4698void LocationsBuilderX86::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
4699 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4700
4701 LocationSummary* locations =
4702 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4703 locations->SetInAt(0, Location::RequiresRegister());
4704 bool is_volatile = field_info.IsVolatile();
4705 Primitive::Type field_type = field_info.GetFieldType();
4706 bool is_byte_type = (field_type == Primitive::kPrimBoolean)
4707 || (field_type == Primitive::kPrimByte);
4708
4709 // The register allocator does not support multiple
4710 // inputs that die at entry with one in a specific register.
4711 if (is_byte_type) {
4712 // Ensure the value is in a byte register.
4713 locations->SetInAt(1, Location::RegisterLocation(EAX));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004714 } else if (Primitive::IsFloatingPointType(field_type)) {
Mark Mendell81489372015-11-04 11:30:41 -05004715 if (is_volatile && field_type == Primitive::kPrimDouble) {
4716 // In order to satisfy the semantics of volatile, this must be a single instruction store.
4717 locations->SetInAt(1, Location::RequiresFpuRegister());
4718 } else {
4719 locations->SetInAt(1, Location::FpuRegisterOrConstant(instruction->InputAt(1)));
4720 }
4721 } else if (is_volatile && field_type == Primitive::kPrimLong) {
4722 // In order to satisfy the semantics of volatile, this must be a single instruction store.
Calin Juravle52c48962014-12-16 17:02:57 +00004723 locations->SetInAt(1, Location::RequiresRegister());
Mark Mendell81489372015-11-04 11:30:41 -05004724
Calin Juravle52c48962014-12-16 17:02:57 +00004725 // 64bits value can be atomically written to an address with movsd and an XMM register.
4726 // We need two XMM registers because there's no easier way to (bit) copy a register pair
4727 // into a single XMM register (we copy each pair part into the XMMs and then interleave them).
4728 // NB: We could make the register allocator understand fp_reg <-> core_reg moves but given the
4729 // isolated cases when we need this it isn't worth adding the extra complexity.
4730 locations->AddTemp(Location::RequiresFpuRegister());
4731 locations->AddTemp(Location::RequiresFpuRegister());
Mark Mendell81489372015-11-04 11:30:41 -05004732 } else {
4733 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4734
4735 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
4736 // Temporary registers for the write barrier.
4737 locations->AddTemp(Location::RequiresRegister()); // May be used for reference poisoning too.
4738 // Ensure the card is in a byte register.
4739 locations->AddTemp(Location::RegisterLocation(ECX));
4740 }
Calin Juravle52c48962014-12-16 17:02:57 +00004741 }
4742}
4743
4744void InstructionCodeGeneratorX86::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004745 const FieldInfo& field_info,
4746 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00004747 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4748
4749 LocationSummary* locations = instruction->GetLocations();
4750 Register base = locations->InAt(0).AsRegister<Register>();
4751 Location value = locations->InAt(1);
4752 bool is_volatile = field_info.IsVolatile();
4753 Primitive::Type field_type = field_info.GetFieldType();
4754 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01004755 bool needs_write_barrier =
4756 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00004757
4758 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004759 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
Calin Juravle52c48962014-12-16 17:02:57 +00004760 }
4761
Mark Mendell81489372015-11-04 11:30:41 -05004762 bool maybe_record_implicit_null_check_done = false;
4763
Calin Juravle52c48962014-12-16 17:02:57 +00004764 switch (field_type) {
4765 case Primitive::kPrimBoolean:
4766 case Primitive::kPrimByte: {
4767 __ movb(Address(base, offset), value.AsRegister<ByteRegister>());
4768 break;
4769 }
4770
4771 case Primitive::kPrimShort:
4772 case Primitive::kPrimChar: {
Mark Mendell81489372015-11-04 11:30:41 -05004773 if (value.IsConstant()) {
4774 int16_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4775 __ movw(Address(base, offset), Immediate(v));
4776 } else {
4777 __ movw(Address(base, offset), value.AsRegister<Register>());
4778 }
Calin Juravle52c48962014-12-16 17:02:57 +00004779 break;
4780 }
4781
4782 case Primitive::kPrimInt:
4783 case Primitive::kPrimNot: {
Roland Levillain4d027112015-07-01 15:41:14 +01004784 if (kPoisonHeapReferences && needs_write_barrier) {
4785 // Note that in the case where `value` is a null reference,
4786 // we do not enter this block, as the reference does not
4787 // need poisoning.
4788 DCHECK_EQ(field_type, Primitive::kPrimNot);
4789 Register temp = locations->GetTemp(0).AsRegister<Register>();
4790 __ movl(temp, value.AsRegister<Register>());
4791 __ PoisonHeapReference(temp);
4792 __ movl(Address(base, offset), temp);
Mark Mendell81489372015-11-04 11:30:41 -05004793 } else if (value.IsConstant()) {
4794 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4795 __ movl(Address(base, offset), Immediate(v));
Roland Levillain4d027112015-07-01 15:41:14 +01004796 } else {
Nicolas Geoffray03971632016-03-17 10:44:24 +00004797 DCHECK(value.IsRegister()) << value;
Roland Levillain4d027112015-07-01 15:41:14 +01004798 __ movl(Address(base, offset), value.AsRegister<Register>());
4799 }
Calin Juravle52c48962014-12-16 17:02:57 +00004800 break;
4801 }
4802
4803 case Primitive::kPrimLong: {
4804 if (is_volatile) {
4805 XmmRegister temp1 = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4806 XmmRegister temp2 = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
4807 __ movd(temp1, value.AsRegisterPairLow<Register>());
4808 __ movd(temp2, value.AsRegisterPairHigh<Register>());
4809 __ punpckldq(temp1, temp2);
4810 __ movsd(Address(base, offset), temp1);
Calin Juravle77520bc2015-01-12 18:45:46 +00004811 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell81489372015-11-04 11:30:41 -05004812 } else if (value.IsConstant()) {
4813 int64_t v = CodeGenerator::GetInt64ValueOf(value.GetConstant());
4814 __ movl(Address(base, offset), Immediate(Low32Bits(v)));
4815 codegen_->MaybeRecordImplicitNullCheck(instruction);
4816 __ movl(Address(base, kX86WordSize + offset), Immediate(High32Bits(v)));
Calin Juravle52c48962014-12-16 17:02:57 +00004817 } else {
4818 __ movl(Address(base, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00004819 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004820 __ movl(Address(base, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
4821 }
Mark Mendell81489372015-11-04 11:30:41 -05004822 maybe_record_implicit_null_check_done = true;
Calin Juravle52c48962014-12-16 17:02:57 +00004823 break;
4824 }
4825
4826 case Primitive::kPrimFloat: {
Mark Mendell81489372015-11-04 11:30:41 -05004827 if (value.IsConstant()) {
4828 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4829 __ movl(Address(base, offset), Immediate(v));
4830 } else {
4831 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4832 }
Calin Juravle52c48962014-12-16 17:02:57 +00004833 break;
4834 }
4835
4836 case Primitive::kPrimDouble: {
Mark Mendell81489372015-11-04 11:30:41 -05004837 if (value.IsConstant()) {
4838 int64_t v = CodeGenerator::GetInt64ValueOf(value.GetConstant());
4839 __ movl(Address(base, offset), Immediate(Low32Bits(v)));
4840 codegen_->MaybeRecordImplicitNullCheck(instruction);
4841 __ movl(Address(base, kX86WordSize + offset), Immediate(High32Bits(v)));
4842 maybe_record_implicit_null_check_done = true;
4843 } else {
4844 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4845 }
Calin Juravle52c48962014-12-16 17:02:57 +00004846 break;
4847 }
4848
4849 case Primitive::kPrimVoid:
4850 LOG(FATAL) << "Unreachable type " << field_type;
4851 UNREACHABLE();
4852 }
4853
Mark Mendell81489372015-11-04 11:30:41 -05004854 if (!maybe_record_implicit_null_check_done) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004855 codegen_->MaybeRecordImplicitNullCheck(instruction);
4856 }
4857
Roland Levillain4d027112015-07-01 15:41:14 +01004858 if (needs_write_barrier) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004859 Register temp = locations->GetTemp(0).AsRegister<Register>();
4860 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004861 codegen_->MarkGCCard(temp, card, base, value.AsRegister<Register>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00004862 }
4863
Calin Juravle52c48962014-12-16 17:02:57 +00004864 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004865 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
Calin Juravle52c48962014-12-16 17:02:57 +00004866 }
4867}
4868
4869void LocationsBuilderX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4870 HandleFieldGet(instruction, instruction->GetFieldInfo());
4871}
4872
4873void InstructionCodeGeneratorX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4874 HandleFieldGet(instruction, instruction->GetFieldInfo());
4875}
4876
4877void LocationsBuilderX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
4878 HandleFieldSet(instruction, instruction->GetFieldInfo());
4879}
4880
4881void InstructionCodeGeneratorX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004882 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00004883}
4884
4885void LocationsBuilderX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
4886 HandleFieldSet(instruction, instruction->GetFieldInfo());
4887}
4888
4889void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004890 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00004891}
4892
4893void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4894 HandleFieldGet(instruction, instruction->GetFieldInfo());
4895}
4896
4897void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4898 HandleFieldGet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004899}
4900
Calin Juravlee460d1d2015-09-29 04:52:17 +01004901void LocationsBuilderX86::VisitUnresolvedInstanceFieldGet(
4902 HUnresolvedInstanceFieldGet* instruction) {
4903 FieldAccessCallingConventionX86 calling_convention;
4904 codegen_->CreateUnresolvedFieldLocationSummary(
4905 instruction, instruction->GetFieldType(), calling_convention);
4906}
4907
4908void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldGet(
4909 HUnresolvedInstanceFieldGet* instruction) {
4910 FieldAccessCallingConventionX86 calling_convention;
4911 codegen_->GenerateUnresolvedFieldAccess(instruction,
4912 instruction->GetFieldType(),
4913 instruction->GetFieldIndex(),
4914 instruction->GetDexPc(),
4915 calling_convention);
4916}
4917
4918void LocationsBuilderX86::VisitUnresolvedInstanceFieldSet(
4919 HUnresolvedInstanceFieldSet* instruction) {
4920 FieldAccessCallingConventionX86 calling_convention;
4921 codegen_->CreateUnresolvedFieldLocationSummary(
4922 instruction, instruction->GetFieldType(), calling_convention);
4923}
4924
4925void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldSet(
4926 HUnresolvedInstanceFieldSet* instruction) {
4927 FieldAccessCallingConventionX86 calling_convention;
4928 codegen_->GenerateUnresolvedFieldAccess(instruction,
4929 instruction->GetFieldType(),
4930 instruction->GetFieldIndex(),
4931 instruction->GetDexPc(),
4932 calling_convention);
4933}
4934
4935void LocationsBuilderX86::VisitUnresolvedStaticFieldGet(
4936 HUnresolvedStaticFieldGet* instruction) {
4937 FieldAccessCallingConventionX86 calling_convention;
4938 codegen_->CreateUnresolvedFieldLocationSummary(
4939 instruction, instruction->GetFieldType(), calling_convention);
4940}
4941
4942void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldGet(
4943 HUnresolvedStaticFieldGet* instruction) {
4944 FieldAccessCallingConventionX86 calling_convention;
4945 codegen_->GenerateUnresolvedFieldAccess(instruction,
4946 instruction->GetFieldType(),
4947 instruction->GetFieldIndex(),
4948 instruction->GetDexPc(),
4949 calling_convention);
4950}
4951
4952void LocationsBuilderX86::VisitUnresolvedStaticFieldSet(
4953 HUnresolvedStaticFieldSet* instruction) {
4954 FieldAccessCallingConventionX86 calling_convention;
4955 codegen_->CreateUnresolvedFieldLocationSummary(
4956 instruction, instruction->GetFieldType(), calling_convention);
4957}
4958
4959void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldSet(
4960 HUnresolvedStaticFieldSet* instruction) {
4961 FieldAccessCallingConventionX86 calling_convention;
4962 codegen_->GenerateUnresolvedFieldAccess(instruction,
4963 instruction->GetFieldType(),
4964 instruction->GetFieldIndex(),
4965 instruction->GetDexPc(),
4966 calling_convention);
4967}
4968
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004969void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004970 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
4971 Location loc = codegen_->GetCompilerOptions().GetImplicitNullChecks()
4972 ? Location::RequiresRegister()
4973 : Location::Any();
4974 locations->SetInAt(0, loc);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004975}
4976
Calin Juravle2ae48182016-03-16 14:05:09 +00004977void CodeGeneratorX86::GenerateImplicitNullCheck(HNullCheck* instruction) {
4978 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004979 return;
4980 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004981 LocationSummary* locations = instruction->GetLocations();
4982 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00004983
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004984 __ testl(EAX, Address(obj.AsRegister<Register>(), 0));
Calin Juravle2ae48182016-03-16 14:05:09 +00004985 RecordPcInfo(instruction, instruction->GetDexPc());
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004986}
4987
Calin Juravle2ae48182016-03-16 14:05:09 +00004988void CodeGeneratorX86::GenerateExplicitNullCheck(HNullCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07004989 SlowPathCode* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00004990 AddSlowPath(slow_path);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004991
4992 LocationSummary* locations = instruction->GetLocations();
4993 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004994
4995 if (obj.IsRegister()) {
Mark Mendell42514f62015-03-31 11:34:22 -04004996 __ testl(obj.AsRegister<Register>(), obj.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004997 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004998 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004999 } else {
5000 DCHECK(obj.IsConstant()) << obj;
David Brazdil77a48ae2015-09-15 12:34:04 +00005001 DCHECK(obj.GetConstant()->IsNullConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005002 __ jmp(slow_path->GetEntryLabel());
5003 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005004 }
5005 __ j(kEqual, slow_path->GetEntryLabel());
5006}
5007
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005008void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00005009 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005010}
5011
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005012void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005013 bool object_array_get_with_read_barrier =
5014 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005015 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00005016 new (GetGraph()->GetArena()) LocationSummary(instruction,
5017 object_array_get_with_read_barrier ?
5018 LocationSummary::kCallOnSlowPath :
5019 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01005020 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005021 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01005022 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005023 locations->SetInAt(0, Location::RequiresRegister());
5024 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005025 if (Primitive::IsFloatingPointType(instruction->GetType())) {
5026 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5027 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005028 // The output overlaps in case of long: we don't want the low move
5029 // to overwrite the array's location. Likewise, in the case of an
5030 // object array get with read barriers enabled, we do not want the
5031 // move to overwrite the array's location, as we need it to emit
5032 // the read barrier.
5033 locations->SetOut(
5034 Location::RequiresRegister(),
5035 (instruction->GetType() == Primitive::kPrimLong || object_array_get_with_read_barrier) ?
5036 Location::kOutputOverlap :
5037 Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005038 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005039}
5040
5041void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
5042 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005043 Location obj_loc = locations->InAt(0);
5044 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005045 Location index = locations->InAt(1);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005046 Location out_loc = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005047 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005048
Calin Juravle77520bc2015-01-12 18:45:46 +00005049 Primitive::Type type = instruction->GetType();
5050 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005051 case Primitive::kPrimBoolean: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005052 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005053 __ movzxb(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005054 break;
5055 }
5056
5057 case Primitive::kPrimByte: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005058 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005059 __ movsxb(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005060 break;
5061 }
5062
5063 case Primitive::kPrimShort: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005064 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005065 __ movsxw(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005066 break;
5067 }
5068
5069 case Primitive::kPrimChar: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005070 Register out = out_loc.AsRegister<Register>();
jessicahandojo4877b792016-09-08 19:49:13 -07005071 if (mirror::kUseStringCompression && instruction->IsStringCharAt()) {
5072 // Branch cases into compressed and uncompressed for each index's type.
5073 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
5074 NearLabel done, not_compressed;
5075 __ cmpl(Address(obj, count_offset), Immediate(0));
5076 codegen_->MaybeRecordImplicitNullCheck(instruction);
5077 __ j(kGreaterEqual, &not_compressed);
5078 __ movzxb(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_1, data_offset));
5079 __ jmp(&done);
5080 __ Bind(&not_compressed);
5081 __ movzxw(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_2, data_offset));
5082 __ Bind(&done);
5083 } else {
5084 // Common case for charAt of array of char or when string compression's
5085 // feature is turned off.
5086 __ movzxw(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_2, data_offset));
5087 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005088 break;
5089 }
5090
Roland Levillain7c1559a2015-12-15 10:55:36 +00005091 case Primitive::kPrimInt: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005092 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005093 __ movl(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005094 break;
5095 }
5096
Roland Levillain7c1559a2015-12-15 10:55:36 +00005097 case Primitive::kPrimNot: {
5098 static_assert(
5099 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
5100 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain7c1559a2015-12-15 10:55:36 +00005101 // /* HeapReference<Object> */ out =
5102 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
5103 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005104 // Note that a potential implicit null check is handled in this
5105 // CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier call.
5106 codegen_->GenerateArrayLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00005107 instruction, out_loc, obj, data_offset, index, /* needs_null_check */ true);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005108 } else {
5109 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005110 __ movl(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset));
5111 codegen_->MaybeRecordImplicitNullCheck(instruction);
5112 // If read barriers are enabled, emit read barriers other than
5113 // Baker's using a slow path (and also unpoison the loaded
5114 // reference, if heap poisoning is enabled).
Roland Levillain7c1559a2015-12-15 10:55:36 +00005115 if (index.IsConstant()) {
5116 uint32_t offset =
5117 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Roland Levillain7c1559a2015-12-15 10:55:36 +00005118 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
5119 } else {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005120 codegen_->MaybeGenerateReadBarrierSlow(
5121 instruction, out_loc, out_loc, obj_loc, data_offset, index);
5122 }
5123 }
5124 break;
5125 }
5126
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005127 case Primitive::kPrimLong: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005128 DCHECK_NE(obj, out_loc.AsRegisterPairLow<Register>());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005129 __ movl(out_loc.AsRegisterPairLow<Register>(),
5130 CodeGeneratorX86::ArrayAddress(obj, index, TIMES_8, data_offset));
5131 codegen_->MaybeRecordImplicitNullCheck(instruction);
5132 __ movl(out_loc.AsRegisterPairHigh<Register>(),
5133 CodeGeneratorX86::ArrayAddress(obj, index, TIMES_8, data_offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005134 break;
5135 }
5136
Mark Mendell7c8d0092015-01-26 11:21:33 -05005137 case Primitive::kPrimFloat: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005138 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005139 __ movss(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005140 break;
5141 }
5142
5143 case Primitive::kPrimDouble: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005144 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005145 __ movsd(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_8, data_offset));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005146 break;
5147 }
5148
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005149 case Primitive::kPrimVoid:
Calin Juravle77520bc2015-01-12 18:45:46 +00005150 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07005151 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005152 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005153
Roland Levillain7c1559a2015-12-15 10:55:36 +00005154 if (type == Primitive::kPrimNot || type == Primitive::kPrimLong) {
5155 // Potential implicit null checks, in the case of reference or
5156 // long arrays, are handled in the previous switch statement.
5157 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00005158 codegen_->MaybeRecordImplicitNullCheck(instruction);
5159 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005160}
5161
5162void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01005163 Primitive::Type value_type = instruction->GetComponentType();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005164
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005165 bool needs_write_barrier =
5166 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Roland Levillain0d5a2812015-11-13 10:07:31 +00005167 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005168
Nicolas Geoffray39468442014-09-02 15:17:15 +01005169 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
5170 instruction,
Vladimir Marko8d49fd72016-08-25 15:20:47 +01005171 may_need_runtime_call_for_type_check ?
Roland Levillain0d5a2812015-11-13 10:07:31 +00005172 LocationSummary::kCallOnSlowPath :
5173 LocationSummary::kNoCall);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005174
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005175 bool is_byte_type = (value_type == Primitive::kPrimBoolean)
5176 || (value_type == Primitive::kPrimByte);
5177 // We need the inputs to be different than the output in case of long operation.
5178 // In case of a byte operation, the register allocator does not support multiple
5179 // inputs that die at entry with one in a specific register.
5180 locations->SetInAt(0, Location::RequiresRegister());
5181 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
5182 if (is_byte_type) {
5183 // Ensure the value is in a byte register.
5184 locations->SetInAt(2, Location::ByteRegisterOrConstant(EAX, instruction->InputAt(2)));
5185 } else if (Primitive::IsFloatingPointType(value_type)) {
Mark Mendell81489372015-11-04 11:30:41 -05005186 locations->SetInAt(2, Location::FpuRegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005187 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005188 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
5189 }
5190 if (needs_write_barrier) {
5191 // Temporary registers for the write barrier.
5192 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
5193 // Ensure the card is in a byte register.
Roland Levillain4f6b0b52015-11-23 19:29:22 +00005194 locations->AddTemp(Location::RegisterLocation(ECX));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005195 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005196}
5197
5198void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
5199 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005200 Location array_loc = locations->InAt(0);
5201 Register array = array_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005202 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005203 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005204 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005205 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
5206 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5207 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005208 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005209 bool needs_write_barrier =
5210 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005211
5212 switch (value_type) {
5213 case Primitive::kPrimBoolean:
5214 case Primitive::kPrimByte: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005215 uint32_t offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005216 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_1, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005217 if (value.IsRegister()) {
5218 __ movb(address, value.AsRegister<ByteRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005219 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005220 __ movb(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005221 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005222 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005223 break;
5224 }
5225
5226 case Primitive::kPrimShort:
5227 case Primitive::kPrimChar: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005228 uint32_t offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005229 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_2, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005230 if (value.IsRegister()) {
5231 __ movw(address, value.AsRegister<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005232 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005233 __ movw(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005234 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005235 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005236 break;
5237 }
5238
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005239 case Primitive::kPrimNot: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005240 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005241 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_4, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005242
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005243 if (!value.IsRegister()) {
5244 // Just setting null.
5245 DCHECK(instruction->InputAt(2)->IsNullConstant());
5246 DCHECK(value.IsConstant()) << value;
5247 __ movl(address, Immediate(0));
Calin Juravle77520bc2015-01-12 18:45:46 +00005248 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005249 DCHECK(!needs_write_barrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005250 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005251 break;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005252 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005253
5254 DCHECK(needs_write_barrier);
5255 Register register_value = value.AsRegister<Register>();
Roland Levillain16d9f942016-08-25 17:27:56 +01005256 // We cannot use a NearLabel for `done`, as its range may be too
5257 // short when Baker read barriers are enabled.
5258 Label done;
5259 NearLabel not_null, do_put;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005260 SlowPathCode* slow_path = nullptr;
Roland Levillain16d9f942016-08-25 17:27:56 +01005261 Location temp_loc = locations->GetTemp(0);
5262 Register temp = temp_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005263 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005264 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathX86(instruction);
5265 codegen_->AddSlowPath(slow_path);
5266 if (instruction->GetValueCanBeNull()) {
5267 __ testl(register_value, register_value);
5268 __ j(kNotEqual, &not_null);
5269 __ movl(address, Immediate(0));
5270 codegen_->MaybeRecordImplicitNullCheck(instruction);
5271 __ jmp(&done);
5272 __ Bind(&not_null);
5273 }
5274
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005275 // Note that when Baker read barriers are enabled, the type
5276 // checks are performed without read barriers. This is fine,
5277 // even in the case where a class object is in the from-space
5278 // after the flip, as a comparison involving such a type would
5279 // not produce a false positive; it may of course produce a
5280 // false negative, in which case we would take the ArraySet
5281 // slow path.
Roland Levillain16d9f942016-08-25 17:27:56 +01005282
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005283 // /* HeapReference<Class> */ temp = array->klass_
5284 __ movl(temp, Address(array, class_offset));
5285 codegen_->MaybeRecordImplicitNullCheck(instruction);
5286 __ MaybeUnpoisonHeapReference(temp);
Roland Levillain16d9f942016-08-25 17:27:56 +01005287
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005288 // /* HeapReference<Class> */ temp = temp->component_type_
5289 __ movl(temp, Address(temp, component_offset));
5290 // If heap poisoning is enabled, no need to unpoison `temp`
5291 // nor the object reference in `register_value->klass`, as
5292 // we are comparing two poisoned references.
5293 __ cmpl(temp, Address(register_value, class_offset));
Roland Levillain16d9f942016-08-25 17:27:56 +01005294
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005295 if (instruction->StaticTypeOfArrayIsObjectArray()) {
5296 __ j(kEqual, &do_put);
5297 // If heap poisoning is enabled, the `temp` reference has
5298 // not been unpoisoned yet; unpoison it now.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005299 __ MaybeUnpoisonHeapReference(temp);
5300
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005301 // If heap poisoning is enabled, no need to unpoison the
5302 // heap reference loaded below, as it is only used for a
5303 // comparison with null.
5304 __ cmpl(Address(temp, super_offset), Immediate(0));
5305 __ j(kNotEqual, slow_path->GetEntryLabel());
5306 __ Bind(&do_put);
5307 } else {
5308 __ j(kNotEqual, slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005309 }
5310 }
5311
5312 if (kPoisonHeapReferences) {
5313 __ movl(temp, register_value);
5314 __ PoisonHeapReference(temp);
5315 __ movl(address, temp);
5316 } else {
5317 __ movl(address, register_value);
5318 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005319 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005320 codegen_->MaybeRecordImplicitNullCheck(instruction);
5321 }
5322
5323 Register card = locations->GetTemp(1).AsRegister<Register>();
5324 codegen_->MarkGCCard(
5325 temp, card, array, value.AsRegister<Register>(), instruction->GetValueCanBeNull());
5326 __ Bind(&done);
5327
5328 if (slow_path != nullptr) {
5329 __ Bind(slow_path->GetExitLabel());
5330 }
5331
5332 break;
5333 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005334
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005335 case Primitive::kPrimInt: {
5336 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005337 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_4, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005338 if (value.IsRegister()) {
5339 __ movl(address, value.AsRegister<Register>());
5340 } else {
5341 DCHECK(value.IsConstant()) << value;
5342 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
5343 __ movl(address, Immediate(v));
5344 }
5345 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005346 break;
5347 }
5348
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005349 case Primitive::kPrimLong: {
5350 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005351 if (value.IsRegisterPair()) {
5352 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset),
5353 value.AsRegisterPairLow<Register>());
5354 codegen_->MaybeRecordImplicitNullCheck(instruction);
5355 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset + kX86WordSize),
5356 value.AsRegisterPairHigh<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005357 } else {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005358 DCHECK(value.IsConstant());
5359 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
5360 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset),
5361 Immediate(Low32Bits(val)));
5362 codegen_->MaybeRecordImplicitNullCheck(instruction);
5363 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset + kX86WordSize),
5364 Immediate(High32Bits(val)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005365 }
5366 break;
5367 }
5368
Mark Mendell7c8d0092015-01-26 11:21:33 -05005369 case Primitive::kPrimFloat: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005370 uint32_t offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005371 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_4, offset);
Mark Mendell81489372015-11-04 11:30:41 -05005372 if (value.IsFpuRegister()) {
5373 __ movss(address, value.AsFpuRegister<XmmRegister>());
5374 } else {
5375 DCHECK(value.IsConstant());
5376 int32_t v = bit_cast<int32_t, float>(value.GetConstant()->AsFloatConstant()->GetValue());
5377 __ movl(address, Immediate(v));
5378 }
5379 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005380 break;
5381 }
5382
5383 case Primitive::kPrimDouble: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005384 uint32_t offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005385 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, offset);
Mark Mendell81489372015-11-04 11:30:41 -05005386 if (value.IsFpuRegister()) {
5387 __ movsd(address, value.AsFpuRegister<XmmRegister>());
5388 } else {
5389 DCHECK(value.IsConstant());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005390 Address address_hi =
5391 CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, offset + kX86WordSize);
Mark Mendell81489372015-11-04 11:30:41 -05005392 int64_t v = bit_cast<int64_t, double>(value.GetConstant()->AsDoubleConstant()->GetValue());
5393 __ movl(address, Immediate(Low32Bits(v)));
5394 codegen_->MaybeRecordImplicitNullCheck(instruction);
5395 __ movl(address_hi, Immediate(High32Bits(v)));
5396 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005397 break;
5398 }
5399
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005400 case Primitive::kPrimVoid:
5401 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07005402 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005403 }
5404}
5405
5406void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
5407 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005408 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellee8d9712016-07-12 11:13:15 -04005409 if (!instruction->IsEmittedAtUseSite()) {
5410 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5411 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005412}
5413
5414void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
Mark Mendellee8d9712016-07-12 11:13:15 -04005415 if (instruction->IsEmittedAtUseSite()) {
5416 return;
5417 }
5418
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005419 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01005420 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00005421 Register obj = locations->InAt(0).AsRegister<Register>();
5422 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005423 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005424 codegen_->MaybeRecordImplicitNullCheck(instruction);
jessicahandojo4877b792016-09-08 19:49:13 -07005425 // Mask out most significant bit in case the array is String's array of char.
5426 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
5427 __ andl(out, Immediate(INT32_MAX));
5428 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005429}
5430
5431void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005432 RegisterSet caller_saves = RegisterSet::Empty();
5433 InvokeRuntimeCallingConvention calling_convention;
5434 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5435 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
5436 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Mark Mendellf60c90b2015-03-04 15:12:59 -05005437 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendellee8d9712016-07-12 11:13:15 -04005438 HInstruction* length = instruction->InputAt(1);
5439 if (!length->IsEmittedAtUseSite()) {
5440 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
5441 }
jessicahandojo4877b792016-09-08 19:49:13 -07005442 // Need register to see array's length.
5443 if (mirror::kUseStringCompression && instruction->IsStringCharAt()) {
5444 locations->AddTemp(Location::RequiresRegister());
5445 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005446}
5447
5448void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
jessicahandojo4877b792016-09-08 19:49:13 -07005449 const bool is_string_compressed_char_at =
5450 mirror::kUseStringCompression && instruction->IsStringCharAt();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005451 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05005452 Location index_loc = locations->InAt(0);
5453 Location length_loc = locations->InAt(1);
Andreas Gampe85b62f22015-09-09 13:15:38 -07005454 SlowPathCode* slow_path =
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01005455 new (GetGraph()->GetArena()) BoundsCheckSlowPathX86(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005456
Mark Mendell99dbd682015-04-22 16:18:52 -04005457 if (length_loc.IsConstant()) {
5458 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
5459 if (index_loc.IsConstant()) {
5460 // BCE will remove the bounds check if we are guarenteed to pass.
5461 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5462 if (index < 0 || index >= length) {
5463 codegen_->AddSlowPath(slow_path);
5464 __ jmp(slow_path->GetEntryLabel());
5465 } else {
5466 // Some optimization after BCE may have generated this, and we should not
5467 // generate a bounds check if it is a valid range.
5468 }
5469 return;
5470 }
5471
5472 // We have to reverse the jump condition because the length is the constant.
5473 Register index_reg = index_loc.AsRegister<Register>();
5474 __ cmpl(index_reg, Immediate(length));
5475 codegen_->AddSlowPath(slow_path);
5476 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005477 } else {
Mark Mendellee8d9712016-07-12 11:13:15 -04005478 HInstruction* array_length = instruction->InputAt(1);
5479 if (array_length->IsEmittedAtUseSite()) {
5480 // Address the length field in the array.
5481 DCHECK(array_length->IsArrayLength());
5482 uint32_t len_offset = CodeGenerator::GetArrayLengthOffset(array_length->AsArrayLength());
5483 Location array_loc = array_length->GetLocations()->InAt(0);
5484 Address array_len(array_loc.AsRegister<Register>(), len_offset);
jessicahandojo4877b792016-09-08 19:49:13 -07005485 if (is_string_compressed_char_at) {
5486 Register length_reg = locations->GetTemp(0).AsRegister<Register>();
5487 __ movl(length_reg, array_len);
5488 codegen_->MaybeRecordImplicitNullCheck(array_length);
5489 __ andl(length_reg, Immediate(INT32_MAX));
5490 codegen_->GenerateIntCompare(length_reg, index_loc);
Mark Mendellee8d9712016-07-12 11:13:15 -04005491 } else {
jessicahandojo4877b792016-09-08 19:49:13 -07005492 // Checking bounds for general case:
5493 // Array of char or string's array with feature compression off.
5494 if (index_loc.IsConstant()) {
5495 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5496 __ cmpl(array_len, Immediate(value));
5497 } else {
5498 __ cmpl(array_len, index_loc.AsRegister<Register>());
5499 }
5500 codegen_->MaybeRecordImplicitNullCheck(array_length);
Mark Mendellee8d9712016-07-12 11:13:15 -04005501 }
Mark Mendell99dbd682015-04-22 16:18:52 -04005502 } else {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005503 codegen_->GenerateIntCompare(length_loc, index_loc);
Mark Mendell99dbd682015-04-22 16:18:52 -04005504 }
5505 codegen_->AddSlowPath(slow_path);
5506 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005507 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005508}
5509
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005510void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005511 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005512}
5513
5514void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005515 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5516}
5517
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005518void LocationsBuilderX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Marko70e97462016-08-09 11:04:26 +01005519 LocationSummary* locations =
5520 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01005521 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005522}
5523
5524void InstructionCodeGeneratorX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005525 HBasicBlock* block = instruction->GetBlock();
5526 if (block->GetLoopInformation() != nullptr) {
5527 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5528 // The back edge will generate the suspend check.
5529 return;
5530 }
5531 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5532 // The goto will generate the suspend check.
5533 return;
5534 }
5535 GenerateSuspendCheck(instruction, nullptr);
5536}
5537
5538void InstructionCodeGeneratorX86::GenerateSuspendCheck(HSuspendCheck* instruction,
5539 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005540 SuspendCheckSlowPathX86* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005541 down_cast<SuspendCheckSlowPathX86*>(instruction->GetSlowPath());
5542 if (slow_path == nullptr) {
5543 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathX86(instruction, successor);
5544 instruction->SetSlowPath(slow_path);
5545 codegen_->AddSlowPath(slow_path);
5546 if (successor != nullptr) {
5547 DCHECK(successor->IsLoopHeader());
5548 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
5549 }
5550 } else {
5551 DCHECK_EQ(slow_path->GetSuccessor(), successor);
5552 }
5553
Andreas Gampe542451c2016-07-26 09:02:02 -07005554 __ fs()->cmpw(Address::Absolute(Thread::ThreadFlagsOffset<kX86PointerSize>().Int32Value()),
Roland Levillain7c1559a2015-12-15 10:55:36 +00005555 Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005556 if (successor == nullptr) {
5557 __ j(kNotEqual, slow_path->GetEntryLabel());
5558 __ Bind(slow_path->GetReturnLabel());
5559 } else {
5560 __ j(kEqual, codegen_->GetLabelOf(successor));
5561 __ jmp(slow_path->GetEntryLabel());
5562 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005563}
5564
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005565X86Assembler* ParallelMoveResolverX86::GetAssembler() const {
5566 return codegen_->GetAssembler();
5567}
5568
Mark Mendell7c8d0092015-01-26 11:21:33 -05005569void ParallelMoveResolverX86::MoveMemoryToMemory32(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005570 ScratchRegisterScope ensure_scratch(
5571 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5572 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5573 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5574 __ movl(temp_reg, Address(ESP, src + stack_offset));
5575 __ movl(Address(ESP, dst + stack_offset), temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005576}
5577
5578void ParallelMoveResolverX86::MoveMemoryToMemory64(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005579 ScratchRegisterScope ensure_scratch(
5580 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5581 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5582 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5583 __ movl(temp_reg, Address(ESP, src + stack_offset));
5584 __ movl(Address(ESP, dst + stack_offset), temp_reg);
5585 __ movl(temp_reg, Address(ESP, src + stack_offset + kX86WordSize));
5586 __ movl(Address(ESP, dst + stack_offset + kX86WordSize), temp_reg);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005587}
5588
5589void ParallelMoveResolverX86::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005590 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005591 Location source = move->GetSource();
5592 Location destination = move->GetDestination();
5593
5594 if (source.IsRegister()) {
5595 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005596 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
David Brazdil74eb1b22015-12-14 11:44:01 +00005597 } else if (destination.IsFpuRegister()) {
5598 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005599 } else {
5600 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00005601 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005602 }
David Brazdil74eb1b22015-12-14 11:44:01 +00005603 } else if (source.IsRegisterPair()) {
5604 size_t elem_size = Primitive::ComponentSize(Primitive::kPrimInt);
5605 // Create stack space for 2 elements.
5606 __ subl(ESP, Immediate(2 * elem_size));
5607 __ movl(Address(ESP, 0), source.AsRegisterPairLow<Register>());
5608 __ movl(Address(ESP, elem_size), source.AsRegisterPairHigh<Register>());
5609 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
5610 // And remove the temporary stack space we allocated.
5611 __ addl(ESP, Immediate(2 * elem_size));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005612 } else if (source.IsFpuRegister()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00005613 if (destination.IsRegister()) {
5614 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
5615 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005616 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
David Brazdil74eb1b22015-12-14 11:44:01 +00005617 } else if (destination.IsRegisterPair()) {
5618 XmmRegister src_reg = source.AsFpuRegister<XmmRegister>();
5619 __ movd(destination.AsRegisterPairLow<Register>(), src_reg);
5620 __ psrlq(src_reg, Immediate(32));
5621 __ movd(destination.AsRegisterPairHigh<Register>(), src_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005622 } else if (destination.IsStackSlot()) {
5623 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
5624 } else {
5625 DCHECK(destination.IsDoubleStackSlot());
5626 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
5627 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005628 } else if (source.IsStackSlot()) {
5629 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005630 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005631 } else if (destination.IsFpuRegister()) {
5632 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005633 } else {
5634 DCHECK(destination.IsStackSlot());
Mark Mendell7c8d0092015-01-26 11:21:33 -05005635 MoveMemoryToMemory32(destination.GetStackIndex(), source.GetStackIndex());
5636 }
5637 } else if (source.IsDoubleStackSlot()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00005638 if (destination.IsRegisterPair()) {
5639 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
5640 __ movl(destination.AsRegisterPairHigh<Register>(),
5641 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
5642 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005643 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
5644 } else {
5645 DCHECK(destination.IsDoubleStackSlot()) << destination;
5646 MoveMemoryToMemory64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005647 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005648 } else if (source.IsConstant()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005649 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00005650 if (constant->IsIntConstant() || constant->IsNullConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05005651 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005652 if (destination.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05005653 if (value == 0) {
5654 __ xorl(destination.AsRegister<Register>(), destination.AsRegister<Register>());
5655 } else {
5656 __ movl(destination.AsRegister<Register>(), Immediate(value));
5657 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005658 } else {
5659 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell09b84632015-02-13 17:48:38 -05005660 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005661 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005662 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005663 float fp_value = constant->AsFloatConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00005664 int32_t value = bit_cast<int32_t, float>(fp_value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005665 Immediate imm(value);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005666 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005667 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
5668 if (value == 0) {
5669 // Easy handling of 0.0.
5670 __ xorps(dest, dest);
5671 } else {
5672 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005673 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5674 Register temp = static_cast<Register>(ensure_scratch.GetRegister());
5675 __ movl(temp, Immediate(value));
5676 __ movd(dest, temp);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005677 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005678 } else {
5679 DCHECK(destination.IsStackSlot()) << destination;
5680 __ movl(Address(ESP, destination.GetStackIndex()), imm);
5681 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005682 } else if (constant->IsLongConstant()) {
5683 int64_t value = constant->AsLongConstant()->GetValue();
5684 int32_t low_value = Low32Bits(value);
5685 int32_t high_value = High32Bits(value);
5686 Immediate low(low_value);
5687 Immediate high(high_value);
5688 if (destination.IsDoubleStackSlot()) {
5689 __ movl(Address(ESP, destination.GetStackIndex()), low);
5690 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
5691 } else {
5692 __ movl(destination.AsRegisterPairLow<Register>(), low);
5693 __ movl(destination.AsRegisterPairHigh<Register>(), high);
5694 }
5695 } else {
5696 DCHECK(constant->IsDoubleConstant());
5697 double dbl_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00005698 int64_t value = bit_cast<int64_t, double>(dbl_value);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005699 int32_t low_value = Low32Bits(value);
5700 int32_t high_value = High32Bits(value);
5701 Immediate low(low_value);
5702 Immediate high(high_value);
5703 if (destination.IsFpuRegister()) {
5704 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
5705 if (value == 0) {
5706 // Easy handling of 0.0.
5707 __ xorpd(dest, dest);
5708 } else {
5709 __ pushl(high);
5710 __ pushl(low);
5711 __ movsd(dest, Address(ESP, 0));
5712 __ addl(ESP, Immediate(8));
5713 }
5714 } else {
5715 DCHECK(destination.IsDoubleStackSlot()) << destination;
5716 __ movl(Address(ESP, destination.GetStackIndex()), low);
5717 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
5718 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005719 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005720 } else {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00005721 LOG(FATAL) << "Unimplemented move: " << destination << " <- " << source;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005722 }
5723}
5724
Mark Mendella5c19ce2015-04-01 12:51:05 -04005725void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005726 Register suggested_scratch = reg == EAX ? EBX : EAX;
5727 ScratchRegisterScope ensure_scratch(
5728 this, reg, suggested_scratch, codegen_->GetNumberOfCoreRegisters());
5729
5730 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5731 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset));
5732 __ movl(Address(ESP, mem + stack_offset), reg);
5733 __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005734}
5735
Mark Mendell7c8d0092015-01-26 11:21:33 -05005736void ParallelMoveResolverX86::Exchange32(XmmRegister reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005737 ScratchRegisterScope ensure_scratch(
5738 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5739
5740 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5741 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5742 __ movl(temp_reg, Address(ESP, mem + stack_offset));
5743 __ movss(Address(ESP, mem + stack_offset), reg);
5744 __ movd(reg, temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005745}
5746
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005747void ParallelMoveResolverX86::Exchange(int mem1, int mem2) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005748 ScratchRegisterScope ensure_scratch1(
5749 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005750
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005751 Register suggested_scratch = ensure_scratch1.GetRegister() == EAX ? EBX : EAX;
5752 ScratchRegisterScope ensure_scratch2(
5753 this, ensure_scratch1.GetRegister(), suggested_scratch, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005754
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005755 int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0;
5756 stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0;
5757 __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset));
5758 __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset));
5759 __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister()));
5760 __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005761}
5762
5763void ParallelMoveResolverX86::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005764 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005765 Location source = move->GetSource();
5766 Location destination = move->GetDestination();
5767
5768 if (source.IsRegister() && destination.IsRegister()) {
Mark Mendell90979812015-07-28 16:41:21 -04005769 // Use XOR swap algorithm to avoid serializing XCHG instruction or using a temporary.
5770 DCHECK_NE(destination.AsRegister<Register>(), source.AsRegister<Register>());
5771 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
5772 __ xorl(source.AsRegister<Register>(), destination.AsRegister<Register>());
5773 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005774 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005775 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005776 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005777 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005778 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
5779 Exchange(destination.GetStackIndex(), source.GetStackIndex());
Mark Mendell7c8d0092015-01-26 11:21:33 -05005780 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
5781 // Use XOR Swap algorithm to avoid a temporary.
5782 DCHECK_NE(source.reg(), destination.reg());
5783 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
5784 __ xorpd(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
5785 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
5786 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
5787 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
5788 } else if (destination.IsFpuRegister() && source.IsStackSlot()) {
5789 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005790 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
5791 // Take advantage of the 16 bytes in the XMM register.
5792 XmmRegister reg = source.AsFpuRegister<XmmRegister>();
5793 Address stack(ESP, destination.GetStackIndex());
5794 // Load the double into the high doubleword.
5795 __ movhpd(reg, stack);
5796
5797 // Store the low double into the destination.
5798 __ movsd(stack, reg);
5799
5800 // Move the high double to the low double.
5801 __ psrldq(reg, Immediate(8));
5802 } else if (destination.IsFpuRegister() && source.IsDoubleStackSlot()) {
5803 // Take advantage of the 16 bytes in the XMM register.
5804 XmmRegister reg = destination.AsFpuRegister<XmmRegister>();
5805 Address stack(ESP, source.GetStackIndex());
5806 // Load the double into the high doubleword.
5807 __ movhpd(reg, stack);
5808
5809 // Store the low double into the destination.
5810 __ movsd(stack, reg);
5811
5812 // Move the high double to the low double.
5813 __ psrldq(reg, Immediate(8));
5814 } else if (destination.IsDoubleStackSlot() && source.IsDoubleStackSlot()) {
5815 Exchange(destination.GetStackIndex(), source.GetStackIndex());
5816 Exchange(destination.GetHighStackIndex(kX86WordSize), source.GetHighStackIndex(kX86WordSize));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005817 } else {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005818 LOG(FATAL) << "Unimplemented: source: " << source << ", destination: " << destination;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005819 }
5820}
5821
5822void ParallelMoveResolverX86::SpillScratch(int reg) {
5823 __ pushl(static_cast<Register>(reg));
5824}
5825
5826void ParallelMoveResolverX86::RestoreScratch(int reg) {
5827 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005828}
5829
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005830HLoadClass::LoadKind CodeGeneratorX86::GetSupportedLoadClassKind(
5831 HLoadClass::LoadKind desired_class_load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005832 switch (desired_class_load_kind) {
5833 case HLoadClass::LoadKind::kReferrersClass:
5834 break;
5835 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
5836 DCHECK(!GetCompilerOptions().GetCompilePic());
5837 break;
5838 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
5839 DCHECK(GetCompilerOptions().GetCompilePic());
5840 FALLTHROUGH_INTENDED;
5841 case HLoadClass::LoadKind::kDexCachePcRelative:
5842 DCHECK(!Runtime::Current()->UseJitCompilation()); // Note: boot image is also non-JIT.
5843 // We disable pc-relative load when there is an irreducible loop, as the optimization
5844 // is incompatible with it.
5845 // TODO: Create as many X86ComputeBaseMethodAddress instructions as needed for methods
5846 // with irreducible loops.
5847 if (GetGraph()->HasIrreducibleLoops()) {
5848 return HLoadClass::LoadKind::kDexCacheViaMethod;
5849 }
5850 break;
5851 case HLoadClass::LoadKind::kBootImageAddress:
5852 break;
5853 case HLoadClass::LoadKind::kDexCacheAddress:
5854 DCHECK(Runtime::Current()->UseJitCompilation());
5855 break;
5856 case HLoadClass::LoadKind::kDexCacheViaMethod:
5857 break;
5858 }
5859 return desired_class_load_kind;
5860}
5861
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005862void LocationsBuilderX86::VisitLoadClass(HLoadClass* cls) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005863 if (cls->NeedsAccessCheck()) {
5864 InvokeRuntimeCallingConvention calling_convention;
5865 CodeGenerator::CreateLoadClassLocationSummary(
5866 cls,
5867 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
5868 Location::RegisterLocation(EAX),
5869 /* code_generator_supports_read_barrier */ true);
5870 return;
5871 }
5872
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005873 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
5874 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005875 ? LocationSummary::kCallOnSlowPath
5876 : LocationSummary::kNoCall;
5877 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005878 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005879 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01005880 }
5881
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005882 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
5883 if (load_kind == HLoadClass::LoadKind::kReferrersClass ||
5884 load_kind == HLoadClass::LoadKind::kDexCacheViaMethod ||
5885 load_kind == HLoadClass::LoadKind::kBootImageLinkTimePcRelative ||
5886 load_kind == HLoadClass::LoadKind::kDexCachePcRelative) {
5887 locations->SetInAt(0, Location::RequiresRegister());
5888 }
5889 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005890}
5891
5892void InstructionCodeGeneratorX86::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005893 LocationSummary* locations = cls->GetLocations();
Calin Juravle98893e12015-10-02 21:05:03 +01005894 if (cls->NeedsAccessCheck()) {
5895 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex());
Serban Constantinescuba45db02016-07-12 22:53:02 +01005896 codegen_->InvokeRuntime(kQuickInitializeTypeAndVerifyAccess, cls, cls->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00005897 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Calin Juravle580b6092015-10-06 17:35:58 +01005898 return;
5899 }
5900
Roland Levillain0d5a2812015-11-13 10:07:31 +00005901 Location out_loc = locations->Out();
5902 Register out = out_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005903
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005904 bool generate_null_check = false;
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005905 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005906 switch (cls->GetLoadKind()) {
5907 case HLoadClass::LoadKind::kReferrersClass: {
5908 DCHECK(!cls->CanCallRuntime());
5909 DCHECK(!cls->MustGenerateClinitCheck());
5910 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5911 Register current_method = locations->InAt(0).AsRegister<Register>();
5912 GenerateGcRootFieldLoad(
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005913 cls,
5914 out_loc,
5915 Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()),
5916 /*fixup_label*/ nullptr,
5917 requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005918 break;
5919 }
5920 case HLoadClass::LoadKind::kBootImageLinkTimeAddress: {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005921 DCHECK(!requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005922 __ movl(out, Immediate(/* placeholder */ 0));
5923 codegen_->RecordTypePatch(cls);
5924 break;
5925 }
5926 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005927 DCHECK(!requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005928 Register method_address = locations->InAt(0).AsRegister<Register>();
5929 __ leal(out, Address(method_address, CodeGeneratorX86::kDummy32BitOffset));
5930 codegen_->RecordTypePatch(cls);
5931 break;
5932 }
5933 case HLoadClass::LoadKind::kBootImageAddress: {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005934 DCHECK(!requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005935 DCHECK_NE(cls->GetAddress(), 0u);
5936 uint32_t address = dchecked_integral_cast<uint32_t>(cls->GetAddress());
5937 __ movl(out, Immediate(address));
5938 codegen_->RecordSimplePatch();
5939 break;
5940 }
5941 case HLoadClass::LoadKind::kDexCacheAddress: {
5942 DCHECK_NE(cls->GetAddress(), 0u);
5943 uint32_t address = dchecked_integral_cast<uint32_t>(cls->GetAddress());
5944 // /* GcRoot<mirror::Class> */ out = *address
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005945 GenerateGcRootFieldLoad(cls,
5946 out_loc,
5947 Address::Absolute(address),
5948 /*fixup_label*/ nullptr,
5949 requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005950 generate_null_check = !cls->IsInDexCache();
5951 break;
5952 }
5953 case HLoadClass::LoadKind::kDexCachePcRelative: {
5954 Register base_reg = locations->InAt(0).AsRegister<Register>();
5955 uint32_t offset = cls->GetDexCacheElementOffset();
5956 Label* fixup_label = codegen_->NewPcRelativeDexCacheArrayPatch(cls->GetDexFile(), offset);
5957 // /* GcRoot<mirror::Class> */ out = *(base + offset) /* PC-relative */
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005958 GenerateGcRootFieldLoad(cls,
5959 out_loc,
5960 Address(base_reg, CodeGeneratorX86::kDummy32BitOffset),
5961 fixup_label,
5962 requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005963 generate_null_check = !cls->IsInDexCache();
5964 break;
5965 }
5966 case HLoadClass::LoadKind::kDexCacheViaMethod: {
5967 // /* GcRoot<mirror::Class>[] */ out =
5968 // current_method.ptr_sized_fields_->dex_cache_resolved_types_
5969 Register current_method = locations->InAt(0).AsRegister<Register>();
5970 __ movl(out, Address(current_method,
5971 ArtMethod::DexCacheResolvedTypesOffset(kX86PointerSize).Int32Value()));
5972 // /* GcRoot<mirror::Class> */ out = out[type_index]
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005973 GenerateGcRootFieldLoad(cls,
5974 out_loc,
5975 Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())),
5976 /*fixup_label*/ nullptr,
5977 requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005978 generate_null_check = !cls->IsInDexCache();
5979 break;
5980 }
5981 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005982
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005983 if (generate_null_check || cls->MustGenerateClinitCheck()) {
5984 DCHECK(cls->CanCallRuntime());
5985 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
5986 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
5987 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005988
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005989 if (generate_null_check) {
5990 __ testl(out, out);
5991 __ j(kEqual, slow_path->GetEntryLabel());
5992 }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005993
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005994 if (cls->MustGenerateClinitCheck()) {
5995 GenerateClassInitializationCheck(slow_path, out);
5996 } else {
5997 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005998 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005999 }
6000}
6001
6002void LocationsBuilderX86::VisitClinitCheck(HClinitCheck* check) {
6003 LocationSummary* locations =
6004 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
6005 locations->SetInAt(0, Location::RequiresRegister());
6006 if (check->HasUses()) {
6007 locations->SetOut(Location::SameAsFirstInput());
6008 }
6009}
6010
6011void InstructionCodeGeneratorX86::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006012 // We assume the class to not be null.
Andreas Gampe85b62f22015-09-09 13:15:38 -07006013 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006014 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006015 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00006016 GenerateClassInitializationCheck(slow_path,
6017 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006018}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006019
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006020void InstructionCodeGeneratorX86::GenerateClassInitializationCheck(
Andreas Gampe85b62f22015-09-09 13:15:38 -07006021 SlowPathCode* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006022 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
6023 Immediate(mirror::Class::kStatusInitialized));
6024 __ j(kLess, slow_path->GetEntryLabel());
6025 __ Bind(slow_path->GetExitLabel());
6026 // No need for memory fence, thanks to the X86 memory model.
6027}
6028
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006029HLoadString::LoadKind CodeGeneratorX86::GetSupportedLoadStringKind(
6030 HLoadString::LoadKind desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006031 switch (desired_string_load_kind) {
6032 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
6033 DCHECK(!GetCompilerOptions().GetCompilePic());
6034 break;
6035 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
6036 DCHECK(GetCompilerOptions().GetCompilePic());
6037 FALLTHROUGH_INTENDED;
Vladimir Markoaad75c62016-10-03 08:46:48 +00006038 case HLoadString::LoadKind::kBssEntry:
Calin Juravleffc87072016-04-20 14:22:09 +01006039 DCHECK(!Runtime::Current()->UseJitCompilation()); // Note: boot image is also non-JIT.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006040 // We disable pc-relative load when there is an irreducible loop, as the optimization
6041 // is incompatible with it.
6042 // TODO: Create as many X86ComputeBaseMethodAddress instructions as needed for methods
6043 // with irreducible loops.
6044 if (GetGraph()->HasIrreducibleLoops()) {
6045 return HLoadString::LoadKind::kDexCacheViaMethod;
6046 }
6047 break;
6048 case HLoadString::LoadKind::kBootImageAddress:
6049 break;
6050 case HLoadString::LoadKind::kDexCacheAddress:
Calin Juravleffc87072016-04-20 14:22:09 +01006051 DCHECK(Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006052 break;
6053 case HLoadString::LoadKind::kDexCacheViaMethod:
6054 break;
6055 }
6056 return desired_string_load_kind;
6057}
6058
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006059void LocationsBuilderX86::VisitLoadString(HLoadString* load) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006060 LocationSummary::CallKind call_kind = (load->NeedsEnvironment() || kEmitCompilerReadBarrier)
Vladimir Markoaad75c62016-10-03 08:46:48 +00006061 ? ((load->GetLoadKind() == HLoadString::LoadKind::kDexCacheViaMethod)
6062 ? LocationSummary::kCallOnMainOnly
6063 : LocationSummary::kCallOnSlowPath)
Nicolas Geoffray917d0162015-11-24 18:25:35 +00006064 : LocationSummary::kNoCall;
6065 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006066 HLoadString::LoadKind load_kind = load->GetLoadKind();
6067 if (load_kind == HLoadString::LoadKind::kDexCacheViaMethod ||
6068 load_kind == HLoadString::LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markoaad75c62016-10-03 08:46:48 +00006069 load_kind == HLoadString::LoadKind::kBssEntry) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006070 locations->SetInAt(0, Location::RequiresRegister());
6071 }
Christina Wadsworth175d09b2016-08-31 16:26:01 -07006072 if (load_kind == HLoadString::LoadKind::kDexCacheViaMethod) {
6073 locations->SetOut(Location::RegisterLocation(EAX));
6074 } else {
6075 locations->SetOut(Location::RequiresRegister());
6076 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006077}
6078
6079void InstructionCodeGeneratorX86::VisitLoadString(HLoadString* load) {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01006080 LocationSummary* locations = load->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006081 Location out_loc = locations->Out();
6082 Register out = out_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006083
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006084 switch (load->GetLoadKind()) {
6085 case HLoadString::LoadKind::kBootImageLinkTimeAddress: {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006086 __ movl(out, Immediate(/* placeholder */ 0));
Vladimir Markoaad75c62016-10-03 08:46:48 +00006087 codegen_->RecordBootStringPatch(load);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006088 return; // No dex cache slow path.
6089 }
6090 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006091 Register method_address = locations->InAt(0).AsRegister<Register>();
6092 __ leal(out, Address(method_address, CodeGeneratorX86::kDummy32BitOffset));
Vladimir Markoaad75c62016-10-03 08:46:48 +00006093 codegen_->RecordBootStringPatch(load);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006094 return; // No dex cache slow path.
6095 }
6096 case HLoadString::LoadKind::kBootImageAddress: {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006097 DCHECK_NE(load->GetAddress(), 0u);
6098 uint32_t address = dchecked_integral_cast<uint32_t>(load->GetAddress());
6099 __ movl(out, Immediate(address));
6100 codegen_->RecordSimplePatch();
6101 return; // No dex cache slow path.
6102 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00006103 case HLoadString::LoadKind::kBssEntry: {
6104 Register method_address = locations->InAt(0).AsRegister<Register>();
6105 Address address = Address(method_address, CodeGeneratorX86::kDummy32BitOffset);
6106 Label* fixup_label = codegen_->NewStringBssEntryPatch(load);
6107 // /* GcRoot<mirror::Class> */ out = *address /* PC-relative */
6108 GenerateGcRootFieldLoad(load, out_loc, address, fixup_label);
6109 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86(load);
6110 codegen_->AddSlowPath(slow_path);
6111 __ testl(out, out);
6112 __ j(kEqual, slow_path->GetEntryLabel());
6113 __ Bind(slow_path->GetExitLabel());
6114 return;
6115 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006116 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07006117 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006118 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006119
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07006120 // TODO: Re-add the compiler code to do string dex cache lookup again.
Christina Wadsworth175d09b2016-08-31 16:26:01 -07006121 InvokeRuntimeCallingConvention calling_convention;
6122 __ movl(calling_convention.GetRegisterAt(0), Immediate(load->GetStringIndex()));
6123 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
6124 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006125}
6126
David Brazdilcb1c0552015-08-04 16:22:25 +01006127static Address GetExceptionTlsAddress() {
Andreas Gampe542451c2016-07-26 09:02:02 -07006128 return Address::Absolute(Thread::ExceptionOffset<kX86PointerSize>().Int32Value());
David Brazdilcb1c0552015-08-04 16:22:25 +01006129}
6130
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006131void LocationsBuilderX86::VisitLoadException(HLoadException* load) {
6132 LocationSummary* locations =
6133 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
6134 locations->SetOut(Location::RequiresRegister());
6135}
6136
6137void InstructionCodeGeneratorX86::VisitLoadException(HLoadException* load) {
David Brazdilcb1c0552015-08-04 16:22:25 +01006138 __ fs()->movl(load->GetLocations()->Out().AsRegister<Register>(), GetExceptionTlsAddress());
6139}
6140
6141void LocationsBuilderX86::VisitClearException(HClearException* clear) {
6142 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
6143}
6144
6145void InstructionCodeGeneratorX86::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
6146 __ fs()->movl(GetExceptionTlsAddress(), Immediate(0));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006147}
6148
6149void LocationsBuilderX86::VisitThrow(HThrow* instruction) {
6150 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006151 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006152 InvokeRuntimeCallingConvention calling_convention;
6153 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6154}
6155
6156void InstructionCodeGeneratorX86::VisitThrow(HThrow* instruction) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01006157 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006158 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006159}
6160
Roland Levillain7c1559a2015-12-15 10:55:36 +00006161static bool TypeCheckNeedsATemporary(TypeCheckKind type_check_kind) {
6162 return kEmitCompilerReadBarrier &&
Vladimir Marko953437b2016-08-24 08:30:46 +00006163 !kUseBakerReadBarrier &&
6164 (type_check_kind == TypeCheckKind::kAbstractClassCheck ||
Roland Levillain7c1559a2015-12-15 10:55:36 +00006165 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
6166 type_check_kind == TypeCheckKind::kArrayObjectCheck);
6167}
6168
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006169void LocationsBuilderX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006170 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain0d5a2812015-11-13 10:07:31 +00006171 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01006172 bool baker_read_barrier_slow_path = false;
Roland Levillain0d5a2812015-11-13 10:07:31 +00006173 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006174 case TypeCheckKind::kExactCheck:
6175 case TypeCheckKind::kAbstractClassCheck:
6176 case TypeCheckKind::kClassHierarchyCheck:
6177 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006178 call_kind =
6179 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Vladimir Marko70e97462016-08-09 11:04:26 +01006180 baker_read_barrier_slow_path = kUseBakerReadBarrier;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006181 break;
6182 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006183 case TypeCheckKind::kUnresolvedCheck:
6184 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006185 call_kind = LocationSummary::kCallOnSlowPath;
6186 break;
6187 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006188
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006189 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01006190 if (baker_read_barrier_slow_path) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006191 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01006192 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006193 locations->SetInAt(0, Location::RequiresRegister());
6194 locations->SetInAt(1, Location::Any());
6195 // Note that TypeCheckSlowPathX86 uses this "out" register too.
6196 locations->SetOut(Location::RequiresRegister());
6197 // When read barriers are enabled, we need a temporary register for
6198 // some cases.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006199 if (TypeCheckNeedsATemporary(type_check_kind)) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006200 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006201 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006202}
6203
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006204void InstructionCodeGeneratorX86::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006205 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006206 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006207 Location obj_loc = locations->InAt(0);
6208 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006209 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006210 Location out_loc = locations->Out();
6211 Register out = out_loc.AsRegister<Register>();
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006212 Location maybe_temp_loc = TypeCheckNeedsATemporary(type_check_kind) ?
Roland Levillain7c1559a2015-12-15 10:55:36 +00006213 locations->GetTemp(0) :
6214 Location::NoLocation();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006215 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006216 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6217 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6218 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Andreas Gampe85b62f22015-09-09 13:15:38 -07006219 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006220 NearLabel done, zero;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006221
6222 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006223 // Avoid null check if we know obj is not null.
6224 if (instruction->MustDoNullCheck()) {
6225 __ testl(obj, obj);
6226 __ j(kEqual, &zero);
6227 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006228
Roland Levillain0d5a2812015-11-13 10:07:31 +00006229 // /* HeapReference<Class> */ out = obj->klass_
Vladimir Marko953437b2016-08-24 08:30:46 +00006230 GenerateReferenceLoadTwoRegisters(instruction, out_loc, obj_loc, class_offset);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006231
Roland Levillain7c1559a2015-12-15 10:55:36 +00006232 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006233 case TypeCheckKind::kExactCheck: {
6234 if (cls.IsRegister()) {
6235 __ cmpl(out, cls.AsRegister<Register>());
6236 } else {
6237 DCHECK(cls.IsStackSlot()) << cls;
6238 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6239 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006240
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006241 // Classes must be equal for the instanceof to succeed.
6242 __ j(kNotEqual, &zero);
6243 __ movl(out, Immediate(1));
6244 __ jmp(&done);
6245 break;
6246 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006247
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006248 case TypeCheckKind::kAbstractClassCheck: {
6249 // If the class is abstract, we eagerly fetch the super class of the
6250 // object to avoid doing a comparison we know will fail.
6251 NearLabel loop;
6252 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006253 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006254 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006255 __ testl(out, out);
6256 // If `out` is null, we use it for the result, and jump to `done`.
6257 __ j(kEqual, &done);
6258 if (cls.IsRegister()) {
6259 __ cmpl(out, cls.AsRegister<Register>());
6260 } else {
6261 DCHECK(cls.IsStackSlot()) << cls;
6262 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6263 }
6264 __ j(kNotEqual, &loop);
6265 __ movl(out, Immediate(1));
6266 if (zero.IsLinked()) {
6267 __ jmp(&done);
6268 }
6269 break;
6270 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006271
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006272 case TypeCheckKind::kClassHierarchyCheck: {
6273 // Walk over the class hierarchy to find a match.
6274 NearLabel loop, success;
6275 __ Bind(&loop);
6276 if (cls.IsRegister()) {
6277 __ cmpl(out, cls.AsRegister<Register>());
6278 } else {
6279 DCHECK(cls.IsStackSlot()) << cls;
6280 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6281 }
6282 __ j(kEqual, &success);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006283 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006284 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006285 __ testl(out, out);
6286 __ j(kNotEqual, &loop);
6287 // If `out` is null, we use it for the result, and jump to `done`.
6288 __ jmp(&done);
6289 __ Bind(&success);
6290 __ movl(out, Immediate(1));
6291 if (zero.IsLinked()) {
6292 __ jmp(&done);
6293 }
6294 break;
6295 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006296
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006297 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006298 // Do an exact check.
6299 NearLabel exact_check;
6300 if (cls.IsRegister()) {
6301 __ cmpl(out, cls.AsRegister<Register>());
6302 } else {
6303 DCHECK(cls.IsStackSlot()) << cls;
6304 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6305 }
6306 __ j(kEqual, &exact_check);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006307 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006308 // /* HeapReference<Class> */ out = out->component_type_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006309 GenerateReferenceLoadOneRegister(instruction, out_loc, component_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006310 __ testl(out, out);
6311 // If `out` is null, we use it for the result, and jump to `done`.
6312 __ j(kEqual, &done);
6313 __ cmpw(Address(out, primitive_offset), Immediate(Primitive::kPrimNot));
6314 __ j(kNotEqual, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006315 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006316 __ movl(out, Immediate(1));
6317 __ jmp(&done);
6318 break;
6319 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006320
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006321 case TypeCheckKind::kArrayCheck: {
6322 if (cls.IsRegister()) {
6323 __ cmpl(out, cls.AsRegister<Register>());
6324 } else {
6325 DCHECK(cls.IsStackSlot()) << cls;
6326 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6327 }
6328 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain0d5a2812015-11-13 10:07:31 +00006329 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
6330 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006331 codegen_->AddSlowPath(slow_path);
6332 __ j(kNotEqual, slow_path->GetEntryLabel());
6333 __ movl(out, Immediate(1));
6334 if (zero.IsLinked()) {
6335 __ jmp(&done);
6336 }
6337 break;
6338 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006339
Calin Juravle98893e12015-10-02 21:05:03 +01006340 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006341 case TypeCheckKind::kInterfaceCheck: {
6342 // Note that we indeed only call on slow path, but we always go
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006343 // into the slow path for the unresolved and interface check
Roland Levillain0d5a2812015-11-13 10:07:31 +00006344 // cases.
6345 //
6346 // We cannot directly call the InstanceofNonTrivial runtime
6347 // entry point without resorting to a type checking slow path
6348 // here (i.e. by calling InvokeRuntime directly), as it would
6349 // require to assign fixed registers for the inputs of this
6350 // HInstanceOf instruction (following the runtime calling
6351 // convention), which might be cluttered by the potential first
6352 // read barrier emission at the beginning of this method.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006353 //
6354 // TODO: Introduce a new runtime entry point taking the object
6355 // to test (instead of its class) as argument, and let it deal
6356 // with the read barrier issues. This will let us refactor this
6357 // case of the `switch` code as it was previously (with a direct
6358 // call to the runtime not using a type checking slow path).
6359 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006360 DCHECK(locations->OnlyCallsOnSlowPath());
6361 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
6362 /* is_fatal */ false);
6363 codegen_->AddSlowPath(slow_path);
6364 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006365 if (zero.IsLinked()) {
6366 __ jmp(&done);
6367 }
6368 break;
6369 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006370 }
6371
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006372 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006373 __ Bind(&zero);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006374 __ xorl(out, out);
6375 }
6376
6377 if (done.IsLinked()) {
6378 __ Bind(&done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006379 }
6380
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006381 if (slow_path != nullptr) {
6382 __ Bind(slow_path->GetExitLabel());
6383 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006384}
6385
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006386void LocationsBuilderX86::VisitCheckCast(HCheckCast* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006387 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
6388 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006389 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
6390 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006391 case TypeCheckKind::kExactCheck:
6392 case TypeCheckKind::kAbstractClassCheck:
6393 case TypeCheckKind::kClassHierarchyCheck:
6394 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006395 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
6396 LocationSummary::kCallOnSlowPath :
6397 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006398 break;
6399 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006400 case TypeCheckKind::kUnresolvedCheck:
6401 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006402 call_kind = LocationSummary::kCallOnSlowPath;
6403 break;
6404 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006405 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
6406 locations->SetInAt(0, Location::RequiresRegister());
6407 locations->SetInAt(1, Location::Any());
6408 // Note that TypeCheckSlowPathX86 uses this "temp" register too.
6409 locations->AddTemp(Location::RequiresRegister());
6410 // When read barriers are enabled, we need an additional temporary
6411 // register for some cases.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006412 if (TypeCheckNeedsATemporary(type_check_kind)) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006413 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006414 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006415}
6416
6417void InstructionCodeGeneratorX86::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006418 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006419 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006420 Location obj_loc = locations->InAt(0);
6421 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006422 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006423 Location temp_loc = locations->GetTemp(0);
6424 Register temp = temp_loc.AsRegister<Register>();
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006425 Location maybe_temp2_loc = TypeCheckNeedsATemporary(type_check_kind) ?
Roland Levillain7c1559a2015-12-15 10:55:36 +00006426 locations->GetTemp(1) :
6427 Location::NoLocation();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006428 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
6429 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6430 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6431 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006432
Roland Levillain0d5a2812015-11-13 10:07:31 +00006433 bool is_type_check_slow_path_fatal =
6434 (type_check_kind == TypeCheckKind::kExactCheck ||
6435 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
6436 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
6437 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
6438 !instruction->CanThrowIntoCatchBlock();
6439 SlowPathCode* type_check_slow_path =
6440 new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
6441 is_type_check_slow_path_fatal);
6442 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006443
Roland Levillain0d5a2812015-11-13 10:07:31 +00006444 NearLabel done;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006445 // Avoid null check if we know obj is not null.
6446 if (instruction->MustDoNullCheck()) {
6447 __ testl(obj, obj);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006448 __ j(kEqual, &done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006449 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006450
Roland Levillain0d5a2812015-11-13 10:07:31 +00006451 // /* HeapReference<Class> */ temp = obj->klass_
Vladimir Marko953437b2016-08-24 08:30:46 +00006452 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006453
Roland Levillain0d5a2812015-11-13 10:07:31 +00006454 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006455 case TypeCheckKind::kExactCheck:
6456 case TypeCheckKind::kArrayCheck: {
6457 if (cls.IsRegister()) {
6458 __ cmpl(temp, cls.AsRegister<Register>());
6459 } else {
6460 DCHECK(cls.IsStackSlot()) << cls;
6461 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6462 }
6463 // Jump to slow path for throwing the exception or doing a
6464 // more involved array check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006465 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006466 break;
6467 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006468
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006469 case TypeCheckKind::kAbstractClassCheck: {
6470 // If the class is abstract, we eagerly fetch the super class of the
6471 // object to avoid doing a comparison we know will fail.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006472 NearLabel loop, compare_classes;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006473 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006474 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006475 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006476
6477 // If the class reference currently in `temp` is not null, jump
6478 // to the `compare_classes` label to compare it with the checked
6479 // class.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006480 __ testl(temp, temp);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006481 __ j(kNotEqual, &compare_classes);
6482 // Otherwise, jump to the slow path to throw the exception.
6483 //
6484 // But before, move back the object's class into `temp` before
6485 // going into the slow path, as it has been overwritten in the
6486 // meantime.
6487 // /* HeapReference<Class> */ temp = obj->klass_
Vladimir Marko953437b2016-08-24 08:30:46 +00006488 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006489 __ jmp(type_check_slow_path->GetEntryLabel());
6490
6491 __ Bind(&compare_classes);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006492 if (cls.IsRegister()) {
6493 __ cmpl(temp, cls.AsRegister<Register>());
6494 } else {
6495 DCHECK(cls.IsStackSlot()) << cls;
6496 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6497 }
6498 __ j(kNotEqual, &loop);
6499 break;
6500 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006501
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006502 case TypeCheckKind::kClassHierarchyCheck: {
6503 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006504 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006505 __ Bind(&loop);
6506 if (cls.IsRegister()) {
6507 __ cmpl(temp, cls.AsRegister<Register>());
6508 } else {
6509 DCHECK(cls.IsStackSlot()) << cls;
6510 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6511 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006512 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006513
Roland Levillain0d5a2812015-11-13 10:07:31 +00006514 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006515 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006516
6517 // If the class reference currently in `temp` is not null, jump
6518 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006519 __ testl(temp, temp);
6520 __ j(kNotEqual, &loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006521 // Otherwise, jump to the slow path to throw the exception.
6522 //
6523 // But before, move back the object's class into `temp` before
6524 // going into the slow path, as it has been overwritten in the
6525 // meantime.
6526 // /* HeapReference<Class> */ temp = obj->klass_
Vladimir Marko953437b2016-08-24 08:30:46 +00006527 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006528 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006529 break;
6530 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006531
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006532 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006533 // Do an exact check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006534 NearLabel check_non_primitive_component_type;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006535 if (cls.IsRegister()) {
6536 __ cmpl(temp, cls.AsRegister<Register>());
6537 } else {
6538 DCHECK(cls.IsStackSlot()) << cls;
6539 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6540 }
6541 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006542
6543 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006544 // /* HeapReference<Class> */ temp = temp->component_type_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006545 GenerateReferenceLoadOneRegister(instruction, temp_loc, component_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006546
6547 // If the component type is not null (i.e. the object is indeed
6548 // an array), jump to label `check_non_primitive_component_type`
6549 // to further check that this component type is not a primitive
6550 // type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006551 __ testl(temp, temp);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006552 __ j(kNotEqual, &check_non_primitive_component_type);
6553 // Otherwise, jump to the slow path to throw the exception.
6554 //
6555 // But before, move back the object's class into `temp` before
6556 // going into the slow path, as it has been overwritten in the
6557 // meantime.
6558 // /* HeapReference<Class> */ temp = obj->klass_
Vladimir Marko953437b2016-08-24 08:30:46 +00006559 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006560 __ jmp(type_check_slow_path->GetEntryLabel());
6561
6562 __ Bind(&check_non_primitive_component_type);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006563 __ cmpw(Address(temp, primitive_offset), Immediate(Primitive::kPrimNot));
Roland Levillain0d5a2812015-11-13 10:07:31 +00006564 __ j(kEqual, &done);
6565 // Same comment as above regarding `temp` and the slow path.
6566 // /* HeapReference<Class> */ temp = obj->klass_
Vladimir Marko953437b2016-08-24 08:30:46 +00006567 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006568 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006569 break;
6570 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006571
Calin Juravle98893e12015-10-02 21:05:03 +01006572 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006573 case TypeCheckKind::kInterfaceCheck:
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006574 // We always go into the type check slow path for the unresolved
6575 // and interface check cases.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006576 //
6577 // We cannot directly call the CheckCast runtime entry point
6578 // without resorting to a type checking slow path here (i.e. by
6579 // calling InvokeRuntime directly), as it would require to
6580 // assign fixed registers for the inputs of this HInstanceOf
6581 // instruction (following the runtime calling convention), which
6582 // might be cluttered by the potential first read barrier
6583 // emission at the beginning of this method.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006584 //
6585 // TODO: Introduce a new runtime entry point taking the object
6586 // to test (instead of its class) as argument, and let it deal
6587 // with the read barrier issues. This will let us refactor this
6588 // case of the `switch` code as it was previously (with a direct
6589 // call to the runtime not using a type checking slow path).
6590 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006591 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006592 break;
6593 }
6594 __ Bind(&done);
6595
Roland Levillain0d5a2812015-11-13 10:07:31 +00006596 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006597}
6598
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006599void LocationsBuilderX86::VisitMonitorOperation(HMonitorOperation* instruction) {
6600 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006601 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006602 InvokeRuntimeCallingConvention calling_convention;
6603 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6604}
6605
6606void InstructionCodeGeneratorX86::VisitMonitorOperation(HMonitorOperation* instruction) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01006607 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject
6608 : kQuickUnlockObject,
Alexandre Rames8158f282015-08-07 10:26:17 +01006609 instruction,
Serban Constantinescuba45db02016-07-12 22:53:02 +01006610 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006611 if (instruction->IsEnter()) {
6612 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
6613 } else {
6614 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
6615 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006616}
6617
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006618void LocationsBuilderX86::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
6619void LocationsBuilderX86::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
6620void LocationsBuilderX86::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
6621
6622void LocationsBuilderX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
6623 LocationSummary* locations =
6624 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6625 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
6626 || instruction->GetResultType() == Primitive::kPrimLong);
6627 locations->SetInAt(0, Location::RequiresRegister());
6628 locations->SetInAt(1, Location::Any());
6629 locations->SetOut(Location::SameAsFirstInput());
6630}
6631
6632void InstructionCodeGeneratorX86::VisitAnd(HAnd* instruction) {
6633 HandleBitwiseOperation(instruction);
6634}
6635
6636void InstructionCodeGeneratorX86::VisitOr(HOr* instruction) {
6637 HandleBitwiseOperation(instruction);
6638}
6639
6640void InstructionCodeGeneratorX86::VisitXor(HXor* instruction) {
6641 HandleBitwiseOperation(instruction);
6642}
6643
6644void InstructionCodeGeneratorX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
6645 LocationSummary* locations = instruction->GetLocations();
6646 Location first = locations->InAt(0);
6647 Location second = locations->InAt(1);
6648 DCHECK(first.Equals(locations->Out()));
6649
6650 if (instruction->GetResultType() == Primitive::kPrimInt) {
6651 if (second.IsRegister()) {
6652 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006653 __ andl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006654 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006655 __ orl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006656 } else {
6657 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006658 __ xorl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006659 }
6660 } else if (second.IsConstant()) {
6661 if (instruction->IsAnd()) {
Roland Levillain199f3362014-11-27 17:15:16 +00006662 __ andl(first.AsRegister<Register>(),
6663 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006664 } else if (instruction->IsOr()) {
Roland Levillain199f3362014-11-27 17:15:16 +00006665 __ orl(first.AsRegister<Register>(),
6666 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006667 } else {
6668 DCHECK(instruction->IsXor());
Roland Levillain199f3362014-11-27 17:15:16 +00006669 __ xorl(first.AsRegister<Register>(),
6670 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006671 }
6672 } else {
6673 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006674 __ andl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006675 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006676 __ orl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006677 } else {
6678 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006679 __ xorl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006680 }
6681 }
6682 } else {
6683 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
6684 if (second.IsRegisterPair()) {
6685 if (instruction->IsAnd()) {
6686 __ andl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
6687 __ andl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
6688 } else if (instruction->IsOr()) {
6689 __ orl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
6690 __ orl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
6691 } else {
6692 DCHECK(instruction->IsXor());
6693 __ xorl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
6694 __ xorl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
6695 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006696 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006697 if (instruction->IsAnd()) {
6698 __ andl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
6699 __ andl(first.AsRegisterPairHigh<Register>(),
6700 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
6701 } else if (instruction->IsOr()) {
6702 __ orl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
6703 __ orl(first.AsRegisterPairHigh<Register>(),
6704 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
6705 } else {
6706 DCHECK(instruction->IsXor());
6707 __ xorl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
6708 __ xorl(first.AsRegisterPairHigh<Register>(),
6709 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
6710 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006711 } else {
6712 DCHECK(second.IsConstant()) << second;
6713 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006714 int32_t low_value = Low32Bits(value);
6715 int32_t high_value = High32Bits(value);
6716 Immediate low(low_value);
6717 Immediate high(high_value);
6718 Register first_low = first.AsRegisterPairLow<Register>();
6719 Register first_high = first.AsRegisterPairHigh<Register>();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006720 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006721 if (low_value == 0) {
6722 __ xorl(first_low, first_low);
6723 } else if (low_value != -1) {
6724 __ andl(first_low, low);
6725 }
6726 if (high_value == 0) {
6727 __ xorl(first_high, first_high);
6728 } else if (high_value != -1) {
6729 __ andl(first_high, high);
6730 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006731 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006732 if (low_value != 0) {
6733 __ orl(first_low, low);
6734 }
6735 if (high_value != 0) {
6736 __ orl(first_high, high);
6737 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006738 } else {
6739 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006740 if (low_value != 0) {
6741 __ xorl(first_low, low);
6742 }
6743 if (high_value != 0) {
6744 __ xorl(first_high, high);
6745 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006746 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006747 }
6748 }
6749}
6750
Roland Levillain7c1559a2015-12-15 10:55:36 +00006751void InstructionCodeGeneratorX86::GenerateReferenceLoadOneRegister(HInstruction* instruction,
6752 Location out,
6753 uint32_t offset,
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006754 Location maybe_temp) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006755 Register out_reg = out.AsRegister<Register>();
6756 if (kEmitCompilerReadBarrier) {
6757 if (kUseBakerReadBarrier) {
6758 // Load with fast path based Baker's read barrier.
6759 // /* HeapReference<Object> */ out = *(out + offset)
6760 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00006761 instruction, out, out_reg, offset, /* needs_null_check */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006762 } else {
6763 // Load with slow path based read barrier.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006764 // Save the value of `out` into `maybe_temp` before overwriting it
Roland Levillain7c1559a2015-12-15 10:55:36 +00006765 // in the following move operation, as we will need it for the
6766 // read barrier below.
Vladimir Marko953437b2016-08-24 08:30:46 +00006767 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006768 __ movl(maybe_temp.AsRegister<Register>(), out_reg);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006769 // /* HeapReference<Object> */ out = *(out + offset)
6770 __ movl(out_reg, Address(out_reg, offset));
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006771 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006772 }
6773 } else {
6774 // Plain load with no read barrier.
6775 // /* HeapReference<Object> */ out = *(out + offset)
6776 __ movl(out_reg, Address(out_reg, offset));
6777 __ MaybeUnpoisonHeapReference(out_reg);
6778 }
6779}
6780
6781void InstructionCodeGeneratorX86::GenerateReferenceLoadTwoRegisters(HInstruction* instruction,
6782 Location out,
6783 Location obj,
Vladimir Marko953437b2016-08-24 08:30:46 +00006784 uint32_t offset) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006785 Register out_reg = out.AsRegister<Register>();
6786 Register obj_reg = obj.AsRegister<Register>();
6787 if (kEmitCompilerReadBarrier) {
6788 if (kUseBakerReadBarrier) {
6789 // Load with fast path based Baker's read barrier.
6790 // /* HeapReference<Object> */ out = *(obj + offset)
6791 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00006792 instruction, out, obj_reg, offset, /* needs_null_check */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006793 } else {
6794 // Load with slow path based read barrier.
6795 // /* HeapReference<Object> */ out = *(obj + offset)
6796 __ movl(out_reg, Address(obj_reg, offset));
6797 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
6798 }
6799 } else {
6800 // Plain load with no read barrier.
6801 // /* HeapReference<Object> */ out = *(obj + offset)
6802 __ movl(out_reg, Address(obj_reg, offset));
6803 __ MaybeUnpoisonHeapReference(out_reg);
6804 }
6805}
6806
6807void InstructionCodeGeneratorX86::GenerateGcRootFieldLoad(HInstruction* instruction,
6808 Location root,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006809 const Address& address,
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006810 Label* fixup_label,
6811 bool requires_read_barrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006812 Register root_reg = root.AsRegister<Register>();
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006813 if (requires_read_barrier) {
6814 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006815 if (kUseBakerReadBarrier) {
6816 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
6817 // Baker's read barrier are used:
6818 //
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006819 // root = *address;
Roland Levillain7c1559a2015-12-15 10:55:36 +00006820 // if (Thread::Current()->GetIsGcMarking()) {
6821 // root = ReadBarrier::Mark(root)
6822 // }
6823
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006824 // /* GcRoot<mirror::Object> */ root = *address
6825 __ movl(root_reg, address);
6826 if (fixup_label != nullptr) {
6827 __ Bind(fixup_label);
6828 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00006829 static_assert(
6830 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
6831 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
6832 "have different sizes.");
6833 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
6834 "art::mirror::CompressedReference<mirror::Object> and int32_t "
6835 "have different sizes.");
6836
Vladimir Marko953437b2016-08-24 08:30:46 +00006837 // Slow path marking the GC root `root`.
6838 SlowPathCode* slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathX86(
6839 instruction, root, /* unpoison */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006840 codegen_->AddSlowPath(slow_path);
6841
Andreas Gampe542451c2016-07-26 09:02:02 -07006842 __ fs()->cmpl(Address::Absolute(Thread::IsGcMarkingOffset<kX86PointerSize>().Int32Value()),
Roland Levillain7c1559a2015-12-15 10:55:36 +00006843 Immediate(0));
6844 __ j(kNotEqual, slow_path->GetEntryLabel());
6845 __ Bind(slow_path->GetExitLabel());
6846 } else {
6847 // GC root loaded through a slow path for read barriers other
6848 // than Baker's.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006849 // /* GcRoot<mirror::Object>* */ root = address
6850 __ leal(root_reg, address);
6851 if (fixup_label != nullptr) {
6852 __ Bind(fixup_label);
6853 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00006854 // /* mirror::Object* */ root = root->Read()
6855 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
6856 }
6857 } else {
6858 // Plain GC root load with no read barrier.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006859 // /* GcRoot<mirror::Object> */ root = *address
6860 __ movl(root_reg, address);
6861 if (fixup_label != nullptr) {
6862 __ Bind(fixup_label);
6863 }
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006864 // Note that GC roots are not affected by heap poisoning, thus we
6865 // do not have to unpoison `root_reg` here.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006866 }
6867}
6868
6869void CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
6870 Location ref,
6871 Register obj,
6872 uint32_t offset,
Roland Levillain7c1559a2015-12-15 10:55:36 +00006873 bool needs_null_check) {
6874 DCHECK(kEmitCompilerReadBarrier);
6875 DCHECK(kUseBakerReadBarrier);
6876
6877 // /* HeapReference<Object> */ ref = *(obj + offset)
6878 Address src(obj, offset);
Vladimir Marko953437b2016-08-24 08:30:46 +00006879 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, needs_null_check);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006880}
6881
6882void CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
6883 Location ref,
6884 Register obj,
6885 uint32_t data_offset,
6886 Location index,
Roland Levillain7c1559a2015-12-15 10:55:36 +00006887 bool needs_null_check) {
6888 DCHECK(kEmitCompilerReadBarrier);
6889 DCHECK(kUseBakerReadBarrier);
6890
Roland Levillain3d312422016-06-23 13:53:42 +01006891 static_assert(
6892 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
6893 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain7c1559a2015-12-15 10:55:36 +00006894 // /* HeapReference<Object> */ ref =
6895 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006896 Address src = CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset);
Vladimir Marko953437b2016-08-24 08:30:46 +00006897 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, needs_null_check);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006898}
6899
6900void CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
6901 Location ref,
6902 Register obj,
6903 const Address& src,
Roland Levillain7c1559a2015-12-15 10:55:36 +00006904 bool needs_null_check) {
6905 DCHECK(kEmitCompilerReadBarrier);
6906 DCHECK(kUseBakerReadBarrier);
6907
6908 // In slow path based read barriers, the read barrier call is
6909 // inserted after the original load. However, in fast path based
6910 // Baker's read barriers, we need to perform the load of
6911 // mirror::Object::monitor_ *before* the original reference load.
6912 // This load-load ordering is required by the read barrier.
6913 // The fast path/slow path (for Baker's algorithm) should look like:
6914 //
6915 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
6916 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
6917 // HeapReference<Object> ref = *src; // Original reference load.
6918 // bool is_gray = (rb_state == ReadBarrier::gray_ptr_);
6919 // if (is_gray) {
6920 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
6921 // }
6922 //
6923 // Note: the original implementation in ReadBarrier::Barrier is
6924 // slightly more complex as:
6925 // - it implements the load-load fence using a data dependency on
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006926 // the high-bits of rb_state, which are expected to be all zeroes
6927 // (we use CodeGeneratorX86::GenerateMemoryBarrier instead here,
6928 // which is a no-op thanks to the x86 memory model);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006929 // - it performs additional checks that we do not do here for
6930 // performance reasons.
6931
6932 Register ref_reg = ref.AsRegister<Register>();
Roland Levillain7c1559a2015-12-15 10:55:36 +00006933 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
6934
Vladimir Marko953437b2016-08-24 08:30:46 +00006935 // Given the numeric representation, it's enough to check the low bit of the rb_state.
6936 static_assert(ReadBarrier::white_ptr_ == 0, "Expecting white to have value 0");
6937 static_assert(ReadBarrier::gray_ptr_ == 1, "Expecting gray to have value 1");
6938 static_assert(ReadBarrier::black_ptr_ == 2, "Expecting black to have value 2");
6939 constexpr uint32_t gray_byte_position = LockWord::kReadBarrierStateShift / kBitsPerByte;
6940 constexpr uint32_t gray_bit_position = LockWord::kReadBarrierStateShift % kBitsPerByte;
6941 constexpr int32_t test_value = static_cast<int8_t>(1 << gray_bit_position);
6942
6943 // if (rb_state == ReadBarrier::gray_ptr_)
6944 // ref = ReadBarrier::Mark(ref);
6945 // At this point, just do the "if" and make sure that flags are preserved until the branch.
6946 __ testb(Address(obj, monitor_offset + gray_byte_position), Immediate(test_value));
Roland Levillain7c1559a2015-12-15 10:55:36 +00006947 if (needs_null_check) {
6948 MaybeRecordImplicitNullCheck(instruction);
6949 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00006950
6951 // Load fence to prevent load-load reordering.
6952 // Note that this is a no-op, thanks to the x86 memory model.
6953 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
6954
6955 // The actual reference load.
6956 // /* HeapReference<Object> */ ref = *src
Vladimir Marko953437b2016-08-24 08:30:46 +00006957 __ movl(ref_reg, src); // Flags are unaffected.
6958
6959 // Note: Reference unpoisoning modifies the flags, so we need to delay it after the branch.
6960 // Slow path marking the object `ref` when it is gray.
6961 SlowPathCode* slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathX86(
6962 instruction, ref, /* unpoison */ true);
6963 AddSlowPath(slow_path);
6964
6965 // We have done the "if" of the gray bit check above, now branch based on the flags.
6966 __ j(kNotZero, slow_path->GetEntryLabel());
Roland Levillain7c1559a2015-12-15 10:55:36 +00006967
6968 // Object* ref = ref_addr->AsMirrorPtr()
6969 __ MaybeUnpoisonHeapReference(ref_reg);
6970
Roland Levillain7c1559a2015-12-15 10:55:36 +00006971 __ Bind(slow_path->GetExitLabel());
6972}
6973
6974void CodeGeneratorX86::GenerateReadBarrierSlow(HInstruction* instruction,
6975 Location out,
6976 Location ref,
6977 Location obj,
6978 uint32_t offset,
6979 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006980 DCHECK(kEmitCompilerReadBarrier);
6981
Roland Levillain7c1559a2015-12-15 10:55:36 +00006982 // Insert a slow path based read barrier *after* the reference load.
6983 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00006984 // If heap poisoning is enabled, the unpoisoning of the loaded
6985 // reference will be carried out by the runtime within the slow
6986 // path.
6987 //
6988 // Note that `ref` currently does not get unpoisoned (when heap
6989 // poisoning is enabled), which is alright as the `ref` argument is
6990 // not used by the artReadBarrierSlow entry point.
6991 //
6992 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
6993 SlowPathCode* slow_path = new (GetGraph()->GetArena())
6994 ReadBarrierForHeapReferenceSlowPathX86(instruction, out, ref, obj, offset, index);
6995 AddSlowPath(slow_path);
6996
Roland Levillain0d5a2812015-11-13 10:07:31 +00006997 __ jmp(slow_path->GetEntryLabel());
6998 __ Bind(slow_path->GetExitLabel());
6999}
7000
Roland Levillain7c1559a2015-12-15 10:55:36 +00007001void CodeGeneratorX86::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
7002 Location out,
7003 Location ref,
7004 Location obj,
7005 uint32_t offset,
7006 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007007 if (kEmitCompilerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007008 // Baker's read barriers shall be handled by the fast path
7009 // (CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier).
7010 DCHECK(!kUseBakerReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007011 // If heap poisoning is enabled, unpoisoning will be taken care of
7012 // by the runtime within the slow path.
Roland Levillain7c1559a2015-12-15 10:55:36 +00007013 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007014 } else if (kPoisonHeapReferences) {
7015 __ UnpoisonHeapReference(out.AsRegister<Register>());
7016 }
7017}
7018
Roland Levillain7c1559a2015-12-15 10:55:36 +00007019void CodeGeneratorX86::GenerateReadBarrierForRootSlow(HInstruction* instruction,
7020 Location out,
7021 Location root) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007022 DCHECK(kEmitCompilerReadBarrier);
7023
Roland Levillain7c1559a2015-12-15 10:55:36 +00007024 // Insert a slow path based read barrier *after* the GC root load.
7025 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00007026 // Note that GC roots are not affected by heap poisoning, so we do
7027 // not need to do anything special for this here.
7028 SlowPathCode* slow_path =
7029 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathX86(instruction, out, root);
7030 AddSlowPath(slow_path);
7031
Roland Levillain0d5a2812015-11-13 10:07:31 +00007032 __ jmp(slow_path->GetEntryLabel());
7033 __ Bind(slow_path->GetExitLabel());
7034}
7035
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007036void LocationsBuilderX86::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
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007041void InstructionCodeGeneratorX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00007042 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00007043 LOG(FATAL) << "Unreachable";
7044}
7045
Mark Mendellfe57faa2015-09-18 09:26:15 -04007046// Simple implementation of packed switch - generate cascaded compare/jumps.
7047void LocationsBuilderX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7048 LocationSummary* locations =
7049 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
7050 locations->SetInAt(0, Location::RequiresRegister());
7051}
7052
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007053void InstructionCodeGeneratorX86::GenPackedSwitchWithCompares(Register value_reg,
7054 int32_t lower_bound,
7055 uint32_t num_entries,
7056 HBasicBlock* switch_block,
7057 HBasicBlock* default_block) {
7058 // Figure out the correct compare values and jump conditions.
7059 // Handle the first compare/branch as a special case because it might
7060 // jump to the default case.
7061 DCHECK_GT(num_entries, 2u);
7062 Condition first_condition;
7063 uint32_t index;
7064 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
7065 if (lower_bound != 0) {
7066 first_condition = kLess;
7067 __ cmpl(value_reg, Immediate(lower_bound));
7068 __ j(first_condition, codegen_->GetLabelOf(default_block));
7069 __ j(kEqual, codegen_->GetLabelOf(successors[0]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007070
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007071 index = 1;
7072 } else {
7073 // Handle all the compare/jumps below.
7074 first_condition = kBelow;
7075 index = 0;
7076 }
7077
7078 // Handle the rest of the compare/jumps.
7079 for (; index + 1 < num_entries; index += 2) {
7080 int32_t compare_to_value = lower_bound + index + 1;
7081 __ cmpl(value_reg, Immediate(compare_to_value));
7082 // Jump to successors[index] if value < case_value[index].
7083 __ j(first_condition, codegen_->GetLabelOf(successors[index]));
7084 // Jump to successors[index + 1] if value == case_value[index + 1].
7085 __ j(kEqual, codegen_->GetLabelOf(successors[index + 1]));
7086 }
7087
7088 if (index != num_entries) {
7089 // There are an odd number of entries. Handle the last one.
7090 DCHECK_EQ(index + 1, num_entries);
7091 __ cmpl(value_reg, Immediate(lower_bound + index));
7092 __ j(kEqual, codegen_->GetLabelOf(successors[index]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007093 }
7094
7095 // And the default for any other value.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007096 if (!codegen_->GoesToNextBlock(switch_block, default_block)) {
7097 __ jmp(codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007098 }
7099}
7100
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007101void InstructionCodeGeneratorX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7102 int32_t lower_bound = switch_instr->GetStartValue();
7103 uint32_t num_entries = switch_instr->GetNumEntries();
7104 LocationSummary* locations = switch_instr->GetLocations();
7105 Register value_reg = locations->InAt(0).AsRegister<Register>();
7106
7107 GenPackedSwitchWithCompares(value_reg,
7108 lower_bound,
7109 num_entries,
7110 switch_instr->GetBlock(),
7111 switch_instr->GetDefaultBlock());
7112}
7113
Mark Mendell805b3b52015-09-18 14:10:29 -04007114void LocationsBuilderX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
7115 LocationSummary* locations =
7116 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
7117 locations->SetInAt(0, Location::RequiresRegister());
7118
7119 // Constant area pointer.
7120 locations->SetInAt(1, Location::RequiresRegister());
7121
7122 // And the temporary we need.
7123 locations->AddTemp(Location::RequiresRegister());
7124}
7125
7126void InstructionCodeGeneratorX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
7127 int32_t lower_bound = switch_instr->GetStartValue();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007128 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendell805b3b52015-09-18 14:10:29 -04007129 LocationSummary* locations = switch_instr->GetLocations();
7130 Register value_reg = locations->InAt(0).AsRegister<Register>();
7131 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
7132
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007133 if (num_entries <= kPackedSwitchJumpTableThreshold) {
7134 GenPackedSwitchWithCompares(value_reg,
7135 lower_bound,
7136 num_entries,
7137 switch_instr->GetBlock(),
7138 default_block);
7139 return;
7140 }
7141
Mark Mendell805b3b52015-09-18 14:10:29 -04007142 // Optimizing has a jump area.
7143 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
7144 Register constant_area = locations->InAt(1).AsRegister<Register>();
7145
7146 // Remove the bias, if needed.
7147 if (lower_bound != 0) {
7148 __ leal(temp_reg, Address(value_reg, -lower_bound));
7149 value_reg = temp_reg;
7150 }
7151
7152 // Is the value in range?
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007153 DCHECK_GE(num_entries, 1u);
Mark Mendell805b3b52015-09-18 14:10:29 -04007154 __ cmpl(value_reg, Immediate(num_entries - 1));
7155 __ j(kAbove, codegen_->GetLabelOf(default_block));
7156
7157 // We are in the range of the table.
7158 // Load (target-constant_area) from the jump table, indexing by the value.
7159 __ movl(temp_reg, codegen_->LiteralCaseTable(switch_instr, constant_area, value_reg));
7160
7161 // Compute the actual target address by adding in constant_area.
7162 __ addl(temp_reg, constant_area);
7163
7164 // And jump.
7165 __ jmp(temp_reg);
7166}
7167
Mark Mendell0616ae02015-04-17 12:49:27 -04007168void LocationsBuilderX86::VisitX86ComputeBaseMethodAddress(
7169 HX86ComputeBaseMethodAddress* insn) {
7170 LocationSummary* locations =
7171 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
7172 locations->SetOut(Location::RequiresRegister());
7173}
7174
7175void InstructionCodeGeneratorX86::VisitX86ComputeBaseMethodAddress(
7176 HX86ComputeBaseMethodAddress* insn) {
7177 LocationSummary* locations = insn->GetLocations();
7178 Register reg = locations->Out().AsRegister<Register>();
7179
7180 // Generate call to next instruction.
7181 Label next_instruction;
7182 __ call(&next_instruction);
7183 __ Bind(&next_instruction);
7184
7185 // Remember this offset for later use with constant area.
7186 codegen_->SetMethodAddressOffset(GetAssembler()->CodeSize());
7187
7188 // Grab the return address off the stack.
7189 __ popl(reg);
7190}
7191
7192void LocationsBuilderX86::VisitX86LoadFromConstantTable(
7193 HX86LoadFromConstantTable* insn) {
7194 LocationSummary* locations =
7195 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
7196
7197 locations->SetInAt(0, Location::RequiresRegister());
7198 locations->SetInAt(1, Location::ConstantLocation(insn->GetConstant()));
7199
7200 // If we don't need to be materialized, we only need the inputs to be set.
David Brazdilb3e773e2016-01-26 11:28:37 +00007201 if (insn->IsEmittedAtUseSite()) {
Mark Mendell0616ae02015-04-17 12:49:27 -04007202 return;
7203 }
7204
7205 switch (insn->GetType()) {
7206 case Primitive::kPrimFloat:
7207 case Primitive::kPrimDouble:
7208 locations->SetOut(Location::RequiresFpuRegister());
7209 break;
7210
7211 case Primitive::kPrimInt:
7212 locations->SetOut(Location::RequiresRegister());
7213 break;
7214
7215 default:
7216 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
7217 }
7218}
7219
7220void InstructionCodeGeneratorX86::VisitX86LoadFromConstantTable(HX86LoadFromConstantTable* insn) {
David Brazdilb3e773e2016-01-26 11:28:37 +00007221 if (insn->IsEmittedAtUseSite()) {
Mark Mendell0616ae02015-04-17 12:49:27 -04007222 return;
7223 }
7224
7225 LocationSummary* locations = insn->GetLocations();
7226 Location out = locations->Out();
7227 Register const_area = locations->InAt(0).AsRegister<Register>();
7228 HConstant *value = insn->GetConstant();
7229
7230 switch (insn->GetType()) {
7231 case Primitive::kPrimFloat:
7232 __ movss(out.AsFpuRegister<XmmRegister>(),
7233 codegen_->LiteralFloatAddress(value->AsFloatConstant()->GetValue(), const_area));
7234 break;
7235
7236 case Primitive::kPrimDouble:
7237 __ movsd(out.AsFpuRegister<XmmRegister>(),
7238 codegen_->LiteralDoubleAddress(value->AsDoubleConstant()->GetValue(), const_area));
7239 break;
7240
7241 case Primitive::kPrimInt:
7242 __ movl(out.AsRegister<Register>(),
7243 codegen_->LiteralInt32Address(value->AsIntConstant()->GetValue(), const_area));
7244 break;
7245
7246 default:
7247 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
7248 }
7249}
7250
Mark Mendell0616ae02015-04-17 12:49:27 -04007251/**
7252 * Class to handle late fixup of offsets into constant area.
7253 */
Vladimir Marko5233f932015-09-29 19:01:15 +01007254class RIPFixup : public AssemblerFixup, public ArenaObject<kArenaAllocCodeGenerator> {
Mark Mendell0616ae02015-04-17 12:49:27 -04007255 public:
Mark Mendell805b3b52015-09-18 14:10:29 -04007256 RIPFixup(CodeGeneratorX86& codegen, size_t offset)
7257 : codegen_(&codegen), offset_into_constant_area_(offset) {}
7258
7259 protected:
7260 void SetOffset(size_t offset) { offset_into_constant_area_ = offset; }
7261
7262 CodeGeneratorX86* codegen_;
Mark Mendell0616ae02015-04-17 12:49:27 -04007263
7264 private:
7265 void Process(const MemoryRegion& region, int pos) OVERRIDE {
7266 // Patch the correct offset for the instruction. The place to patch is the
7267 // last 4 bytes of the instruction.
7268 // The value to patch is the distance from the offset in the constant area
7269 // from the address computed by the HX86ComputeBaseMethodAddress instruction.
Mark Mendell805b3b52015-09-18 14:10:29 -04007270 int32_t constant_offset = codegen_->ConstantAreaStart() + offset_into_constant_area_;
7271 int32_t relative_position = constant_offset - codegen_->GetMethodAddressOffset();;
Mark Mendell0616ae02015-04-17 12:49:27 -04007272
7273 // Patch in the right value.
7274 region.StoreUnaligned<int32_t>(pos - 4, relative_position);
7275 }
7276
Mark Mendell0616ae02015-04-17 12:49:27 -04007277 // Location in constant area that the fixup refers to.
Mark Mendell805b3b52015-09-18 14:10:29 -04007278 int32_t offset_into_constant_area_;
Mark Mendell0616ae02015-04-17 12:49:27 -04007279};
7280
Mark Mendell805b3b52015-09-18 14:10:29 -04007281/**
7282 * Class to handle late fixup of offsets to a jump table that will be created in the
7283 * constant area.
7284 */
7285class JumpTableRIPFixup : public RIPFixup {
7286 public:
7287 JumpTableRIPFixup(CodeGeneratorX86& codegen, HX86PackedSwitch* switch_instr)
7288 : RIPFixup(codegen, static_cast<size_t>(-1)), switch_instr_(switch_instr) {}
7289
7290 void CreateJumpTable() {
7291 X86Assembler* assembler = codegen_->GetAssembler();
7292
7293 // Ensure that the reference to the jump table has the correct offset.
7294 const int32_t offset_in_constant_table = assembler->ConstantAreaSize();
7295 SetOffset(offset_in_constant_table);
7296
7297 // The label values in the jump table are computed relative to the
7298 // instruction addressing the constant area.
7299 const int32_t relative_offset = codegen_->GetMethodAddressOffset();
7300
7301 // Populate the jump table with the correct values for the jump table.
7302 int32_t num_entries = switch_instr_->GetNumEntries();
7303 HBasicBlock* block = switch_instr_->GetBlock();
7304 const ArenaVector<HBasicBlock*>& successors = block->GetSuccessors();
7305 // The value that we want is the target offset - the position of the table.
7306 for (int32_t i = 0; i < num_entries; i++) {
7307 HBasicBlock* b = successors[i];
7308 Label* l = codegen_->GetLabelOf(b);
7309 DCHECK(l->IsBound());
7310 int32_t offset_to_block = l->Position() - relative_offset;
7311 assembler->AppendInt32(offset_to_block);
7312 }
7313 }
7314
7315 private:
7316 const HX86PackedSwitch* switch_instr_;
7317};
7318
7319void CodeGeneratorX86::Finalize(CodeAllocator* allocator) {
7320 // Generate the constant area if needed.
7321 X86Assembler* assembler = GetAssembler();
7322 if (!assembler->IsConstantAreaEmpty() || !fixups_to_jump_tables_.empty()) {
7323 // Align to 4 byte boundary to reduce cache misses, as the data is 4 and 8
7324 // byte values.
7325 assembler->Align(4, 0);
7326 constant_area_start_ = assembler->CodeSize();
7327
7328 // Populate any jump tables.
7329 for (auto jump_table : fixups_to_jump_tables_) {
7330 jump_table->CreateJumpTable();
7331 }
7332
7333 // And now add the constant area to the generated code.
7334 assembler->AddConstantArea();
7335 }
7336
7337 // And finish up.
7338 CodeGenerator::Finalize(allocator);
7339}
7340
Mark Mendell0616ae02015-04-17 12:49:27 -04007341Address CodeGeneratorX86::LiteralDoubleAddress(double v, Register reg) {
7342 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddDouble(v));
7343 return Address(reg, kDummy32BitOffset, fixup);
7344}
7345
7346Address CodeGeneratorX86::LiteralFloatAddress(float v, Register reg) {
7347 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddFloat(v));
7348 return Address(reg, kDummy32BitOffset, fixup);
7349}
7350
7351Address CodeGeneratorX86::LiteralInt32Address(int32_t v, Register reg) {
7352 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt32(v));
7353 return Address(reg, kDummy32BitOffset, fixup);
7354}
7355
7356Address CodeGeneratorX86::LiteralInt64Address(int64_t v, Register reg) {
7357 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt64(v));
7358 return Address(reg, kDummy32BitOffset, fixup);
7359}
7360
Aart Bika19616e2016-02-01 18:57:58 -08007361void CodeGeneratorX86::Load32BitValue(Register dest, int32_t value) {
7362 if (value == 0) {
7363 __ xorl(dest, dest);
7364 } else {
7365 __ movl(dest, Immediate(value));
7366 }
7367}
7368
7369void CodeGeneratorX86::Compare32BitValue(Register dest, int32_t value) {
7370 if (value == 0) {
7371 __ testl(dest, dest);
7372 } else {
7373 __ cmpl(dest, Immediate(value));
7374 }
7375}
7376
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007377void CodeGeneratorX86::GenerateIntCompare(Location lhs, Location rhs) {
7378 Register lhs_reg = lhs.AsRegister<Register>();
jessicahandojo4877b792016-09-08 19:49:13 -07007379 GenerateIntCompare(lhs_reg, rhs);
7380}
7381
7382void CodeGeneratorX86::GenerateIntCompare(Register lhs, Location rhs) {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007383 if (rhs.IsConstant()) {
7384 int32_t value = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
jessicahandojo4877b792016-09-08 19:49:13 -07007385 Compare32BitValue(lhs, value);
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007386 } else if (rhs.IsStackSlot()) {
jessicahandojo4877b792016-09-08 19:49:13 -07007387 __ cmpl(lhs, Address(ESP, rhs.GetStackIndex()));
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007388 } else {
jessicahandojo4877b792016-09-08 19:49:13 -07007389 __ cmpl(lhs, rhs.AsRegister<Register>());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007390 }
7391}
7392
7393Address CodeGeneratorX86::ArrayAddress(Register obj,
7394 Location index,
7395 ScaleFactor scale,
7396 uint32_t data_offset) {
7397 return index.IsConstant() ?
7398 Address(obj, (index.GetConstant()->AsIntConstant()->GetValue() << scale) + data_offset) :
7399 Address(obj, index.AsRegister<Register>(), scale, data_offset);
7400}
7401
Mark Mendell805b3b52015-09-18 14:10:29 -04007402Address CodeGeneratorX86::LiteralCaseTable(HX86PackedSwitch* switch_instr,
7403 Register reg,
7404 Register value) {
7405 // Create a fixup to be used to create and address the jump table.
7406 JumpTableRIPFixup* table_fixup =
7407 new (GetGraph()->GetArena()) JumpTableRIPFixup(*this, switch_instr);
7408
7409 // We have to populate the jump tables.
7410 fixups_to_jump_tables_.push_back(table_fixup);
7411
7412 // We want a scaled address, as we are extracting the correct offset from the table.
7413 return Address(reg, value, TIMES_4, kDummy32BitOffset, table_fixup);
7414}
7415
Andreas Gampe85b62f22015-09-09 13:15:38 -07007416// TODO: target as memory.
7417void CodeGeneratorX86::MoveFromReturnRegister(Location target, Primitive::Type type) {
7418 if (!target.IsValid()) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007419 DCHECK_EQ(type, Primitive::kPrimVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07007420 return;
7421 }
7422
7423 DCHECK_NE(type, Primitive::kPrimVoid);
7424
7425 Location return_loc = InvokeDexCallingConventionVisitorX86().GetReturnLocation(type);
7426 if (target.Equals(return_loc)) {
7427 return;
7428 }
7429
7430 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
7431 // with the else branch.
7432 if (type == Primitive::kPrimLong) {
7433 HParallelMove parallel_move(GetGraph()->GetArena());
7434 parallel_move.AddMove(return_loc.ToLow(), target.ToLow(), Primitive::kPrimInt, nullptr);
7435 parallel_move.AddMove(return_loc.ToHigh(), target.ToHigh(), Primitive::kPrimInt, nullptr);
7436 GetMoveResolver()->EmitNativeCode(&parallel_move);
7437 } else {
7438 // Let the parallel move resolver take care of all of this.
7439 HParallelMove parallel_move(GetGraph()->GetArena());
7440 parallel_move.AddMove(return_loc, target, type, nullptr);
7441 GetMoveResolver()->EmitNativeCode(&parallel_move);
7442 }
7443}
7444
Roland Levillain4d027112015-07-01 15:41:14 +01007445#undef __
7446
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00007447} // namespace x86
7448} // namespace art