blob: d93001630b7c45e092fbbd09e75f47b29a830b14 [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 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100845 blocked_core_registers_[ESP] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100846}
847
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100848InstructionCodeGeneratorX86::InstructionCodeGeneratorX86(HGraph* graph, CodeGeneratorX86* codegen)
Aart Bik42249c32016-01-07 15:33:50 -0800849 : InstructionCodeGenerator(graph, codegen),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100850 assembler_(codegen->GetAssembler()),
851 codegen_(codegen) {}
852
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100853static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +0100854 return dwarf::Reg::X86Core(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100855}
856
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000857void CodeGeneratorX86::GenerateFrameEntry() {
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100858 __ cfi().SetCurrentCFAOffset(kX86WordSize); // return address
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000859 __ Bind(&frame_entry_label_);
Roland Levillain199f3362014-11-27 17:15:16 +0000860 bool skip_overflow_check =
861 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000862 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Calin Juravle93edf732015-01-20 20:14:07 +0000863
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000864 if (!skip_overflow_check) {
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100865 __ testl(EAX, Address(ESP, -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86))));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100866 RecordPcInfo(nullptr, 0);
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100867 }
868
Mark Mendell5f874182015-03-04 15:42:45 -0500869 if (HasEmptyFrame()) {
870 return;
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000871 }
Mark Mendell5f874182015-03-04 15:42:45 -0500872
873 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
874 Register reg = kCoreCalleeSaves[i];
875 if (allocated_registers_.ContainsCoreRegister(reg)) {
876 __ pushl(reg);
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100877 __ cfi().AdjustCFAOffset(kX86WordSize);
878 __ cfi().RelOffset(DWARFReg(reg), 0);
Mark Mendell5f874182015-03-04 15:42:45 -0500879 }
880 }
881
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100882 int adjust = GetFrameSize() - FrameEntrySpillSize();
883 __ subl(ESP, Immediate(adjust));
884 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +0100885 // Save the current method if we need it. Note that we do not
886 // do this in HCurrentMethod, as the instruction might have been removed
887 // in the SSA graph.
888 if (RequiresCurrentMethod()) {
889 __ movl(Address(ESP, kCurrentMethodStackOffset), kMethodRegisterArgument);
890 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000891}
892
893void CodeGeneratorX86::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +0100894 __ cfi().RememberState();
895 if (!HasEmptyFrame()) {
896 int adjust = GetFrameSize() - FrameEntrySpillSize();
897 __ addl(ESP, Immediate(adjust));
898 __ cfi().AdjustCFAOffset(-adjust);
Mark Mendell5f874182015-03-04 15:42:45 -0500899
David Srbeckyc34dc932015-04-12 09:27:43 +0100900 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
901 Register reg = kCoreCalleeSaves[i];
902 if (allocated_registers_.ContainsCoreRegister(reg)) {
903 __ popl(reg);
904 __ cfi().AdjustCFAOffset(-static_cast<int>(kX86WordSize));
905 __ cfi().Restore(DWARFReg(reg));
906 }
Mark Mendell5f874182015-03-04 15:42:45 -0500907 }
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000908 }
David Srbeckyc34dc932015-04-12 09:27:43 +0100909 __ ret();
910 __ cfi().RestoreState();
911 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000912}
913
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100914void CodeGeneratorX86::Bind(HBasicBlock* block) {
915 __ Bind(GetLabelOf(block));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000916}
917
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100918Location InvokeDexCallingConventionVisitorX86::GetReturnLocation(Primitive::Type type) const {
919 switch (type) {
920 case Primitive::kPrimBoolean:
921 case Primitive::kPrimByte:
922 case Primitive::kPrimChar:
923 case Primitive::kPrimShort:
924 case Primitive::kPrimInt:
925 case Primitive::kPrimNot:
926 return Location::RegisterLocation(EAX);
927
928 case Primitive::kPrimLong:
929 return Location::RegisterPairLocation(EAX, EDX);
930
931 case Primitive::kPrimVoid:
932 return Location::NoLocation();
933
934 case Primitive::kPrimDouble:
935 case Primitive::kPrimFloat:
936 return Location::FpuRegisterLocation(XMM0);
937 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +0100938
939 UNREACHABLE();
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100940}
941
942Location InvokeDexCallingConventionVisitorX86::GetMethodLocation() const {
943 return Location::RegisterLocation(kMethodRegisterArgument);
944}
945
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100946Location InvokeDexCallingConventionVisitorX86::GetNextLocation(Primitive::Type type) {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100947 switch (type) {
948 case Primitive::kPrimBoolean:
949 case Primitive::kPrimByte:
950 case Primitive::kPrimChar:
951 case Primitive::kPrimShort:
952 case Primitive::kPrimInt:
953 case Primitive::kPrimNot: {
954 uint32_t index = gp_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000955 stack_index_++;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100956 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100957 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100958 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000959 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100960 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100961 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100962
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000963 case Primitive::kPrimLong: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100964 uint32_t index = gp_index_;
965 gp_index_ += 2;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000966 stack_index_ += 2;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100967 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100968 X86ManagedRegister pair = X86ManagedRegister::FromRegisterPair(
969 calling_convention.GetRegisterPairAt(index));
970 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100971 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000972 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
973 }
974 }
975
976 case Primitive::kPrimFloat: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100977 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000978 stack_index_++;
979 if (index < calling_convention.GetNumberOfFpuRegisters()) {
980 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
981 } else {
982 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
983 }
984 }
985
986 case Primitive::kPrimDouble: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100987 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000988 stack_index_ += 2;
989 if (index < calling_convention.GetNumberOfFpuRegisters()) {
990 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
991 } else {
992 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100993 }
994 }
995
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100996 case Primitive::kPrimVoid:
997 LOG(FATAL) << "Unexpected parameter type " << type;
998 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100999 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00001000 return Location::NoLocation();
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001001}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001002
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001003void CodeGeneratorX86::Move32(Location destination, Location source) {
1004 if (source.Equals(destination)) {
1005 return;
1006 }
1007 if (destination.IsRegister()) {
1008 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001009 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001010 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001011 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001012 } else {
1013 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001014 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001015 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001016 } else if (destination.IsFpuRegister()) {
1017 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001018 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001019 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001020 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001021 } else {
1022 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001023 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001024 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001025 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001026 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001027 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001028 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001029 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001030 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Mark Mendell7c8d0092015-01-26 11:21:33 -05001031 } else if (source.IsConstant()) {
1032 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001033 int32_t value = GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05001034 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001035 } else {
1036 DCHECK(source.IsStackSlot());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001037 __ pushl(Address(ESP, source.GetStackIndex()));
1038 __ popl(Address(ESP, destination.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001039 }
1040 }
1041}
1042
1043void CodeGeneratorX86::Move64(Location destination, Location source) {
1044 if (source.Equals(destination)) {
1045 return;
1046 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001047 if (destination.IsRegisterPair()) {
1048 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001049 EmitParallelMoves(
1050 Location::RegisterLocation(source.AsRegisterPairHigh<Register>()),
1051 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001052 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001053 Location::RegisterLocation(source.AsRegisterPairLow<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001054 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()),
1055 Primitive::kPrimInt);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001056 } else if (source.IsFpuRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001057 XmmRegister src_reg = source.AsFpuRegister<XmmRegister>();
1058 __ movd(destination.AsRegisterPairLow<Register>(), src_reg);
1059 __ psrlq(src_reg, Immediate(32));
1060 __ movd(destination.AsRegisterPairHigh<Register>(), src_reg);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001061 } else {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001062 // No conflict possible, so just do the moves.
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001063 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001064 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
1065 __ movl(destination.AsRegisterPairHigh<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001066 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
1067 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001068 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05001069 if (source.IsFpuRegister()) {
1070 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
1071 } else if (source.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001072 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01001073 } else if (source.IsRegisterPair()) {
1074 size_t elem_size = Primitive::ComponentSize(Primitive::kPrimInt);
1075 // Create stack space for 2 elements.
1076 __ subl(ESP, Immediate(2 * elem_size));
1077 __ movl(Address(ESP, 0), source.AsRegisterPairLow<Register>());
1078 __ movl(Address(ESP, elem_size), source.AsRegisterPairHigh<Register>());
1079 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
1080 // And remove the temporary stack space we allocated.
1081 __ addl(ESP, Immediate(2 * elem_size));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001082 } else {
1083 LOG(FATAL) << "Unimplemented";
1084 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001085 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001086 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001087 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001088 // No conflict possible, so just do the moves.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001089 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegisterPairLow<Register>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001090 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001091 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001092 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001093 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001094 } else if (source.IsConstant()) {
1095 HConstant* constant = source.GetConstant();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001096 DCHECK(constant->IsLongConstant() || constant->IsDoubleConstant());
1097 int64_t value = GetInt64ValueOf(constant);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001098 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(Low32Bits(value)));
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001099 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
1100 Immediate(High32Bits(value)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001101 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001102 DCHECK(source.IsDoubleStackSlot()) << source;
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001103 EmitParallelMoves(
1104 Location::StackSlot(source.GetStackIndex()),
1105 Location::StackSlot(destination.GetStackIndex()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001106 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001107 Location::StackSlot(source.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001108 Location::StackSlot(destination.GetHighStackIndex(kX86WordSize)),
1109 Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001110 }
1111 }
1112}
1113
Calin Juravle175dc732015-08-25 15:42:32 +01001114void CodeGeneratorX86::MoveConstant(Location location, int32_t value) {
1115 DCHECK(location.IsRegister());
1116 __ movl(location.AsRegister<Register>(), Immediate(value));
1117}
1118
Calin Juravlee460d1d2015-09-29 04:52:17 +01001119void CodeGeneratorX86::MoveLocation(Location dst, Location src, Primitive::Type dst_type) {
David Brazdil74eb1b22015-12-14 11:44:01 +00001120 HParallelMove move(GetGraph()->GetArena());
1121 if (dst_type == Primitive::kPrimLong && !src.IsConstant() && !src.IsFpuRegister()) {
1122 move.AddMove(src.ToLow(), dst.ToLow(), Primitive::kPrimInt, nullptr);
1123 move.AddMove(src.ToHigh(), dst.ToHigh(), Primitive::kPrimInt, nullptr);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001124 } else {
David Brazdil74eb1b22015-12-14 11:44:01 +00001125 move.AddMove(src, dst, dst_type, nullptr);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001126 }
David Brazdil74eb1b22015-12-14 11:44:01 +00001127 GetMoveResolver()->EmitNativeCode(&move);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001128}
1129
1130void CodeGeneratorX86::AddLocationAsTemp(Location location, LocationSummary* locations) {
1131 if (location.IsRegister()) {
1132 locations->AddTemp(location);
1133 } else if (location.IsRegisterPair()) {
1134 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
1135 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
1136 } else {
1137 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1138 }
1139}
1140
David Brazdilfc6a86a2015-06-26 10:33:45 +00001141void InstructionCodeGeneratorX86::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001142 DCHECK(!successor->IsExitBlock());
1143
1144 HBasicBlock* block = got->GetBlock();
1145 HInstruction* previous = got->GetPrevious();
1146
1147 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +00001148 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001149 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1150 return;
1151 }
1152
1153 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1154 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1155 }
1156 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001157 __ jmp(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001158 }
1159}
1160
David Brazdilfc6a86a2015-06-26 10:33:45 +00001161void LocationsBuilderX86::VisitGoto(HGoto* got) {
1162 got->SetLocations(nullptr);
1163}
1164
1165void InstructionCodeGeneratorX86::VisitGoto(HGoto* got) {
1166 HandleGoto(got, got->GetSuccessor());
1167}
1168
1169void LocationsBuilderX86::VisitTryBoundary(HTryBoundary* try_boundary) {
1170 try_boundary->SetLocations(nullptr);
1171}
1172
1173void InstructionCodeGeneratorX86::VisitTryBoundary(HTryBoundary* try_boundary) {
1174 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
1175 if (!successor->IsExitBlock()) {
1176 HandleGoto(try_boundary, successor);
1177 }
1178}
1179
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001180void LocationsBuilderX86::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001181 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001182}
1183
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001184void InstructionCodeGeneratorX86::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001185}
1186
Mark Mendell152408f2015-12-31 12:28:50 -05001187template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001188void InstructionCodeGeneratorX86::GenerateFPJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001189 LabelType* true_label,
1190 LabelType* false_label) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001191 if (cond->IsFPConditionTrueIfNaN()) {
1192 __ j(kUnordered, true_label);
1193 } else if (cond->IsFPConditionFalseIfNaN()) {
1194 __ j(kUnordered, false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001195 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001196 __ j(X86UnsignedOrFPCondition(cond->GetCondition()), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001197}
1198
Mark Mendell152408f2015-12-31 12:28:50 -05001199template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001200void InstructionCodeGeneratorX86::GenerateLongComparesAndJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001201 LabelType* true_label,
1202 LabelType* false_label) {
Mark Mendellc4701932015-04-10 13:18:51 -04001203 LocationSummary* locations = cond->GetLocations();
1204 Location left = locations->InAt(0);
1205 Location right = locations->InAt(1);
1206 IfCondition if_cond = cond->GetCondition();
1207
Mark Mendellc4701932015-04-10 13:18:51 -04001208 Register left_high = left.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001209 Register left_low = left.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001210 IfCondition true_high_cond = if_cond;
1211 IfCondition false_high_cond = cond->GetOppositeCondition();
Aart Bike9f37602015-10-09 11:15:55 -07001212 Condition final_condition = X86UnsignedOrFPCondition(if_cond); // unsigned on lower part
Mark Mendellc4701932015-04-10 13:18:51 -04001213
1214 // Set the conditions for the test, remembering that == needs to be
1215 // decided using the low words.
1216 switch (if_cond) {
1217 case kCondEQ:
Mark Mendellc4701932015-04-10 13:18:51 -04001218 case kCondNE:
Roland Levillain4fa13f62015-07-06 18:11:54 +01001219 // Nothing to do.
Mark Mendellc4701932015-04-10 13:18:51 -04001220 break;
1221 case kCondLT:
1222 false_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -04001223 break;
1224 case kCondLE:
1225 true_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -04001226 break;
1227 case kCondGT:
1228 false_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -04001229 break;
1230 case kCondGE:
1231 true_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -04001232 break;
Aart Bike9f37602015-10-09 11:15:55 -07001233 case kCondB:
1234 false_high_cond = kCondA;
1235 break;
1236 case kCondBE:
1237 true_high_cond = kCondB;
1238 break;
1239 case kCondA:
1240 false_high_cond = kCondB;
1241 break;
1242 case kCondAE:
1243 true_high_cond = kCondA;
1244 break;
Mark Mendellc4701932015-04-10 13:18:51 -04001245 }
1246
1247 if (right.IsConstant()) {
1248 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
Mark Mendellc4701932015-04-10 13:18:51 -04001249 int32_t val_high = High32Bits(value);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001250 int32_t val_low = Low32Bits(value);
Mark Mendellc4701932015-04-10 13:18:51 -04001251
Aart Bika19616e2016-02-01 18:57:58 -08001252 codegen_->Compare32BitValue(left_high, val_high);
Mark Mendellc4701932015-04-10 13:18:51 -04001253 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001254 __ j(X86Condition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001255 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001256 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001257 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001258 __ j(X86Condition(true_high_cond), true_label);
1259 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001260 }
1261 // Must be equal high, so compare the lows.
Aart Bika19616e2016-02-01 18:57:58 -08001262 codegen_->Compare32BitValue(left_low, val_low);
Mark Mendell8659e842016-02-16 10:41:46 -05001263 } else if (right.IsRegisterPair()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001264 Register right_high = right.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001265 Register right_low = right.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001266
1267 __ cmpl(left_high, right_high);
1268 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001269 __ j(X86Condition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001270 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001271 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001272 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001273 __ j(X86Condition(true_high_cond), true_label);
1274 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001275 }
1276 // Must be equal high, so compare the lows.
1277 __ cmpl(left_low, right_low);
Mark Mendell8659e842016-02-16 10:41:46 -05001278 } else {
1279 DCHECK(right.IsDoubleStackSlot());
1280 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
1281 if (if_cond == kCondNE) {
1282 __ j(X86Condition(true_high_cond), true_label);
1283 } else if (if_cond == kCondEQ) {
1284 __ j(X86Condition(false_high_cond), false_label);
1285 } else {
1286 __ j(X86Condition(true_high_cond), true_label);
1287 __ j(X86Condition(false_high_cond), false_label);
1288 }
1289 // Must be equal high, so compare the lows.
1290 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Mark Mendellc4701932015-04-10 13:18:51 -04001291 }
1292 // The last comparison might be unsigned.
1293 __ j(final_condition, true_label);
1294}
1295
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001296void InstructionCodeGeneratorX86::GenerateFPCompare(Location lhs,
1297 Location rhs,
1298 HInstruction* insn,
1299 bool is_double) {
1300 HX86LoadFromConstantTable* const_area = insn->InputAt(1)->AsX86LoadFromConstantTable();
1301 if (is_double) {
1302 if (rhs.IsFpuRegister()) {
1303 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1304 } else if (const_area != nullptr) {
1305 DCHECK(const_area->IsEmittedAtUseSite());
1306 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(),
1307 codegen_->LiteralDoubleAddress(
1308 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
1309 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
1310 } else {
1311 DCHECK(rhs.IsDoubleStackSlot());
1312 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(), Address(ESP, rhs.GetStackIndex()));
1313 }
1314 } else {
1315 if (rhs.IsFpuRegister()) {
1316 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1317 } else if (const_area != nullptr) {
1318 DCHECK(const_area->IsEmittedAtUseSite());
1319 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(),
1320 codegen_->LiteralFloatAddress(
1321 const_area->GetConstant()->AsFloatConstant()->GetValue(),
1322 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
1323 } else {
1324 DCHECK(rhs.IsStackSlot());
1325 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(), Address(ESP, rhs.GetStackIndex()));
1326 }
1327 }
1328}
1329
Mark Mendell152408f2015-12-31 12:28:50 -05001330template<class LabelType>
David Brazdil0debae72015-11-12 18:37:00 +00001331void InstructionCodeGeneratorX86::GenerateCompareTestAndBranch(HCondition* condition,
Mark Mendell152408f2015-12-31 12:28:50 -05001332 LabelType* true_target_in,
1333 LabelType* false_target_in) {
David Brazdil0debae72015-11-12 18:37:00 +00001334 // Generated branching requires both targets to be explicit. If either of the
1335 // targets is nullptr (fallthrough) use and bind `fallthrough_target` instead.
Mark Mendell152408f2015-12-31 12:28:50 -05001336 LabelType fallthrough_target;
1337 LabelType* true_target = true_target_in == nullptr ? &fallthrough_target : true_target_in;
1338 LabelType* false_target = false_target_in == nullptr ? &fallthrough_target : false_target_in;
David Brazdil0debae72015-11-12 18:37:00 +00001339
Mark Mendellc4701932015-04-10 13:18:51 -04001340 LocationSummary* locations = condition->GetLocations();
1341 Location left = locations->InAt(0);
1342 Location right = locations->InAt(1);
1343
Mark Mendellc4701932015-04-10 13:18:51 -04001344 Primitive::Type type = condition->InputAt(0)->GetType();
1345 switch (type) {
1346 case Primitive::kPrimLong:
1347 GenerateLongComparesAndJumps(condition, true_target, false_target);
1348 break;
1349 case Primitive::kPrimFloat:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001350 GenerateFPCompare(left, right, condition, false);
Mark Mendellc4701932015-04-10 13:18:51 -04001351 GenerateFPJumps(condition, true_target, false_target);
1352 break;
1353 case Primitive::kPrimDouble:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001354 GenerateFPCompare(left, right, condition, true);
Mark Mendellc4701932015-04-10 13:18:51 -04001355 GenerateFPJumps(condition, true_target, false_target);
1356 break;
1357 default:
1358 LOG(FATAL) << "Unexpected compare type " << type;
1359 }
1360
David Brazdil0debae72015-11-12 18:37:00 +00001361 if (false_target != &fallthrough_target) {
Mark Mendellc4701932015-04-10 13:18:51 -04001362 __ jmp(false_target);
1363 }
David Brazdil0debae72015-11-12 18:37:00 +00001364
1365 if (fallthrough_target.IsLinked()) {
1366 __ Bind(&fallthrough_target);
1367 }
Mark Mendellc4701932015-04-10 13:18:51 -04001368}
1369
David Brazdil0debae72015-11-12 18:37:00 +00001370static bool AreEflagsSetFrom(HInstruction* cond, HInstruction* branch) {
1371 // Moves may affect the eflags register (move zero uses xorl), so the EFLAGS
1372 // are set only strictly before `branch`. We can't use the eflags on long/FP
1373 // conditions if they are materialized due to the complex branching.
1374 return cond->IsCondition() &&
1375 cond->GetNext() == branch &&
1376 cond->InputAt(0)->GetType() != Primitive::kPrimLong &&
1377 !Primitive::IsFloatingPointType(cond->InputAt(0)->GetType());
1378}
1379
Mark Mendell152408f2015-12-31 12:28:50 -05001380template<class LabelType>
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001381void InstructionCodeGeneratorX86::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00001382 size_t condition_input_index,
Mark Mendell152408f2015-12-31 12:28:50 -05001383 LabelType* true_target,
1384 LabelType* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00001385 HInstruction* cond = instruction->InputAt(condition_input_index);
1386
1387 if (true_target == nullptr && false_target == nullptr) {
1388 // Nothing to do. The code always falls through.
1389 return;
1390 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00001391 // Constant condition, statically compared against "true" (integer value 1).
1392 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00001393 if (true_target != nullptr) {
1394 __ jmp(true_target);
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001395 }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001396 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00001397 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00001398 if (false_target != nullptr) {
1399 __ jmp(false_target);
1400 }
1401 }
1402 return;
1403 }
1404
1405 // The following code generates these patterns:
1406 // (1) true_target == nullptr && false_target != nullptr
1407 // - opposite condition true => branch to false_target
1408 // (2) true_target != nullptr && false_target == nullptr
1409 // - condition true => branch to true_target
1410 // (3) true_target != nullptr && false_target != nullptr
1411 // - condition true => branch to true_target
1412 // - branch to false_target
1413 if (IsBooleanValueOrMaterializedCondition(cond)) {
1414 if (AreEflagsSetFrom(cond, instruction)) {
1415 if (true_target == nullptr) {
1416 __ j(X86Condition(cond->AsCondition()->GetOppositeCondition()), false_target);
1417 } else {
1418 __ j(X86Condition(cond->AsCondition()->GetCondition()), true_target);
1419 }
1420 } else {
1421 // Materialized condition, compare against 0.
1422 Location lhs = instruction->GetLocations()->InAt(condition_input_index);
1423 if (lhs.IsRegister()) {
1424 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
1425 } else {
1426 __ cmpl(Address(ESP, lhs.GetStackIndex()), Immediate(0));
1427 }
1428 if (true_target == nullptr) {
1429 __ j(kEqual, false_target);
1430 } else {
1431 __ j(kNotEqual, true_target);
1432 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001433 }
1434 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001435 // Condition has not been materialized, use its inputs as the comparison and
1436 // its condition as the branch condition.
Mark Mendellb8b97692015-05-22 16:58:19 -04001437 HCondition* condition = cond->AsCondition();
David Brazdil0debae72015-11-12 18:37:00 +00001438
1439 // If this is a long or FP comparison that has been folded into
1440 // the HCondition, generate the comparison directly.
1441 Primitive::Type type = condition->InputAt(0)->GetType();
1442 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
1443 GenerateCompareTestAndBranch(condition, true_target, false_target);
1444 return;
1445 }
1446
1447 Location lhs = condition->GetLocations()->InAt(0);
1448 Location rhs = condition->GetLocations()->InAt(1);
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001449 // LHS is guaranteed to be in a register (see LocationsBuilderX86::HandleCondition).
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001450 codegen_->GenerateIntCompare(lhs, rhs);
David Brazdil0debae72015-11-12 18:37:00 +00001451 if (true_target == nullptr) {
1452 __ j(X86Condition(condition->GetOppositeCondition()), false_target);
1453 } else {
Mark Mendellb8b97692015-05-22 16:58:19 -04001454 __ j(X86Condition(condition->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -07001455 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001456 }
David Brazdil0debae72015-11-12 18:37:00 +00001457
1458 // If neither branch falls through (case 3), the conditional branch to `true_target`
1459 // was already emitted (case 2) and we need to emit a jump to `false_target`.
1460 if (true_target != nullptr && false_target != nullptr) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001461 __ jmp(false_target);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001462 }
1463}
1464
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001465void LocationsBuilderX86::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001466 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
1467 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001468 locations->SetInAt(0, Location::Any());
1469 }
1470}
1471
1472void InstructionCodeGeneratorX86::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001473 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
1474 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
1475 Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
1476 nullptr : codegen_->GetLabelOf(true_successor);
1477 Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
1478 nullptr : codegen_->GetLabelOf(false_successor);
1479 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001480}
1481
1482void LocationsBuilderX86::VisitDeoptimize(HDeoptimize* deoptimize) {
1483 LocationSummary* locations = new (GetGraph()->GetArena())
1484 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01001485 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
David Brazdil0debae72015-11-12 18:37:00 +00001486 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001487 locations->SetInAt(0, Location::Any());
1488 }
1489}
1490
1491void InstructionCodeGeneratorX86::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08001492 SlowPathCode* slow_path = deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathX86>(deoptimize);
David Brazdil74eb1b22015-12-14 11:44:01 +00001493 GenerateTestAndBranch<Label>(deoptimize,
1494 /* condition_input_index */ 0,
1495 slow_path->GetEntryLabel(),
1496 /* false_target */ nullptr);
1497}
1498
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001499static bool SelectCanUseCMOV(HSelect* select) {
1500 // There are no conditional move instructions for XMMs.
1501 if (Primitive::IsFloatingPointType(select->GetType())) {
1502 return false;
1503 }
1504
1505 // A FP condition doesn't generate the single CC that we need.
1506 // In 32 bit mode, a long condition doesn't generate a single CC either.
1507 HInstruction* condition = select->GetCondition();
1508 if (condition->IsCondition()) {
1509 Primitive::Type compare_type = condition->InputAt(0)->GetType();
1510 if (compare_type == Primitive::kPrimLong ||
1511 Primitive::IsFloatingPointType(compare_type)) {
1512 return false;
1513 }
1514 }
1515
1516 // We can generate a CMOV for this Select.
1517 return true;
1518}
1519
David Brazdil74eb1b22015-12-14 11:44:01 +00001520void LocationsBuilderX86::VisitSelect(HSelect* select) {
1521 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001522 if (Primitive::IsFloatingPointType(select->GetType())) {
David Brazdil74eb1b22015-12-14 11:44:01 +00001523 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001524 locations->SetInAt(1, Location::Any());
David Brazdil74eb1b22015-12-14 11:44:01 +00001525 } else {
1526 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001527 if (SelectCanUseCMOV(select)) {
1528 if (select->InputAt(1)->IsConstant()) {
1529 // Cmov can't handle a constant value.
1530 locations->SetInAt(1, Location::RequiresRegister());
1531 } else {
1532 locations->SetInAt(1, Location::Any());
1533 }
1534 } else {
1535 locations->SetInAt(1, Location::Any());
1536 }
David Brazdil74eb1b22015-12-14 11:44:01 +00001537 }
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001538 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
1539 locations->SetInAt(2, Location::RequiresRegister());
David Brazdil74eb1b22015-12-14 11:44:01 +00001540 }
1541 locations->SetOut(Location::SameAsFirstInput());
1542}
1543
1544void InstructionCodeGeneratorX86::VisitSelect(HSelect* select) {
1545 LocationSummary* locations = select->GetLocations();
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001546 DCHECK(locations->InAt(0).Equals(locations->Out()));
1547 if (SelectCanUseCMOV(select)) {
1548 // If both the condition and the source types are integer, we can generate
1549 // a CMOV to implement Select.
1550
1551 HInstruction* select_condition = select->GetCondition();
1552 Condition cond = kNotEqual;
1553
1554 // Figure out how to test the 'condition'.
1555 if (select_condition->IsCondition()) {
1556 HCondition* condition = select_condition->AsCondition();
1557 if (!condition->IsEmittedAtUseSite()) {
1558 // This was a previously materialized condition.
1559 // Can we use the existing condition code?
1560 if (AreEflagsSetFrom(condition, select)) {
1561 // Materialization was the previous instruction. Condition codes are right.
1562 cond = X86Condition(condition->GetCondition());
1563 } else {
1564 // No, we have to recreate the condition code.
1565 Register cond_reg = locations->InAt(2).AsRegister<Register>();
1566 __ testl(cond_reg, cond_reg);
1567 }
1568 } else {
1569 // We can't handle FP or long here.
1570 DCHECK_NE(condition->InputAt(0)->GetType(), Primitive::kPrimLong);
1571 DCHECK(!Primitive::IsFloatingPointType(condition->InputAt(0)->GetType()));
1572 LocationSummary* cond_locations = condition->GetLocations();
Roland Levillain0b671c02016-08-19 12:02:34 +01001573 codegen_->GenerateIntCompare(cond_locations->InAt(0), cond_locations->InAt(1));
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001574 cond = X86Condition(condition->GetCondition());
1575 }
1576 } else {
1577 // Must be a boolean condition, which needs to be compared to 0.
1578 Register cond_reg = locations->InAt(2).AsRegister<Register>();
1579 __ testl(cond_reg, cond_reg);
1580 }
1581
1582 // If the condition is true, overwrite the output, which already contains false.
1583 Location false_loc = locations->InAt(0);
1584 Location true_loc = locations->InAt(1);
1585 if (select->GetType() == Primitive::kPrimLong) {
1586 // 64 bit conditional move.
1587 Register false_high = false_loc.AsRegisterPairHigh<Register>();
1588 Register false_low = false_loc.AsRegisterPairLow<Register>();
1589 if (true_loc.IsRegisterPair()) {
1590 __ cmovl(cond, false_high, true_loc.AsRegisterPairHigh<Register>());
1591 __ cmovl(cond, false_low, true_loc.AsRegisterPairLow<Register>());
1592 } else {
1593 __ cmovl(cond, false_high, Address(ESP, true_loc.GetHighStackIndex(kX86WordSize)));
1594 __ cmovl(cond, false_low, Address(ESP, true_loc.GetStackIndex()));
1595 }
1596 } else {
1597 // 32 bit conditional move.
1598 Register false_reg = false_loc.AsRegister<Register>();
1599 if (true_loc.IsRegister()) {
1600 __ cmovl(cond, false_reg, true_loc.AsRegister<Register>());
1601 } else {
1602 __ cmovl(cond, false_reg, Address(ESP, true_loc.GetStackIndex()));
1603 }
1604 }
1605 } else {
1606 NearLabel false_target;
1607 GenerateTestAndBranch<NearLabel>(
1608 select, /* condition_input_index */ 2, /* true_target */ nullptr, &false_target);
1609 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
1610 __ Bind(&false_target);
1611 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001612}
1613
David Srbecky0cf44932015-12-09 14:09:59 +00001614void LocationsBuilderX86::VisitNativeDebugInfo(HNativeDebugInfo* info) {
1615 new (GetGraph()->GetArena()) LocationSummary(info);
1616}
1617
David Srbeckyd28f4a02016-03-14 17:14:24 +00001618void InstructionCodeGeneratorX86::VisitNativeDebugInfo(HNativeDebugInfo*) {
1619 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00001620}
1621
1622void CodeGeneratorX86::GenerateNop() {
1623 __ nop();
David Srbecky0cf44932015-12-09 14:09:59 +00001624}
1625
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001626void LocationsBuilderX86::HandleCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001627 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +01001628 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Mark Mendellc4701932015-04-10 13:18:51 -04001629 // Handle the long/FP comparisons made in instruction simplification.
1630 switch (cond->InputAt(0)->GetType()) {
1631 case Primitive::kPrimLong: {
1632 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell8659e842016-02-16 10:41:46 -05001633 locations->SetInAt(1, Location::Any());
David Brazdilb3e773e2016-01-26 11:28:37 +00001634 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001635 locations->SetOut(Location::RequiresRegister());
1636 }
1637 break;
1638 }
1639 case Primitive::kPrimFloat:
1640 case Primitive::kPrimDouble: {
1641 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001642 if (cond->InputAt(1)->IsX86LoadFromConstantTable()) {
1643 DCHECK(cond->InputAt(1)->IsEmittedAtUseSite());
1644 } else if (cond->InputAt(1)->IsConstant()) {
1645 locations->SetInAt(1, Location::RequiresFpuRegister());
1646 } else {
1647 locations->SetInAt(1, Location::Any());
1648 }
David Brazdilb3e773e2016-01-26 11:28:37 +00001649 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001650 locations->SetOut(Location::RequiresRegister());
1651 }
1652 break;
1653 }
1654 default:
1655 locations->SetInAt(0, Location::RequiresRegister());
1656 locations->SetInAt(1, Location::Any());
David Brazdilb3e773e2016-01-26 11:28:37 +00001657 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001658 // We need a byte register.
1659 locations->SetOut(Location::RegisterLocation(ECX));
1660 }
1661 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001662 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001663}
1664
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001665void InstructionCodeGeneratorX86::HandleCondition(HCondition* cond) {
David Brazdilb3e773e2016-01-26 11:28:37 +00001666 if (cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001667 return;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001668 }
Mark Mendellc4701932015-04-10 13:18:51 -04001669
1670 LocationSummary* locations = cond->GetLocations();
1671 Location lhs = locations->InAt(0);
1672 Location rhs = locations->InAt(1);
1673 Register reg = locations->Out().AsRegister<Register>();
Mark Mendell152408f2015-12-31 12:28:50 -05001674 NearLabel true_label, false_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001675
1676 switch (cond->InputAt(0)->GetType()) {
1677 default: {
1678 // Integer case.
1679
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01001680 // Clear output register: setb only sets the low byte.
Mark Mendellc4701932015-04-10 13:18:51 -04001681 __ xorl(reg, reg);
Roland Levillain0b671c02016-08-19 12:02:34 +01001682 codegen_->GenerateIntCompare(lhs, rhs);
Aart Bike9f37602015-10-09 11:15:55 -07001683 __ setb(X86Condition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001684 return;
1685 }
1686 case Primitive::kPrimLong:
1687 GenerateLongComparesAndJumps(cond, &true_label, &false_label);
1688 break;
1689 case Primitive::kPrimFloat:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001690 GenerateFPCompare(lhs, rhs, cond, false);
Mark Mendellc4701932015-04-10 13:18:51 -04001691 GenerateFPJumps(cond, &true_label, &false_label);
1692 break;
1693 case Primitive::kPrimDouble:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001694 GenerateFPCompare(lhs, rhs, cond, true);
Mark Mendellc4701932015-04-10 13:18:51 -04001695 GenerateFPJumps(cond, &true_label, &false_label);
1696 break;
1697 }
1698
1699 // Convert the jumps into the result.
Mark Mendell0c9497d2015-08-21 09:30:05 -04001700 NearLabel done_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001701
Roland Levillain4fa13f62015-07-06 18:11:54 +01001702 // False case: result = 0.
Mark Mendellc4701932015-04-10 13:18:51 -04001703 __ Bind(&false_label);
1704 __ xorl(reg, reg);
1705 __ jmp(&done_label);
1706
Roland Levillain4fa13f62015-07-06 18:11:54 +01001707 // True case: result = 1.
Mark Mendellc4701932015-04-10 13:18:51 -04001708 __ Bind(&true_label);
1709 __ movl(reg, Immediate(1));
1710 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07001711}
1712
1713void LocationsBuilderX86::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001714 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001715}
1716
1717void InstructionCodeGeneratorX86::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001718 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001719}
1720
1721void LocationsBuilderX86::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001722 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001723}
1724
1725void InstructionCodeGeneratorX86::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001726 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001727}
1728
1729void LocationsBuilderX86::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001730 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001731}
1732
1733void InstructionCodeGeneratorX86::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001734 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001735}
1736
1737void LocationsBuilderX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001738 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001739}
1740
1741void InstructionCodeGeneratorX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001742 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001743}
1744
1745void LocationsBuilderX86::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001746 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001747}
1748
1749void InstructionCodeGeneratorX86::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001750 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001751}
1752
1753void LocationsBuilderX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001754 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001755}
1756
1757void InstructionCodeGeneratorX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001758 HandleCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001759}
1760
Aart Bike9f37602015-10-09 11:15:55 -07001761void LocationsBuilderX86::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001762 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001763}
1764
1765void InstructionCodeGeneratorX86::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001766 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001767}
1768
1769void LocationsBuilderX86::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001770 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001771}
1772
1773void InstructionCodeGeneratorX86::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001774 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001775}
1776
1777void LocationsBuilderX86::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001778 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001779}
1780
1781void InstructionCodeGeneratorX86::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001782 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001783}
1784
1785void LocationsBuilderX86::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001786 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001787}
1788
1789void InstructionCodeGeneratorX86::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001790 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001791}
1792
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001793void LocationsBuilderX86::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001794 LocationSummary* locations =
1795 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001796 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001797}
1798
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001799void InstructionCodeGeneratorX86::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001800 // Will be generated at use site.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001801}
1802
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001803void LocationsBuilderX86::VisitNullConstant(HNullConstant* constant) {
1804 LocationSummary* locations =
1805 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1806 locations->SetOut(Location::ConstantLocation(constant));
1807}
1808
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001809void InstructionCodeGeneratorX86::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001810 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001811}
1812
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001813void LocationsBuilderX86::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001814 LocationSummary* locations =
1815 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001816 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001817}
1818
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001819void InstructionCodeGeneratorX86::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001820 // Will be generated at use site.
1821}
1822
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001823void LocationsBuilderX86::VisitFloatConstant(HFloatConstant* constant) {
1824 LocationSummary* locations =
1825 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1826 locations->SetOut(Location::ConstantLocation(constant));
1827}
1828
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001829void InstructionCodeGeneratorX86::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001830 // Will be generated at use site.
1831}
1832
1833void LocationsBuilderX86::VisitDoubleConstant(HDoubleConstant* constant) {
1834 LocationSummary* locations =
1835 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1836 locations->SetOut(Location::ConstantLocation(constant));
1837}
1838
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001839void InstructionCodeGeneratorX86::VisitDoubleConstant(HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001840 // Will be generated at use site.
1841}
1842
Calin Juravle27df7582015-04-17 19:12:31 +01001843void LocationsBuilderX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1844 memory_barrier->SetLocations(nullptr);
1845}
1846
1847void InstructionCodeGeneratorX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00001848 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01001849}
1850
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001851void LocationsBuilderX86::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001852 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001853}
1854
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001855void InstructionCodeGeneratorX86::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001856 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001857}
1858
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001859void LocationsBuilderX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001860 LocationSummary* locations =
1861 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001862 switch (ret->InputAt(0)->GetType()) {
1863 case Primitive::kPrimBoolean:
1864 case Primitive::kPrimByte:
1865 case Primitive::kPrimChar:
1866 case Primitive::kPrimShort:
1867 case Primitive::kPrimInt:
1868 case Primitive::kPrimNot:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001869 locations->SetInAt(0, Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001870 break;
1871
1872 case Primitive::kPrimLong:
1873 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001874 0, Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001875 break;
1876
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001877 case Primitive::kPrimFloat:
1878 case Primitive::kPrimDouble:
1879 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001880 0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001881 break;
1882
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001883 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001884 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001885 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001886}
1887
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001888void InstructionCodeGeneratorX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001889 if (kIsDebugBuild) {
1890 switch (ret->InputAt(0)->GetType()) {
1891 case Primitive::kPrimBoolean:
1892 case Primitive::kPrimByte:
1893 case Primitive::kPrimChar:
1894 case Primitive::kPrimShort:
1895 case Primitive::kPrimInt:
1896 case Primitive::kPrimNot:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001897 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<Register>(), EAX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001898 break;
1899
1900 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001901 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairLow<Register>(), EAX);
1902 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairHigh<Register>(), EDX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001903 break;
1904
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001905 case Primitive::kPrimFloat:
1906 case Primitive::kPrimDouble:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001907 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>(), XMM0);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001908 break;
1909
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001910 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001911 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001912 }
1913 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001914 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001915}
1916
Calin Juravle175dc732015-08-25 15:42:32 +01001917void LocationsBuilderX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
1918 // The trampoline uses the same calling convention as dex calling conventions,
1919 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
1920 // the method_idx.
1921 HandleInvoke(invoke);
1922}
1923
1924void InstructionCodeGeneratorX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
1925 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
1926}
1927
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001928void LocationsBuilderX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00001929 // Explicit clinit checks triggered by static invokes must have been pruned by
1930 // art::PrepareForRegisterAllocation.
1931 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001932
Mark Mendellfb8d2792015-03-31 22:16:59 -04001933 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001934 if (intrinsic.TryDispatch(invoke)) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00001935 if (invoke->GetLocations()->CanCall() && invoke->HasPcRelativeDexCache()) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00001936 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00001937 }
Mark Mendell09ed1a32015-03-25 08:30:06 -04001938 return;
1939 }
1940
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001941 HandleInvoke(invoke);
Nicolas Geoffray94015b92015-06-04 18:21:04 +01001942
Vladimir Marko0f7dca42015-11-02 14:36:43 +00001943 // For PC-relative dex cache the invoke has an extra input, the PC-relative address base.
1944 if (invoke->HasPcRelativeDexCache()) {
Vladimir Markob4536b72015-11-24 13:45:23 +00001945 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00001946 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001947}
1948
Mark Mendell09ed1a32015-03-25 08:30:06 -04001949static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86* codegen) {
1950 if (invoke->GetLocations()->Intrinsified()) {
1951 IntrinsicCodeGeneratorX86 intrinsic(codegen);
1952 intrinsic.Dispatch(invoke);
1953 return true;
1954 }
1955 return false;
1956}
1957
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001958void InstructionCodeGeneratorX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00001959 // Explicit clinit checks triggered by static invokes must have been pruned by
1960 // art::PrepareForRegisterAllocation.
1961 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001962
Mark Mendell09ed1a32015-03-25 08:30:06 -04001963 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1964 return;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001965 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001966
Nicolas Geoffray94015b92015-06-04 18:21:04 +01001967 LocationSummary* locations = invoke->GetLocations();
Mark Mendell09ed1a32015-03-25 08:30:06 -04001968 codegen_->GenerateStaticOrDirectCall(
Nicolas Geoffray94015b92015-06-04 18:21:04 +01001969 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Mingyao Yang8693fe12015-04-17 16:51:08 -07001970 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001971}
1972
1973void LocationsBuilderX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00001974 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
1975 if (intrinsic.TryDispatch(invoke)) {
1976 return;
1977 }
1978
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001979 HandleInvoke(invoke);
1980}
1981
1982void LocationsBuilderX86::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001983 InvokeDexCallingConventionVisitorX86 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001984 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001985}
1986
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001987void InstructionCodeGeneratorX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampebfb5ba92015-09-01 15:45:02 +00001988 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1989 return;
1990 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001991
Andreas Gampebfb5ba92015-09-01 15:45:02 +00001992 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001993 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001994 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001995}
1996
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001997void LocationsBuilderX86::VisitInvokeInterface(HInvokeInterface* invoke) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00001998 // This call to HandleInvoke allocates a temporary (core) register
1999 // which is also used to transfer the hidden argument from FP to
2000 // core register.
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002001 HandleInvoke(invoke);
2002 // Add the hidden argument.
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002003 invoke->GetLocations()->AddTemp(Location::FpuRegisterLocation(XMM7));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002004}
2005
2006void InstructionCodeGeneratorX86::VisitInvokeInterface(HInvokeInterface* invoke) {
2007 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain0d5a2812015-11-13 10:07:31 +00002008 LocationSummary* locations = invoke->GetLocations();
2009 Register temp = locations->GetTemp(0).AsRegister<Register>();
2010 XmmRegister hidden_reg = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002011 Location receiver = locations->InAt(0);
2012 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2013
Roland Levillain0d5a2812015-11-13 10:07:31 +00002014 // Set the hidden argument. This is safe to do this here, as XMM7
2015 // won't be modified thereafter, before the `call` instruction.
2016 DCHECK_EQ(XMM7, hidden_reg);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002017 __ movl(temp, Immediate(invoke->GetDexMethodIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002018 __ movd(hidden_reg, temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002019
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002020 if (receiver.IsStackSlot()) {
2021 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002022 // /* HeapReference<Class> */ temp = temp->klass_
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002023 __ movl(temp, Address(temp, class_offset));
2024 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002025 // /* HeapReference<Class> */ temp = receiver->klass_
Roland Levillain271ab9c2014-11-27 15:23:57 +00002026 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002027 }
Roland Levillain4d027112015-07-01 15:41:14 +01002028 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00002029 // Instead of simply (possibly) unpoisoning `temp` here, we should
2030 // emit a read barrier for the previous class reference load.
2031 // However this is not required in practice, as this is an
2032 // intermediate/temporary reference and because the current
2033 // concurrent copying collector keeps the from-space memory
2034 // intact/accessible until the end of the marking phase (the
2035 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01002036 __ MaybeUnpoisonHeapReference(temp);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002037 // temp = temp->GetAddressOfIMT()
2038 __ movl(temp,
2039 Address(temp, mirror::Class::ImtPtrOffset(kX86PointerSize).Uint32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002040 // temp = temp->GetImtEntryAt(method_offset);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002041 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00002042 invoke->GetImtIndex(), kX86PointerSize));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002043 __ movl(temp, Address(temp, method_offset));
2044 // call temp->GetEntryPoint();
Roland Levillain0d5a2812015-11-13 10:07:31 +00002045 __ call(Address(temp,
Andreas Gampe542451c2016-07-26 09:02:02 -07002046 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86PointerSize).Int32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002047
2048 DCHECK(!codegen_->IsLeafMethod());
2049 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2050}
2051
Roland Levillain88cb1752014-10-20 16:36:47 +01002052void LocationsBuilderX86::VisitNeg(HNeg* neg) {
2053 LocationSummary* locations =
2054 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2055 switch (neg->GetResultType()) {
2056 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002057 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01002058 locations->SetInAt(0, Location::RequiresRegister());
2059 locations->SetOut(Location::SameAsFirstInput());
2060 break;
2061
Roland Levillain88cb1752014-10-20 16:36:47 +01002062 case Primitive::kPrimFloat:
Roland Levillain5368c212014-11-27 15:03:41 +00002063 locations->SetInAt(0, Location::RequiresFpuRegister());
2064 locations->SetOut(Location::SameAsFirstInput());
2065 locations->AddTemp(Location::RequiresRegister());
2066 locations->AddTemp(Location::RequiresFpuRegister());
2067 break;
2068
Roland Levillain88cb1752014-10-20 16:36:47 +01002069 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002070 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00002071 locations->SetOut(Location::SameAsFirstInput());
2072 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01002073 break;
2074
2075 default:
2076 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2077 }
2078}
2079
2080void InstructionCodeGeneratorX86::VisitNeg(HNeg* neg) {
2081 LocationSummary* locations = neg->GetLocations();
2082 Location out = locations->Out();
2083 Location in = locations->InAt(0);
2084 switch (neg->GetResultType()) {
2085 case Primitive::kPrimInt:
2086 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002087 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002088 __ negl(out.AsRegister<Register>());
Roland Levillain88cb1752014-10-20 16:36:47 +01002089 break;
2090
2091 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002092 DCHECK(in.IsRegisterPair());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002093 DCHECK(in.Equals(out));
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002094 __ negl(out.AsRegisterPairLow<Register>());
2095 // Negation is similar to subtraction from zero. The least
2096 // significant byte triggers a borrow when it is different from
2097 // zero; to take it into account, add 1 to the most significant
2098 // byte if the carry flag (CF) is set to 1 after the first NEGL
2099 // operation.
2100 __ adcl(out.AsRegisterPairHigh<Register>(), Immediate(0));
2101 __ negl(out.AsRegisterPairHigh<Register>());
2102 break;
2103
Roland Levillain5368c212014-11-27 15:03:41 +00002104 case Primitive::kPrimFloat: {
2105 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002106 Register constant = locations->GetTemp(0).AsRegister<Register>();
2107 XmmRegister mask = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002108 // Implement float negation with an exclusive or with value
2109 // 0x80000000 (mask for bit 31, representing the sign of a
2110 // single-precision floating-point number).
2111 __ movl(constant, Immediate(INT32_C(0x80000000)));
2112 __ movd(mask, constant);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002113 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00002114 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002115 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00002116
Roland Levillain5368c212014-11-27 15:03:41 +00002117 case Primitive::kPrimDouble: {
2118 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002119 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002120 // Implement double negation with an exclusive or with value
2121 // 0x8000000000000000 (mask for bit 63, representing the sign of
2122 // a double-precision floating-point number).
2123 __ LoadLongConstant(mask, INT64_C(0x8000000000000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002124 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01002125 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002126 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002127
2128 default:
2129 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2130 }
2131}
2132
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002133void LocationsBuilderX86::VisitX86FPNeg(HX86FPNeg* neg) {
2134 LocationSummary* locations =
2135 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2136 DCHECK(Primitive::IsFloatingPointType(neg->GetType()));
2137 locations->SetInAt(0, Location::RequiresFpuRegister());
2138 locations->SetInAt(1, Location::RequiresRegister());
2139 locations->SetOut(Location::SameAsFirstInput());
2140 locations->AddTemp(Location::RequiresFpuRegister());
2141}
2142
2143void InstructionCodeGeneratorX86::VisitX86FPNeg(HX86FPNeg* neg) {
2144 LocationSummary* locations = neg->GetLocations();
2145 Location out = locations->Out();
2146 DCHECK(locations->InAt(0).Equals(out));
2147
2148 Register constant_area = locations->InAt(1).AsRegister<Register>();
2149 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
2150 if (neg->GetType() == Primitive::kPrimFloat) {
2151 __ movss(mask, codegen_->LiteralInt32Address(INT32_C(0x80000000), constant_area));
2152 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
2153 } else {
2154 __ movsd(mask, codegen_->LiteralInt64Address(INT64_C(0x8000000000000000), constant_area));
2155 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
2156 }
2157}
2158
Roland Levillaindff1f282014-11-05 14:15:05 +00002159void LocationsBuilderX86::VisitTypeConversion(HTypeConversion* conversion) {
Roland Levillaindff1f282014-11-05 14:15:05 +00002160 Primitive::Type result_type = conversion->GetResultType();
2161 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002162 DCHECK_NE(result_type, input_type);
Roland Levillain624279f2014-12-04 11:54:28 +00002163
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002164 // The float-to-long and double-to-long type conversions rely on a
2165 // call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00002166 LocationSummary::CallKind call_kind =
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002167 ((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
2168 && result_type == Primitive::kPrimLong)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01002169 ? LocationSummary::kCallOnMainOnly
Roland Levillain624279f2014-12-04 11:54:28 +00002170 : LocationSummary::kNoCall;
2171 LocationSummary* locations =
2172 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
2173
David Brazdilb2bd1c52015-03-25 11:17:37 +00002174 // The Java language does not allow treating boolean as an integral type but
2175 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00002176
Roland Levillaindff1f282014-11-05 14:15:05 +00002177 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002178 case Primitive::kPrimByte:
2179 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002180 case Primitive::kPrimLong: {
2181 // Type conversion from long to byte is a result of code transformations.
2182 HInstruction* input = conversion->InputAt(0);
2183 Location input_location = input->IsConstant()
2184 ? Location::ConstantLocation(input->AsConstant())
2185 : Location::RegisterPairLocation(EAX, EDX);
2186 locations->SetInAt(0, input_location);
2187 // Make the output overlap to please the register allocator. This greatly simplifies
2188 // the validation of the linear scan implementation
2189 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2190 break;
2191 }
David Brazdil46e2a392015-03-16 17:31:52 +00002192 case Primitive::kPrimBoolean:
2193 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002194 case Primitive::kPrimShort:
2195 case Primitive::kPrimInt:
2196 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002197 // Processing a Dex `int-to-byte' instruction.
Mark Mendell5f874182015-03-04 15:42:45 -05002198 locations->SetInAt(0, Location::ByteRegisterOrConstant(ECX, conversion->InputAt(0)));
2199 // Make the output overlap to please the register allocator. This greatly simplifies
2200 // the validation of the linear scan implementation
2201 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Roland Levillain51d3fc42014-11-13 14:11:42 +00002202 break;
2203
2204 default:
2205 LOG(FATAL) << "Unexpected type conversion from " << input_type
2206 << " to " << result_type;
2207 }
2208 break;
2209
Roland Levillain01a8d712014-11-14 16:27:39 +00002210 case Primitive::kPrimShort:
2211 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002212 case Primitive::kPrimLong:
2213 // Type conversion from long to short is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002214 case Primitive::kPrimBoolean:
2215 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002216 case Primitive::kPrimByte:
2217 case Primitive::kPrimInt:
2218 case Primitive::kPrimChar:
2219 // Processing a Dex `int-to-short' instruction.
2220 locations->SetInAt(0, Location::Any());
2221 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2222 break;
2223
2224 default:
2225 LOG(FATAL) << "Unexpected type conversion from " << input_type
2226 << " to " << result_type;
2227 }
2228 break;
2229
Roland Levillain946e1432014-11-11 17:35:19 +00002230 case Primitive::kPrimInt:
2231 switch (input_type) {
2232 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002233 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002234 locations->SetInAt(0, Location::Any());
2235 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2236 break;
2237
2238 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00002239 // Processing a Dex `float-to-int' instruction.
2240 locations->SetInAt(0, Location::RequiresFpuRegister());
2241 locations->SetOut(Location::RequiresRegister());
2242 locations->AddTemp(Location::RequiresFpuRegister());
2243 break;
2244
Roland Levillain946e1432014-11-11 17:35:19 +00002245 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002246 // Processing a Dex `double-to-int' instruction.
2247 locations->SetInAt(0, Location::RequiresFpuRegister());
2248 locations->SetOut(Location::RequiresRegister());
2249 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00002250 break;
2251
2252 default:
2253 LOG(FATAL) << "Unexpected type conversion from " << input_type
2254 << " to " << result_type;
2255 }
2256 break;
2257
Roland Levillaindff1f282014-11-05 14:15:05 +00002258 case Primitive::kPrimLong:
2259 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002260 case Primitive::kPrimBoolean:
2261 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002262 case Primitive::kPrimByte:
2263 case Primitive::kPrimShort:
2264 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002265 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002266 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002267 locations->SetInAt(0, Location::RegisterLocation(EAX));
2268 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
2269 break;
2270
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002271 case Primitive::kPrimFloat:
Vladimir Marko949c91f2015-01-27 10:48:44 +00002272 case Primitive::kPrimDouble: {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002273 // Processing a Dex `float-to-long' or 'double-to-long' instruction.
Vladimir Marko949c91f2015-01-27 10:48:44 +00002274 InvokeRuntimeCallingConvention calling_convention;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002275 XmmRegister parameter = calling_convention.GetFpuRegisterAt(0);
2276 locations->SetInAt(0, Location::FpuRegisterLocation(parameter));
2277
Vladimir Marko949c91f2015-01-27 10:48:44 +00002278 // The runtime helper puts the result in EAX, EDX.
2279 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Vladimir Marko949c91f2015-01-27 10:48:44 +00002280 }
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002281 break;
Roland Levillaindff1f282014-11-05 14:15:05 +00002282
2283 default:
2284 LOG(FATAL) << "Unexpected type conversion from " << input_type
2285 << " to " << result_type;
2286 }
2287 break;
2288
Roland Levillain981e4542014-11-14 11:47:14 +00002289 case Primitive::kPrimChar:
2290 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002291 case Primitive::kPrimLong:
2292 // Type conversion from long to char is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002293 case Primitive::kPrimBoolean:
2294 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002295 case Primitive::kPrimByte:
2296 case Primitive::kPrimShort:
2297 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002298 // Processing a Dex `int-to-char' instruction.
2299 locations->SetInAt(0, Location::Any());
2300 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2301 break;
2302
2303 default:
2304 LOG(FATAL) << "Unexpected type conversion from " << input_type
2305 << " to " << result_type;
2306 }
2307 break;
2308
Roland Levillaindff1f282014-11-05 14:15:05 +00002309 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002310 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002311 case Primitive::kPrimBoolean:
2312 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002313 case Primitive::kPrimByte:
2314 case Primitive::kPrimShort:
2315 case Primitive::kPrimInt:
2316 case Primitive::kPrimChar:
2317 // Processing a Dex `int-to-float' instruction.
2318 locations->SetInAt(0, Location::RequiresRegister());
2319 locations->SetOut(Location::RequiresFpuRegister());
2320 break;
2321
2322 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002323 // Processing a Dex `long-to-float' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002324 locations->SetInAt(0, Location::Any());
2325 locations->SetOut(Location::Any());
Roland Levillain6d0e4832014-11-27 18:31:21 +00002326 break;
2327
Roland Levillaincff13742014-11-17 14:32:17 +00002328 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002329 // Processing a Dex `double-to-float' instruction.
2330 locations->SetInAt(0, Location::RequiresFpuRegister());
2331 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002332 break;
2333
2334 default:
2335 LOG(FATAL) << "Unexpected type conversion from " << input_type
2336 << " to " << result_type;
2337 };
2338 break;
2339
Roland Levillaindff1f282014-11-05 14:15:05 +00002340 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002341 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002342 case Primitive::kPrimBoolean:
2343 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002344 case Primitive::kPrimByte:
2345 case Primitive::kPrimShort:
2346 case Primitive::kPrimInt:
2347 case Primitive::kPrimChar:
2348 // Processing a Dex `int-to-double' instruction.
2349 locations->SetInAt(0, Location::RequiresRegister());
2350 locations->SetOut(Location::RequiresFpuRegister());
2351 break;
2352
2353 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002354 // Processing a Dex `long-to-double' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002355 locations->SetInAt(0, Location::Any());
2356 locations->SetOut(Location::Any());
Roland Levillain647b9ed2014-11-27 12:06:00 +00002357 break;
2358
Roland Levillaincff13742014-11-17 14:32:17 +00002359 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002360 // Processing a Dex `float-to-double' instruction.
2361 locations->SetInAt(0, Location::RequiresFpuRegister());
2362 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002363 break;
2364
2365 default:
2366 LOG(FATAL) << "Unexpected type conversion from " << input_type
2367 << " to " << result_type;
2368 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002369 break;
2370
2371 default:
2372 LOG(FATAL) << "Unexpected type conversion from " << input_type
2373 << " to " << result_type;
2374 }
2375}
2376
2377void InstructionCodeGeneratorX86::VisitTypeConversion(HTypeConversion* conversion) {
2378 LocationSummary* locations = conversion->GetLocations();
2379 Location out = locations->Out();
2380 Location in = locations->InAt(0);
2381 Primitive::Type result_type = conversion->GetResultType();
2382 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002383 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00002384 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002385 case Primitive::kPrimByte:
2386 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002387 case Primitive::kPrimLong:
2388 // Type conversion from long to byte is a result of code transformations.
2389 if (in.IsRegisterPair()) {
2390 __ movsxb(out.AsRegister<Register>(), in.AsRegisterPairLow<ByteRegister>());
2391 } else {
2392 DCHECK(in.GetConstant()->IsLongConstant());
2393 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2394 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
2395 }
2396 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002397 case Primitive::kPrimBoolean:
2398 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002399 case Primitive::kPrimShort:
2400 case Primitive::kPrimInt:
2401 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002402 // Processing a Dex `int-to-byte' instruction.
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002403 if (in.IsRegister()) {
2404 __ movsxb(out.AsRegister<Register>(), in.AsRegister<ByteRegister>());
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002405 } else {
2406 DCHECK(in.GetConstant()->IsIntConstant());
2407 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
2408 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
2409 }
Roland Levillain51d3fc42014-11-13 14:11:42 +00002410 break;
2411
2412 default:
2413 LOG(FATAL) << "Unexpected type conversion from " << input_type
2414 << " to " << result_type;
2415 }
2416 break;
2417
Roland Levillain01a8d712014-11-14 16:27:39 +00002418 case Primitive::kPrimShort:
2419 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002420 case Primitive::kPrimLong:
2421 // Type conversion from long to short is a result of code transformations.
2422 if (in.IsRegisterPair()) {
2423 __ movsxw(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
2424 } else if (in.IsDoubleStackSlot()) {
2425 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
2426 } else {
2427 DCHECK(in.GetConstant()->IsLongConstant());
2428 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2429 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
2430 }
2431 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002432 case Primitive::kPrimBoolean:
2433 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002434 case Primitive::kPrimByte:
2435 case Primitive::kPrimInt:
2436 case Primitive::kPrimChar:
2437 // Processing a Dex `int-to-short' instruction.
2438 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002439 __ movsxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain01a8d712014-11-14 16:27:39 +00002440 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002441 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain01a8d712014-11-14 16:27:39 +00002442 } else {
2443 DCHECK(in.GetConstant()->IsIntConstant());
2444 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002445 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
Roland Levillain01a8d712014-11-14 16:27:39 +00002446 }
2447 break;
2448
2449 default:
2450 LOG(FATAL) << "Unexpected type conversion from " << input_type
2451 << " to " << result_type;
2452 }
2453 break;
2454
Roland Levillain946e1432014-11-11 17:35:19 +00002455 case Primitive::kPrimInt:
2456 switch (input_type) {
2457 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002458 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002459 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002460 __ movl(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00002461 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002462 __ movl(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain946e1432014-11-11 17:35:19 +00002463 } else {
2464 DCHECK(in.IsConstant());
2465 DCHECK(in.GetConstant()->IsLongConstant());
2466 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002467 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00002468 }
2469 break;
2470
Roland Levillain3f8f9362014-12-02 17:45:01 +00002471 case Primitive::kPrimFloat: {
2472 // Processing a Dex `float-to-int' instruction.
2473 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2474 Register output = out.AsRegister<Register>();
2475 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002476 NearLabel done, nan;
Roland Levillain3f8f9362014-12-02 17:45:01 +00002477
2478 __ movl(output, Immediate(kPrimIntMax));
2479 // temp = int-to-float(output)
2480 __ cvtsi2ss(temp, output);
2481 // if input >= temp goto done
2482 __ comiss(input, temp);
2483 __ j(kAboveEqual, &done);
2484 // if input == NaN goto nan
2485 __ j(kUnordered, &nan);
2486 // output = float-to-int-truncate(input)
2487 __ cvttss2si(output, input);
2488 __ jmp(&done);
2489 __ Bind(&nan);
2490 // output = 0
2491 __ xorl(output, output);
2492 __ Bind(&done);
2493 break;
2494 }
2495
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002496 case Primitive::kPrimDouble: {
2497 // Processing a Dex `double-to-int' instruction.
2498 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2499 Register output = out.AsRegister<Register>();
2500 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002501 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002502
2503 __ movl(output, Immediate(kPrimIntMax));
2504 // temp = int-to-double(output)
2505 __ cvtsi2sd(temp, output);
2506 // if input >= temp goto done
2507 __ comisd(input, temp);
2508 __ j(kAboveEqual, &done);
2509 // if input == NaN goto nan
2510 __ j(kUnordered, &nan);
2511 // output = double-to-int-truncate(input)
2512 __ cvttsd2si(output, input);
2513 __ jmp(&done);
2514 __ Bind(&nan);
2515 // output = 0
2516 __ xorl(output, output);
2517 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00002518 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002519 }
Roland Levillain946e1432014-11-11 17:35:19 +00002520
2521 default:
2522 LOG(FATAL) << "Unexpected type conversion from " << input_type
2523 << " to " << result_type;
2524 }
2525 break;
2526
Roland Levillaindff1f282014-11-05 14:15:05 +00002527 case Primitive::kPrimLong:
2528 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002529 case Primitive::kPrimBoolean:
2530 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002531 case Primitive::kPrimByte:
2532 case Primitive::kPrimShort:
2533 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002534 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002535 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002536 DCHECK_EQ(out.AsRegisterPairLow<Register>(), EAX);
2537 DCHECK_EQ(out.AsRegisterPairHigh<Register>(), EDX);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002538 DCHECK_EQ(in.AsRegister<Register>(), EAX);
Roland Levillaindff1f282014-11-05 14:15:05 +00002539 __ cdq();
2540 break;
2541
2542 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00002543 // Processing a Dex `float-to-long' instruction.
Serban Constantinescuba45db02016-07-12 22:53:02 +01002544 codegen_->InvokeRuntime(kQuickF2l, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00002545 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
Roland Levillain624279f2014-12-04 11:54:28 +00002546 break;
2547
Roland Levillaindff1f282014-11-05 14:15:05 +00002548 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002549 // Processing a Dex `double-to-long' instruction.
Serban Constantinescuba45db02016-07-12 22:53:02 +01002550 codegen_->InvokeRuntime(kQuickD2l, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00002551 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
Roland Levillaindff1f282014-11-05 14:15:05 +00002552 break;
2553
2554 default:
2555 LOG(FATAL) << "Unexpected type conversion from " << input_type
2556 << " to " << result_type;
2557 }
2558 break;
2559
Roland Levillain981e4542014-11-14 11:47:14 +00002560 case Primitive::kPrimChar:
2561 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002562 case Primitive::kPrimLong:
2563 // Type conversion from long to short is a result of code transformations.
2564 if (in.IsRegisterPair()) {
2565 __ movzxw(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
2566 } else if (in.IsDoubleStackSlot()) {
2567 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
2568 } else {
2569 DCHECK(in.GetConstant()->IsLongConstant());
2570 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2571 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
2572 }
2573 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002574 case Primitive::kPrimBoolean:
2575 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002576 case Primitive::kPrimByte:
2577 case Primitive::kPrimShort:
2578 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002579 // Processing a Dex `Process a Dex `int-to-char'' instruction.
2580 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002581 __ movzxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain981e4542014-11-14 11:47:14 +00002582 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002583 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain981e4542014-11-14 11:47:14 +00002584 } else {
2585 DCHECK(in.GetConstant()->IsIntConstant());
2586 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002587 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
Roland Levillain981e4542014-11-14 11:47:14 +00002588 }
2589 break;
2590
2591 default:
2592 LOG(FATAL) << "Unexpected type conversion from " << input_type
2593 << " to " << result_type;
2594 }
2595 break;
2596
Roland Levillaindff1f282014-11-05 14:15:05 +00002597 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002598 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002599 case Primitive::kPrimBoolean:
2600 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002601 case Primitive::kPrimByte:
2602 case Primitive::kPrimShort:
2603 case Primitive::kPrimInt:
2604 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002605 // Processing a Dex `int-to-float' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002606 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002607 break;
2608
Roland Levillain6d0e4832014-11-27 18:31:21 +00002609 case Primitive::kPrimLong: {
2610 // Processing a Dex `long-to-float' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002611 size_t adjustment = 0;
Roland Levillain6d0e4832014-11-27 18:31:21 +00002612
Roland Levillain232ade02015-04-20 15:14:36 +01002613 // Create stack space for the call to
2614 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstps below.
2615 // TODO: enhance register allocator to ask for stack temporaries.
2616 if (!in.IsDoubleStackSlot() || !out.IsStackSlot()) {
2617 adjustment = Primitive::ComponentSize(Primitive::kPrimLong);
2618 __ subl(ESP, Immediate(adjustment));
2619 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002620
Roland Levillain232ade02015-04-20 15:14:36 +01002621 // Load the value to the FP stack, using temporaries if needed.
2622 PushOntoFPStack(in, 0, adjustment, false, true);
2623
2624 if (out.IsStackSlot()) {
2625 __ fstps(Address(ESP, out.GetStackIndex() + adjustment));
2626 } else {
2627 __ fstps(Address(ESP, 0));
2628 Location stack_temp = Location::StackSlot(0);
2629 codegen_->Move32(out, stack_temp);
2630 }
2631
2632 // Remove the temporary stack space we allocated.
2633 if (adjustment != 0) {
2634 __ addl(ESP, Immediate(adjustment));
2635 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002636 break;
2637 }
2638
Roland Levillaincff13742014-11-17 14:32:17 +00002639 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002640 // Processing a Dex `double-to-float' instruction.
2641 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002642 break;
2643
2644 default:
2645 LOG(FATAL) << "Unexpected type conversion from " << input_type
2646 << " to " << result_type;
2647 };
2648 break;
2649
Roland Levillaindff1f282014-11-05 14:15:05 +00002650 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002651 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002652 case Primitive::kPrimBoolean:
2653 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002654 case Primitive::kPrimByte:
2655 case Primitive::kPrimShort:
2656 case Primitive::kPrimInt:
2657 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002658 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002659 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002660 break;
2661
Roland Levillain647b9ed2014-11-27 12:06:00 +00002662 case Primitive::kPrimLong: {
2663 // Processing a Dex `long-to-double' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002664 size_t adjustment = 0;
Roland Levillain647b9ed2014-11-27 12:06:00 +00002665
Roland Levillain232ade02015-04-20 15:14:36 +01002666 // Create stack space for the call to
2667 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstpl below.
2668 // TODO: enhance register allocator to ask for stack temporaries.
2669 if (!in.IsDoubleStackSlot() || !out.IsDoubleStackSlot()) {
2670 adjustment = Primitive::ComponentSize(Primitive::kPrimLong);
2671 __ subl(ESP, Immediate(adjustment));
2672 }
2673
2674 // Load the value to the FP stack, using temporaries if needed.
2675 PushOntoFPStack(in, 0, adjustment, false, true);
2676
2677 if (out.IsDoubleStackSlot()) {
2678 __ fstpl(Address(ESP, out.GetStackIndex() + adjustment));
2679 } else {
2680 __ fstpl(Address(ESP, 0));
2681 Location stack_temp = Location::DoubleStackSlot(0);
2682 codegen_->Move64(out, stack_temp);
2683 }
2684
2685 // Remove the temporary stack space we allocated.
2686 if (adjustment != 0) {
2687 __ addl(ESP, Immediate(adjustment));
2688 }
Roland Levillain647b9ed2014-11-27 12:06:00 +00002689 break;
2690 }
2691
Roland Levillaincff13742014-11-17 14:32:17 +00002692 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002693 // Processing a Dex `float-to-double' instruction.
2694 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002695 break;
2696
2697 default:
2698 LOG(FATAL) << "Unexpected type conversion from " << input_type
2699 << " to " << result_type;
2700 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002701 break;
2702
2703 default:
2704 LOG(FATAL) << "Unexpected type conversion from " << input_type
2705 << " to " << result_type;
2706 }
2707}
2708
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002709void LocationsBuilderX86::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002710 LocationSummary* locations =
2711 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002712 switch (add->GetResultType()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002713 case Primitive::kPrimInt: {
2714 locations->SetInAt(0, Location::RequiresRegister());
2715 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
2716 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2717 break;
2718 }
2719
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002720 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002721 locations->SetInAt(0, Location::RequiresRegister());
2722 locations->SetInAt(1, Location::Any());
2723 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002724 break;
2725 }
2726
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002727 case Primitive::kPrimFloat:
2728 case Primitive::kPrimDouble: {
2729 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002730 if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
2731 DCHECK(add->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00002732 } else if (add->InputAt(1)->IsConstant()) {
2733 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002734 } else {
2735 locations->SetInAt(1, Location::Any());
2736 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002737 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002738 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002739 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002740
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002741 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002742 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
2743 break;
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002744 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002745}
2746
2747void InstructionCodeGeneratorX86::VisitAdd(HAdd* add) {
2748 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002749 Location first = locations->InAt(0);
2750 Location second = locations->InAt(1);
Mark Mendell09b84632015-02-13 17:48:38 -05002751 Location out = locations->Out();
2752
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002753 switch (add->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002754 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002755 if (second.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002756 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2757 __ addl(out.AsRegister<Register>(), second.AsRegister<Register>());
Mark Mendell33bf2452015-05-27 10:08:24 -04002758 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
2759 __ addl(out.AsRegister<Register>(), first.AsRegister<Register>());
Mark Mendell09b84632015-02-13 17:48:38 -05002760 } else {
2761 __ leal(out.AsRegister<Register>(), Address(
2762 first.AsRegister<Register>(), second.AsRegister<Register>(), TIMES_1, 0));
2763 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002764 } else if (second.IsConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002765 int32_t value = second.GetConstant()->AsIntConstant()->GetValue();
2766 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2767 __ addl(out.AsRegister<Register>(), Immediate(value));
2768 } else {
2769 __ leal(out.AsRegister<Register>(), Address(first.AsRegister<Register>(), value));
2770 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002771 } else {
Mark Mendell09b84632015-02-13 17:48:38 -05002772 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002773 __ addl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002774 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002775 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002776 }
2777
2778 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002779 if (second.IsRegisterPair()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002780 __ addl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
2781 __ adcl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002782 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002783 __ addl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
2784 __ adcl(first.AsRegisterPairHigh<Register>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002785 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002786 } else {
2787 DCHECK(second.IsConstant()) << second;
2788 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2789 __ addl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
2790 __ adcl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002791 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002792 break;
2793 }
2794
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002795 case Primitive::kPrimFloat: {
2796 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002797 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04002798 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
2799 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00002800 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04002801 __ addss(first.AsFpuRegister<XmmRegister>(),
2802 codegen_->LiteralFloatAddress(
2803 const_area->GetConstant()->AsFloatConstant()->GetValue(),
2804 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2805 } else {
2806 DCHECK(second.IsStackSlot());
2807 __ addss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002808 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002809 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002810 }
2811
2812 case Primitive::kPrimDouble: {
2813 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002814 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04002815 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
2816 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00002817 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04002818 __ addsd(first.AsFpuRegister<XmmRegister>(),
2819 codegen_->LiteralDoubleAddress(
2820 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
2821 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2822 } else {
2823 DCHECK(second.IsDoubleStackSlot());
2824 __ addsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002825 }
2826 break;
2827 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002828
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002829 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002830 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002831 }
2832}
2833
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002834void LocationsBuilderX86::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002835 LocationSummary* locations =
2836 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002837 switch (sub->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002838 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002839 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002840 locations->SetInAt(0, Location::RequiresRegister());
2841 locations->SetInAt(1, Location::Any());
2842 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002843 break;
2844 }
Calin Juravle11351682014-10-23 15:38:15 +01002845 case Primitive::kPrimFloat:
2846 case Primitive::kPrimDouble: {
2847 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002848 if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
2849 DCHECK(sub->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00002850 } else if (sub->InputAt(1)->IsConstant()) {
2851 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002852 } else {
2853 locations->SetInAt(1, Location::Any());
2854 }
Calin Juravle11351682014-10-23 15:38:15 +01002855 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002856 break;
Calin Juravle11351682014-10-23 15:38:15 +01002857 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002858
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002859 default:
Calin Juravle11351682014-10-23 15:38:15 +01002860 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002861 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002862}
2863
2864void InstructionCodeGeneratorX86::VisitSub(HSub* sub) {
2865 LocationSummary* locations = sub->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002866 Location first = locations->InAt(0);
2867 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01002868 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002869 switch (sub->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002870 case Primitive::kPrimInt: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002871 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002872 __ subl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002873 } else if (second.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002874 __ subl(first.AsRegister<Register>(),
2875 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002876 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002877 __ subl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002878 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002879 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002880 }
2881
2882 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002883 if (second.IsRegisterPair()) {
Calin Juravle11351682014-10-23 15:38:15 +01002884 __ subl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
2885 __ sbbl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002886 } else if (second.IsDoubleStackSlot()) {
Calin Juravle11351682014-10-23 15:38:15 +01002887 __ subl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002888 __ sbbl(first.AsRegisterPairHigh<Register>(),
2889 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002890 } else {
2891 DCHECK(second.IsConstant()) << second;
2892 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2893 __ subl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
2894 __ sbbl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002895 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002896 break;
2897 }
2898
Calin Juravle11351682014-10-23 15:38:15 +01002899 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04002900 if (second.IsFpuRegister()) {
2901 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2902 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
2903 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00002904 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04002905 __ subss(first.AsFpuRegister<XmmRegister>(),
2906 codegen_->LiteralFloatAddress(
2907 const_area->GetConstant()->AsFloatConstant()->GetValue(),
2908 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2909 } else {
2910 DCHECK(second.IsStackSlot());
2911 __ subss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
2912 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002913 break;
Calin Juravle11351682014-10-23 15:38:15 +01002914 }
2915
2916 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04002917 if (second.IsFpuRegister()) {
2918 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2919 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
2920 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00002921 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04002922 __ subsd(first.AsFpuRegister<XmmRegister>(),
2923 codegen_->LiteralDoubleAddress(
2924 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
2925 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2926 } else {
2927 DCHECK(second.IsDoubleStackSlot());
2928 __ subsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
2929 }
Calin Juravle11351682014-10-23 15:38:15 +01002930 break;
2931 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002932
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002933 default:
Calin Juravle11351682014-10-23 15:38:15 +01002934 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002935 }
2936}
2937
Calin Juravle34bacdf2014-10-07 20:23:36 +01002938void LocationsBuilderX86::VisitMul(HMul* mul) {
2939 LocationSummary* locations =
2940 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
2941 switch (mul->GetResultType()) {
2942 case Primitive::kPrimInt:
2943 locations->SetInAt(0, Location::RequiresRegister());
2944 locations->SetInAt(1, Location::Any());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002945 if (mul->InputAt(1)->IsIntConstant()) {
2946 // Can use 3 operand multiply.
2947 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2948 } else {
2949 locations->SetOut(Location::SameAsFirstInput());
2950 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002951 break;
2952 case Primitive::kPrimLong: {
2953 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002954 locations->SetInAt(1, Location::Any());
2955 locations->SetOut(Location::SameAsFirstInput());
2956 // Needed for imul on 32bits with 64bits output.
2957 locations->AddTemp(Location::RegisterLocation(EAX));
2958 locations->AddTemp(Location::RegisterLocation(EDX));
2959 break;
2960 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002961 case Primitive::kPrimFloat:
2962 case Primitive::kPrimDouble: {
2963 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002964 if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
2965 DCHECK(mul->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00002966 } else if (mul->InputAt(1)->IsConstant()) {
2967 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002968 } else {
2969 locations->SetInAt(1, Location::Any());
2970 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002971 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002972 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002973 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002974
2975 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002976 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002977 }
2978}
2979
2980void InstructionCodeGeneratorX86::VisitMul(HMul* mul) {
2981 LocationSummary* locations = mul->GetLocations();
2982 Location first = locations->InAt(0);
2983 Location second = locations->InAt(1);
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002984 Location out = locations->Out();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002985
2986 switch (mul->GetResultType()) {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002987 case Primitive::kPrimInt:
2988 // The constant may have ended up in a register, so test explicitly to avoid
2989 // problems where the output may not be the same as the first operand.
2990 if (mul->InputAt(1)->IsIntConstant()) {
2991 Immediate imm(mul->InputAt(1)->AsIntConstant()->GetValue());
2992 __ imull(out.AsRegister<Register>(), first.AsRegister<Register>(), imm);
2993 } else if (second.IsRegister()) {
2994 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002995 __ imull(first.AsRegister<Register>(), second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002996 } else {
2997 DCHECK(second.IsStackSlot());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002998 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002999 __ imull(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01003000 }
3001 break;
Calin Juravle34bacdf2014-10-07 20:23:36 +01003002
3003 case Primitive::kPrimLong: {
Calin Juravle34bacdf2014-10-07 20:23:36 +01003004 Register in1_hi = first.AsRegisterPairHigh<Register>();
3005 Register in1_lo = first.AsRegisterPairLow<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003006 Register eax = locations->GetTemp(0).AsRegister<Register>();
3007 Register edx = locations->GetTemp(1).AsRegister<Register>();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003008
3009 DCHECK_EQ(EAX, eax);
3010 DCHECK_EQ(EDX, edx);
3011
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003012 // input: in1 - 64 bits, in2 - 64 bits.
Calin Juravle34bacdf2014-10-07 20:23:36 +01003013 // output: in1
3014 // formula: in1.hi : in1.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
3015 // parts: in1.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
3016 // parts: in1.lo = (in1.lo * in2.lo)[31:0]
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003017 if (second.IsConstant()) {
3018 DCHECK(second.GetConstant()->IsLongConstant());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003019
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003020 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3021 int32_t low_value = Low32Bits(value);
3022 int32_t high_value = High32Bits(value);
3023 Immediate low(low_value);
3024 Immediate high(high_value);
3025
3026 __ movl(eax, high);
3027 // eax <- in1.lo * in2.hi
3028 __ imull(eax, in1_lo);
3029 // in1.hi <- in1.hi * in2.lo
3030 __ imull(in1_hi, low);
3031 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3032 __ addl(in1_hi, eax);
3033 // move in2_lo to eax to prepare for double precision
3034 __ movl(eax, low);
3035 // edx:eax <- in1.lo * in2.lo
3036 __ mull(in1_lo);
3037 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3038 __ addl(in1_hi, edx);
3039 // in1.lo <- (in1.lo * in2.lo)[31:0];
3040 __ movl(in1_lo, eax);
3041 } else if (second.IsRegisterPair()) {
3042 Register in2_hi = second.AsRegisterPairHigh<Register>();
3043 Register in2_lo = second.AsRegisterPairLow<Register>();
3044
3045 __ movl(eax, in2_hi);
3046 // eax <- in1.lo * in2.hi
3047 __ imull(eax, in1_lo);
3048 // in1.hi <- in1.hi * in2.lo
3049 __ imull(in1_hi, in2_lo);
3050 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3051 __ addl(in1_hi, eax);
3052 // move in1_lo to eax to prepare for double precision
3053 __ movl(eax, in1_lo);
3054 // edx:eax <- in1.lo * in2.lo
3055 __ mull(in2_lo);
3056 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3057 __ addl(in1_hi, edx);
3058 // in1.lo <- (in1.lo * in2.lo)[31:0];
3059 __ movl(in1_lo, eax);
3060 } else {
3061 DCHECK(second.IsDoubleStackSlot()) << second;
3062 Address in2_hi(ESP, second.GetHighStackIndex(kX86WordSize));
3063 Address in2_lo(ESP, second.GetStackIndex());
3064
3065 __ movl(eax, in2_hi);
3066 // eax <- in1.lo * in2.hi
3067 __ imull(eax, in1_lo);
3068 // in1.hi <- in1.hi * in2.lo
3069 __ imull(in1_hi, in2_lo);
3070 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3071 __ addl(in1_hi, eax);
3072 // move in1_lo to eax to prepare for double precision
3073 __ movl(eax, in1_lo);
3074 // edx:eax <- in1.lo * in2.lo
3075 __ mull(in2_lo);
3076 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3077 __ addl(in1_hi, edx);
3078 // in1.lo <- (in1.lo * in2.lo)[31:0];
3079 __ movl(in1_lo, eax);
3080 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003081
3082 break;
3083 }
3084
Calin Juravleb5bfa962014-10-21 18:02:24 +01003085 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003086 DCHECK(first.Equals(locations->Out()));
3087 if (second.IsFpuRegister()) {
3088 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3089 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3090 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003091 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003092 __ mulss(first.AsFpuRegister<XmmRegister>(),
3093 codegen_->LiteralFloatAddress(
3094 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3095 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3096 } else {
3097 DCHECK(second.IsStackSlot());
3098 __ mulss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3099 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003100 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003101 }
3102
3103 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003104 DCHECK(first.Equals(locations->Out()));
3105 if (second.IsFpuRegister()) {
3106 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3107 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3108 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003109 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003110 __ mulsd(first.AsFpuRegister<XmmRegister>(),
3111 codegen_->LiteralDoubleAddress(
3112 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3113 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3114 } else {
3115 DCHECK(second.IsDoubleStackSlot());
3116 __ mulsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3117 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003118 break;
3119 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003120
3121 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003122 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003123 }
3124}
3125
Roland Levillain232ade02015-04-20 15:14:36 +01003126void InstructionCodeGeneratorX86::PushOntoFPStack(Location source,
3127 uint32_t temp_offset,
3128 uint32_t stack_adjustment,
3129 bool is_fp,
3130 bool is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003131 if (source.IsStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01003132 DCHECK(!is_wide);
3133 if (is_fp) {
3134 __ flds(Address(ESP, source.GetStackIndex() + stack_adjustment));
3135 } else {
3136 __ filds(Address(ESP, source.GetStackIndex() + stack_adjustment));
3137 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003138 } else if (source.IsDoubleStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01003139 DCHECK(is_wide);
3140 if (is_fp) {
3141 __ fldl(Address(ESP, source.GetStackIndex() + stack_adjustment));
3142 } else {
3143 __ fildl(Address(ESP, source.GetStackIndex() + stack_adjustment));
3144 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003145 } else {
3146 // Write the value to the temporary location on the stack and load to FP stack.
Roland Levillain232ade02015-04-20 15:14:36 +01003147 if (!is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003148 Location stack_temp = Location::StackSlot(temp_offset);
3149 codegen_->Move32(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01003150 if (is_fp) {
3151 __ flds(Address(ESP, temp_offset));
3152 } else {
3153 __ filds(Address(ESP, temp_offset));
3154 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003155 } else {
3156 Location stack_temp = Location::DoubleStackSlot(temp_offset);
3157 codegen_->Move64(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01003158 if (is_fp) {
3159 __ fldl(Address(ESP, temp_offset));
3160 } else {
3161 __ fildl(Address(ESP, temp_offset));
3162 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003163 }
3164 }
3165}
3166
3167void InstructionCodeGeneratorX86::GenerateRemFP(HRem *rem) {
3168 Primitive::Type type = rem->GetResultType();
3169 bool is_float = type == Primitive::kPrimFloat;
3170 size_t elem_size = Primitive::ComponentSize(type);
3171 LocationSummary* locations = rem->GetLocations();
3172 Location first = locations->InAt(0);
3173 Location second = locations->InAt(1);
3174 Location out = locations->Out();
3175
3176 // Create stack space for 2 elements.
3177 // TODO: enhance register allocator to ask for stack temporaries.
3178 __ subl(ESP, Immediate(2 * elem_size));
3179
3180 // Load the values to the FP stack in reverse order, using temporaries if needed.
Roland Levillain232ade02015-04-20 15:14:36 +01003181 const bool is_wide = !is_float;
3182 PushOntoFPStack(second, elem_size, 2 * elem_size, /* is_fp */ true, is_wide);
3183 PushOntoFPStack(first, 0, 2 * elem_size, /* is_fp */ true, is_wide);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003184
3185 // Loop doing FPREM until we stabilize.
Mark Mendell0c9497d2015-08-21 09:30:05 -04003186 NearLabel retry;
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003187 __ Bind(&retry);
3188 __ fprem();
3189
3190 // Move FP status to AX.
3191 __ fstsw();
3192
3193 // And see if the argument reduction is complete. This is signaled by the
3194 // C2 FPU flag bit set to 0.
3195 __ andl(EAX, Immediate(kC2ConditionMask));
3196 __ j(kNotEqual, &retry);
3197
3198 // We have settled on the final value. Retrieve it into an XMM register.
3199 // Store FP top of stack to real stack.
3200 if (is_float) {
3201 __ fsts(Address(ESP, 0));
3202 } else {
3203 __ fstl(Address(ESP, 0));
3204 }
3205
3206 // Pop the 2 items from the FP stack.
3207 __ fucompp();
3208
3209 // Load the value from the stack into an XMM register.
3210 DCHECK(out.IsFpuRegister()) << out;
3211 if (is_float) {
3212 __ movss(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
3213 } else {
3214 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
3215 }
3216
3217 // And remove the temporary stack space we allocated.
3218 __ addl(ESP, Immediate(2 * elem_size));
3219}
3220
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003221
3222void InstructionCodeGeneratorX86::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3223 DCHECK(instruction->IsDiv() || instruction->IsRem());
3224
3225 LocationSummary* locations = instruction->GetLocations();
3226 DCHECK(locations->InAt(1).IsConstant());
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003227 DCHECK(locations->InAt(1).GetConstant()->IsIntConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003228
3229 Register out_register = locations->Out().AsRegister<Register>();
3230 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003231 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003232
3233 DCHECK(imm == 1 || imm == -1);
3234
3235 if (instruction->IsRem()) {
3236 __ xorl(out_register, out_register);
3237 } else {
3238 __ movl(out_register, input_register);
3239 if (imm == -1) {
3240 __ negl(out_register);
3241 }
3242 }
3243}
3244
3245
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003246void InstructionCodeGeneratorX86::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003247 LocationSummary* locations = instruction->GetLocations();
3248
3249 Register out_register = locations->Out().AsRegister<Register>();
3250 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003251 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003252 DCHECK(IsPowerOfTwo(AbsOrMin(imm)));
3253 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003254
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003255 Register num = locations->GetTemp(0).AsRegister<Register>();
3256
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003257 __ leal(num, Address(input_register, abs_imm - 1));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003258 __ testl(input_register, input_register);
3259 __ cmovl(kGreaterEqual, num, input_register);
3260 int shift = CTZ(imm);
3261 __ sarl(num, Immediate(shift));
3262
3263 if (imm < 0) {
3264 __ negl(num);
3265 }
3266
3267 __ movl(out_register, num);
3268}
3269
3270void InstructionCodeGeneratorX86::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3271 DCHECK(instruction->IsDiv() || instruction->IsRem());
3272
3273 LocationSummary* locations = instruction->GetLocations();
3274 int imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
3275
3276 Register eax = locations->InAt(0).AsRegister<Register>();
3277 Register out = locations->Out().AsRegister<Register>();
3278 Register num;
3279 Register edx;
3280
3281 if (instruction->IsDiv()) {
3282 edx = locations->GetTemp(0).AsRegister<Register>();
3283 num = locations->GetTemp(1).AsRegister<Register>();
3284 } else {
3285 edx = locations->Out().AsRegister<Register>();
3286 num = locations->GetTemp(0).AsRegister<Register>();
3287 }
3288
3289 DCHECK_EQ(EAX, eax);
3290 DCHECK_EQ(EDX, edx);
3291 if (instruction->IsDiv()) {
3292 DCHECK_EQ(EAX, out);
3293 } else {
3294 DCHECK_EQ(EDX, out);
3295 }
3296
3297 int64_t magic;
3298 int shift;
3299 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
3300
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003301 // Save the numerator.
3302 __ movl(num, eax);
3303
3304 // EAX = magic
3305 __ movl(eax, Immediate(magic));
3306
3307 // EDX:EAX = magic * numerator
3308 __ imull(num);
3309
3310 if (imm > 0 && magic < 0) {
3311 // EDX += num
3312 __ addl(edx, num);
3313 } else if (imm < 0 && magic > 0) {
3314 __ subl(edx, num);
3315 }
3316
3317 // Shift if needed.
3318 if (shift != 0) {
3319 __ sarl(edx, Immediate(shift));
3320 }
3321
3322 // EDX += 1 if EDX < 0
3323 __ movl(eax, edx);
3324 __ shrl(edx, Immediate(31));
3325 __ addl(edx, eax);
3326
3327 if (instruction->IsRem()) {
3328 __ movl(eax, num);
3329 __ imull(edx, Immediate(imm));
3330 __ subl(eax, edx);
3331 __ movl(edx, eax);
3332 } else {
3333 __ movl(eax, edx);
3334 }
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003335}
3336
Calin Juravlebacfec32014-11-14 15:54:36 +00003337void InstructionCodeGeneratorX86::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3338 DCHECK(instruction->IsDiv() || instruction->IsRem());
3339
3340 LocationSummary* locations = instruction->GetLocations();
3341 Location out = locations->Out();
3342 Location first = locations->InAt(0);
3343 Location second = locations->InAt(1);
3344 bool is_div = instruction->IsDiv();
3345
3346 switch (instruction->GetResultType()) {
3347 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003348 DCHECK_EQ(EAX, first.AsRegister<Register>());
3349 DCHECK_EQ(is_div ? EAX : EDX, out.AsRegister<Register>());
Calin Juravlebacfec32014-11-14 15:54:36 +00003350
Vladimir Marko13c86fd2015-11-11 12:37:46 +00003351 if (second.IsConstant()) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003352 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003353
3354 if (imm == 0) {
3355 // Do not generate anything for 0. DivZeroCheck would forbid any generated code.
3356 } else if (imm == 1 || imm == -1) {
3357 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003358 } else if (is_div && IsPowerOfTwo(AbsOrMin(imm))) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003359 DivByPowerOfTwo(instruction->AsDiv());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003360 } else {
3361 DCHECK(imm <= -2 || imm >= 2);
3362 GenerateDivRemWithAnyConstant(instruction);
3363 }
3364 } else {
David Srbecky9cd6d372016-02-09 15:24:47 +00003365 SlowPathCode* slow_path = new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86(
3366 instruction, out.AsRegister<Register>(), is_div);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003367 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00003368
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003369 Register second_reg = second.AsRegister<Register>();
3370 // 0x80000000/-1 triggers an arithmetic exception!
3371 // Dividing by -1 is actually negation and -0x800000000 = 0x80000000 so
3372 // it's safe to just use negl instead of more complex comparisons.
Calin Juravlebacfec32014-11-14 15:54:36 +00003373
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003374 __ cmpl(second_reg, Immediate(-1));
3375 __ j(kEqual, slow_path->GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +00003376
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003377 // edx:eax <- sign-extended of eax
3378 __ cdq();
3379 // eax = quotient, edx = remainder
3380 __ idivl(second_reg);
3381 __ Bind(slow_path->GetExitLabel());
3382 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003383 break;
3384 }
3385
3386 case Primitive::kPrimLong: {
3387 InvokeRuntimeCallingConvention calling_convention;
3388 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
3389 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
3390 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
3391 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
3392 DCHECK_EQ(EAX, out.AsRegisterPairLow<Register>());
3393 DCHECK_EQ(EDX, out.AsRegisterPairHigh<Register>());
3394
3395 if (is_div) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01003396 codegen_->InvokeRuntime(kQuickLdiv, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003397 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003398 } else {
Serban Constantinescuba45db02016-07-12 22:53:02 +01003399 codegen_->InvokeRuntime(kQuickLmod, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003400 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003401 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003402 break;
3403 }
3404
3405 default:
3406 LOG(FATAL) << "Unexpected type for GenerateDivRemIntegral " << instruction->GetResultType();
3407 }
3408}
3409
Calin Juravle7c4954d2014-10-28 16:57:40 +00003410void LocationsBuilderX86::VisitDiv(HDiv* div) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003411 LocationSummary::CallKind call_kind = (div->GetResultType() == Primitive::kPrimLong)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003412 ? LocationSummary::kCallOnMainOnly
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003413 : LocationSummary::kNoCall;
3414 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
3415
Calin Juravle7c4954d2014-10-28 16:57:40 +00003416 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00003417 case Primitive::kPrimInt: {
3418 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003419 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00003420 locations->SetOut(Location::SameAsFirstInput());
3421 // Intel uses edx:eax as the dividend.
3422 locations->AddTemp(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003423 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3424 // which enforces results to be in EAX and EDX, things are simpler if we use EAX also as
3425 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003426 if (div->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003427 locations->AddTemp(Location::RequiresRegister());
3428 }
Calin Juravled0d48522014-11-04 16:40:20 +00003429 break;
3430 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003431 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003432 InvokeRuntimeCallingConvention calling_convention;
3433 locations->SetInAt(0, Location::RegisterPairLocation(
3434 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3435 locations->SetInAt(1, Location::RegisterPairLocation(
3436 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3437 // Runtime helper puts the result in EAX, EDX.
3438 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Calin Juravle7c4954d2014-10-28 16:57:40 +00003439 break;
3440 }
3441 case Primitive::kPrimFloat:
3442 case Primitive::kPrimDouble: {
3443 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003444 if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3445 DCHECK(div->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003446 } else if (div->InputAt(1)->IsConstant()) {
3447 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003448 } else {
3449 locations->SetInAt(1, Location::Any());
3450 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003451 locations->SetOut(Location::SameAsFirstInput());
3452 break;
3453 }
3454
3455 default:
3456 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3457 }
3458}
3459
3460void InstructionCodeGeneratorX86::VisitDiv(HDiv* div) {
3461 LocationSummary* locations = div->GetLocations();
3462 Location first = locations->InAt(0);
3463 Location second = locations->InAt(1);
Calin Juravle7c4954d2014-10-28 16:57:40 +00003464
3465 switch (div->GetResultType()) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003466 case Primitive::kPrimInt:
Calin Juravle7c4954d2014-10-28 16:57:40 +00003467 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003468 GenerateDivRemIntegral(div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00003469 break;
3470 }
3471
3472 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003473 if (second.IsFpuRegister()) {
3474 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3475 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3476 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003477 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003478 __ divss(first.AsFpuRegister<XmmRegister>(),
3479 codegen_->LiteralFloatAddress(
3480 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3481 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3482 } else {
3483 DCHECK(second.IsStackSlot());
3484 __ divss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3485 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003486 break;
3487 }
3488
3489 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003490 if (second.IsFpuRegister()) {
3491 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3492 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3493 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003494 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003495 __ divsd(first.AsFpuRegister<XmmRegister>(),
3496 codegen_->LiteralDoubleAddress(
3497 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3498 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3499 } else {
3500 DCHECK(second.IsDoubleStackSlot());
3501 __ divsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3502 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003503 break;
3504 }
3505
3506 default:
3507 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3508 }
3509}
3510
Calin Juravlebacfec32014-11-14 15:54:36 +00003511void LocationsBuilderX86::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00003512 Primitive::Type type = rem->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003513
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003514 LocationSummary::CallKind call_kind = (rem->GetResultType() == Primitive::kPrimLong)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003515 ? LocationSummary::kCallOnMainOnly
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003516 : LocationSummary::kNoCall;
3517 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
Calin Juravlebacfec32014-11-14 15:54:36 +00003518
Calin Juravled2ec87d2014-12-08 14:24:46 +00003519 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003520 case Primitive::kPrimInt: {
3521 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003522 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00003523 locations->SetOut(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003524 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3525 // which enforces results to be in EAX and EDX, things are simpler if we use EDX also as
3526 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003527 if (rem->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003528 locations->AddTemp(Location::RequiresRegister());
3529 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003530 break;
3531 }
3532 case Primitive::kPrimLong: {
3533 InvokeRuntimeCallingConvention calling_convention;
3534 locations->SetInAt(0, Location::RegisterPairLocation(
3535 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3536 locations->SetInAt(1, Location::RegisterPairLocation(
3537 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3538 // Runtime helper puts the result in EAX, EDX.
3539 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
3540 break;
3541 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003542 case Primitive::kPrimDouble:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003543 case Primitive::kPrimFloat: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003544 locations->SetInAt(0, Location::Any());
3545 locations->SetInAt(1, Location::Any());
3546 locations->SetOut(Location::RequiresFpuRegister());
3547 locations->AddTemp(Location::RegisterLocation(EAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00003548 break;
3549 }
3550
3551 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003552 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003553 }
3554}
3555
3556void InstructionCodeGeneratorX86::VisitRem(HRem* rem) {
3557 Primitive::Type type = rem->GetResultType();
3558 switch (type) {
3559 case Primitive::kPrimInt:
3560 case Primitive::kPrimLong: {
3561 GenerateDivRemIntegral(rem);
3562 break;
3563 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003564 case Primitive::kPrimFloat:
Calin Juravlebacfec32014-11-14 15:54:36 +00003565 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003566 GenerateRemFP(rem);
Calin Juravlebacfec32014-11-14 15:54:36 +00003567 break;
3568 }
3569 default:
3570 LOG(FATAL) << "Unexpected rem type " << type;
3571 }
3572}
3573
Calin Juravled0d48522014-11-04 16:40:20 +00003574void LocationsBuilderX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003575 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003576 switch (instruction->GetType()) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003577 case Primitive::kPrimBoolean:
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003578 case Primitive::kPrimByte:
3579 case Primitive::kPrimChar:
3580 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003581 case Primitive::kPrimInt: {
3582 locations->SetInAt(0, Location::Any());
3583 break;
3584 }
3585 case Primitive::kPrimLong: {
3586 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
3587 if (!instruction->IsConstant()) {
3588 locations->AddTemp(Location::RequiresRegister());
3589 }
3590 break;
3591 }
3592 default:
3593 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
3594 }
Calin Juravled0d48522014-11-04 16:40:20 +00003595}
3596
3597void InstructionCodeGeneratorX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003598 SlowPathCode* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86(instruction);
Calin Juravled0d48522014-11-04 16:40:20 +00003599 codegen_->AddSlowPath(slow_path);
3600
3601 LocationSummary* locations = instruction->GetLocations();
3602 Location value = locations->InAt(0);
3603
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003604 switch (instruction->GetType()) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003605 case Primitive::kPrimBoolean:
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003606 case Primitive::kPrimByte:
3607 case Primitive::kPrimChar:
3608 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003609 case Primitive::kPrimInt: {
3610 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003611 __ testl(value.AsRegister<Register>(), value.AsRegister<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003612 __ j(kEqual, slow_path->GetEntryLabel());
3613 } else if (value.IsStackSlot()) {
3614 __ cmpl(Address(ESP, value.GetStackIndex()), Immediate(0));
3615 __ j(kEqual, slow_path->GetEntryLabel());
3616 } else {
3617 DCHECK(value.IsConstant()) << value;
3618 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01003619 __ jmp(slow_path->GetEntryLabel());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003620 }
3621 }
3622 break;
Calin Juravled0d48522014-11-04 16:40:20 +00003623 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003624 case Primitive::kPrimLong: {
3625 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003626 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003627 __ movl(temp, value.AsRegisterPairLow<Register>());
3628 __ orl(temp, value.AsRegisterPairHigh<Register>());
3629 __ j(kEqual, slow_path->GetEntryLabel());
3630 } else {
3631 DCHECK(value.IsConstant()) << value;
3632 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
3633 __ jmp(slow_path->GetEntryLabel());
3634 }
3635 }
3636 break;
3637 }
3638 default:
3639 LOG(FATAL) << "Unexpected type for HDivZeroCheck" << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00003640 }
Calin Juravled0d48522014-11-04 16:40:20 +00003641}
3642
Calin Juravle9aec02f2014-11-18 23:06:35 +00003643void LocationsBuilderX86::HandleShift(HBinaryOperation* op) {
3644 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3645
3646 LocationSummary* locations =
3647 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
3648
3649 switch (op->GetResultType()) {
Mark P Mendell73945692015-04-29 14:56:17 +00003650 case Primitive::kPrimInt:
Calin Juravle9aec02f2014-11-18 23:06:35 +00003651 case Primitive::kPrimLong: {
Mark P Mendell73945692015-04-29 14:56:17 +00003652 // Can't have Location::Any() and output SameAsFirstInput()
Calin Juravle9aec02f2014-11-18 23:06:35 +00003653 locations->SetInAt(0, Location::RequiresRegister());
Mark P Mendell73945692015-04-29 14:56:17 +00003654 // The shift count needs to be in CL or a constant.
3655 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, op->InputAt(1)));
Calin Juravle9aec02f2014-11-18 23:06:35 +00003656 locations->SetOut(Location::SameAsFirstInput());
3657 break;
3658 }
3659 default:
3660 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
3661 }
3662}
3663
3664void InstructionCodeGeneratorX86::HandleShift(HBinaryOperation* op) {
3665 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3666
3667 LocationSummary* locations = op->GetLocations();
3668 Location first = locations->InAt(0);
3669 Location second = locations->InAt(1);
3670 DCHECK(first.Equals(locations->Out()));
3671
3672 switch (op->GetResultType()) {
3673 case Primitive::kPrimInt: {
Mark P Mendell73945692015-04-29 14:56:17 +00003674 DCHECK(first.IsRegister());
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003675 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003676 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003677 Register second_reg = second.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003678 DCHECK_EQ(ECX, second_reg);
3679 if (op->IsShl()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003680 __ shll(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003681 } else if (op->IsShr()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003682 __ sarl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003683 } else {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003684 __ shrl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003685 }
3686 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003687 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance;
Mark P Mendell73945692015-04-29 14:56:17 +00003688 if (shift == 0) {
3689 return;
3690 }
3691 Immediate imm(shift);
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003692 if (op->IsShl()) {
3693 __ shll(first_reg, imm);
3694 } else if (op->IsShr()) {
3695 __ sarl(first_reg, imm);
3696 } else {
3697 __ shrl(first_reg, imm);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003698 }
3699 }
3700 break;
3701 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003702 case Primitive::kPrimLong: {
Mark P Mendell73945692015-04-29 14:56:17 +00003703 if (second.IsRegister()) {
3704 Register second_reg = second.AsRegister<Register>();
3705 DCHECK_EQ(ECX, second_reg);
3706 if (op->IsShl()) {
3707 GenerateShlLong(first, second_reg);
3708 } else if (op->IsShr()) {
3709 GenerateShrLong(first, second_reg);
3710 } else {
3711 GenerateUShrLong(first, second_reg);
3712 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003713 } else {
Mark P Mendell73945692015-04-29 14:56:17 +00003714 // Shift by a constant.
Roland Levillain5b5b9312016-03-22 14:57:31 +00003715 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance;
Mark P Mendell73945692015-04-29 14:56:17 +00003716 // Nothing to do if the shift is 0, as the input is already the output.
3717 if (shift != 0) {
3718 if (op->IsShl()) {
3719 GenerateShlLong(first, shift);
3720 } else if (op->IsShr()) {
3721 GenerateShrLong(first, shift);
3722 } else {
3723 GenerateUShrLong(first, shift);
3724 }
3725 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003726 }
3727 break;
3728 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003729 default:
3730 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
3731 }
3732}
3733
Mark P Mendell73945692015-04-29 14:56:17 +00003734void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, int shift) {
3735 Register low = loc.AsRegisterPairLow<Register>();
3736 Register high = loc.AsRegisterPairHigh<Register>();
Mark Mendellba56d062015-05-05 21:34:03 -04003737 if (shift == 1) {
3738 // This is just an addition.
3739 __ addl(low, low);
3740 __ adcl(high, high);
3741 } else if (shift == 32) {
Mark P Mendell73945692015-04-29 14:56:17 +00003742 // Shift by 32 is easy. High gets low, and low gets 0.
3743 codegen_->EmitParallelMoves(
3744 loc.ToLow(),
3745 loc.ToHigh(),
3746 Primitive::kPrimInt,
3747 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
3748 loc.ToLow(),
3749 Primitive::kPrimInt);
3750 } else if (shift > 32) {
3751 // Low part becomes 0. High part is low part << (shift-32).
3752 __ movl(high, low);
3753 __ shll(high, Immediate(shift - 32));
3754 __ xorl(low, low);
3755 } else {
3756 // Between 1 and 31.
3757 __ shld(high, low, Immediate(shift));
3758 __ shll(low, Immediate(shift));
3759 }
3760}
3761
Calin Juravle9aec02f2014-11-18 23:06:35 +00003762void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04003763 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00003764 __ shld(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>(), shifter);
3765 __ shll(loc.AsRegisterPairLow<Register>(), shifter);
3766 __ testl(shifter, Immediate(32));
3767 __ j(kEqual, &done);
3768 __ movl(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>());
3769 __ movl(loc.AsRegisterPairLow<Register>(), Immediate(0));
3770 __ Bind(&done);
3771}
3772
Mark P Mendell73945692015-04-29 14:56:17 +00003773void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, int shift) {
3774 Register low = loc.AsRegisterPairLow<Register>();
3775 Register high = loc.AsRegisterPairHigh<Register>();
3776 if (shift == 32) {
3777 // Need to copy the sign.
3778 DCHECK_NE(low, high);
3779 __ movl(low, high);
3780 __ sarl(high, Immediate(31));
3781 } else if (shift > 32) {
3782 DCHECK_NE(low, high);
3783 // High part becomes sign. Low part is shifted by shift - 32.
3784 __ movl(low, high);
3785 __ sarl(high, Immediate(31));
3786 __ sarl(low, Immediate(shift - 32));
3787 } else {
3788 // Between 1 and 31.
3789 __ shrd(low, high, Immediate(shift));
3790 __ sarl(high, Immediate(shift));
3791 }
3792}
3793
Calin Juravle9aec02f2014-11-18 23:06:35 +00003794void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04003795 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00003796 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
3797 __ sarl(loc.AsRegisterPairHigh<Register>(), shifter);
3798 __ testl(shifter, Immediate(32));
3799 __ j(kEqual, &done);
3800 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
3801 __ sarl(loc.AsRegisterPairHigh<Register>(), Immediate(31));
3802 __ Bind(&done);
3803}
3804
Mark P Mendell73945692015-04-29 14:56:17 +00003805void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, int shift) {
3806 Register low = loc.AsRegisterPairLow<Register>();
3807 Register high = loc.AsRegisterPairHigh<Register>();
3808 if (shift == 32) {
3809 // Shift by 32 is easy. Low gets high, and high gets 0.
3810 codegen_->EmitParallelMoves(
3811 loc.ToHigh(),
3812 loc.ToLow(),
3813 Primitive::kPrimInt,
3814 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
3815 loc.ToHigh(),
3816 Primitive::kPrimInt);
3817 } else if (shift > 32) {
3818 // Low part is high >> (shift - 32). High part becomes 0.
3819 __ movl(low, high);
3820 __ shrl(low, Immediate(shift - 32));
3821 __ xorl(high, high);
3822 } else {
3823 // Between 1 and 31.
3824 __ shrd(low, high, Immediate(shift));
3825 __ shrl(high, Immediate(shift));
3826 }
3827}
3828
Calin Juravle9aec02f2014-11-18 23:06:35 +00003829void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04003830 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00003831 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
3832 __ shrl(loc.AsRegisterPairHigh<Register>(), shifter);
3833 __ testl(shifter, Immediate(32));
3834 __ j(kEqual, &done);
3835 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
3836 __ movl(loc.AsRegisterPairHigh<Register>(), Immediate(0));
3837 __ Bind(&done);
3838}
3839
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003840void LocationsBuilderX86::VisitRor(HRor* ror) {
3841 LocationSummary* locations =
3842 new (GetGraph()->GetArena()) LocationSummary(ror, LocationSummary::kNoCall);
3843
3844 switch (ror->GetResultType()) {
3845 case Primitive::kPrimLong:
3846 // Add the temporary needed.
3847 locations->AddTemp(Location::RequiresRegister());
3848 FALLTHROUGH_INTENDED;
3849 case Primitive::kPrimInt:
3850 locations->SetInAt(0, Location::RequiresRegister());
3851 // The shift count needs to be in CL (unless it is a constant).
3852 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, ror->InputAt(1)));
3853 locations->SetOut(Location::SameAsFirstInput());
3854 break;
3855 default:
3856 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
3857 UNREACHABLE();
3858 }
3859}
3860
3861void InstructionCodeGeneratorX86::VisitRor(HRor* ror) {
3862 LocationSummary* locations = ror->GetLocations();
3863 Location first = locations->InAt(0);
3864 Location second = locations->InAt(1);
3865
3866 if (ror->GetResultType() == Primitive::kPrimInt) {
3867 Register first_reg = first.AsRegister<Register>();
3868 if (second.IsRegister()) {
3869 Register second_reg = second.AsRegister<Register>();
3870 __ rorl(first_reg, second_reg);
3871 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003872 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003873 __ rorl(first_reg, imm);
3874 }
3875 return;
3876 }
3877
3878 DCHECK_EQ(ror->GetResultType(), Primitive::kPrimLong);
3879 Register first_reg_lo = first.AsRegisterPairLow<Register>();
3880 Register first_reg_hi = first.AsRegisterPairHigh<Register>();
3881 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
3882 if (second.IsRegister()) {
3883 Register second_reg = second.AsRegister<Register>();
3884 DCHECK_EQ(second_reg, ECX);
3885 __ movl(temp_reg, first_reg_hi);
3886 __ shrd(first_reg_hi, first_reg_lo, second_reg);
3887 __ shrd(first_reg_lo, temp_reg, second_reg);
3888 __ movl(temp_reg, first_reg_hi);
3889 __ testl(second_reg, Immediate(32));
3890 __ cmovl(kNotEqual, first_reg_hi, first_reg_lo);
3891 __ cmovl(kNotEqual, first_reg_lo, temp_reg);
3892 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003893 int32_t shift_amt = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance;
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003894 if (shift_amt == 0) {
3895 // Already fine.
3896 return;
3897 }
3898 if (shift_amt == 32) {
3899 // Just swap.
3900 __ movl(temp_reg, first_reg_lo);
3901 __ movl(first_reg_lo, first_reg_hi);
3902 __ movl(first_reg_hi, temp_reg);
3903 return;
3904 }
3905
3906 Immediate imm(shift_amt);
3907 // Save the constents of the low value.
3908 __ movl(temp_reg, first_reg_lo);
3909
3910 // Shift right into low, feeding bits from high.
3911 __ shrd(first_reg_lo, first_reg_hi, imm);
3912
3913 // Shift right into high, feeding bits from the original low.
3914 __ shrd(first_reg_hi, temp_reg, imm);
3915
3916 // Swap if needed.
3917 if (shift_amt > 32) {
3918 __ movl(temp_reg, first_reg_lo);
3919 __ movl(first_reg_lo, first_reg_hi);
3920 __ movl(first_reg_hi, temp_reg);
3921 }
3922 }
3923}
3924
Calin Juravle9aec02f2014-11-18 23:06:35 +00003925void LocationsBuilderX86::VisitShl(HShl* shl) {
3926 HandleShift(shl);
3927}
3928
3929void InstructionCodeGeneratorX86::VisitShl(HShl* shl) {
3930 HandleShift(shl);
3931}
3932
3933void LocationsBuilderX86::VisitShr(HShr* shr) {
3934 HandleShift(shr);
3935}
3936
3937void InstructionCodeGeneratorX86::VisitShr(HShr* shr) {
3938 HandleShift(shr);
3939}
3940
3941void LocationsBuilderX86::VisitUShr(HUShr* ushr) {
3942 HandleShift(ushr);
3943}
3944
3945void InstructionCodeGeneratorX86::VisitUShr(HUShr* ushr) {
3946 HandleShift(ushr);
3947}
3948
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003949void LocationsBuilderX86::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003950 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003951 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003952 locations->SetOut(Location::RegisterLocation(EAX));
David Brazdil6de19382016-01-08 17:37:10 +00003953 if (instruction->IsStringAlloc()) {
3954 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
3955 } else {
3956 InvokeRuntimeCallingConvention calling_convention;
3957 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3958 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3959 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003960}
3961
3962void InstructionCodeGeneratorX86::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01003963 // Note: if heap poisoning is enabled, the entry point takes cares
3964 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00003965 if (instruction->IsStringAlloc()) {
3966 // String is allocated through StringFactory. Call NewEmptyString entry point.
3967 Register temp = instruction->GetLocations()->GetTemp(0).AsRegister<Register>();
Andreas Gampe542451c2016-07-26 09:02:02 -07003968 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86PointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00003969 __ fs()->movl(temp, Address::Absolute(QUICK_ENTRY_POINT(pNewEmptyString)));
3970 __ call(Address(temp, code_offset.Int32Value()));
3971 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3972 } else {
Serban Constantinescuba45db02016-07-12 22:53:02 +01003973 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
David Brazdil6de19382016-01-08 17:37:10 +00003974 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
3975 DCHECK(!codegen_->IsLeafMethod());
3976 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003977}
3978
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003979void LocationsBuilderX86::VisitNewArray(HNewArray* instruction) {
3980 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003981 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003982 locations->SetOut(Location::RegisterLocation(EAX));
3983 InvokeRuntimeCallingConvention calling_convention;
3984 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003985 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003986 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003987}
3988
3989void InstructionCodeGeneratorX86::VisitNewArray(HNewArray* instruction) {
3990 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003991 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
Roland Levillain4d027112015-07-01 15:41:14 +01003992 // Note: if heap poisoning is enabled, the entry point takes cares
3993 // of poisoning the reference.
Serban Constantinescuba45db02016-07-12 22:53:02 +01003994 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003995 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003996 DCHECK(!codegen_->IsLeafMethod());
3997}
3998
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003999void LocationsBuilderX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004000 LocationSummary* locations =
4001 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01004002 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
4003 if (location.IsStackSlot()) {
4004 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4005 } else if (location.IsDoubleStackSlot()) {
4006 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004007 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01004008 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004009}
4010
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004011void InstructionCodeGeneratorX86::VisitParameterValue(
4012 HParameterValue* instruction ATTRIBUTE_UNUSED) {
4013}
4014
4015void LocationsBuilderX86::VisitCurrentMethod(HCurrentMethod* instruction) {
4016 LocationSummary* locations =
4017 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4018 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
4019}
4020
4021void InstructionCodeGeneratorX86::VisitCurrentMethod(HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004022}
4023
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004024void LocationsBuilderX86::VisitClassTableGet(HClassTableGet* instruction) {
4025 LocationSummary* locations =
4026 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4027 locations->SetInAt(0, Location::RequiresRegister());
4028 locations->SetOut(Location::RequiresRegister());
4029}
4030
4031void InstructionCodeGeneratorX86::VisitClassTableGet(HClassTableGet* instruction) {
4032 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004033 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004034 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004035 instruction->GetIndex(), kX86PointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004036 __ movl(locations->Out().AsRegister<Register>(),
4037 Address(locations->InAt(0).AsRegister<Register>(), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004038 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004039 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00004040 instruction->GetIndex(), kX86PointerSize));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004041 __ movl(locations->Out().AsRegister<Register>(),
4042 Address(locations->InAt(0).AsRegister<Register>(),
4043 mirror::Class::ImtPtrOffset(kX86PointerSize).Uint32Value()));
4044 // temp = temp->GetImtEntryAt(method_offset);
4045 __ movl(locations->Out().AsRegister<Register>(),
4046 Address(locations->Out().AsRegister<Register>(), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004047 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004048}
4049
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004050void LocationsBuilderX86::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004051 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004052 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01004053 locations->SetInAt(0, Location::RequiresRegister());
4054 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004055}
4056
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004057void InstructionCodeGeneratorX86::VisitNot(HNot* not_) {
4058 LocationSummary* locations = not_->GetLocations();
Roland Levillain70566432014-10-24 16:20:17 +01004059 Location in = locations->InAt(0);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01004060 Location out = locations->Out();
Roland Levillain70566432014-10-24 16:20:17 +01004061 DCHECK(in.Equals(out));
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00004062 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004063 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00004064 __ notl(out.AsRegister<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004065 break;
4066
4067 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01004068 __ notl(out.AsRegisterPairLow<Register>());
4069 __ notl(out.AsRegisterPairHigh<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004070 break;
4071
4072 default:
4073 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
4074 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004075}
4076
David Brazdil66d126e2015-04-03 16:02:44 +01004077void LocationsBuilderX86::VisitBooleanNot(HBooleanNot* bool_not) {
4078 LocationSummary* locations =
4079 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
4080 locations->SetInAt(0, Location::RequiresRegister());
4081 locations->SetOut(Location::SameAsFirstInput());
4082}
4083
4084void InstructionCodeGeneratorX86::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01004085 LocationSummary* locations = bool_not->GetLocations();
4086 Location in = locations->InAt(0);
4087 Location out = locations->Out();
4088 DCHECK(in.Equals(out));
4089 __ xorl(out.AsRegister<Register>(), Immediate(1));
4090}
4091
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004092void LocationsBuilderX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004093 LocationSummary* locations =
4094 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00004095 switch (compare->InputAt(0)->GetType()) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00004096 case Primitive::kPrimBoolean:
4097 case Primitive::kPrimByte:
4098 case Primitive::kPrimShort:
4099 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08004100 case Primitive::kPrimInt:
Calin Juravleddb7df22014-11-25 20:56:51 +00004101 case Primitive::kPrimLong: {
4102 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravleddb7df22014-11-25 20:56:51 +00004103 locations->SetInAt(1, Location::Any());
4104 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4105 break;
4106 }
4107 case Primitive::kPrimFloat:
4108 case Primitive::kPrimDouble: {
4109 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004110 if (compare->InputAt(1)->IsX86LoadFromConstantTable()) {
4111 DCHECK(compare->InputAt(1)->IsEmittedAtUseSite());
4112 } else if (compare->InputAt(1)->IsConstant()) {
4113 locations->SetInAt(1, Location::RequiresFpuRegister());
4114 } else {
4115 locations->SetInAt(1, Location::Any());
4116 }
Calin Juravleddb7df22014-11-25 20:56:51 +00004117 locations->SetOut(Location::RequiresRegister());
4118 break;
4119 }
4120 default:
4121 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
4122 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004123}
4124
4125void InstructionCodeGeneratorX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004126 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004127 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00004128 Location left = locations->InAt(0);
4129 Location right = locations->InAt(1);
4130
Mark Mendell0c9497d2015-08-21 09:30:05 -04004131 NearLabel less, greater, done;
Aart Bika19616e2016-02-01 18:57:58 -08004132 Condition less_cond = kLess;
4133
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004134 switch (compare->InputAt(0)->GetType()) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00004135 case Primitive::kPrimBoolean:
4136 case Primitive::kPrimByte:
4137 case Primitive::kPrimShort:
4138 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08004139 case Primitive::kPrimInt: {
Roland Levillain0b671c02016-08-19 12:02:34 +01004140 codegen_->GenerateIntCompare(left, right);
Aart Bika19616e2016-02-01 18:57:58 -08004141 break;
4142 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004143 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004144 Register left_low = left.AsRegisterPairLow<Register>();
4145 Register left_high = left.AsRegisterPairHigh<Register>();
4146 int32_t val_low = 0;
4147 int32_t val_high = 0;
4148 bool right_is_const = false;
4149
4150 if (right.IsConstant()) {
4151 DCHECK(right.GetConstant()->IsLongConstant());
4152 right_is_const = true;
4153 int64_t val = right.GetConstant()->AsLongConstant()->GetValue();
4154 val_low = Low32Bits(val);
4155 val_high = High32Bits(val);
4156 }
4157
Calin Juravleddb7df22014-11-25 20:56:51 +00004158 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004159 __ cmpl(left_high, right.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004160 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004161 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004162 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004163 DCHECK(right_is_const) << right;
Aart Bika19616e2016-02-01 18:57:58 -08004164 codegen_->Compare32BitValue(left_high, val_high);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004165 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004166 __ j(kLess, &less); // Signed compare.
4167 __ j(kGreater, &greater); // Signed compare.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004168 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004169 __ cmpl(left_low, right.AsRegisterPairLow<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004170 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004171 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004172 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004173 DCHECK(right_is_const) << right;
Aart Bika19616e2016-02-01 18:57:58 -08004174 codegen_->Compare32BitValue(left_low, val_low);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004175 }
Aart Bika19616e2016-02-01 18:57:58 -08004176 less_cond = kBelow; // for CF (unsigned).
Calin Juravleddb7df22014-11-25 20:56:51 +00004177 break;
4178 }
4179 case Primitive::kPrimFloat: {
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004180 GenerateFPCompare(left, right, compare, false);
Calin Juravleddb7df22014-11-25 20:56:51 +00004181 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08004182 less_cond = kBelow; // for CF (floats).
Calin Juravleddb7df22014-11-25 20:56:51 +00004183 break;
4184 }
4185 case Primitive::kPrimDouble: {
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004186 GenerateFPCompare(left, right, compare, true);
Calin Juravleddb7df22014-11-25 20:56:51 +00004187 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08004188 less_cond = kBelow; // for CF (floats).
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004189 break;
4190 }
4191 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00004192 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004193 }
Aart Bika19616e2016-02-01 18:57:58 -08004194
Calin Juravleddb7df22014-11-25 20:56:51 +00004195 __ movl(out, Immediate(0));
4196 __ j(kEqual, &done);
Aart Bika19616e2016-02-01 18:57:58 -08004197 __ j(less_cond, &less);
Calin Juravleddb7df22014-11-25 20:56:51 +00004198
4199 __ Bind(&greater);
4200 __ movl(out, Immediate(1));
4201 __ jmp(&done);
4202
4203 __ Bind(&less);
4204 __ movl(out, Immediate(-1));
4205
4206 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004207}
4208
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004209void LocationsBuilderX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004210 LocationSummary* locations =
4211 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko372f10e2016-05-17 16:30:10 +01004212 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01004213 locations->SetInAt(i, Location::Any());
4214 }
4215 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004216}
4217
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004218void InstructionCodeGeneratorX86::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004219 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004220}
4221
Roland Levillain7c1559a2015-12-15 10:55:36 +00004222void CodeGeneratorX86::GenerateMemoryBarrier(MemBarrierKind kind) {
Calin Juravle52c48962014-12-16 17:02:57 +00004223 /*
4224 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
4225 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
4226 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
4227 */
4228 switch (kind) {
4229 case MemBarrierKind::kAnyAny: {
Mark P Mendell17077d82015-12-16 19:15:59 +00004230 MemoryFence();
Calin Juravle52c48962014-12-16 17:02:57 +00004231 break;
4232 }
4233 case MemBarrierKind::kAnyStore:
4234 case MemBarrierKind::kLoadAny:
4235 case MemBarrierKind::kStoreStore: {
4236 // nop
4237 break;
4238 }
Mark Mendell7aa04a12016-01-27 22:39:07 -05004239 case MemBarrierKind::kNTStoreStore:
4240 // Non-Temporal Store/Store needs an explicit fence.
4241 MemoryFence(/* non-temporal */ true);
4242 break;
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004243 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004244}
4245
Vladimir Markodc151b22015-10-15 18:02:30 +01004246HInvokeStaticOrDirect::DispatchInfo CodeGeneratorX86::GetSupportedInvokeStaticOrDirectDispatch(
4247 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004248 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00004249 HInvokeStaticOrDirect::DispatchInfo dispatch_info = desired_dispatch_info;
4250
4251 // We disable pc-relative load when there is an irreducible loop, as the optimization
4252 // is incompatible with it.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004253 // TODO: Create as many X86ComputeBaseMethodAddress instructions
4254 // as needed for methods with irreducible loops.
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00004255 if (GetGraph()->HasIrreducibleLoops() &&
4256 (dispatch_info.method_load_kind ==
4257 HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative)) {
4258 dispatch_info.method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod;
4259 }
4260 switch (dispatch_info.code_ptr_location) {
Vladimir Markodc151b22015-10-15 18:02:30 +01004261 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
4262 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
4263 // For direct code, we actually prefer to call via the code pointer from ArtMethod*.
4264 // (Though the direct CALL ptr16:32 is available for consideration).
4265 return HInvokeStaticOrDirect::DispatchInfo {
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00004266 dispatch_info.method_load_kind,
Vladimir Markodc151b22015-10-15 18:02:30 +01004267 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00004268 dispatch_info.method_load_data,
Vladimir Markodc151b22015-10-15 18:02:30 +01004269 0u
4270 };
4271 default:
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00004272 return dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01004273 }
4274}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004275
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004276Register CodeGeneratorX86::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
4277 Register temp) {
4278 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
Vladimir Markoc53c0792015-11-19 15:48:33 +00004279 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004280 if (!invoke->GetLocations()->Intrinsified()) {
4281 return location.AsRegister<Register>();
4282 }
4283 // For intrinsics we allow any location, so it may be on the stack.
4284 if (!location.IsRegister()) {
4285 __ movl(temp, Address(ESP, location.GetStackIndex()));
4286 return temp;
4287 }
4288 // For register locations, check if the register was saved. If so, get it from the stack.
4289 // Note: There is a chance that the register was saved but not overwritten, so we could
4290 // save one load. However, since this is just an intrinsic slow path we prefer this
4291 // simple and more robust approach rather that trying to determine if that's the case.
4292 SlowPathCode* slow_path = GetCurrentSlowPath();
Serguei Katkov288c7a82016-05-16 11:53:15 +06004293 if (slow_path != nullptr) {
4294 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
4295 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
4296 __ movl(temp, Address(ESP, stack_offset));
4297 return temp;
4298 }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004299 }
4300 return location.AsRegister<Register>();
4301}
4302
Serguei Katkov288c7a82016-05-16 11:53:15 +06004303Location CodeGeneratorX86::GenerateCalleeMethodStaticOrDirectCall(HInvokeStaticOrDirect* invoke,
4304 Location temp) {
Vladimir Marko58155012015-08-19 12:49:41 +00004305 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
4306 switch (invoke->GetMethodLoadKind()) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004307 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
Vladimir Marko58155012015-08-19 12:49:41 +00004308 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004309 uint32_t offset =
4310 GetThreadOffset<kX86PointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
4311 __ fs()->movl(temp.AsRegister<Register>(), Address::Absolute(offset));
Vladimir Marko58155012015-08-19 12:49:41 +00004312 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004313 }
Vladimir Marko58155012015-08-19 12:49:41 +00004314 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00004315 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004316 break;
4317 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
4318 __ movl(temp.AsRegister<Register>(), Immediate(invoke->GetMethodAddress()));
4319 break;
4320 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004321 __ movl(temp.AsRegister<Register>(), Immediate(/* placeholder */ 0));
Vladimir Markoaad75c62016-10-03 08:46:48 +00004322 method_patches_.emplace_back(*invoke->GetTargetMethod().dex_file,
4323 invoke->GetTargetMethod().dex_method_index);
Vladimir Marko58155012015-08-19 12:49:41 +00004324 __ Bind(&method_patches_.back().label); // Bind the label at the end of the "movl" insn.
4325 break;
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004326 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
4327 Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
4328 temp.AsRegister<Register>());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004329 __ movl(temp.AsRegister<Register>(), Address(base_reg, kDummy32BitOffset));
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004330 // Bind a new fixup label at the end of the "movl" insn.
4331 uint32_t offset = invoke->GetDexCacheArrayOffset();
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004332 __ Bind(NewPcRelativeDexCacheArrayPatch(invoke->GetDexFile(), offset));
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004333 break;
4334 }
Vladimir Marko58155012015-08-19 12:49:41 +00004335 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004336 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004337 Register method_reg;
4338 Register reg = temp.AsRegister<Register>();
4339 if (current_method.IsRegister()) {
4340 method_reg = current_method.AsRegister<Register>();
4341 } else {
David Brazdil58282f42016-01-14 12:45:10 +00004342 DCHECK(invoke->GetLocations()->Intrinsified());
Vladimir Marko58155012015-08-19 12:49:41 +00004343 DCHECK(!current_method.IsValid());
4344 method_reg = reg;
4345 __ movl(reg, Address(ESP, kCurrentMethodStackOffset));
4346 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00004347 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +01004348 __ movl(reg, Address(method_reg,
4349 ArtMethod::DexCacheResolvedMethodsOffset(kX86PointerSize).Int32Value()));
Vladimir Marko40ecb122016-04-06 17:33:41 +01004350 // temp = temp[index_in_cache];
4351 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
4352 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Vladimir Marko58155012015-08-19 12:49:41 +00004353 __ movl(reg, Address(reg, CodeGenerator::GetCachePointerOffset(index_in_cache)));
4354 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +01004355 }
Vladimir Marko58155012015-08-19 12:49:41 +00004356 }
Serguei Katkov288c7a82016-05-16 11:53:15 +06004357 return callee_method;
4358}
4359
4360void CodeGeneratorX86::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
4361 Location callee_method = GenerateCalleeMethodStaticOrDirectCall(invoke, temp);
Vladimir Marko58155012015-08-19 12:49:41 +00004362
4363 switch (invoke->GetCodePtrLocation()) {
4364 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
4365 __ call(GetFrameEntryLabel());
4366 break;
4367 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative: {
Vladimir Markoaad75c62016-10-03 08:46:48 +00004368 relative_call_patches_.emplace_back(*invoke->GetTargetMethod().dex_file,
4369 invoke->GetTargetMethod().dex_method_index);
Vladimir Marko58155012015-08-19 12:49:41 +00004370 Label* label = &relative_call_patches_.back().label;
4371 __ call(label); // Bind to the patch label, override at link time.
4372 __ Bind(label); // Bind the label at the end of the "call" insn.
4373 break;
4374 }
4375 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
4376 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
Vladimir Markodc151b22015-10-15 18:02:30 +01004377 // Filtered out by GetSupportedInvokeStaticOrDirectDispatch().
4378 LOG(FATAL) << "Unsupported";
4379 UNREACHABLE();
Vladimir Marko58155012015-08-19 12:49:41 +00004380 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
4381 // (callee_method + offset_of_quick_compiled_code)()
4382 __ call(Address(callee_method.AsRegister<Register>(),
4383 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07004384 kX86PointerSize).Int32Value()));
Vladimir Marko58155012015-08-19 12:49:41 +00004385 break;
Mark Mendell09ed1a32015-03-25 08:30:06 -04004386 }
4387
4388 DCHECK(!IsLeafMethod());
Mark Mendell09ed1a32015-03-25 08:30:06 -04004389}
4390
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004391void CodeGeneratorX86::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_in) {
4392 Register temp = temp_in.AsRegister<Register>();
4393 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
4394 invoke->GetVTableIndex(), kX86PointerSize).Uint32Value();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004395
4396 // Use the calling convention instead of the location of the receiver, as
4397 // intrinsics may have put the receiver in a different register. In the intrinsics
4398 // slow path, the arguments have been moved to the right place, so here we are
4399 // guaranteed that the receiver is the first register of the calling convention.
4400 InvokeDexCallingConvention calling_convention;
4401 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004402 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004403 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004404 __ movl(temp, Address(receiver, class_offset));
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004405 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00004406 // Instead of simply (possibly) unpoisoning `temp` here, we should
4407 // emit a read barrier for the previous class reference load.
4408 // However this is not required in practice, as this is an
4409 // intermediate/temporary reference and because the current
4410 // concurrent copying collector keeps the from-space memory
4411 // intact/accessible until the end of the marking phase (the
4412 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004413 __ MaybeUnpoisonHeapReference(temp);
4414 // temp = temp->GetMethodAt(method_offset);
4415 __ movl(temp, Address(temp, method_offset));
4416 // call temp->GetEntryPoint();
4417 __ call(Address(
Andreas Gampe542451c2016-07-26 09:02:02 -07004418 temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86PointerSize).Int32Value()));
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004419}
4420
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004421void CodeGeneratorX86::RecordSimplePatch() {
4422 if (GetCompilerOptions().GetIncludePatchInformation()) {
4423 simple_patches_.emplace_back();
4424 __ Bind(&simple_patches_.back());
4425 }
4426}
4427
Vladimir Markoaad75c62016-10-03 08:46:48 +00004428void CodeGeneratorX86::RecordBootStringPatch(HLoadString* load_string) {
4429 DCHECK(GetCompilerOptions().IsBootImage());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004430 string_patches_.emplace_back(load_string->GetDexFile(), load_string->GetStringIndex());
4431 __ Bind(&string_patches_.back().label);
4432}
4433
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004434void CodeGeneratorX86::RecordTypePatch(HLoadClass* load_class) {
4435 type_patches_.emplace_back(load_class->GetDexFile(), load_class->GetTypeIndex());
4436 __ Bind(&type_patches_.back().label);
4437}
4438
Vladimir Markoaad75c62016-10-03 08:46:48 +00004439Label* CodeGeneratorX86::NewStringBssEntryPatch(HLoadString* load_string) {
4440 DCHECK(!GetCompilerOptions().IsBootImage());
4441 string_patches_.emplace_back(load_string->GetDexFile(), load_string->GetStringIndex());
4442 return &string_patches_.back().label;
4443}
4444
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004445Label* CodeGeneratorX86::NewPcRelativeDexCacheArrayPatch(const DexFile& dex_file,
4446 uint32_t element_offset) {
4447 // Add the patch entry and bind its label at the end of the instruction.
4448 pc_relative_dex_cache_patches_.emplace_back(dex_file, element_offset);
4449 return &pc_relative_dex_cache_patches_.back().label;
4450}
4451
Vladimir Markoaad75c62016-10-03 08:46:48 +00004452// The label points to the end of the "movl" or another instruction but the literal offset
4453// for method patch needs to point to the embedded constant which occupies the last 4 bytes.
4454constexpr uint32_t kLabelPositionToLiteralOffsetAdjustment = 4u;
4455
4456template <LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
4457inline void CodeGeneratorX86::EmitPcRelativeLinkerPatches(
4458 const ArenaDeque<PatchInfo<Label>>& infos,
4459 ArenaVector<LinkerPatch>* linker_patches) {
4460 for (const PatchInfo<Label>& info : infos) {
4461 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
4462 linker_patches->push_back(
4463 Factory(literal_offset, &info.dex_file, GetMethodAddressOffset(), info.index));
4464 }
4465}
4466
Vladimir Marko58155012015-08-19 12:49:41 +00004467void CodeGeneratorX86::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
4468 DCHECK(linker_patches->empty());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004469 size_t size =
4470 method_patches_.size() +
4471 relative_call_patches_.size() +
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004472 pc_relative_dex_cache_patches_.size() +
4473 simple_patches_.size() +
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004474 string_patches_.size() +
4475 type_patches_.size();
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004476 linker_patches->reserve(size);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004477 for (const PatchInfo<Label>& info : method_patches_) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004478 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Markoaad75c62016-10-03 08:46:48 +00004479 linker_patches->push_back(LinkerPatch::MethodPatch(literal_offset, &info.dex_file, info.index));
Vladimir Marko58155012015-08-19 12:49:41 +00004480 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00004481 for (const PatchInfo<Label>& info : relative_call_patches_) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004482 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Markoaad75c62016-10-03 08:46:48 +00004483 linker_patches->push_back(
4484 LinkerPatch::RelativeCodePatch(literal_offset, &info.dex_file, info.index));
Vladimir Marko58155012015-08-19 12:49:41 +00004485 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00004486 EmitPcRelativeLinkerPatches<LinkerPatch::DexCacheArrayPatch>(pc_relative_dex_cache_patches_,
4487 linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004488 for (const Label& label : simple_patches_) {
4489 uint32_t literal_offset = label.Position() - kLabelPositionToLiteralOffsetAdjustment;
4490 linker_patches->push_back(LinkerPatch::RecordPosition(literal_offset));
4491 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00004492 if (!GetCompilerOptions().IsBootImage()) {
4493 EmitPcRelativeLinkerPatches<LinkerPatch::StringBssEntryPatch>(string_patches_, linker_patches);
4494 } else if (GetCompilerOptions().GetCompilePic()) {
4495 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeStringPatch>(string_patches_, linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004496 } else {
Vladimir Markoaad75c62016-10-03 08:46:48 +00004497 for (const PatchInfo<Label>& info : string_patches_) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004498 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Markoaad75c62016-10-03 08:46:48 +00004499 linker_patches->push_back(
4500 LinkerPatch::StringPatch(literal_offset, &info.dex_file, info.index));
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004501 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00004502 }
4503 if (GetCompilerOptions().GetCompilePic()) {
4504 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeTypePatch>(type_patches_, linker_patches);
4505 } else {
4506 for (const PatchInfo<Label>& info : type_patches_) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004507 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Markoaad75c62016-10-03 08:46:48 +00004508 linker_patches->push_back(LinkerPatch::TypePatch(literal_offset, &info.dex_file, info.index));
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004509 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004510 }
Vladimir Marko58155012015-08-19 12:49:41 +00004511}
4512
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004513void CodeGeneratorX86::MarkGCCard(Register temp,
4514 Register card,
4515 Register object,
4516 Register value,
4517 bool value_can_be_null) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004518 NearLabel is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004519 if (value_can_be_null) {
4520 __ testl(value, value);
4521 __ j(kEqual, &is_null);
4522 }
Andreas Gampe542451c2016-07-26 09:02:02 -07004523 __ fs()->movl(card, Address::Absolute(Thread::CardTableOffset<kX86PointerSize>().Int32Value()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004524 __ movl(temp, object);
4525 __ shrl(temp, Immediate(gc::accounting::CardTable::kCardShift));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00004526 __ movb(Address(temp, card, TIMES_1, 0),
4527 X86ManagedRegister::FromCpuRegister(card).AsByteRegister());
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004528 if (value_can_be_null) {
4529 __ Bind(&is_null);
4530 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004531}
4532
Calin Juravle52c48962014-12-16 17:02:57 +00004533void LocationsBuilderX86::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
4534 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain0d5a2812015-11-13 10:07:31 +00004535
4536 bool object_field_get_with_read_barrier =
4537 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004538 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00004539 new (GetGraph()->GetArena()) LocationSummary(instruction,
4540 kEmitCompilerReadBarrier ?
4541 LocationSummary::kCallOnSlowPath :
4542 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01004543 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004544 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01004545 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004546 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004547
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004548 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4549 locations->SetOut(Location::RequiresFpuRegister());
4550 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004551 // The output overlaps in case of long: we don't want the low move
4552 // to overwrite the object's location. Likewise, in the case of
4553 // an object field get with read barriers enabled, we do not want
4554 // the move to overwrite the object's location, as we need it to emit
4555 // the read barrier.
4556 locations->SetOut(
4557 Location::RequiresRegister(),
4558 (object_field_get_with_read_barrier || instruction->GetType() == Primitive::kPrimLong) ?
4559 Location::kOutputOverlap :
4560 Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004561 }
Calin Juravle52c48962014-12-16 17:02:57 +00004562
4563 if (field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong)) {
4564 // Long values can be loaded atomically into an XMM using movsd.
Roland Levillain7c1559a2015-12-15 10:55:36 +00004565 // So we use an XMM register as a temp to achieve atomicity (first
4566 // load the temp into the XMM and then copy the XMM into the
4567 // output, 32 bits at a time).
Calin Juravle52c48962014-12-16 17:02:57 +00004568 locations->AddTemp(Location::RequiresFpuRegister());
4569 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004570}
4571
Calin Juravle52c48962014-12-16 17:02:57 +00004572void InstructionCodeGeneratorX86::HandleFieldGet(HInstruction* instruction,
4573 const FieldInfo& field_info) {
4574 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004575
Calin Juravle52c48962014-12-16 17:02:57 +00004576 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004577 Location base_loc = locations->InAt(0);
4578 Register base = base_loc.AsRegister<Register>();
Calin Juravle52c48962014-12-16 17:02:57 +00004579 Location out = locations->Out();
4580 bool is_volatile = field_info.IsVolatile();
4581 Primitive::Type field_type = field_info.GetFieldType();
4582 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4583
4584 switch (field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004585 case Primitive::kPrimBoolean: {
Calin Juravle52c48962014-12-16 17:02:57 +00004586 __ movzxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004587 break;
4588 }
4589
4590 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00004591 __ movsxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004592 break;
4593 }
4594
4595 case Primitive::kPrimShort: {
Calin Juravle52c48962014-12-16 17:02:57 +00004596 __ movsxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004597 break;
4598 }
4599
4600 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00004601 __ movzxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004602 break;
4603 }
4604
4605 case Primitive::kPrimInt:
Calin Juravle52c48962014-12-16 17:02:57 +00004606 __ movl(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004607 break;
Roland Levillain7c1559a2015-12-15 10:55:36 +00004608
4609 case Primitive::kPrimNot: {
4610 // /* HeapReference<Object> */ out = *(base + offset)
4611 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004612 // Note that a potential implicit null check is handled in this
4613 // CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier call.
4614 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00004615 instruction, out, base, offset, /* needs_null_check */ true);
Roland Levillain7c1559a2015-12-15 10:55:36 +00004616 if (is_volatile) {
4617 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4618 }
4619 } else {
4620 __ movl(out.AsRegister<Register>(), Address(base, offset));
4621 codegen_->MaybeRecordImplicitNullCheck(instruction);
4622 if (is_volatile) {
4623 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4624 }
4625 // If read barriers are enabled, emit read barriers other than
4626 // Baker's using a slow path (and also unpoison the loaded
4627 // reference, if heap poisoning is enabled).
4628 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
4629 }
4630 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004631 }
4632
4633 case Primitive::kPrimLong: {
Calin Juravle52c48962014-12-16 17:02:57 +00004634 if (is_volatile) {
4635 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4636 __ movsd(temp, Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004637 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004638 __ movd(out.AsRegisterPairLow<Register>(), temp);
4639 __ psrlq(temp, Immediate(32));
4640 __ movd(out.AsRegisterPairHigh<Register>(), temp);
4641 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004642 DCHECK_NE(base, out.AsRegisterPairLow<Register>());
Calin Juravle52c48962014-12-16 17:02:57 +00004643 __ movl(out.AsRegisterPairLow<Register>(), Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004644 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004645 __ movl(out.AsRegisterPairHigh<Register>(), Address(base, kX86WordSize + offset));
4646 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004647 break;
4648 }
4649
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004650 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00004651 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004652 break;
4653 }
4654
4655 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00004656 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004657 break;
4658 }
4659
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004660 case Primitive::kPrimVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00004661 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004662 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004663 }
Calin Juravle52c48962014-12-16 17:02:57 +00004664
Roland Levillain7c1559a2015-12-15 10:55:36 +00004665 if (field_type == Primitive::kPrimNot || field_type == Primitive::kPrimLong) {
4666 // Potential implicit null checks, in the case of reference or
4667 // long fields, are handled in the previous switch statement.
4668 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00004669 codegen_->MaybeRecordImplicitNullCheck(instruction);
4670 }
4671
Calin Juravle52c48962014-12-16 17:02:57 +00004672 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004673 if (field_type == Primitive::kPrimNot) {
4674 // Memory barriers, in the case of references, are also handled
4675 // in the previous switch statement.
4676 } else {
4677 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4678 }
Roland Levillain4d027112015-07-01 15:41:14 +01004679 }
Calin Juravle52c48962014-12-16 17:02:57 +00004680}
4681
4682void LocationsBuilderX86::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
4683 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4684
4685 LocationSummary* locations =
4686 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4687 locations->SetInAt(0, Location::RequiresRegister());
4688 bool is_volatile = field_info.IsVolatile();
4689 Primitive::Type field_type = field_info.GetFieldType();
4690 bool is_byte_type = (field_type == Primitive::kPrimBoolean)
4691 || (field_type == Primitive::kPrimByte);
4692
4693 // The register allocator does not support multiple
4694 // inputs that die at entry with one in a specific register.
4695 if (is_byte_type) {
4696 // Ensure the value is in a byte register.
4697 locations->SetInAt(1, Location::RegisterLocation(EAX));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004698 } else if (Primitive::IsFloatingPointType(field_type)) {
Mark Mendell81489372015-11-04 11:30:41 -05004699 if (is_volatile && field_type == Primitive::kPrimDouble) {
4700 // In order to satisfy the semantics of volatile, this must be a single instruction store.
4701 locations->SetInAt(1, Location::RequiresFpuRegister());
4702 } else {
4703 locations->SetInAt(1, Location::FpuRegisterOrConstant(instruction->InputAt(1)));
4704 }
4705 } else if (is_volatile && field_type == Primitive::kPrimLong) {
4706 // In order to satisfy the semantics of volatile, this must be a single instruction store.
Calin Juravle52c48962014-12-16 17:02:57 +00004707 locations->SetInAt(1, Location::RequiresRegister());
Mark Mendell81489372015-11-04 11:30:41 -05004708
Calin Juravle52c48962014-12-16 17:02:57 +00004709 // 64bits value can be atomically written to an address with movsd and an XMM register.
4710 // We need two XMM registers because there's no easier way to (bit) copy a register pair
4711 // into a single XMM register (we copy each pair part into the XMMs and then interleave them).
4712 // NB: We could make the register allocator understand fp_reg <-> core_reg moves but given the
4713 // isolated cases when we need this it isn't worth adding the extra complexity.
4714 locations->AddTemp(Location::RequiresFpuRegister());
4715 locations->AddTemp(Location::RequiresFpuRegister());
Mark Mendell81489372015-11-04 11:30:41 -05004716 } else {
4717 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4718
4719 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
4720 // Temporary registers for the write barrier.
4721 locations->AddTemp(Location::RequiresRegister()); // May be used for reference poisoning too.
4722 // Ensure the card is in a byte register.
4723 locations->AddTemp(Location::RegisterLocation(ECX));
4724 }
Calin Juravle52c48962014-12-16 17:02:57 +00004725 }
4726}
4727
4728void InstructionCodeGeneratorX86::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004729 const FieldInfo& field_info,
4730 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00004731 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4732
4733 LocationSummary* locations = instruction->GetLocations();
4734 Register base = locations->InAt(0).AsRegister<Register>();
4735 Location value = locations->InAt(1);
4736 bool is_volatile = field_info.IsVolatile();
4737 Primitive::Type field_type = field_info.GetFieldType();
4738 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01004739 bool needs_write_barrier =
4740 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00004741
4742 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004743 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
Calin Juravle52c48962014-12-16 17:02:57 +00004744 }
4745
Mark Mendell81489372015-11-04 11:30:41 -05004746 bool maybe_record_implicit_null_check_done = false;
4747
Calin Juravle52c48962014-12-16 17:02:57 +00004748 switch (field_type) {
4749 case Primitive::kPrimBoolean:
4750 case Primitive::kPrimByte: {
4751 __ movb(Address(base, offset), value.AsRegister<ByteRegister>());
4752 break;
4753 }
4754
4755 case Primitive::kPrimShort:
4756 case Primitive::kPrimChar: {
Mark Mendell81489372015-11-04 11:30:41 -05004757 if (value.IsConstant()) {
4758 int16_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4759 __ movw(Address(base, offset), Immediate(v));
4760 } else {
4761 __ movw(Address(base, offset), value.AsRegister<Register>());
4762 }
Calin Juravle52c48962014-12-16 17:02:57 +00004763 break;
4764 }
4765
4766 case Primitive::kPrimInt:
4767 case Primitive::kPrimNot: {
Roland Levillain4d027112015-07-01 15:41:14 +01004768 if (kPoisonHeapReferences && needs_write_barrier) {
4769 // Note that in the case where `value` is a null reference,
4770 // we do not enter this block, as the reference does not
4771 // need poisoning.
4772 DCHECK_EQ(field_type, Primitive::kPrimNot);
4773 Register temp = locations->GetTemp(0).AsRegister<Register>();
4774 __ movl(temp, value.AsRegister<Register>());
4775 __ PoisonHeapReference(temp);
4776 __ movl(Address(base, offset), temp);
Mark Mendell81489372015-11-04 11:30:41 -05004777 } else if (value.IsConstant()) {
4778 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4779 __ movl(Address(base, offset), Immediate(v));
Roland Levillain4d027112015-07-01 15:41:14 +01004780 } else {
Nicolas Geoffray03971632016-03-17 10:44:24 +00004781 DCHECK(value.IsRegister()) << value;
Roland Levillain4d027112015-07-01 15:41:14 +01004782 __ movl(Address(base, offset), value.AsRegister<Register>());
4783 }
Calin Juravle52c48962014-12-16 17:02:57 +00004784 break;
4785 }
4786
4787 case Primitive::kPrimLong: {
4788 if (is_volatile) {
4789 XmmRegister temp1 = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4790 XmmRegister temp2 = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
4791 __ movd(temp1, value.AsRegisterPairLow<Register>());
4792 __ movd(temp2, value.AsRegisterPairHigh<Register>());
4793 __ punpckldq(temp1, temp2);
4794 __ movsd(Address(base, offset), temp1);
Calin Juravle77520bc2015-01-12 18:45:46 +00004795 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell81489372015-11-04 11:30:41 -05004796 } else if (value.IsConstant()) {
4797 int64_t v = CodeGenerator::GetInt64ValueOf(value.GetConstant());
4798 __ movl(Address(base, offset), Immediate(Low32Bits(v)));
4799 codegen_->MaybeRecordImplicitNullCheck(instruction);
4800 __ movl(Address(base, kX86WordSize + offset), Immediate(High32Bits(v)));
Calin Juravle52c48962014-12-16 17:02:57 +00004801 } else {
4802 __ movl(Address(base, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00004803 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004804 __ movl(Address(base, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
4805 }
Mark Mendell81489372015-11-04 11:30:41 -05004806 maybe_record_implicit_null_check_done = true;
Calin Juravle52c48962014-12-16 17:02:57 +00004807 break;
4808 }
4809
4810 case Primitive::kPrimFloat: {
Mark Mendell81489372015-11-04 11:30:41 -05004811 if (value.IsConstant()) {
4812 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4813 __ movl(Address(base, offset), Immediate(v));
4814 } else {
4815 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4816 }
Calin Juravle52c48962014-12-16 17:02:57 +00004817 break;
4818 }
4819
4820 case Primitive::kPrimDouble: {
Mark Mendell81489372015-11-04 11:30:41 -05004821 if (value.IsConstant()) {
4822 int64_t v = CodeGenerator::GetInt64ValueOf(value.GetConstant());
4823 __ movl(Address(base, offset), Immediate(Low32Bits(v)));
4824 codegen_->MaybeRecordImplicitNullCheck(instruction);
4825 __ movl(Address(base, kX86WordSize + offset), Immediate(High32Bits(v)));
4826 maybe_record_implicit_null_check_done = true;
4827 } else {
4828 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4829 }
Calin Juravle52c48962014-12-16 17:02:57 +00004830 break;
4831 }
4832
4833 case Primitive::kPrimVoid:
4834 LOG(FATAL) << "Unreachable type " << field_type;
4835 UNREACHABLE();
4836 }
4837
Mark Mendell81489372015-11-04 11:30:41 -05004838 if (!maybe_record_implicit_null_check_done) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004839 codegen_->MaybeRecordImplicitNullCheck(instruction);
4840 }
4841
Roland Levillain4d027112015-07-01 15:41:14 +01004842 if (needs_write_barrier) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004843 Register temp = locations->GetTemp(0).AsRegister<Register>();
4844 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004845 codegen_->MarkGCCard(temp, card, base, value.AsRegister<Register>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00004846 }
4847
Calin Juravle52c48962014-12-16 17:02:57 +00004848 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004849 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
Calin Juravle52c48962014-12-16 17:02:57 +00004850 }
4851}
4852
4853void LocationsBuilderX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4854 HandleFieldGet(instruction, instruction->GetFieldInfo());
4855}
4856
4857void InstructionCodeGeneratorX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4858 HandleFieldGet(instruction, instruction->GetFieldInfo());
4859}
4860
4861void LocationsBuilderX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
4862 HandleFieldSet(instruction, instruction->GetFieldInfo());
4863}
4864
4865void InstructionCodeGeneratorX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004866 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00004867}
4868
4869void LocationsBuilderX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
4870 HandleFieldSet(instruction, instruction->GetFieldInfo());
4871}
4872
4873void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004874 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00004875}
4876
4877void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4878 HandleFieldGet(instruction, instruction->GetFieldInfo());
4879}
4880
4881void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4882 HandleFieldGet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004883}
4884
Calin Juravlee460d1d2015-09-29 04:52:17 +01004885void LocationsBuilderX86::VisitUnresolvedInstanceFieldGet(
4886 HUnresolvedInstanceFieldGet* instruction) {
4887 FieldAccessCallingConventionX86 calling_convention;
4888 codegen_->CreateUnresolvedFieldLocationSummary(
4889 instruction, instruction->GetFieldType(), calling_convention);
4890}
4891
4892void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldGet(
4893 HUnresolvedInstanceFieldGet* instruction) {
4894 FieldAccessCallingConventionX86 calling_convention;
4895 codegen_->GenerateUnresolvedFieldAccess(instruction,
4896 instruction->GetFieldType(),
4897 instruction->GetFieldIndex(),
4898 instruction->GetDexPc(),
4899 calling_convention);
4900}
4901
4902void LocationsBuilderX86::VisitUnresolvedInstanceFieldSet(
4903 HUnresolvedInstanceFieldSet* instruction) {
4904 FieldAccessCallingConventionX86 calling_convention;
4905 codegen_->CreateUnresolvedFieldLocationSummary(
4906 instruction, instruction->GetFieldType(), calling_convention);
4907}
4908
4909void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldSet(
4910 HUnresolvedInstanceFieldSet* instruction) {
4911 FieldAccessCallingConventionX86 calling_convention;
4912 codegen_->GenerateUnresolvedFieldAccess(instruction,
4913 instruction->GetFieldType(),
4914 instruction->GetFieldIndex(),
4915 instruction->GetDexPc(),
4916 calling_convention);
4917}
4918
4919void LocationsBuilderX86::VisitUnresolvedStaticFieldGet(
4920 HUnresolvedStaticFieldGet* instruction) {
4921 FieldAccessCallingConventionX86 calling_convention;
4922 codegen_->CreateUnresolvedFieldLocationSummary(
4923 instruction, instruction->GetFieldType(), calling_convention);
4924}
4925
4926void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldGet(
4927 HUnresolvedStaticFieldGet* instruction) {
4928 FieldAccessCallingConventionX86 calling_convention;
4929 codegen_->GenerateUnresolvedFieldAccess(instruction,
4930 instruction->GetFieldType(),
4931 instruction->GetFieldIndex(),
4932 instruction->GetDexPc(),
4933 calling_convention);
4934}
4935
4936void LocationsBuilderX86::VisitUnresolvedStaticFieldSet(
4937 HUnresolvedStaticFieldSet* instruction) {
4938 FieldAccessCallingConventionX86 calling_convention;
4939 codegen_->CreateUnresolvedFieldLocationSummary(
4940 instruction, instruction->GetFieldType(), calling_convention);
4941}
4942
4943void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldSet(
4944 HUnresolvedStaticFieldSet* instruction) {
4945 FieldAccessCallingConventionX86 calling_convention;
4946 codegen_->GenerateUnresolvedFieldAccess(instruction,
4947 instruction->GetFieldType(),
4948 instruction->GetFieldIndex(),
4949 instruction->GetDexPc(),
4950 calling_convention);
4951}
4952
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004953void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004954 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
4955 Location loc = codegen_->GetCompilerOptions().GetImplicitNullChecks()
4956 ? Location::RequiresRegister()
4957 : Location::Any();
4958 locations->SetInAt(0, loc);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004959}
4960
Calin Juravle2ae48182016-03-16 14:05:09 +00004961void CodeGeneratorX86::GenerateImplicitNullCheck(HNullCheck* instruction) {
4962 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004963 return;
4964 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004965 LocationSummary* locations = instruction->GetLocations();
4966 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00004967
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004968 __ testl(EAX, Address(obj.AsRegister<Register>(), 0));
Calin Juravle2ae48182016-03-16 14:05:09 +00004969 RecordPcInfo(instruction, instruction->GetDexPc());
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004970}
4971
Calin Juravle2ae48182016-03-16 14:05:09 +00004972void CodeGeneratorX86::GenerateExplicitNullCheck(HNullCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07004973 SlowPathCode* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00004974 AddSlowPath(slow_path);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004975
4976 LocationSummary* locations = instruction->GetLocations();
4977 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004978
4979 if (obj.IsRegister()) {
Mark Mendell42514f62015-03-31 11:34:22 -04004980 __ testl(obj.AsRegister<Register>(), obj.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004981 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004982 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004983 } else {
4984 DCHECK(obj.IsConstant()) << obj;
David Brazdil77a48ae2015-09-15 12:34:04 +00004985 DCHECK(obj.GetConstant()->IsNullConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004986 __ jmp(slow_path->GetEntryLabel());
4987 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004988 }
4989 __ j(kEqual, slow_path->GetEntryLabel());
4990}
4991
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004992void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00004993 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004994}
4995
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004996void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004997 bool object_array_get_with_read_barrier =
4998 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004999 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00005000 new (GetGraph()->GetArena()) LocationSummary(instruction,
5001 object_array_get_with_read_barrier ?
5002 LocationSummary::kCallOnSlowPath :
5003 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01005004 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005005 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01005006 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005007 locations->SetInAt(0, Location::RequiresRegister());
5008 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005009 if (Primitive::IsFloatingPointType(instruction->GetType())) {
5010 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5011 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005012 // The output overlaps in case of long: we don't want the low move
5013 // to overwrite the array's location. Likewise, in the case of an
5014 // object array get with read barriers enabled, we do not want the
5015 // move to overwrite the array's location, as we need it to emit
5016 // the read barrier.
5017 locations->SetOut(
5018 Location::RequiresRegister(),
5019 (instruction->GetType() == Primitive::kPrimLong || object_array_get_with_read_barrier) ?
5020 Location::kOutputOverlap :
5021 Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005022 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005023}
5024
5025void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
5026 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005027 Location obj_loc = locations->InAt(0);
5028 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005029 Location index = locations->InAt(1);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005030 Location out_loc = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005031 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005032
Calin Juravle77520bc2015-01-12 18:45:46 +00005033 Primitive::Type type = instruction->GetType();
5034 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005035 case Primitive::kPrimBoolean: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005036 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005037 __ movzxb(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005038 break;
5039 }
5040
5041 case Primitive::kPrimByte: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005042 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005043 __ movsxb(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005044 break;
5045 }
5046
5047 case Primitive::kPrimShort: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005048 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005049 __ movsxw(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005050 break;
5051 }
5052
5053 case Primitive::kPrimChar: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005054 Register out = out_loc.AsRegister<Register>();
jessicahandojo4877b792016-09-08 19:49:13 -07005055 if (mirror::kUseStringCompression && instruction->IsStringCharAt()) {
5056 // Branch cases into compressed and uncompressed for each index's type.
5057 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
5058 NearLabel done, not_compressed;
5059 __ cmpl(Address(obj, count_offset), Immediate(0));
5060 codegen_->MaybeRecordImplicitNullCheck(instruction);
5061 __ j(kGreaterEqual, &not_compressed);
5062 __ movzxb(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_1, data_offset));
5063 __ jmp(&done);
5064 __ Bind(&not_compressed);
5065 __ movzxw(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_2, data_offset));
5066 __ Bind(&done);
5067 } else {
5068 // Common case for charAt of array of char or when string compression's
5069 // feature is turned off.
5070 __ movzxw(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_2, data_offset));
5071 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005072 break;
5073 }
5074
Roland Levillain7c1559a2015-12-15 10:55:36 +00005075 case Primitive::kPrimInt: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005076 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005077 __ movl(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005078 break;
5079 }
5080
Roland Levillain7c1559a2015-12-15 10:55:36 +00005081 case Primitive::kPrimNot: {
5082 static_assert(
5083 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
5084 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain7c1559a2015-12-15 10:55:36 +00005085 // /* HeapReference<Object> */ out =
5086 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
5087 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005088 // Note that a potential implicit null check is handled in this
5089 // CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier call.
5090 codegen_->GenerateArrayLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00005091 instruction, out_loc, obj, data_offset, index, /* needs_null_check */ true);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005092 } else {
5093 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005094 __ movl(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset));
5095 codegen_->MaybeRecordImplicitNullCheck(instruction);
5096 // If read barriers are enabled, emit read barriers other than
5097 // Baker's using a slow path (and also unpoison the loaded
5098 // reference, if heap poisoning is enabled).
Roland Levillain7c1559a2015-12-15 10:55:36 +00005099 if (index.IsConstant()) {
5100 uint32_t offset =
5101 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Roland Levillain7c1559a2015-12-15 10:55:36 +00005102 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
5103 } else {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005104 codegen_->MaybeGenerateReadBarrierSlow(
5105 instruction, out_loc, out_loc, obj_loc, data_offset, index);
5106 }
5107 }
5108 break;
5109 }
5110
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005111 case Primitive::kPrimLong: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005112 DCHECK_NE(obj, out_loc.AsRegisterPairLow<Register>());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005113 __ movl(out_loc.AsRegisterPairLow<Register>(),
5114 CodeGeneratorX86::ArrayAddress(obj, index, TIMES_8, data_offset));
5115 codegen_->MaybeRecordImplicitNullCheck(instruction);
5116 __ movl(out_loc.AsRegisterPairHigh<Register>(),
5117 CodeGeneratorX86::ArrayAddress(obj, index, TIMES_8, data_offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005118 break;
5119 }
5120
Mark Mendell7c8d0092015-01-26 11:21:33 -05005121 case Primitive::kPrimFloat: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005122 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005123 __ movss(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005124 break;
5125 }
5126
5127 case Primitive::kPrimDouble: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005128 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005129 __ movsd(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_8, data_offset));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005130 break;
5131 }
5132
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005133 case Primitive::kPrimVoid:
Calin Juravle77520bc2015-01-12 18:45:46 +00005134 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07005135 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005136 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005137
Roland Levillain7c1559a2015-12-15 10:55:36 +00005138 if (type == Primitive::kPrimNot || type == Primitive::kPrimLong) {
5139 // Potential implicit null checks, in the case of reference or
5140 // long arrays, are handled in the previous switch statement.
5141 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00005142 codegen_->MaybeRecordImplicitNullCheck(instruction);
5143 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005144}
5145
5146void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01005147 Primitive::Type value_type = instruction->GetComponentType();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005148
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005149 bool needs_write_barrier =
5150 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Roland Levillain0d5a2812015-11-13 10:07:31 +00005151 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005152
Nicolas Geoffray39468442014-09-02 15:17:15 +01005153 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
5154 instruction,
Vladimir Marko8d49fd72016-08-25 15:20:47 +01005155 may_need_runtime_call_for_type_check ?
Roland Levillain0d5a2812015-11-13 10:07:31 +00005156 LocationSummary::kCallOnSlowPath :
5157 LocationSummary::kNoCall);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005158
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005159 bool is_byte_type = (value_type == Primitive::kPrimBoolean)
5160 || (value_type == Primitive::kPrimByte);
5161 // We need the inputs to be different than the output in case of long operation.
5162 // In case of a byte operation, the register allocator does not support multiple
5163 // inputs that die at entry with one in a specific register.
5164 locations->SetInAt(0, Location::RequiresRegister());
5165 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
5166 if (is_byte_type) {
5167 // Ensure the value is in a byte register.
5168 locations->SetInAt(2, Location::ByteRegisterOrConstant(EAX, instruction->InputAt(2)));
5169 } else if (Primitive::IsFloatingPointType(value_type)) {
Mark Mendell81489372015-11-04 11:30:41 -05005170 locations->SetInAt(2, Location::FpuRegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005171 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005172 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
5173 }
5174 if (needs_write_barrier) {
5175 // Temporary registers for the write barrier.
5176 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
5177 // Ensure the card is in a byte register.
Roland Levillain4f6b0b52015-11-23 19:29:22 +00005178 locations->AddTemp(Location::RegisterLocation(ECX));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005179 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005180}
5181
5182void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
5183 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005184 Location array_loc = locations->InAt(0);
5185 Register array = array_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005186 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005187 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005188 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005189 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
5190 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5191 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005192 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005193 bool needs_write_barrier =
5194 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005195
5196 switch (value_type) {
5197 case Primitive::kPrimBoolean:
5198 case Primitive::kPrimByte: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005199 uint32_t offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005200 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_1, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005201 if (value.IsRegister()) {
5202 __ movb(address, value.AsRegister<ByteRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005203 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005204 __ movb(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005205 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005206 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005207 break;
5208 }
5209
5210 case Primitive::kPrimShort:
5211 case Primitive::kPrimChar: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005212 uint32_t offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005213 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_2, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005214 if (value.IsRegister()) {
5215 __ movw(address, value.AsRegister<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005216 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005217 __ movw(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005218 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005219 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005220 break;
5221 }
5222
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005223 case Primitive::kPrimNot: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005224 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005225 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_4, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005226
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005227 if (!value.IsRegister()) {
5228 // Just setting null.
5229 DCHECK(instruction->InputAt(2)->IsNullConstant());
5230 DCHECK(value.IsConstant()) << value;
5231 __ movl(address, Immediate(0));
Calin Juravle77520bc2015-01-12 18:45:46 +00005232 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005233 DCHECK(!needs_write_barrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005234 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005235 break;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005236 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005237
5238 DCHECK(needs_write_barrier);
5239 Register register_value = value.AsRegister<Register>();
Roland Levillain16d9f942016-08-25 17:27:56 +01005240 // We cannot use a NearLabel for `done`, as its range may be too
5241 // short when Baker read barriers are enabled.
5242 Label done;
5243 NearLabel not_null, do_put;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005244 SlowPathCode* slow_path = nullptr;
Roland Levillain16d9f942016-08-25 17:27:56 +01005245 Location temp_loc = locations->GetTemp(0);
5246 Register temp = temp_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005247 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005248 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathX86(instruction);
5249 codegen_->AddSlowPath(slow_path);
5250 if (instruction->GetValueCanBeNull()) {
5251 __ testl(register_value, register_value);
5252 __ j(kNotEqual, &not_null);
5253 __ movl(address, Immediate(0));
5254 codegen_->MaybeRecordImplicitNullCheck(instruction);
5255 __ jmp(&done);
5256 __ Bind(&not_null);
5257 }
5258
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005259 // Note that when Baker read barriers are enabled, the type
5260 // checks are performed without read barriers. This is fine,
5261 // even in the case where a class object is in the from-space
5262 // after the flip, as a comparison involving such a type would
5263 // not produce a false positive; it may of course produce a
5264 // false negative, in which case we would take the ArraySet
5265 // slow path.
Roland Levillain16d9f942016-08-25 17:27:56 +01005266
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005267 // /* HeapReference<Class> */ temp = array->klass_
5268 __ movl(temp, Address(array, class_offset));
5269 codegen_->MaybeRecordImplicitNullCheck(instruction);
5270 __ MaybeUnpoisonHeapReference(temp);
Roland Levillain16d9f942016-08-25 17:27:56 +01005271
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005272 // /* HeapReference<Class> */ temp = temp->component_type_
5273 __ movl(temp, Address(temp, component_offset));
5274 // If heap poisoning is enabled, no need to unpoison `temp`
5275 // nor the object reference in `register_value->klass`, as
5276 // we are comparing two poisoned references.
5277 __ cmpl(temp, Address(register_value, class_offset));
Roland Levillain16d9f942016-08-25 17:27:56 +01005278
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005279 if (instruction->StaticTypeOfArrayIsObjectArray()) {
5280 __ j(kEqual, &do_put);
5281 // If heap poisoning is enabled, the `temp` reference has
5282 // not been unpoisoned yet; unpoison it now.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005283 __ MaybeUnpoisonHeapReference(temp);
5284
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005285 // If heap poisoning is enabled, no need to unpoison the
5286 // heap reference loaded below, as it is only used for a
5287 // comparison with null.
5288 __ cmpl(Address(temp, super_offset), Immediate(0));
5289 __ j(kNotEqual, slow_path->GetEntryLabel());
5290 __ Bind(&do_put);
5291 } else {
5292 __ j(kNotEqual, slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005293 }
5294 }
5295
5296 if (kPoisonHeapReferences) {
5297 __ movl(temp, register_value);
5298 __ PoisonHeapReference(temp);
5299 __ movl(address, temp);
5300 } else {
5301 __ movl(address, register_value);
5302 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005303 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005304 codegen_->MaybeRecordImplicitNullCheck(instruction);
5305 }
5306
5307 Register card = locations->GetTemp(1).AsRegister<Register>();
5308 codegen_->MarkGCCard(
5309 temp, card, array, value.AsRegister<Register>(), instruction->GetValueCanBeNull());
5310 __ Bind(&done);
5311
5312 if (slow_path != nullptr) {
5313 __ Bind(slow_path->GetExitLabel());
5314 }
5315
5316 break;
5317 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005318
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005319 case Primitive::kPrimInt: {
5320 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005321 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_4, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005322 if (value.IsRegister()) {
5323 __ movl(address, value.AsRegister<Register>());
5324 } else {
5325 DCHECK(value.IsConstant()) << value;
5326 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
5327 __ movl(address, Immediate(v));
5328 }
5329 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005330 break;
5331 }
5332
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005333 case Primitive::kPrimLong: {
5334 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005335 if (value.IsRegisterPair()) {
5336 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset),
5337 value.AsRegisterPairLow<Register>());
5338 codegen_->MaybeRecordImplicitNullCheck(instruction);
5339 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset + kX86WordSize),
5340 value.AsRegisterPairHigh<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005341 } else {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005342 DCHECK(value.IsConstant());
5343 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
5344 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset),
5345 Immediate(Low32Bits(val)));
5346 codegen_->MaybeRecordImplicitNullCheck(instruction);
5347 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset + kX86WordSize),
5348 Immediate(High32Bits(val)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005349 }
5350 break;
5351 }
5352
Mark Mendell7c8d0092015-01-26 11:21:33 -05005353 case Primitive::kPrimFloat: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005354 uint32_t offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005355 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_4, offset);
Mark Mendell81489372015-11-04 11:30:41 -05005356 if (value.IsFpuRegister()) {
5357 __ movss(address, value.AsFpuRegister<XmmRegister>());
5358 } else {
5359 DCHECK(value.IsConstant());
5360 int32_t v = bit_cast<int32_t, float>(value.GetConstant()->AsFloatConstant()->GetValue());
5361 __ movl(address, Immediate(v));
5362 }
5363 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005364 break;
5365 }
5366
5367 case Primitive::kPrimDouble: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005368 uint32_t offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005369 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, offset);
Mark Mendell81489372015-11-04 11:30:41 -05005370 if (value.IsFpuRegister()) {
5371 __ movsd(address, value.AsFpuRegister<XmmRegister>());
5372 } else {
5373 DCHECK(value.IsConstant());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005374 Address address_hi =
5375 CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, offset + kX86WordSize);
Mark Mendell81489372015-11-04 11:30:41 -05005376 int64_t v = bit_cast<int64_t, double>(value.GetConstant()->AsDoubleConstant()->GetValue());
5377 __ movl(address, Immediate(Low32Bits(v)));
5378 codegen_->MaybeRecordImplicitNullCheck(instruction);
5379 __ movl(address_hi, Immediate(High32Bits(v)));
5380 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005381 break;
5382 }
5383
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005384 case Primitive::kPrimVoid:
5385 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07005386 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005387 }
5388}
5389
5390void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
5391 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005392 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellee8d9712016-07-12 11:13:15 -04005393 if (!instruction->IsEmittedAtUseSite()) {
5394 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5395 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005396}
5397
5398void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
Mark Mendellee8d9712016-07-12 11:13:15 -04005399 if (instruction->IsEmittedAtUseSite()) {
5400 return;
5401 }
5402
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005403 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01005404 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00005405 Register obj = locations->InAt(0).AsRegister<Register>();
5406 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005407 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005408 codegen_->MaybeRecordImplicitNullCheck(instruction);
jessicahandojo4877b792016-09-08 19:49:13 -07005409 // Mask out most significant bit in case the array is String's array of char.
5410 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
5411 __ andl(out, Immediate(INT32_MAX));
5412 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005413}
5414
5415void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005416 RegisterSet caller_saves = RegisterSet::Empty();
5417 InvokeRuntimeCallingConvention calling_convention;
5418 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5419 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
5420 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Mark Mendellf60c90b2015-03-04 15:12:59 -05005421 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendellee8d9712016-07-12 11:13:15 -04005422 HInstruction* length = instruction->InputAt(1);
5423 if (!length->IsEmittedAtUseSite()) {
5424 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
5425 }
jessicahandojo4877b792016-09-08 19:49:13 -07005426 // Need register to see array's length.
5427 if (mirror::kUseStringCompression && instruction->IsStringCharAt()) {
5428 locations->AddTemp(Location::RequiresRegister());
5429 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005430}
5431
5432void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
jessicahandojo4877b792016-09-08 19:49:13 -07005433 const bool is_string_compressed_char_at =
5434 mirror::kUseStringCompression && instruction->IsStringCharAt();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005435 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05005436 Location index_loc = locations->InAt(0);
5437 Location length_loc = locations->InAt(1);
Andreas Gampe85b62f22015-09-09 13:15:38 -07005438 SlowPathCode* slow_path =
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01005439 new (GetGraph()->GetArena()) BoundsCheckSlowPathX86(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005440
Mark Mendell99dbd682015-04-22 16:18:52 -04005441 if (length_loc.IsConstant()) {
5442 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
5443 if (index_loc.IsConstant()) {
5444 // BCE will remove the bounds check if we are guarenteed to pass.
5445 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5446 if (index < 0 || index >= length) {
5447 codegen_->AddSlowPath(slow_path);
5448 __ jmp(slow_path->GetEntryLabel());
5449 } else {
5450 // Some optimization after BCE may have generated this, and we should not
5451 // generate a bounds check if it is a valid range.
5452 }
5453 return;
5454 }
5455
5456 // We have to reverse the jump condition because the length is the constant.
5457 Register index_reg = index_loc.AsRegister<Register>();
5458 __ cmpl(index_reg, Immediate(length));
5459 codegen_->AddSlowPath(slow_path);
5460 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005461 } else {
Mark Mendellee8d9712016-07-12 11:13:15 -04005462 HInstruction* array_length = instruction->InputAt(1);
5463 if (array_length->IsEmittedAtUseSite()) {
5464 // Address the length field in the array.
5465 DCHECK(array_length->IsArrayLength());
5466 uint32_t len_offset = CodeGenerator::GetArrayLengthOffset(array_length->AsArrayLength());
5467 Location array_loc = array_length->GetLocations()->InAt(0);
5468 Address array_len(array_loc.AsRegister<Register>(), len_offset);
jessicahandojo4877b792016-09-08 19:49:13 -07005469 if (is_string_compressed_char_at) {
5470 Register length_reg = locations->GetTemp(0).AsRegister<Register>();
5471 __ movl(length_reg, array_len);
5472 codegen_->MaybeRecordImplicitNullCheck(array_length);
5473 __ andl(length_reg, Immediate(INT32_MAX));
5474 codegen_->GenerateIntCompare(length_reg, index_loc);
Mark Mendellee8d9712016-07-12 11:13:15 -04005475 } else {
jessicahandojo4877b792016-09-08 19:49:13 -07005476 // Checking bounds for general case:
5477 // Array of char or string's array with feature compression off.
5478 if (index_loc.IsConstant()) {
5479 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5480 __ cmpl(array_len, Immediate(value));
5481 } else {
5482 __ cmpl(array_len, index_loc.AsRegister<Register>());
5483 }
5484 codegen_->MaybeRecordImplicitNullCheck(array_length);
Mark Mendellee8d9712016-07-12 11:13:15 -04005485 }
Mark Mendell99dbd682015-04-22 16:18:52 -04005486 } else {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005487 codegen_->GenerateIntCompare(length_loc, index_loc);
Mark Mendell99dbd682015-04-22 16:18:52 -04005488 }
5489 codegen_->AddSlowPath(slow_path);
5490 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005491 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005492}
5493
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005494void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005495 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005496}
5497
5498void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005499 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5500}
5501
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005502void LocationsBuilderX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Marko70e97462016-08-09 11:04:26 +01005503 LocationSummary* locations =
5504 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01005505 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005506}
5507
5508void InstructionCodeGeneratorX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005509 HBasicBlock* block = instruction->GetBlock();
5510 if (block->GetLoopInformation() != nullptr) {
5511 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5512 // The back edge will generate the suspend check.
5513 return;
5514 }
5515 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5516 // The goto will generate the suspend check.
5517 return;
5518 }
5519 GenerateSuspendCheck(instruction, nullptr);
5520}
5521
5522void InstructionCodeGeneratorX86::GenerateSuspendCheck(HSuspendCheck* instruction,
5523 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005524 SuspendCheckSlowPathX86* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005525 down_cast<SuspendCheckSlowPathX86*>(instruction->GetSlowPath());
5526 if (slow_path == nullptr) {
5527 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathX86(instruction, successor);
5528 instruction->SetSlowPath(slow_path);
5529 codegen_->AddSlowPath(slow_path);
5530 if (successor != nullptr) {
5531 DCHECK(successor->IsLoopHeader());
5532 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
5533 }
5534 } else {
5535 DCHECK_EQ(slow_path->GetSuccessor(), successor);
5536 }
5537
Andreas Gampe542451c2016-07-26 09:02:02 -07005538 __ fs()->cmpw(Address::Absolute(Thread::ThreadFlagsOffset<kX86PointerSize>().Int32Value()),
Roland Levillain7c1559a2015-12-15 10:55:36 +00005539 Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005540 if (successor == nullptr) {
5541 __ j(kNotEqual, slow_path->GetEntryLabel());
5542 __ Bind(slow_path->GetReturnLabel());
5543 } else {
5544 __ j(kEqual, codegen_->GetLabelOf(successor));
5545 __ jmp(slow_path->GetEntryLabel());
5546 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005547}
5548
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005549X86Assembler* ParallelMoveResolverX86::GetAssembler() const {
5550 return codegen_->GetAssembler();
5551}
5552
Mark Mendell7c8d0092015-01-26 11:21:33 -05005553void ParallelMoveResolverX86::MoveMemoryToMemory32(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005554 ScratchRegisterScope ensure_scratch(
5555 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5556 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5557 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5558 __ movl(temp_reg, Address(ESP, src + stack_offset));
5559 __ movl(Address(ESP, dst + stack_offset), temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005560}
5561
5562void ParallelMoveResolverX86::MoveMemoryToMemory64(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005563 ScratchRegisterScope ensure_scratch(
5564 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5565 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5566 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5567 __ movl(temp_reg, Address(ESP, src + stack_offset));
5568 __ movl(Address(ESP, dst + stack_offset), temp_reg);
5569 __ movl(temp_reg, Address(ESP, src + stack_offset + kX86WordSize));
5570 __ movl(Address(ESP, dst + stack_offset + kX86WordSize), temp_reg);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005571}
5572
5573void ParallelMoveResolverX86::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005574 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005575 Location source = move->GetSource();
5576 Location destination = move->GetDestination();
5577
5578 if (source.IsRegister()) {
5579 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005580 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
David Brazdil74eb1b22015-12-14 11:44:01 +00005581 } else if (destination.IsFpuRegister()) {
5582 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005583 } else {
5584 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00005585 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005586 }
David Brazdil74eb1b22015-12-14 11:44:01 +00005587 } else if (source.IsRegisterPair()) {
5588 size_t elem_size = Primitive::ComponentSize(Primitive::kPrimInt);
5589 // Create stack space for 2 elements.
5590 __ subl(ESP, Immediate(2 * elem_size));
5591 __ movl(Address(ESP, 0), source.AsRegisterPairLow<Register>());
5592 __ movl(Address(ESP, elem_size), source.AsRegisterPairHigh<Register>());
5593 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
5594 // And remove the temporary stack space we allocated.
5595 __ addl(ESP, Immediate(2 * elem_size));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005596 } else if (source.IsFpuRegister()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00005597 if (destination.IsRegister()) {
5598 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
5599 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005600 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
David Brazdil74eb1b22015-12-14 11:44:01 +00005601 } else if (destination.IsRegisterPair()) {
5602 XmmRegister src_reg = source.AsFpuRegister<XmmRegister>();
5603 __ movd(destination.AsRegisterPairLow<Register>(), src_reg);
5604 __ psrlq(src_reg, Immediate(32));
5605 __ movd(destination.AsRegisterPairHigh<Register>(), src_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005606 } else if (destination.IsStackSlot()) {
5607 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
5608 } else {
5609 DCHECK(destination.IsDoubleStackSlot());
5610 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
5611 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005612 } else if (source.IsStackSlot()) {
5613 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005614 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005615 } else if (destination.IsFpuRegister()) {
5616 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005617 } else {
5618 DCHECK(destination.IsStackSlot());
Mark Mendell7c8d0092015-01-26 11:21:33 -05005619 MoveMemoryToMemory32(destination.GetStackIndex(), source.GetStackIndex());
5620 }
5621 } else if (source.IsDoubleStackSlot()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00005622 if (destination.IsRegisterPair()) {
5623 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
5624 __ movl(destination.AsRegisterPairHigh<Register>(),
5625 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
5626 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005627 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
5628 } else {
5629 DCHECK(destination.IsDoubleStackSlot()) << destination;
5630 MoveMemoryToMemory64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005631 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005632 } else if (source.IsConstant()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005633 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00005634 if (constant->IsIntConstant() || constant->IsNullConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05005635 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005636 if (destination.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05005637 if (value == 0) {
5638 __ xorl(destination.AsRegister<Register>(), destination.AsRegister<Register>());
5639 } else {
5640 __ movl(destination.AsRegister<Register>(), Immediate(value));
5641 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005642 } else {
5643 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell09b84632015-02-13 17:48:38 -05005644 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005645 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005646 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005647 float fp_value = constant->AsFloatConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00005648 int32_t value = bit_cast<int32_t, float>(fp_value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005649 Immediate imm(value);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005650 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005651 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
5652 if (value == 0) {
5653 // Easy handling of 0.0.
5654 __ xorps(dest, dest);
5655 } else {
5656 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005657 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5658 Register temp = static_cast<Register>(ensure_scratch.GetRegister());
5659 __ movl(temp, Immediate(value));
5660 __ movd(dest, temp);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005661 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005662 } else {
5663 DCHECK(destination.IsStackSlot()) << destination;
5664 __ movl(Address(ESP, destination.GetStackIndex()), imm);
5665 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005666 } else if (constant->IsLongConstant()) {
5667 int64_t value = constant->AsLongConstant()->GetValue();
5668 int32_t low_value = Low32Bits(value);
5669 int32_t high_value = High32Bits(value);
5670 Immediate low(low_value);
5671 Immediate high(high_value);
5672 if (destination.IsDoubleStackSlot()) {
5673 __ movl(Address(ESP, destination.GetStackIndex()), low);
5674 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
5675 } else {
5676 __ movl(destination.AsRegisterPairLow<Register>(), low);
5677 __ movl(destination.AsRegisterPairHigh<Register>(), high);
5678 }
5679 } else {
5680 DCHECK(constant->IsDoubleConstant());
5681 double dbl_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00005682 int64_t value = bit_cast<int64_t, double>(dbl_value);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005683 int32_t low_value = Low32Bits(value);
5684 int32_t high_value = High32Bits(value);
5685 Immediate low(low_value);
5686 Immediate high(high_value);
5687 if (destination.IsFpuRegister()) {
5688 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
5689 if (value == 0) {
5690 // Easy handling of 0.0.
5691 __ xorpd(dest, dest);
5692 } else {
5693 __ pushl(high);
5694 __ pushl(low);
5695 __ movsd(dest, Address(ESP, 0));
5696 __ addl(ESP, Immediate(8));
5697 }
5698 } else {
5699 DCHECK(destination.IsDoubleStackSlot()) << destination;
5700 __ movl(Address(ESP, destination.GetStackIndex()), low);
5701 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
5702 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005703 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005704 } else {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00005705 LOG(FATAL) << "Unimplemented move: " << destination << " <- " << source;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005706 }
5707}
5708
Mark Mendella5c19ce2015-04-01 12:51:05 -04005709void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005710 Register suggested_scratch = reg == EAX ? EBX : EAX;
5711 ScratchRegisterScope ensure_scratch(
5712 this, reg, suggested_scratch, codegen_->GetNumberOfCoreRegisters());
5713
5714 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5715 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset));
5716 __ movl(Address(ESP, mem + stack_offset), reg);
5717 __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005718}
5719
Mark Mendell7c8d0092015-01-26 11:21:33 -05005720void ParallelMoveResolverX86::Exchange32(XmmRegister reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005721 ScratchRegisterScope ensure_scratch(
5722 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5723
5724 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5725 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5726 __ movl(temp_reg, Address(ESP, mem + stack_offset));
5727 __ movss(Address(ESP, mem + stack_offset), reg);
5728 __ movd(reg, temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005729}
5730
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005731void ParallelMoveResolverX86::Exchange(int mem1, int mem2) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005732 ScratchRegisterScope ensure_scratch1(
5733 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005734
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005735 Register suggested_scratch = ensure_scratch1.GetRegister() == EAX ? EBX : EAX;
5736 ScratchRegisterScope ensure_scratch2(
5737 this, ensure_scratch1.GetRegister(), suggested_scratch, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005738
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005739 int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0;
5740 stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0;
5741 __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset));
5742 __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset));
5743 __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister()));
5744 __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005745}
5746
5747void ParallelMoveResolverX86::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005748 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005749 Location source = move->GetSource();
5750 Location destination = move->GetDestination();
5751
5752 if (source.IsRegister() && destination.IsRegister()) {
Mark Mendell90979812015-07-28 16:41:21 -04005753 // Use XOR swap algorithm to avoid serializing XCHG instruction or using a temporary.
5754 DCHECK_NE(destination.AsRegister<Register>(), source.AsRegister<Register>());
5755 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
5756 __ xorl(source.AsRegister<Register>(), destination.AsRegister<Register>());
5757 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005758 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005759 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005760 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005761 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005762 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
5763 Exchange(destination.GetStackIndex(), source.GetStackIndex());
Mark Mendell7c8d0092015-01-26 11:21:33 -05005764 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
5765 // Use XOR Swap algorithm to avoid a temporary.
5766 DCHECK_NE(source.reg(), destination.reg());
5767 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
5768 __ xorpd(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
5769 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
5770 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
5771 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
5772 } else if (destination.IsFpuRegister() && source.IsStackSlot()) {
5773 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005774 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
5775 // Take advantage of the 16 bytes in the XMM register.
5776 XmmRegister reg = source.AsFpuRegister<XmmRegister>();
5777 Address stack(ESP, destination.GetStackIndex());
5778 // Load the double into the high doubleword.
5779 __ movhpd(reg, stack);
5780
5781 // Store the low double into the destination.
5782 __ movsd(stack, reg);
5783
5784 // Move the high double to the low double.
5785 __ psrldq(reg, Immediate(8));
5786 } else if (destination.IsFpuRegister() && source.IsDoubleStackSlot()) {
5787 // Take advantage of the 16 bytes in the XMM register.
5788 XmmRegister reg = destination.AsFpuRegister<XmmRegister>();
5789 Address stack(ESP, source.GetStackIndex());
5790 // Load the double into the high doubleword.
5791 __ movhpd(reg, stack);
5792
5793 // Store the low double into the destination.
5794 __ movsd(stack, reg);
5795
5796 // Move the high double to the low double.
5797 __ psrldq(reg, Immediate(8));
5798 } else if (destination.IsDoubleStackSlot() && source.IsDoubleStackSlot()) {
5799 Exchange(destination.GetStackIndex(), source.GetStackIndex());
5800 Exchange(destination.GetHighStackIndex(kX86WordSize), source.GetHighStackIndex(kX86WordSize));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005801 } else {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005802 LOG(FATAL) << "Unimplemented: source: " << source << ", destination: " << destination;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005803 }
5804}
5805
5806void ParallelMoveResolverX86::SpillScratch(int reg) {
5807 __ pushl(static_cast<Register>(reg));
5808}
5809
5810void ParallelMoveResolverX86::RestoreScratch(int reg) {
5811 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005812}
5813
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005814HLoadClass::LoadKind CodeGeneratorX86::GetSupportedLoadClassKind(
5815 HLoadClass::LoadKind desired_class_load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005816 switch (desired_class_load_kind) {
5817 case HLoadClass::LoadKind::kReferrersClass:
5818 break;
5819 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
5820 DCHECK(!GetCompilerOptions().GetCompilePic());
5821 break;
5822 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
5823 DCHECK(GetCompilerOptions().GetCompilePic());
5824 FALLTHROUGH_INTENDED;
5825 case HLoadClass::LoadKind::kDexCachePcRelative:
5826 DCHECK(!Runtime::Current()->UseJitCompilation()); // Note: boot image is also non-JIT.
5827 // We disable pc-relative load when there is an irreducible loop, as the optimization
5828 // is incompatible with it.
5829 // TODO: Create as many X86ComputeBaseMethodAddress instructions as needed for methods
5830 // with irreducible loops.
5831 if (GetGraph()->HasIrreducibleLoops()) {
5832 return HLoadClass::LoadKind::kDexCacheViaMethod;
5833 }
5834 break;
5835 case HLoadClass::LoadKind::kBootImageAddress:
5836 break;
5837 case HLoadClass::LoadKind::kDexCacheAddress:
5838 DCHECK(Runtime::Current()->UseJitCompilation());
5839 break;
5840 case HLoadClass::LoadKind::kDexCacheViaMethod:
5841 break;
5842 }
5843 return desired_class_load_kind;
5844}
5845
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005846void LocationsBuilderX86::VisitLoadClass(HLoadClass* cls) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005847 if (cls->NeedsAccessCheck()) {
5848 InvokeRuntimeCallingConvention calling_convention;
5849 CodeGenerator::CreateLoadClassLocationSummary(
5850 cls,
5851 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
5852 Location::RegisterLocation(EAX),
5853 /* code_generator_supports_read_barrier */ true);
5854 return;
5855 }
5856
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005857 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
5858 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005859 ? LocationSummary::kCallOnSlowPath
5860 : LocationSummary::kNoCall;
5861 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005862 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005863 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01005864 }
5865
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005866 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
5867 if (load_kind == HLoadClass::LoadKind::kReferrersClass ||
5868 load_kind == HLoadClass::LoadKind::kDexCacheViaMethod ||
5869 load_kind == HLoadClass::LoadKind::kBootImageLinkTimePcRelative ||
5870 load_kind == HLoadClass::LoadKind::kDexCachePcRelative) {
5871 locations->SetInAt(0, Location::RequiresRegister());
5872 }
5873 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005874}
5875
5876void InstructionCodeGeneratorX86::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005877 LocationSummary* locations = cls->GetLocations();
Calin Juravle98893e12015-10-02 21:05:03 +01005878 if (cls->NeedsAccessCheck()) {
5879 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex());
Serban Constantinescuba45db02016-07-12 22:53:02 +01005880 codegen_->InvokeRuntime(kQuickInitializeTypeAndVerifyAccess, cls, cls->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00005881 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Calin Juravle580b6092015-10-06 17:35:58 +01005882 return;
5883 }
5884
Roland Levillain0d5a2812015-11-13 10:07:31 +00005885 Location out_loc = locations->Out();
5886 Register out = out_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005887
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005888 bool generate_null_check = false;
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005889 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005890 switch (cls->GetLoadKind()) {
5891 case HLoadClass::LoadKind::kReferrersClass: {
5892 DCHECK(!cls->CanCallRuntime());
5893 DCHECK(!cls->MustGenerateClinitCheck());
5894 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5895 Register current_method = locations->InAt(0).AsRegister<Register>();
5896 GenerateGcRootFieldLoad(
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005897 cls,
5898 out_loc,
5899 Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()),
Roland Levillain00468f32016-10-27 18:02:48 +01005900 /* fixup_label */ nullptr,
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005901 requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005902 break;
5903 }
5904 case HLoadClass::LoadKind::kBootImageLinkTimeAddress: {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005905 DCHECK(!requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005906 __ movl(out, Immediate(/* placeholder */ 0));
5907 codegen_->RecordTypePatch(cls);
5908 break;
5909 }
5910 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005911 DCHECK(!requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005912 Register method_address = locations->InAt(0).AsRegister<Register>();
5913 __ leal(out, Address(method_address, CodeGeneratorX86::kDummy32BitOffset));
5914 codegen_->RecordTypePatch(cls);
5915 break;
5916 }
5917 case HLoadClass::LoadKind::kBootImageAddress: {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005918 DCHECK(!requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005919 DCHECK_NE(cls->GetAddress(), 0u);
5920 uint32_t address = dchecked_integral_cast<uint32_t>(cls->GetAddress());
5921 __ movl(out, Immediate(address));
5922 codegen_->RecordSimplePatch();
5923 break;
5924 }
5925 case HLoadClass::LoadKind::kDexCacheAddress: {
5926 DCHECK_NE(cls->GetAddress(), 0u);
5927 uint32_t address = dchecked_integral_cast<uint32_t>(cls->GetAddress());
5928 // /* GcRoot<mirror::Class> */ out = *address
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005929 GenerateGcRootFieldLoad(cls,
5930 out_loc,
5931 Address::Absolute(address),
Roland Levillain00468f32016-10-27 18:02:48 +01005932 /* fixup_label */ nullptr,
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005933 requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005934 generate_null_check = !cls->IsInDexCache();
5935 break;
5936 }
5937 case HLoadClass::LoadKind::kDexCachePcRelative: {
5938 Register base_reg = locations->InAt(0).AsRegister<Register>();
5939 uint32_t offset = cls->GetDexCacheElementOffset();
5940 Label* fixup_label = codegen_->NewPcRelativeDexCacheArrayPatch(cls->GetDexFile(), offset);
5941 // /* GcRoot<mirror::Class> */ out = *(base + offset) /* PC-relative */
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005942 GenerateGcRootFieldLoad(cls,
5943 out_loc,
5944 Address(base_reg, CodeGeneratorX86::kDummy32BitOffset),
5945 fixup_label,
5946 requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005947 generate_null_check = !cls->IsInDexCache();
5948 break;
5949 }
5950 case HLoadClass::LoadKind::kDexCacheViaMethod: {
5951 // /* GcRoot<mirror::Class>[] */ out =
5952 // current_method.ptr_sized_fields_->dex_cache_resolved_types_
5953 Register current_method = locations->InAt(0).AsRegister<Register>();
5954 __ movl(out, Address(current_method,
5955 ArtMethod::DexCacheResolvedTypesOffset(kX86PointerSize).Int32Value()));
5956 // /* GcRoot<mirror::Class> */ out = out[type_index]
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005957 GenerateGcRootFieldLoad(cls,
5958 out_loc,
5959 Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())),
Roland Levillain00468f32016-10-27 18:02:48 +01005960 /* fixup_label */ nullptr,
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005961 requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005962 generate_null_check = !cls->IsInDexCache();
5963 break;
5964 }
5965 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005966
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005967 if (generate_null_check || cls->MustGenerateClinitCheck()) {
5968 DCHECK(cls->CanCallRuntime());
5969 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
5970 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
5971 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005972
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005973 if (generate_null_check) {
5974 __ testl(out, out);
5975 __ j(kEqual, slow_path->GetEntryLabel());
5976 }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005977
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005978 if (cls->MustGenerateClinitCheck()) {
5979 GenerateClassInitializationCheck(slow_path, out);
5980 } else {
5981 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005982 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005983 }
5984}
5985
5986void LocationsBuilderX86::VisitClinitCheck(HClinitCheck* check) {
5987 LocationSummary* locations =
5988 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
5989 locations->SetInAt(0, Location::RequiresRegister());
5990 if (check->HasUses()) {
5991 locations->SetOut(Location::SameAsFirstInput());
5992 }
5993}
5994
5995void InstructionCodeGeneratorX86::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005996 // We assume the class to not be null.
Andreas Gampe85b62f22015-09-09 13:15:38 -07005997 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005998 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005999 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00006000 GenerateClassInitializationCheck(slow_path,
6001 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006002}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006003
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006004void InstructionCodeGeneratorX86::GenerateClassInitializationCheck(
Andreas Gampe85b62f22015-09-09 13:15:38 -07006005 SlowPathCode* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006006 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
6007 Immediate(mirror::Class::kStatusInitialized));
6008 __ j(kLess, slow_path->GetEntryLabel());
6009 __ Bind(slow_path->GetExitLabel());
6010 // No need for memory fence, thanks to the X86 memory model.
6011}
6012
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006013HLoadString::LoadKind CodeGeneratorX86::GetSupportedLoadStringKind(
6014 HLoadString::LoadKind desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006015 switch (desired_string_load_kind) {
6016 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
6017 DCHECK(!GetCompilerOptions().GetCompilePic());
6018 break;
6019 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
6020 DCHECK(GetCompilerOptions().GetCompilePic());
6021 FALLTHROUGH_INTENDED;
Vladimir Markoaad75c62016-10-03 08:46:48 +00006022 case HLoadString::LoadKind::kBssEntry:
Calin Juravleffc87072016-04-20 14:22:09 +01006023 DCHECK(!Runtime::Current()->UseJitCompilation()); // Note: boot image is also non-JIT.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006024 // We disable pc-relative load when there is an irreducible loop, as the optimization
6025 // is incompatible with it.
6026 // TODO: Create as many X86ComputeBaseMethodAddress instructions as needed for methods
6027 // with irreducible loops.
6028 if (GetGraph()->HasIrreducibleLoops()) {
6029 return HLoadString::LoadKind::kDexCacheViaMethod;
6030 }
6031 break;
6032 case HLoadString::LoadKind::kBootImageAddress:
6033 break;
6034 case HLoadString::LoadKind::kDexCacheAddress:
Calin Juravleffc87072016-04-20 14:22:09 +01006035 DCHECK(Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006036 break;
6037 case HLoadString::LoadKind::kDexCacheViaMethod:
6038 break;
6039 }
6040 return desired_string_load_kind;
6041}
6042
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006043void LocationsBuilderX86::VisitLoadString(HLoadString* load) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006044 LocationSummary::CallKind call_kind = (load->NeedsEnvironment() || kEmitCompilerReadBarrier)
Vladimir Markoaad75c62016-10-03 08:46:48 +00006045 ? ((load->GetLoadKind() == HLoadString::LoadKind::kDexCacheViaMethod)
6046 ? LocationSummary::kCallOnMainOnly
6047 : LocationSummary::kCallOnSlowPath)
Nicolas Geoffray917d0162015-11-24 18:25:35 +00006048 : LocationSummary::kNoCall;
6049 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006050 HLoadString::LoadKind load_kind = load->GetLoadKind();
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006051 if (load_kind == HLoadString::LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markoaad75c62016-10-03 08:46:48 +00006052 load_kind == HLoadString::LoadKind::kBssEntry) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006053 locations->SetInAt(0, Location::RequiresRegister());
6054 }
Christina Wadsworth175d09b2016-08-31 16:26:01 -07006055 if (load_kind == HLoadString::LoadKind::kDexCacheViaMethod) {
6056 locations->SetOut(Location::RegisterLocation(EAX));
6057 } else {
6058 locations->SetOut(Location::RequiresRegister());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006059 if (load_kind == HLoadString::LoadKind::kBssEntry) {
6060 if (!kUseReadBarrier || kUseBakerReadBarrier) {
6061 // Rely on the pResolveString and/or marking to save everything.
6062 RegisterSet caller_saves = RegisterSet::Empty();
6063 InvokeRuntimeCallingConvention calling_convention;
6064 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6065 locations->SetCustomSlowPathCallerSaves(caller_saves);
6066 } else {
6067 // For non-Baker read barrier we have a temp-clobbering call.
6068 }
6069 }
Christina Wadsworth175d09b2016-08-31 16:26:01 -07006070 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006071}
6072
6073void InstructionCodeGeneratorX86::VisitLoadString(HLoadString* load) {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01006074 LocationSummary* locations = load->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006075 Location out_loc = locations->Out();
6076 Register out = out_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006077
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006078 switch (load->GetLoadKind()) {
6079 case HLoadString::LoadKind::kBootImageLinkTimeAddress: {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006080 __ movl(out, Immediate(/* placeholder */ 0));
Vladimir Markoaad75c62016-10-03 08:46:48 +00006081 codegen_->RecordBootStringPatch(load);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006082 return; // No dex cache slow path.
6083 }
6084 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006085 Register method_address = locations->InAt(0).AsRegister<Register>();
6086 __ leal(out, Address(method_address, CodeGeneratorX86::kDummy32BitOffset));
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::kBootImageAddress: {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006091 DCHECK_NE(load->GetAddress(), 0u);
6092 uint32_t address = dchecked_integral_cast<uint32_t>(load->GetAddress());
6093 __ movl(out, Immediate(address));
6094 codegen_->RecordSimplePatch();
6095 return; // No dex cache slow path.
6096 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00006097 case HLoadString::LoadKind::kBssEntry: {
6098 Register method_address = locations->InAt(0).AsRegister<Register>();
6099 Address address = Address(method_address, CodeGeneratorX86::kDummy32BitOffset);
6100 Label* fixup_label = codegen_->NewStringBssEntryPatch(load);
6101 // /* GcRoot<mirror::Class> */ out = *address /* PC-relative */
Roland Levillain00468f32016-10-27 18:02:48 +01006102 GenerateGcRootFieldLoad(load, out_loc, address, fixup_label, kEmitCompilerReadBarrier);
Vladimir Markoaad75c62016-10-03 08:46:48 +00006103 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86(load);
6104 codegen_->AddSlowPath(slow_path);
6105 __ testl(out, out);
6106 __ j(kEqual, slow_path->GetEntryLabel());
6107 __ Bind(slow_path->GetExitLabel());
6108 return;
6109 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006110 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07006111 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006112 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006113
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07006114 // TODO: Re-add the compiler code to do string dex cache lookup again.
Christina Wadsworth175d09b2016-08-31 16:26:01 -07006115 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006116 DCHECK_EQ(calling_convention.GetRegisterAt(0), out);
Christina Wadsworth175d09b2016-08-31 16:26:01 -07006117 __ movl(calling_convention.GetRegisterAt(0), Immediate(load->GetStringIndex()));
6118 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
6119 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006120}
6121
David Brazdilcb1c0552015-08-04 16:22:25 +01006122static Address GetExceptionTlsAddress() {
Andreas Gampe542451c2016-07-26 09:02:02 -07006123 return Address::Absolute(Thread::ExceptionOffset<kX86PointerSize>().Int32Value());
David Brazdilcb1c0552015-08-04 16:22:25 +01006124}
6125
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006126void LocationsBuilderX86::VisitLoadException(HLoadException* load) {
6127 LocationSummary* locations =
6128 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
6129 locations->SetOut(Location::RequiresRegister());
6130}
6131
6132void InstructionCodeGeneratorX86::VisitLoadException(HLoadException* load) {
David Brazdilcb1c0552015-08-04 16:22:25 +01006133 __ fs()->movl(load->GetLocations()->Out().AsRegister<Register>(), GetExceptionTlsAddress());
6134}
6135
6136void LocationsBuilderX86::VisitClearException(HClearException* clear) {
6137 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
6138}
6139
6140void InstructionCodeGeneratorX86::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
6141 __ fs()->movl(GetExceptionTlsAddress(), Immediate(0));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006142}
6143
6144void LocationsBuilderX86::VisitThrow(HThrow* instruction) {
6145 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006146 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006147 InvokeRuntimeCallingConvention calling_convention;
6148 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6149}
6150
6151void InstructionCodeGeneratorX86::VisitThrow(HThrow* instruction) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01006152 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006153 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006154}
6155
Roland Levillain7c1559a2015-12-15 10:55:36 +00006156static bool TypeCheckNeedsATemporary(TypeCheckKind type_check_kind) {
6157 return kEmitCompilerReadBarrier &&
Vladimir Marko953437b2016-08-24 08:30:46 +00006158 !kUseBakerReadBarrier &&
6159 (type_check_kind == TypeCheckKind::kAbstractClassCheck ||
Roland Levillain7c1559a2015-12-15 10:55:36 +00006160 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
6161 type_check_kind == TypeCheckKind::kArrayObjectCheck);
6162}
6163
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006164void LocationsBuilderX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006165 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain0d5a2812015-11-13 10:07:31 +00006166 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01006167 bool baker_read_barrier_slow_path = false;
Roland Levillain0d5a2812015-11-13 10:07:31 +00006168 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006169 case TypeCheckKind::kExactCheck:
6170 case TypeCheckKind::kAbstractClassCheck:
6171 case TypeCheckKind::kClassHierarchyCheck:
6172 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006173 call_kind =
6174 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Vladimir Marko70e97462016-08-09 11:04:26 +01006175 baker_read_barrier_slow_path = kUseBakerReadBarrier;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006176 break;
6177 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006178 case TypeCheckKind::kUnresolvedCheck:
6179 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006180 call_kind = LocationSummary::kCallOnSlowPath;
6181 break;
6182 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006183
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006184 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01006185 if (baker_read_barrier_slow_path) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006186 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01006187 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006188 locations->SetInAt(0, Location::RequiresRegister());
6189 locations->SetInAt(1, Location::Any());
6190 // Note that TypeCheckSlowPathX86 uses this "out" register too.
6191 locations->SetOut(Location::RequiresRegister());
6192 // When read barriers are enabled, we need a temporary register for
6193 // some cases.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006194 if (TypeCheckNeedsATemporary(type_check_kind)) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006195 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006196 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006197}
6198
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006199void InstructionCodeGeneratorX86::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006200 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006201 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006202 Location obj_loc = locations->InAt(0);
6203 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006204 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006205 Location out_loc = locations->Out();
6206 Register out = out_loc.AsRegister<Register>();
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006207 Location maybe_temp_loc = TypeCheckNeedsATemporary(type_check_kind) ?
Roland Levillain7c1559a2015-12-15 10:55:36 +00006208 locations->GetTemp(0) :
6209 Location::NoLocation();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006210 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006211 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6212 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6213 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Andreas Gampe85b62f22015-09-09 13:15:38 -07006214 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006215 NearLabel done, zero;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006216
6217 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006218 // Avoid null check if we know obj is not null.
6219 if (instruction->MustDoNullCheck()) {
6220 __ testl(obj, obj);
6221 __ j(kEqual, &zero);
6222 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006223
Roland Levillain0d5a2812015-11-13 10:07:31 +00006224 // /* HeapReference<Class> */ out = obj->klass_
Vladimir Marko953437b2016-08-24 08:30:46 +00006225 GenerateReferenceLoadTwoRegisters(instruction, out_loc, obj_loc, class_offset);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006226
Roland Levillain7c1559a2015-12-15 10:55:36 +00006227 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006228 case TypeCheckKind::kExactCheck: {
6229 if (cls.IsRegister()) {
6230 __ cmpl(out, cls.AsRegister<Register>());
6231 } else {
6232 DCHECK(cls.IsStackSlot()) << cls;
6233 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6234 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006235
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006236 // Classes must be equal for the instanceof to succeed.
6237 __ j(kNotEqual, &zero);
6238 __ movl(out, Immediate(1));
6239 __ jmp(&done);
6240 break;
6241 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006242
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006243 case TypeCheckKind::kAbstractClassCheck: {
6244 // If the class is abstract, we eagerly fetch the super class of the
6245 // object to avoid doing a comparison we know will fail.
6246 NearLabel loop;
6247 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006248 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006249 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006250 __ testl(out, out);
6251 // If `out` is null, we use it for the result, and jump to `done`.
6252 __ j(kEqual, &done);
6253 if (cls.IsRegister()) {
6254 __ cmpl(out, cls.AsRegister<Register>());
6255 } else {
6256 DCHECK(cls.IsStackSlot()) << cls;
6257 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6258 }
6259 __ j(kNotEqual, &loop);
6260 __ movl(out, Immediate(1));
6261 if (zero.IsLinked()) {
6262 __ jmp(&done);
6263 }
6264 break;
6265 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006266
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006267 case TypeCheckKind::kClassHierarchyCheck: {
6268 // Walk over the class hierarchy to find a match.
6269 NearLabel loop, success;
6270 __ Bind(&loop);
6271 if (cls.IsRegister()) {
6272 __ cmpl(out, cls.AsRegister<Register>());
6273 } else {
6274 DCHECK(cls.IsStackSlot()) << cls;
6275 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6276 }
6277 __ j(kEqual, &success);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006278 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006279 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006280 __ testl(out, out);
6281 __ j(kNotEqual, &loop);
6282 // If `out` is null, we use it for the result, and jump to `done`.
6283 __ jmp(&done);
6284 __ Bind(&success);
6285 __ movl(out, Immediate(1));
6286 if (zero.IsLinked()) {
6287 __ jmp(&done);
6288 }
6289 break;
6290 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006291
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006292 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006293 // Do an exact check.
6294 NearLabel exact_check;
6295 if (cls.IsRegister()) {
6296 __ cmpl(out, cls.AsRegister<Register>());
6297 } else {
6298 DCHECK(cls.IsStackSlot()) << cls;
6299 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6300 }
6301 __ j(kEqual, &exact_check);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006302 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006303 // /* HeapReference<Class> */ out = out->component_type_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006304 GenerateReferenceLoadOneRegister(instruction, out_loc, component_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006305 __ testl(out, out);
6306 // If `out` is null, we use it for the result, and jump to `done`.
6307 __ j(kEqual, &done);
6308 __ cmpw(Address(out, primitive_offset), Immediate(Primitive::kPrimNot));
6309 __ j(kNotEqual, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006310 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006311 __ movl(out, Immediate(1));
6312 __ jmp(&done);
6313 break;
6314 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006315
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006316 case TypeCheckKind::kArrayCheck: {
6317 if (cls.IsRegister()) {
6318 __ cmpl(out, cls.AsRegister<Register>());
6319 } else {
6320 DCHECK(cls.IsStackSlot()) << cls;
6321 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6322 }
6323 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain0d5a2812015-11-13 10:07:31 +00006324 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
6325 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006326 codegen_->AddSlowPath(slow_path);
6327 __ j(kNotEqual, slow_path->GetEntryLabel());
6328 __ movl(out, Immediate(1));
6329 if (zero.IsLinked()) {
6330 __ jmp(&done);
6331 }
6332 break;
6333 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006334
Calin Juravle98893e12015-10-02 21:05:03 +01006335 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006336 case TypeCheckKind::kInterfaceCheck: {
6337 // Note that we indeed only call on slow path, but we always go
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006338 // into the slow path for the unresolved and interface check
Roland Levillain0d5a2812015-11-13 10:07:31 +00006339 // cases.
6340 //
6341 // We cannot directly call the InstanceofNonTrivial runtime
6342 // entry point without resorting to a type checking slow path
6343 // here (i.e. by calling InvokeRuntime directly), as it would
6344 // require to assign fixed registers for the inputs of this
6345 // HInstanceOf instruction (following the runtime calling
6346 // convention), which might be cluttered by the potential first
6347 // read barrier emission at the beginning of this method.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006348 //
6349 // TODO: Introduce a new runtime entry point taking the object
6350 // to test (instead of its class) as argument, and let it deal
6351 // with the read barrier issues. This will let us refactor this
6352 // case of the `switch` code as it was previously (with a direct
6353 // call to the runtime not using a type checking slow path).
6354 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006355 DCHECK(locations->OnlyCallsOnSlowPath());
6356 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
6357 /* is_fatal */ false);
6358 codegen_->AddSlowPath(slow_path);
6359 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006360 if (zero.IsLinked()) {
6361 __ jmp(&done);
6362 }
6363 break;
6364 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006365 }
6366
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006367 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006368 __ Bind(&zero);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006369 __ xorl(out, out);
6370 }
6371
6372 if (done.IsLinked()) {
6373 __ Bind(&done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006374 }
6375
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006376 if (slow_path != nullptr) {
6377 __ Bind(slow_path->GetExitLabel());
6378 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006379}
6380
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006381void LocationsBuilderX86::VisitCheckCast(HCheckCast* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006382 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
6383 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006384 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
6385 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006386 case TypeCheckKind::kExactCheck:
6387 case TypeCheckKind::kAbstractClassCheck:
6388 case TypeCheckKind::kClassHierarchyCheck:
6389 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006390 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
6391 LocationSummary::kCallOnSlowPath :
6392 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006393 break;
6394 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006395 case TypeCheckKind::kUnresolvedCheck:
6396 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006397 call_kind = LocationSummary::kCallOnSlowPath;
6398 break;
6399 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006400 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
6401 locations->SetInAt(0, Location::RequiresRegister());
6402 locations->SetInAt(1, Location::Any());
6403 // Note that TypeCheckSlowPathX86 uses this "temp" register too.
6404 locations->AddTemp(Location::RequiresRegister());
6405 // When read barriers are enabled, we need an additional temporary
6406 // register for some cases.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006407 if (TypeCheckNeedsATemporary(type_check_kind)) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006408 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006409 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006410}
6411
6412void InstructionCodeGeneratorX86::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006413 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006414 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006415 Location obj_loc = locations->InAt(0);
6416 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006417 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006418 Location temp_loc = locations->GetTemp(0);
6419 Register temp = temp_loc.AsRegister<Register>();
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006420 Location maybe_temp2_loc = TypeCheckNeedsATemporary(type_check_kind) ?
Roland Levillain7c1559a2015-12-15 10:55:36 +00006421 locations->GetTemp(1) :
6422 Location::NoLocation();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006423 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
6424 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6425 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6426 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006427
Roland Levillain0d5a2812015-11-13 10:07:31 +00006428 bool is_type_check_slow_path_fatal =
6429 (type_check_kind == TypeCheckKind::kExactCheck ||
6430 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
6431 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
6432 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
6433 !instruction->CanThrowIntoCatchBlock();
6434 SlowPathCode* type_check_slow_path =
6435 new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
6436 is_type_check_slow_path_fatal);
6437 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006438
Roland Levillain0d5a2812015-11-13 10:07:31 +00006439 NearLabel done;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006440 // Avoid null check if we know obj is not null.
6441 if (instruction->MustDoNullCheck()) {
6442 __ testl(obj, obj);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006443 __ j(kEqual, &done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006444 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006445
Roland Levillain0d5a2812015-11-13 10:07:31 +00006446 // /* HeapReference<Class> */ temp = obj->klass_
Vladimir Marko953437b2016-08-24 08:30:46 +00006447 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006448
Roland Levillain0d5a2812015-11-13 10:07:31 +00006449 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006450 case TypeCheckKind::kExactCheck:
6451 case TypeCheckKind::kArrayCheck: {
6452 if (cls.IsRegister()) {
6453 __ cmpl(temp, cls.AsRegister<Register>());
6454 } else {
6455 DCHECK(cls.IsStackSlot()) << cls;
6456 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6457 }
6458 // Jump to slow path for throwing the exception or doing a
6459 // more involved array check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006460 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006461 break;
6462 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006463
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006464 case TypeCheckKind::kAbstractClassCheck: {
6465 // If the class is abstract, we eagerly fetch the super class of the
6466 // object to avoid doing a comparison we know will fail.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006467 NearLabel loop, compare_classes;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006468 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006469 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006470 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006471
6472 // If the class reference currently in `temp` is not null, jump
6473 // to the `compare_classes` label to compare it with the checked
6474 // class.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006475 __ testl(temp, temp);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006476 __ j(kNotEqual, &compare_classes);
6477 // Otherwise, jump to the slow path to throw the exception.
6478 //
6479 // But before, move back the object's class into `temp` before
6480 // going into the slow path, as it has been overwritten in the
6481 // meantime.
6482 // /* HeapReference<Class> */ temp = obj->klass_
Vladimir Marko953437b2016-08-24 08:30:46 +00006483 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006484 __ jmp(type_check_slow_path->GetEntryLabel());
6485
6486 __ Bind(&compare_classes);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006487 if (cls.IsRegister()) {
6488 __ cmpl(temp, cls.AsRegister<Register>());
6489 } else {
6490 DCHECK(cls.IsStackSlot()) << cls;
6491 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6492 }
6493 __ j(kNotEqual, &loop);
6494 break;
6495 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006496
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006497 case TypeCheckKind::kClassHierarchyCheck: {
6498 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006499 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006500 __ Bind(&loop);
6501 if (cls.IsRegister()) {
6502 __ cmpl(temp, cls.AsRegister<Register>());
6503 } else {
6504 DCHECK(cls.IsStackSlot()) << cls;
6505 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6506 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006507 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006508
Roland Levillain0d5a2812015-11-13 10:07:31 +00006509 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006510 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006511
6512 // If the class reference currently in `temp` is not null, jump
6513 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006514 __ testl(temp, temp);
6515 __ j(kNotEqual, &loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006516 // Otherwise, jump to the slow path to throw the exception.
6517 //
6518 // But before, move back the object's class into `temp` before
6519 // going into the slow path, as it has been overwritten in the
6520 // meantime.
6521 // /* HeapReference<Class> */ temp = obj->klass_
Vladimir Marko953437b2016-08-24 08:30:46 +00006522 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006523 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006524 break;
6525 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006526
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006527 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006528 // Do an exact check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006529 NearLabel check_non_primitive_component_type;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006530 if (cls.IsRegister()) {
6531 __ cmpl(temp, cls.AsRegister<Register>());
6532 } else {
6533 DCHECK(cls.IsStackSlot()) << cls;
6534 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6535 }
6536 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006537
6538 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006539 // /* HeapReference<Class> */ temp = temp->component_type_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006540 GenerateReferenceLoadOneRegister(instruction, temp_loc, component_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006541
6542 // If the component type is not null (i.e. the object is indeed
6543 // an array), jump to label `check_non_primitive_component_type`
6544 // to further check that this component type is not a primitive
6545 // type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006546 __ testl(temp, temp);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006547 __ j(kNotEqual, &check_non_primitive_component_type);
6548 // Otherwise, jump to the slow path to throw the exception.
6549 //
6550 // But before, move back the object's class into `temp` before
6551 // going into the slow path, as it has been overwritten in the
6552 // meantime.
6553 // /* HeapReference<Class> */ temp = obj->klass_
Vladimir Marko953437b2016-08-24 08:30:46 +00006554 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006555 __ jmp(type_check_slow_path->GetEntryLabel());
6556
6557 __ Bind(&check_non_primitive_component_type);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006558 __ cmpw(Address(temp, primitive_offset), Immediate(Primitive::kPrimNot));
Roland Levillain0d5a2812015-11-13 10:07:31 +00006559 __ j(kEqual, &done);
6560 // Same comment as above regarding `temp` and the slow path.
6561 // /* HeapReference<Class> */ temp = obj->klass_
Vladimir Marko953437b2016-08-24 08:30:46 +00006562 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006563 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006564 break;
6565 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006566
Calin Juravle98893e12015-10-02 21:05:03 +01006567 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006568 case TypeCheckKind::kInterfaceCheck:
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006569 // We always go into the type check slow path for the unresolved
6570 // and interface check cases.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006571 //
6572 // We cannot directly call the CheckCast runtime entry point
6573 // without resorting to a type checking slow path here (i.e. by
6574 // calling InvokeRuntime directly), as it would require to
6575 // assign fixed registers for the inputs of this HInstanceOf
6576 // instruction (following the runtime calling convention), which
6577 // might be cluttered by the potential first read barrier
6578 // emission at the beginning of this method.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006579 //
6580 // TODO: Introduce a new runtime entry point taking the object
6581 // to test (instead of its class) as argument, and let it deal
6582 // with the read barrier issues. This will let us refactor this
6583 // case of the `switch` code as it was previously (with a direct
6584 // call to the runtime not using a type checking slow path).
6585 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006586 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006587 break;
6588 }
6589 __ Bind(&done);
6590
Roland Levillain0d5a2812015-11-13 10:07:31 +00006591 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006592}
6593
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006594void LocationsBuilderX86::VisitMonitorOperation(HMonitorOperation* instruction) {
6595 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006596 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006597 InvokeRuntimeCallingConvention calling_convention;
6598 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6599}
6600
6601void InstructionCodeGeneratorX86::VisitMonitorOperation(HMonitorOperation* instruction) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01006602 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject
6603 : kQuickUnlockObject,
Alexandre Rames8158f282015-08-07 10:26:17 +01006604 instruction,
Serban Constantinescuba45db02016-07-12 22:53:02 +01006605 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006606 if (instruction->IsEnter()) {
6607 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
6608 } else {
6609 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
6610 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006611}
6612
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006613void LocationsBuilderX86::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
6614void LocationsBuilderX86::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
6615void LocationsBuilderX86::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
6616
6617void LocationsBuilderX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
6618 LocationSummary* locations =
6619 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6620 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
6621 || instruction->GetResultType() == Primitive::kPrimLong);
6622 locations->SetInAt(0, Location::RequiresRegister());
6623 locations->SetInAt(1, Location::Any());
6624 locations->SetOut(Location::SameAsFirstInput());
6625}
6626
6627void InstructionCodeGeneratorX86::VisitAnd(HAnd* instruction) {
6628 HandleBitwiseOperation(instruction);
6629}
6630
6631void InstructionCodeGeneratorX86::VisitOr(HOr* instruction) {
6632 HandleBitwiseOperation(instruction);
6633}
6634
6635void InstructionCodeGeneratorX86::VisitXor(HXor* instruction) {
6636 HandleBitwiseOperation(instruction);
6637}
6638
6639void InstructionCodeGeneratorX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
6640 LocationSummary* locations = instruction->GetLocations();
6641 Location first = locations->InAt(0);
6642 Location second = locations->InAt(1);
6643 DCHECK(first.Equals(locations->Out()));
6644
6645 if (instruction->GetResultType() == Primitive::kPrimInt) {
6646 if (second.IsRegister()) {
6647 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006648 __ andl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006649 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006650 __ orl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006651 } else {
6652 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006653 __ xorl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006654 }
6655 } else if (second.IsConstant()) {
6656 if (instruction->IsAnd()) {
Roland Levillain199f3362014-11-27 17:15:16 +00006657 __ andl(first.AsRegister<Register>(),
6658 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006659 } else if (instruction->IsOr()) {
Roland Levillain199f3362014-11-27 17:15:16 +00006660 __ orl(first.AsRegister<Register>(),
6661 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006662 } else {
6663 DCHECK(instruction->IsXor());
Roland Levillain199f3362014-11-27 17:15:16 +00006664 __ xorl(first.AsRegister<Register>(),
6665 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006666 }
6667 } else {
6668 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006669 __ andl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006670 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006671 __ orl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006672 } else {
6673 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006674 __ xorl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006675 }
6676 }
6677 } else {
6678 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
6679 if (second.IsRegisterPair()) {
6680 if (instruction->IsAnd()) {
6681 __ andl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
6682 __ andl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
6683 } else if (instruction->IsOr()) {
6684 __ orl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
6685 __ orl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
6686 } else {
6687 DCHECK(instruction->IsXor());
6688 __ xorl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
6689 __ xorl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
6690 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006691 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006692 if (instruction->IsAnd()) {
6693 __ andl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
6694 __ andl(first.AsRegisterPairHigh<Register>(),
6695 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
6696 } else if (instruction->IsOr()) {
6697 __ orl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
6698 __ orl(first.AsRegisterPairHigh<Register>(),
6699 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
6700 } else {
6701 DCHECK(instruction->IsXor());
6702 __ xorl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
6703 __ xorl(first.AsRegisterPairHigh<Register>(),
6704 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
6705 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006706 } else {
6707 DCHECK(second.IsConstant()) << second;
6708 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006709 int32_t low_value = Low32Bits(value);
6710 int32_t high_value = High32Bits(value);
6711 Immediate low(low_value);
6712 Immediate high(high_value);
6713 Register first_low = first.AsRegisterPairLow<Register>();
6714 Register first_high = first.AsRegisterPairHigh<Register>();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006715 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006716 if (low_value == 0) {
6717 __ xorl(first_low, first_low);
6718 } else if (low_value != -1) {
6719 __ andl(first_low, low);
6720 }
6721 if (high_value == 0) {
6722 __ xorl(first_high, first_high);
6723 } else if (high_value != -1) {
6724 __ andl(first_high, high);
6725 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006726 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006727 if (low_value != 0) {
6728 __ orl(first_low, low);
6729 }
6730 if (high_value != 0) {
6731 __ orl(first_high, high);
6732 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006733 } else {
6734 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006735 if (low_value != 0) {
6736 __ xorl(first_low, low);
6737 }
6738 if (high_value != 0) {
6739 __ xorl(first_high, high);
6740 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006741 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006742 }
6743 }
6744}
6745
Roland Levillain7c1559a2015-12-15 10:55:36 +00006746void InstructionCodeGeneratorX86::GenerateReferenceLoadOneRegister(HInstruction* instruction,
6747 Location out,
6748 uint32_t offset,
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006749 Location maybe_temp) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006750 Register out_reg = out.AsRegister<Register>();
6751 if (kEmitCompilerReadBarrier) {
6752 if (kUseBakerReadBarrier) {
6753 // Load with fast path based Baker's read barrier.
6754 // /* HeapReference<Object> */ out = *(out + offset)
6755 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00006756 instruction, out, out_reg, offset, /* needs_null_check */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006757 } else {
6758 // Load with slow path based read barrier.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006759 // Save the value of `out` into `maybe_temp` before overwriting it
Roland Levillain7c1559a2015-12-15 10:55:36 +00006760 // in the following move operation, as we will need it for the
6761 // read barrier below.
Vladimir Marko953437b2016-08-24 08:30:46 +00006762 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006763 __ movl(maybe_temp.AsRegister<Register>(), out_reg);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006764 // /* HeapReference<Object> */ out = *(out + offset)
6765 __ movl(out_reg, Address(out_reg, offset));
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006766 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006767 }
6768 } else {
6769 // Plain load with no read barrier.
6770 // /* HeapReference<Object> */ out = *(out + offset)
6771 __ movl(out_reg, Address(out_reg, offset));
6772 __ MaybeUnpoisonHeapReference(out_reg);
6773 }
6774}
6775
6776void InstructionCodeGeneratorX86::GenerateReferenceLoadTwoRegisters(HInstruction* instruction,
6777 Location out,
6778 Location obj,
Vladimir Marko953437b2016-08-24 08:30:46 +00006779 uint32_t offset) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006780 Register out_reg = out.AsRegister<Register>();
6781 Register obj_reg = obj.AsRegister<Register>();
6782 if (kEmitCompilerReadBarrier) {
6783 if (kUseBakerReadBarrier) {
6784 // Load with fast path based Baker's read barrier.
6785 // /* HeapReference<Object> */ out = *(obj + offset)
6786 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00006787 instruction, out, obj_reg, offset, /* needs_null_check */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006788 } else {
6789 // Load with slow path based read barrier.
6790 // /* HeapReference<Object> */ out = *(obj + offset)
6791 __ movl(out_reg, Address(obj_reg, offset));
6792 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
6793 }
6794 } else {
6795 // Plain load with no read barrier.
6796 // /* HeapReference<Object> */ out = *(obj + offset)
6797 __ movl(out_reg, Address(obj_reg, offset));
6798 __ MaybeUnpoisonHeapReference(out_reg);
6799 }
6800}
6801
6802void InstructionCodeGeneratorX86::GenerateGcRootFieldLoad(HInstruction* instruction,
6803 Location root,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006804 const Address& address,
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006805 Label* fixup_label,
6806 bool requires_read_barrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006807 Register root_reg = root.AsRegister<Register>();
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006808 if (requires_read_barrier) {
6809 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006810 if (kUseBakerReadBarrier) {
6811 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
6812 // Baker's read barrier are used:
6813 //
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006814 // root = *address;
Roland Levillain7c1559a2015-12-15 10:55:36 +00006815 // if (Thread::Current()->GetIsGcMarking()) {
6816 // root = ReadBarrier::Mark(root)
6817 // }
6818
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006819 // /* GcRoot<mirror::Object> */ root = *address
6820 __ movl(root_reg, address);
6821 if (fixup_label != nullptr) {
6822 __ Bind(fixup_label);
6823 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00006824 static_assert(
6825 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
6826 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
6827 "have different sizes.");
6828 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
6829 "art::mirror::CompressedReference<mirror::Object> and int32_t "
6830 "have different sizes.");
6831
Vladimir Marko953437b2016-08-24 08:30:46 +00006832 // Slow path marking the GC root `root`.
6833 SlowPathCode* slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathX86(
6834 instruction, root, /* unpoison */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006835 codegen_->AddSlowPath(slow_path);
6836
Andreas Gampe542451c2016-07-26 09:02:02 -07006837 __ fs()->cmpl(Address::Absolute(Thread::IsGcMarkingOffset<kX86PointerSize>().Int32Value()),
Roland Levillain7c1559a2015-12-15 10:55:36 +00006838 Immediate(0));
6839 __ j(kNotEqual, slow_path->GetEntryLabel());
6840 __ Bind(slow_path->GetExitLabel());
6841 } else {
6842 // GC root loaded through a slow path for read barriers other
6843 // than Baker's.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006844 // /* GcRoot<mirror::Object>* */ root = address
6845 __ leal(root_reg, address);
6846 if (fixup_label != nullptr) {
6847 __ Bind(fixup_label);
6848 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00006849 // /* mirror::Object* */ root = root->Read()
6850 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
6851 }
6852 } else {
6853 // Plain GC root load with no read barrier.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006854 // /* GcRoot<mirror::Object> */ root = *address
6855 __ movl(root_reg, address);
6856 if (fixup_label != nullptr) {
6857 __ Bind(fixup_label);
6858 }
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006859 // Note that GC roots are not affected by heap poisoning, thus we
6860 // do not have to unpoison `root_reg` here.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006861 }
6862}
6863
6864void CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
6865 Location ref,
6866 Register obj,
6867 uint32_t offset,
Roland Levillain7c1559a2015-12-15 10:55:36 +00006868 bool needs_null_check) {
6869 DCHECK(kEmitCompilerReadBarrier);
6870 DCHECK(kUseBakerReadBarrier);
6871
6872 // /* HeapReference<Object> */ ref = *(obj + offset)
6873 Address src(obj, offset);
Vladimir Marko953437b2016-08-24 08:30:46 +00006874 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, needs_null_check);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006875}
6876
6877void CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
6878 Location ref,
6879 Register obj,
6880 uint32_t data_offset,
6881 Location index,
Roland Levillain7c1559a2015-12-15 10:55:36 +00006882 bool needs_null_check) {
6883 DCHECK(kEmitCompilerReadBarrier);
6884 DCHECK(kUseBakerReadBarrier);
6885
Roland Levillain3d312422016-06-23 13:53:42 +01006886 static_assert(
6887 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
6888 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain7c1559a2015-12-15 10:55:36 +00006889 // /* HeapReference<Object> */ ref =
6890 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006891 Address src = CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset);
Vladimir Marko953437b2016-08-24 08:30:46 +00006892 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, needs_null_check);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006893}
6894
6895void CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
6896 Location ref,
6897 Register obj,
6898 const Address& src,
Roland Levillain7c1559a2015-12-15 10:55:36 +00006899 bool needs_null_check) {
6900 DCHECK(kEmitCompilerReadBarrier);
6901 DCHECK(kUseBakerReadBarrier);
6902
6903 // In slow path based read barriers, the read barrier call is
6904 // inserted after the original load. However, in fast path based
6905 // Baker's read barriers, we need to perform the load of
6906 // mirror::Object::monitor_ *before* the original reference load.
6907 // This load-load ordering is required by the read barrier.
6908 // The fast path/slow path (for Baker's algorithm) should look like:
6909 //
6910 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
6911 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
6912 // HeapReference<Object> ref = *src; // Original reference load.
6913 // bool is_gray = (rb_state == ReadBarrier::gray_ptr_);
6914 // if (is_gray) {
6915 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
6916 // }
6917 //
6918 // Note: the original implementation in ReadBarrier::Barrier is
6919 // slightly more complex as:
6920 // - it implements the load-load fence using a data dependency on
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006921 // the high-bits of rb_state, which are expected to be all zeroes
6922 // (we use CodeGeneratorX86::GenerateMemoryBarrier instead here,
6923 // which is a no-op thanks to the x86 memory model);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006924 // - it performs additional checks that we do not do here for
6925 // performance reasons.
6926
6927 Register ref_reg = ref.AsRegister<Register>();
Roland Levillain7c1559a2015-12-15 10:55:36 +00006928 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
6929
Vladimir Marko953437b2016-08-24 08:30:46 +00006930 // Given the numeric representation, it's enough to check the low bit of the rb_state.
6931 static_assert(ReadBarrier::white_ptr_ == 0, "Expecting white to have value 0");
6932 static_assert(ReadBarrier::gray_ptr_ == 1, "Expecting gray to have value 1");
6933 static_assert(ReadBarrier::black_ptr_ == 2, "Expecting black to have value 2");
6934 constexpr uint32_t gray_byte_position = LockWord::kReadBarrierStateShift / kBitsPerByte;
6935 constexpr uint32_t gray_bit_position = LockWord::kReadBarrierStateShift % kBitsPerByte;
6936 constexpr int32_t test_value = static_cast<int8_t>(1 << gray_bit_position);
6937
6938 // if (rb_state == ReadBarrier::gray_ptr_)
6939 // ref = ReadBarrier::Mark(ref);
6940 // At this point, just do the "if" and make sure that flags are preserved until the branch.
6941 __ testb(Address(obj, monitor_offset + gray_byte_position), Immediate(test_value));
Roland Levillain7c1559a2015-12-15 10:55:36 +00006942 if (needs_null_check) {
6943 MaybeRecordImplicitNullCheck(instruction);
6944 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00006945
6946 // Load fence to prevent load-load reordering.
6947 // Note that this is a no-op, thanks to the x86 memory model.
6948 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
6949
6950 // The actual reference load.
6951 // /* HeapReference<Object> */ ref = *src
Vladimir Marko953437b2016-08-24 08:30:46 +00006952 __ movl(ref_reg, src); // Flags are unaffected.
6953
6954 // Note: Reference unpoisoning modifies the flags, so we need to delay it after the branch.
6955 // Slow path marking the object `ref` when it is gray.
6956 SlowPathCode* slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathX86(
6957 instruction, ref, /* unpoison */ true);
6958 AddSlowPath(slow_path);
6959
6960 // We have done the "if" of the gray bit check above, now branch based on the flags.
6961 __ j(kNotZero, slow_path->GetEntryLabel());
Roland Levillain7c1559a2015-12-15 10:55:36 +00006962
6963 // Object* ref = ref_addr->AsMirrorPtr()
6964 __ MaybeUnpoisonHeapReference(ref_reg);
6965
Roland Levillain7c1559a2015-12-15 10:55:36 +00006966 __ Bind(slow_path->GetExitLabel());
6967}
6968
6969void CodeGeneratorX86::GenerateReadBarrierSlow(HInstruction* instruction,
6970 Location out,
6971 Location ref,
6972 Location obj,
6973 uint32_t offset,
6974 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006975 DCHECK(kEmitCompilerReadBarrier);
6976
Roland Levillain7c1559a2015-12-15 10:55:36 +00006977 // Insert a slow path based read barrier *after* the reference load.
6978 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00006979 // If heap poisoning is enabled, the unpoisoning of the loaded
6980 // reference will be carried out by the runtime within the slow
6981 // path.
6982 //
6983 // Note that `ref` currently does not get unpoisoned (when heap
6984 // poisoning is enabled), which is alright as the `ref` argument is
6985 // not used by the artReadBarrierSlow entry point.
6986 //
6987 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
6988 SlowPathCode* slow_path = new (GetGraph()->GetArena())
6989 ReadBarrierForHeapReferenceSlowPathX86(instruction, out, ref, obj, offset, index);
6990 AddSlowPath(slow_path);
6991
Roland Levillain0d5a2812015-11-13 10:07:31 +00006992 __ jmp(slow_path->GetEntryLabel());
6993 __ Bind(slow_path->GetExitLabel());
6994}
6995
Roland Levillain7c1559a2015-12-15 10:55:36 +00006996void CodeGeneratorX86::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
6997 Location out,
6998 Location ref,
6999 Location obj,
7000 uint32_t offset,
7001 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007002 if (kEmitCompilerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007003 // Baker's read barriers shall be handled by the fast path
7004 // (CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier).
7005 DCHECK(!kUseBakerReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007006 // If heap poisoning is enabled, unpoisoning will be taken care of
7007 // by the runtime within the slow path.
Roland Levillain7c1559a2015-12-15 10:55:36 +00007008 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007009 } else if (kPoisonHeapReferences) {
7010 __ UnpoisonHeapReference(out.AsRegister<Register>());
7011 }
7012}
7013
Roland Levillain7c1559a2015-12-15 10:55:36 +00007014void CodeGeneratorX86::GenerateReadBarrierForRootSlow(HInstruction* instruction,
7015 Location out,
7016 Location root) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007017 DCHECK(kEmitCompilerReadBarrier);
7018
Roland Levillain7c1559a2015-12-15 10:55:36 +00007019 // Insert a slow path based read barrier *after* the GC root load.
7020 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00007021 // Note that GC roots are not affected by heap poisoning, so we do
7022 // not need to do anything special for this here.
7023 SlowPathCode* slow_path =
7024 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathX86(instruction, out, root);
7025 AddSlowPath(slow_path);
7026
Roland Levillain0d5a2812015-11-13 10:07:31 +00007027 __ jmp(slow_path->GetEntryLabel());
7028 __ Bind(slow_path->GetExitLabel());
7029}
7030
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007031void LocationsBuilderX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00007032 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00007033 LOG(FATAL) << "Unreachable";
7034}
7035
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007036void InstructionCodeGeneratorX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00007037 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00007038 LOG(FATAL) << "Unreachable";
7039}
7040
Mark Mendellfe57faa2015-09-18 09:26:15 -04007041// Simple implementation of packed switch - generate cascaded compare/jumps.
7042void LocationsBuilderX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7043 LocationSummary* locations =
7044 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
7045 locations->SetInAt(0, Location::RequiresRegister());
7046}
7047
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007048void InstructionCodeGeneratorX86::GenPackedSwitchWithCompares(Register value_reg,
7049 int32_t lower_bound,
7050 uint32_t num_entries,
7051 HBasicBlock* switch_block,
7052 HBasicBlock* default_block) {
7053 // Figure out the correct compare values and jump conditions.
7054 // Handle the first compare/branch as a special case because it might
7055 // jump to the default case.
7056 DCHECK_GT(num_entries, 2u);
7057 Condition first_condition;
7058 uint32_t index;
7059 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
7060 if (lower_bound != 0) {
7061 first_condition = kLess;
7062 __ cmpl(value_reg, Immediate(lower_bound));
7063 __ j(first_condition, codegen_->GetLabelOf(default_block));
7064 __ j(kEqual, codegen_->GetLabelOf(successors[0]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007065
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007066 index = 1;
7067 } else {
7068 // Handle all the compare/jumps below.
7069 first_condition = kBelow;
7070 index = 0;
7071 }
7072
7073 // Handle the rest of the compare/jumps.
7074 for (; index + 1 < num_entries; index += 2) {
7075 int32_t compare_to_value = lower_bound + index + 1;
7076 __ cmpl(value_reg, Immediate(compare_to_value));
7077 // Jump to successors[index] if value < case_value[index].
7078 __ j(first_condition, codegen_->GetLabelOf(successors[index]));
7079 // Jump to successors[index + 1] if value == case_value[index + 1].
7080 __ j(kEqual, codegen_->GetLabelOf(successors[index + 1]));
7081 }
7082
7083 if (index != num_entries) {
7084 // There are an odd number of entries. Handle the last one.
7085 DCHECK_EQ(index + 1, num_entries);
7086 __ cmpl(value_reg, Immediate(lower_bound + index));
7087 __ j(kEqual, codegen_->GetLabelOf(successors[index]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007088 }
7089
7090 // And the default for any other value.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007091 if (!codegen_->GoesToNextBlock(switch_block, default_block)) {
7092 __ jmp(codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007093 }
7094}
7095
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007096void InstructionCodeGeneratorX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7097 int32_t lower_bound = switch_instr->GetStartValue();
7098 uint32_t num_entries = switch_instr->GetNumEntries();
7099 LocationSummary* locations = switch_instr->GetLocations();
7100 Register value_reg = locations->InAt(0).AsRegister<Register>();
7101
7102 GenPackedSwitchWithCompares(value_reg,
7103 lower_bound,
7104 num_entries,
7105 switch_instr->GetBlock(),
7106 switch_instr->GetDefaultBlock());
7107}
7108
Mark Mendell805b3b52015-09-18 14:10:29 -04007109void LocationsBuilderX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
7110 LocationSummary* locations =
7111 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
7112 locations->SetInAt(0, Location::RequiresRegister());
7113
7114 // Constant area pointer.
7115 locations->SetInAt(1, Location::RequiresRegister());
7116
7117 // And the temporary we need.
7118 locations->AddTemp(Location::RequiresRegister());
7119}
7120
7121void InstructionCodeGeneratorX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
7122 int32_t lower_bound = switch_instr->GetStartValue();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007123 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendell805b3b52015-09-18 14:10:29 -04007124 LocationSummary* locations = switch_instr->GetLocations();
7125 Register value_reg = locations->InAt(0).AsRegister<Register>();
7126 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
7127
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007128 if (num_entries <= kPackedSwitchJumpTableThreshold) {
7129 GenPackedSwitchWithCompares(value_reg,
7130 lower_bound,
7131 num_entries,
7132 switch_instr->GetBlock(),
7133 default_block);
7134 return;
7135 }
7136
Mark Mendell805b3b52015-09-18 14:10:29 -04007137 // Optimizing has a jump area.
7138 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
7139 Register constant_area = locations->InAt(1).AsRegister<Register>();
7140
7141 // Remove the bias, if needed.
7142 if (lower_bound != 0) {
7143 __ leal(temp_reg, Address(value_reg, -lower_bound));
7144 value_reg = temp_reg;
7145 }
7146
7147 // Is the value in range?
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007148 DCHECK_GE(num_entries, 1u);
Mark Mendell805b3b52015-09-18 14:10:29 -04007149 __ cmpl(value_reg, Immediate(num_entries - 1));
7150 __ j(kAbove, codegen_->GetLabelOf(default_block));
7151
7152 // We are in the range of the table.
7153 // Load (target-constant_area) from the jump table, indexing by the value.
7154 __ movl(temp_reg, codegen_->LiteralCaseTable(switch_instr, constant_area, value_reg));
7155
7156 // Compute the actual target address by adding in constant_area.
7157 __ addl(temp_reg, constant_area);
7158
7159 // And jump.
7160 __ jmp(temp_reg);
7161}
7162
Mark Mendell0616ae02015-04-17 12:49:27 -04007163void LocationsBuilderX86::VisitX86ComputeBaseMethodAddress(
7164 HX86ComputeBaseMethodAddress* insn) {
7165 LocationSummary* locations =
7166 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
7167 locations->SetOut(Location::RequiresRegister());
7168}
7169
7170void InstructionCodeGeneratorX86::VisitX86ComputeBaseMethodAddress(
7171 HX86ComputeBaseMethodAddress* insn) {
7172 LocationSummary* locations = insn->GetLocations();
7173 Register reg = locations->Out().AsRegister<Register>();
7174
7175 // Generate call to next instruction.
7176 Label next_instruction;
7177 __ call(&next_instruction);
7178 __ Bind(&next_instruction);
7179
7180 // Remember this offset for later use with constant area.
7181 codegen_->SetMethodAddressOffset(GetAssembler()->CodeSize());
7182
7183 // Grab the return address off the stack.
7184 __ popl(reg);
7185}
7186
7187void LocationsBuilderX86::VisitX86LoadFromConstantTable(
7188 HX86LoadFromConstantTable* insn) {
7189 LocationSummary* locations =
7190 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
7191
7192 locations->SetInAt(0, Location::RequiresRegister());
7193 locations->SetInAt(1, Location::ConstantLocation(insn->GetConstant()));
7194
7195 // If we don't need to be materialized, we only need the inputs to be set.
David Brazdilb3e773e2016-01-26 11:28:37 +00007196 if (insn->IsEmittedAtUseSite()) {
Mark Mendell0616ae02015-04-17 12:49:27 -04007197 return;
7198 }
7199
7200 switch (insn->GetType()) {
7201 case Primitive::kPrimFloat:
7202 case Primitive::kPrimDouble:
7203 locations->SetOut(Location::RequiresFpuRegister());
7204 break;
7205
7206 case Primitive::kPrimInt:
7207 locations->SetOut(Location::RequiresRegister());
7208 break;
7209
7210 default:
7211 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
7212 }
7213}
7214
7215void InstructionCodeGeneratorX86::VisitX86LoadFromConstantTable(HX86LoadFromConstantTable* insn) {
David Brazdilb3e773e2016-01-26 11:28:37 +00007216 if (insn->IsEmittedAtUseSite()) {
Mark Mendell0616ae02015-04-17 12:49:27 -04007217 return;
7218 }
7219
7220 LocationSummary* locations = insn->GetLocations();
7221 Location out = locations->Out();
7222 Register const_area = locations->InAt(0).AsRegister<Register>();
7223 HConstant *value = insn->GetConstant();
7224
7225 switch (insn->GetType()) {
7226 case Primitive::kPrimFloat:
7227 __ movss(out.AsFpuRegister<XmmRegister>(),
7228 codegen_->LiteralFloatAddress(value->AsFloatConstant()->GetValue(), const_area));
7229 break;
7230
7231 case Primitive::kPrimDouble:
7232 __ movsd(out.AsFpuRegister<XmmRegister>(),
7233 codegen_->LiteralDoubleAddress(value->AsDoubleConstant()->GetValue(), const_area));
7234 break;
7235
7236 case Primitive::kPrimInt:
7237 __ movl(out.AsRegister<Register>(),
7238 codegen_->LiteralInt32Address(value->AsIntConstant()->GetValue(), const_area));
7239 break;
7240
7241 default:
7242 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
7243 }
7244}
7245
Mark Mendell0616ae02015-04-17 12:49:27 -04007246/**
7247 * Class to handle late fixup of offsets into constant area.
7248 */
Vladimir Marko5233f932015-09-29 19:01:15 +01007249class RIPFixup : public AssemblerFixup, public ArenaObject<kArenaAllocCodeGenerator> {
Mark Mendell0616ae02015-04-17 12:49:27 -04007250 public:
Mark Mendell805b3b52015-09-18 14:10:29 -04007251 RIPFixup(CodeGeneratorX86& codegen, size_t offset)
7252 : codegen_(&codegen), offset_into_constant_area_(offset) {}
7253
7254 protected:
7255 void SetOffset(size_t offset) { offset_into_constant_area_ = offset; }
7256
7257 CodeGeneratorX86* codegen_;
Mark Mendell0616ae02015-04-17 12:49:27 -04007258
7259 private:
7260 void Process(const MemoryRegion& region, int pos) OVERRIDE {
7261 // Patch the correct offset for the instruction. The place to patch is the
7262 // last 4 bytes of the instruction.
7263 // The value to patch is the distance from the offset in the constant area
7264 // from the address computed by the HX86ComputeBaseMethodAddress instruction.
Mark Mendell805b3b52015-09-18 14:10:29 -04007265 int32_t constant_offset = codegen_->ConstantAreaStart() + offset_into_constant_area_;
7266 int32_t relative_position = constant_offset - codegen_->GetMethodAddressOffset();;
Mark Mendell0616ae02015-04-17 12:49:27 -04007267
7268 // Patch in the right value.
7269 region.StoreUnaligned<int32_t>(pos - 4, relative_position);
7270 }
7271
Mark Mendell0616ae02015-04-17 12:49:27 -04007272 // Location in constant area that the fixup refers to.
Mark Mendell805b3b52015-09-18 14:10:29 -04007273 int32_t offset_into_constant_area_;
Mark Mendell0616ae02015-04-17 12:49:27 -04007274};
7275
Mark Mendell805b3b52015-09-18 14:10:29 -04007276/**
7277 * Class to handle late fixup of offsets to a jump table that will be created in the
7278 * constant area.
7279 */
7280class JumpTableRIPFixup : public RIPFixup {
7281 public:
7282 JumpTableRIPFixup(CodeGeneratorX86& codegen, HX86PackedSwitch* switch_instr)
7283 : RIPFixup(codegen, static_cast<size_t>(-1)), switch_instr_(switch_instr) {}
7284
7285 void CreateJumpTable() {
7286 X86Assembler* assembler = codegen_->GetAssembler();
7287
7288 // Ensure that the reference to the jump table has the correct offset.
7289 const int32_t offset_in_constant_table = assembler->ConstantAreaSize();
7290 SetOffset(offset_in_constant_table);
7291
7292 // The label values in the jump table are computed relative to the
7293 // instruction addressing the constant area.
7294 const int32_t relative_offset = codegen_->GetMethodAddressOffset();
7295
7296 // Populate the jump table with the correct values for the jump table.
7297 int32_t num_entries = switch_instr_->GetNumEntries();
7298 HBasicBlock* block = switch_instr_->GetBlock();
7299 const ArenaVector<HBasicBlock*>& successors = block->GetSuccessors();
7300 // The value that we want is the target offset - the position of the table.
7301 for (int32_t i = 0; i < num_entries; i++) {
7302 HBasicBlock* b = successors[i];
7303 Label* l = codegen_->GetLabelOf(b);
7304 DCHECK(l->IsBound());
7305 int32_t offset_to_block = l->Position() - relative_offset;
7306 assembler->AppendInt32(offset_to_block);
7307 }
7308 }
7309
7310 private:
7311 const HX86PackedSwitch* switch_instr_;
7312};
7313
7314void CodeGeneratorX86::Finalize(CodeAllocator* allocator) {
7315 // Generate the constant area if needed.
7316 X86Assembler* assembler = GetAssembler();
7317 if (!assembler->IsConstantAreaEmpty() || !fixups_to_jump_tables_.empty()) {
7318 // Align to 4 byte boundary to reduce cache misses, as the data is 4 and 8
7319 // byte values.
7320 assembler->Align(4, 0);
7321 constant_area_start_ = assembler->CodeSize();
7322
7323 // Populate any jump tables.
7324 for (auto jump_table : fixups_to_jump_tables_) {
7325 jump_table->CreateJumpTable();
7326 }
7327
7328 // And now add the constant area to the generated code.
7329 assembler->AddConstantArea();
7330 }
7331
7332 // And finish up.
7333 CodeGenerator::Finalize(allocator);
7334}
7335
Mark Mendell0616ae02015-04-17 12:49:27 -04007336Address CodeGeneratorX86::LiteralDoubleAddress(double v, Register reg) {
7337 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddDouble(v));
7338 return Address(reg, kDummy32BitOffset, fixup);
7339}
7340
7341Address CodeGeneratorX86::LiteralFloatAddress(float v, Register reg) {
7342 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddFloat(v));
7343 return Address(reg, kDummy32BitOffset, fixup);
7344}
7345
7346Address CodeGeneratorX86::LiteralInt32Address(int32_t v, Register reg) {
7347 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt32(v));
7348 return Address(reg, kDummy32BitOffset, fixup);
7349}
7350
7351Address CodeGeneratorX86::LiteralInt64Address(int64_t v, Register reg) {
7352 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt64(v));
7353 return Address(reg, kDummy32BitOffset, fixup);
7354}
7355
Aart Bika19616e2016-02-01 18:57:58 -08007356void CodeGeneratorX86::Load32BitValue(Register dest, int32_t value) {
7357 if (value == 0) {
7358 __ xorl(dest, dest);
7359 } else {
7360 __ movl(dest, Immediate(value));
7361 }
7362}
7363
7364void CodeGeneratorX86::Compare32BitValue(Register dest, int32_t value) {
7365 if (value == 0) {
7366 __ testl(dest, dest);
7367 } else {
7368 __ cmpl(dest, Immediate(value));
7369 }
7370}
7371
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007372void CodeGeneratorX86::GenerateIntCompare(Location lhs, Location rhs) {
7373 Register lhs_reg = lhs.AsRegister<Register>();
jessicahandojo4877b792016-09-08 19:49:13 -07007374 GenerateIntCompare(lhs_reg, rhs);
7375}
7376
7377void CodeGeneratorX86::GenerateIntCompare(Register lhs, Location rhs) {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007378 if (rhs.IsConstant()) {
7379 int32_t value = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
jessicahandojo4877b792016-09-08 19:49:13 -07007380 Compare32BitValue(lhs, value);
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007381 } else if (rhs.IsStackSlot()) {
jessicahandojo4877b792016-09-08 19:49:13 -07007382 __ cmpl(lhs, Address(ESP, rhs.GetStackIndex()));
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007383 } else {
jessicahandojo4877b792016-09-08 19:49:13 -07007384 __ cmpl(lhs, rhs.AsRegister<Register>());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007385 }
7386}
7387
7388Address CodeGeneratorX86::ArrayAddress(Register obj,
7389 Location index,
7390 ScaleFactor scale,
7391 uint32_t data_offset) {
7392 return index.IsConstant() ?
7393 Address(obj, (index.GetConstant()->AsIntConstant()->GetValue() << scale) + data_offset) :
7394 Address(obj, index.AsRegister<Register>(), scale, data_offset);
7395}
7396
Mark Mendell805b3b52015-09-18 14:10:29 -04007397Address CodeGeneratorX86::LiteralCaseTable(HX86PackedSwitch* switch_instr,
7398 Register reg,
7399 Register value) {
7400 // Create a fixup to be used to create and address the jump table.
7401 JumpTableRIPFixup* table_fixup =
7402 new (GetGraph()->GetArena()) JumpTableRIPFixup(*this, switch_instr);
7403
7404 // We have to populate the jump tables.
7405 fixups_to_jump_tables_.push_back(table_fixup);
7406
7407 // We want a scaled address, as we are extracting the correct offset from the table.
7408 return Address(reg, value, TIMES_4, kDummy32BitOffset, table_fixup);
7409}
7410
Andreas Gampe85b62f22015-09-09 13:15:38 -07007411// TODO: target as memory.
7412void CodeGeneratorX86::MoveFromReturnRegister(Location target, Primitive::Type type) {
7413 if (!target.IsValid()) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007414 DCHECK_EQ(type, Primitive::kPrimVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07007415 return;
7416 }
7417
7418 DCHECK_NE(type, Primitive::kPrimVoid);
7419
7420 Location return_loc = InvokeDexCallingConventionVisitorX86().GetReturnLocation(type);
7421 if (target.Equals(return_loc)) {
7422 return;
7423 }
7424
7425 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
7426 // with the else branch.
7427 if (type == Primitive::kPrimLong) {
7428 HParallelMove parallel_move(GetGraph()->GetArena());
7429 parallel_move.AddMove(return_loc.ToLow(), target.ToLow(), Primitive::kPrimInt, nullptr);
7430 parallel_move.AddMove(return_loc.ToHigh(), target.ToHigh(), Primitive::kPrimInt, nullptr);
7431 GetMoveResolver()->EmitNativeCode(&parallel_move);
7432 } else {
7433 // Let the parallel move resolver take care of all of this.
7434 HParallelMove parallel_move(GetGraph()->GetArena());
7435 parallel_move.AddMove(return_loc, target, type, nullptr);
7436 GetMoveResolver()->EmitNativeCode(&parallel_move);
7437 }
7438}
7439
Roland Levillain4d027112015-07-01 15:41:14 +01007440#undef __
7441
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00007442} // namespace x86
7443} // namespace art