blob: 49426440f626784d38753cb8e8da8fe3edacb231 [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);
153 }
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000154 x86_codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100155 locations->InAt(0),
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000156 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100157 Primitive::kPrimInt,
Mark Mendellee8d9712016-07-12 11:13:15 -0400158 length_loc,
Nicolas Geoffray90218252015-04-15 11:56:51 +0100159 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
160 Primitive::kPrimInt);
Serban Constantinescuba45db02016-07-12 22:53:02 +0100161 QuickEntrypointEnum entrypoint = instruction_->AsBoundsCheck()->IsStringCharAt()
162 ? kQuickThrowStringBounds
163 : kQuickThrowArrayBounds;
164 x86_codegen->InvokeRuntime(entrypoint, instruction_, instruction_->GetDexPc(), this);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100165 CheckEntrypointTypes<kQuickThrowStringBounds, void, int32_t, int32_t>();
Roland Levillain888d0672015-11-23 18:53:50 +0000166 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100167 }
168
Alexandre Rames8158f282015-08-07 10:26:17 +0100169 bool IsFatal() const OVERRIDE { return true; }
170
Alexandre Rames9931f312015-06-19 14:47:01 +0100171 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathX86"; }
172
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100173 private:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100174 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathX86);
175};
176
Andreas Gampe85b62f22015-09-09 13:15:38 -0700177class SuspendCheckSlowPathX86 : public SlowPathCode {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000178 public:
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000179 SuspendCheckSlowPathX86(HSuspendCheck* instruction, HBasicBlock* successor)
David Srbecky9cd6d372016-02-09 15:24:47 +0000180 : SlowPathCode(instruction), successor_(successor) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000181
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000182 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100183 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000184 __ Bind(GetEntryLabel());
Serban Constantinescuba45db02016-07-12 22:53:02 +0100185 x86_codegen->InvokeRuntime(kQuickTestSuspend, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000186 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100187 if (successor_ == nullptr) {
188 __ jmp(GetReturnLabel());
189 } else {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100190 __ jmp(x86_codegen->GetLabelOf(successor_));
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100191 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000192 }
193
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100194 Label* GetReturnLabel() {
195 DCHECK(successor_ == nullptr);
196 return &return_label_;
197 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000198
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100199 HBasicBlock* GetSuccessor() const {
200 return successor_;
201 }
202
Alexandre Rames9931f312015-06-19 14:47:01 +0100203 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathX86"; }
204
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000205 private:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100206 HBasicBlock* const successor_;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000207 Label return_label_;
208
209 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathX86);
210};
211
Vladimir Marko63dccbbe2016-09-21 13:51:10 +0100212class LoadStringSlowPathX86 : public SlowPathCode {
213 public:
214 explicit LoadStringSlowPathX86(HLoadString* instruction): SlowPathCode(instruction) {}
215
216 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
217 LocationSummary* locations = instruction_->GetLocations();
218 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
219
220 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
221 __ Bind(GetEntryLabel());
222 SaveLiveRegisters(codegen, locations);
223
224 InvokeRuntimeCallingConvention calling_convention;
225 const uint32_t string_index = instruction_->AsLoadString()->GetStringIndex();
226 __ movl(calling_convention.GetRegisterAt(0), Immediate(string_index));
227 x86_codegen->InvokeRuntime(kQuickResolveString, instruction_, instruction_->GetDexPc(), this);
228 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
229 x86_codegen->Move32(locations->Out(), Location::RegisterLocation(EAX));
230 RestoreLiveRegisters(codegen, locations);
231
232 // Store the resolved String to the BSS entry.
233 Register method_address = locations->InAt(0).AsRegister<Register>();
234 __ movl(Address(method_address, CodeGeneratorX86::kDummy32BitOffset),
235 locations->Out().AsRegister<Register>());
236 Label* fixup_label = x86_codegen->NewStringBssEntryPatch(instruction_->AsLoadString());
237 __ Bind(fixup_label);
238
239 __ jmp(GetExitLabel());
240 }
241
242 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathX86"; }
243
244 private:
245 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathX86);
246};
247
Andreas Gampe85b62f22015-09-09 13:15:38 -0700248class LoadClassSlowPathX86 : public SlowPathCode {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000249 public:
250 LoadClassSlowPathX86(HLoadClass* cls,
251 HInstruction* at,
252 uint32_t dex_pc,
253 bool do_clinit)
David Srbecky9cd6d372016-02-09 15:24:47 +0000254 : SlowPathCode(at), cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000255 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
256 }
257
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000258 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000259 LocationSummary* locations = at_->GetLocations();
260 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
261 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000262 SaveLiveRegisters(codegen, locations);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000263
264 InvokeRuntimeCallingConvention calling_convention;
265 __ movl(calling_convention.GetRegisterAt(0), Immediate(cls_->GetTypeIndex()));
Serban Constantinescuba45db02016-07-12 22:53:02 +0100266 x86_codegen->InvokeRuntime(do_clinit_ ? kQuickInitializeStaticStorage
267 : kQuickInitializeType,
Alexandre Rames8158f282015-08-07 10:26:17 +0100268 at_, dex_pc_, this);
Roland Levillain888d0672015-11-23 18:53:50 +0000269 if (do_clinit_) {
270 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
271 } else {
272 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
273 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000274
275 // Move the class to the desired location.
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000276 Location out = locations->Out();
277 if (out.IsValid()) {
278 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
279 x86_codegen->Move32(out, Location::RegisterLocation(EAX));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000280 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000281
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000282 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000283 __ jmp(GetExitLabel());
284 }
285
Alexandre Rames9931f312015-06-19 14:47:01 +0100286 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathX86"; }
287
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000288 private:
289 // The class this slow path will load.
290 HLoadClass* const cls_;
291
292 // The instruction where this slow path is happening.
293 // (Might be the load class or an initialization check).
294 HInstruction* const at_;
295
296 // The dex PC of `at_`.
297 const uint32_t dex_pc_;
298
299 // Whether to initialize the class.
300 const bool do_clinit_;
301
302 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathX86);
303};
304
Andreas Gampe85b62f22015-09-09 13:15:38 -0700305class TypeCheckSlowPathX86 : public SlowPathCode {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000306 public:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000307 TypeCheckSlowPathX86(HInstruction* instruction, bool is_fatal)
David Srbecky9cd6d372016-02-09 15:24:47 +0000308 : SlowPathCode(instruction), is_fatal_(is_fatal) {}
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000309
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000310 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000311 LocationSummary* locations = instruction_->GetLocations();
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100312 Location object_class = instruction_->IsCheckCast() ? locations->GetTemp(0)
313 : locations->Out();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000314 DCHECK(instruction_->IsCheckCast()
315 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000316
317 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
318 __ Bind(GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000319
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000320 if (!is_fatal_) {
321 SaveLiveRegisters(codegen, locations);
322 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000323
324 // We're moving two locations to locations that could overlap, so we need a parallel
325 // move resolver.
326 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000327 x86_codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100328 locations->InAt(1),
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000329 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100330 Primitive::kPrimNot,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100331 object_class,
Nicolas Geoffray90218252015-04-15 11:56:51 +0100332 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
333 Primitive::kPrimNot);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000334
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000335 if (instruction_->IsInstanceOf()) {
Serban Constantinescuba45db02016-07-12 22:53:02 +0100336 x86_codegen->InvokeRuntime(kQuickInstanceofNonTrivial,
Alexandre Rames8158f282015-08-07 10:26:17 +0100337 instruction_,
338 instruction_->GetDexPc(),
339 this);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000340 CheckEntrypointTypes<
Andreas Gampe67409972016-07-19 22:34:53 -0700341 kQuickInstanceofNonTrivial, size_t, const mirror::Class*, const mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000342 } else {
343 DCHECK(instruction_->IsCheckCast());
Serban Constantinescuba45db02016-07-12 22:53:02 +0100344 x86_codegen->InvokeRuntime(kQuickCheckCast, instruction_, instruction_->GetDexPc(), this);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000345 CheckEntrypointTypes<kQuickCheckCast, void, const mirror::Class*, const mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000346 }
347
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000348 if (!is_fatal_) {
349 if (instruction_->IsInstanceOf()) {
350 x86_codegen->Move32(locations->Out(), Location::RegisterLocation(EAX));
351 }
352 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray75374372015-09-17 17:12:19 +0000353
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000354 __ jmp(GetExitLabel());
355 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000356 }
357
Alexandre Rames9931f312015-06-19 14:47:01 +0100358 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathX86"; }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000359 bool IsFatal() const OVERRIDE { return is_fatal_; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100360
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000361 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000362 const bool is_fatal_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000363
364 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathX86);
365};
366
Andreas Gampe85b62f22015-09-09 13:15:38 -0700367class DeoptimizationSlowPathX86 : public SlowPathCode {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700368 public:
Aart Bik42249c32016-01-07 15:33:50 -0800369 explicit DeoptimizationSlowPathX86(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000370 : SlowPathCode(instruction) {}
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700371
372 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames8158f282015-08-07 10:26:17 +0100373 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700374 __ Bind(GetEntryLabel());
Serban Constantinescuba45db02016-07-12 22:53:02 +0100375 x86_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000376 CheckEntrypointTypes<kQuickDeoptimize, void, void>();
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700377 }
378
Alexandre Rames9931f312015-06-19 14:47:01 +0100379 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathX86"; }
380
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700381 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700382 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathX86);
383};
384
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100385class ArraySetSlowPathX86 : public SlowPathCode {
386 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000387 explicit ArraySetSlowPathX86(HInstruction* instruction) : SlowPathCode(instruction) {}
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100388
389 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
390 LocationSummary* locations = instruction_->GetLocations();
391 __ Bind(GetEntryLabel());
392 SaveLiveRegisters(codegen, locations);
393
394 InvokeRuntimeCallingConvention calling_convention;
395 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
396 parallel_move.AddMove(
397 locations->InAt(0),
398 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
399 Primitive::kPrimNot,
400 nullptr);
401 parallel_move.AddMove(
402 locations->InAt(1),
403 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
404 Primitive::kPrimInt,
405 nullptr);
406 parallel_move.AddMove(
407 locations->InAt(2),
408 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
409 Primitive::kPrimNot,
410 nullptr);
411 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
412
413 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Serban Constantinescuba45db02016-07-12 22:53:02 +0100414 x86_codegen->InvokeRuntime(kQuickAputObject, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000415 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100416 RestoreLiveRegisters(codegen, locations);
417 __ jmp(GetExitLabel());
418 }
419
420 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathX86"; }
421
422 private:
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100423 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathX86);
424};
425
Roland Levillain7c1559a2015-12-15 10:55:36 +0000426// Slow path marking an object during a read barrier.
427class ReadBarrierMarkSlowPathX86 : public SlowPathCode {
428 public:
Vladimir Marko953437b2016-08-24 08:30:46 +0000429 ReadBarrierMarkSlowPathX86(HInstruction* instruction, Location obj, bool unpoison)
430 : SlowPathCode(instruction), obj_(obj), unpoison_(unpoison) {
Roland Levillain7c1559a2015-12-15 10:55:36 +0000431 DCHECK(kEmitCompilerReadBarrier);
432 }
433
434 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathX86"; }
435
436 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
437 LocationSummary* locations = instruction_->GetLocations();
Roland Levillain02b75802016-07-13 11:54:35 +0100438 Register reg = obj_.AsRegister<Register>();
Roland Levillain7c1559a2015-12-15 10:55:36 +0000439 DCHECK(locations->CanCall());
Roland Levillain02b75802016-07-13 11:54:35 +0100440 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg));
Roland Levillain7c1559a2015-12-15 10:55:36 +0000441 DCHECK(instruction_->IsInstanceFieldGet() ||
442 instruction_->IsStaticFieldGet() ||
443 instruction_->IsArrayGet() ||
Roland Levillain16d9f942016-08-25 17:27:56 +0100444 instruction_->IsArraySet() ||
Roland Levillain7c1559a2015-12-15 10:55:36 +0000445 instruction_->IsLoadClass() ||
446 instruction_->IsLoadString() ||
447 instruction_->IsInstanceOf() ||
Roland Levillain3d312422016-06-23 13:53:42 +0100448 instruction_->IsCheckCast() ||
Roland Levillain0b671c02016-08-19 12:02:34 +0100449 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()) ||
450 (instruction_->IsInvokeStaticOrDirect() && instruction_->GetLocations()->Intrinsified()))
Roland Levillain7c1559a2015-12-15 10:55:36 +0000451 << "Unexpected instruction in read barrier marking slow path: "
452 << instruction_->DebugName();
453
454 __ Bind(GetEntryLabel());
Vladimir Marko953437b2016-08-24 08:30:46 +0000455 if (unpoison_) {
456 // Object* ref = ref_addr->AsMirrorPtr()
457 __ MaybeUnpoisonHeapReference(reg);
458 }
Roland Levillain4359e612016-07-20 11:32:19 +0100459 // No need to save live registers; it's taken care of by the
460 // entrypoint. Also, there is no need to update the stack mask,
461 // as this runtime call will not trigger a garbage collection.
Roland Levillain7c1559a2015-12-15 10:55:36 +0000462 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Roland Levillain02b75802016-07-13 11:54:35 +0100463 DCHECK_NE(reg, ESP);
464 DCHECK(0 <= reg && reg < kNumberOfCpuRegisters) << reg;
465 // "Compact" slow path, saving two moves.
466 //
467 // Instead of using the standard runtime calling convention (input
468 // and output in EAX):
469 //
470 // EAX <- obj
471 // EAX <- ReadBarrierMark(EAX)
472 // obj <- EAX
473 //
474 // we just use rX (the register holding `obj`) as input and output
475 // of a dedicated entrypoint:
476 //
477 // rX <- ReadBarrierMarkRegX(rX)
478 //
479 int32_t entry_point_offset =
Andreas Gampe542451c2016-07-26 09:02:02 -0700480 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kX86PointerSize>(reg);
Roland Levillaindec8f632016-07-22 17:10:06 +0100481 // This runtime call does not require a stack map.
482 x86_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
Roland Levillain7c1559a2015-12-15 10:55:36 +0000483 __ jmp(GetExitLabel());
484 }
485
486 private:
Roland Levillain7c1559a2015-12-15 10:55:36 +0000487 const Location obj_;
Vladimir Marko953437b2016-08-24 08:30:46 +0000488 const bool unpoison_;
Roland Levillain7c1559a2015-12-15 10:55:36 +0000489
490 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathX86);
491};
492
Roland Levillain0d5a2812015-11-13 10:07:31 +0000493// Slow path generating a read barrier for a heap reference.
494class ReadBarrierForHeapReferenceSlowPathX86 : public SlowPathCode {
495 public:
496 ReadBarrierForHeapReferenceSlowPathX86(HInstruction* instruction,
497 Location out,
498 Location ref,
499 Location obj,
500 uint32_t offset,
501 Location index)
David Srbecky9cd6d372016-02-09 15:24:47 +0000502 : SlowPathCode(instruction),
Roland Levillain0d5a2812015-11-13 10:07:31 +0000503 out_(out),
504 ref_(ref),
505 obj_(obj),
506 offset_(offset),
507 index_(index) {
508 DCHECK(kEmitCompilerReadBarrier);
509 // If `obj` is equal to `out` or `ref`, it means the initial object
510 // has been overwritten by (or after) the heap object reference load
511 // to be instrumented, e.g.:
512 //
513 // __ movl(out, Address(out, offset));
Roland Levillain7c1559a2015-12-15 10:55:36 +0000514 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000515 //
516 // In that case, we have lost the information about the original
517 // object, and the emitted read barrier cannot work properly.
518 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
519 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
520 }
521
522 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
523 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
524 LocationSummary* locations = instruction_->GetLocations();
525 Register reg_out = out_.AsRegister<Register>();
526 DCHECK(locations->CanCall());
527 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
Roland Levillain3d312422016-06-23 13:53:42 +0100528 DCHECK(instruction_->IsInstanceFieldGet() ||
529 instruction_->IsStaticFieldGet() ||
530 instruction_->IsArrayGet() ||
531 instruction_->IsInstanceOf() ||
532 instruction_->IsCheckCast() ||
Roland Levillaindec8f632016-07-22 17:10:06 +0100533 (instruction_->IsInvokeVirtual()) && instruction_->GetLocations()->Intrinsified())
Roland Levillain7c1559a2015-12-15 10:55:36 +0000534 << "Unexpected instruction in read barrier for heap reference slow path: "
535 << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000536
537 __ Bind(GetEntryLabel());
538 SaveLiveRegisters(codegen, locations);
539
540 // We may have to change the index's value, but as `index_` is a
541 // constant member (like other "inputs" of this slow path),
542 // introduce a copy of it, `index`.
543 Location index = index_;
544 if (index_.IsValid()) {
Roland Levillain3d312422016-06-23 13:53:42 +0100545 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
Roland Levillain0d5a2812015-11-13 10:07:31 +0000546 if (instruction_->IsArrayGet()) {
547 // Compute the actual memory offset and store it in `index`.
548 Register index_reg = index_.AsRegister<Register>();
549 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg));
550 if (codegen->IsCoreCalleeSaveRegister(index_reg)) {
551 // We are about to change the value of `index_reg` (see the
552 // calls to art::x86::X86Assembler::shll and
553 // art::x86::X86Assembler::AddImmediate below), but it has
554 // not been saved by the previous call to
555 // art::SlowPathCode::SaveLiveRegisters, as it is a
556 // callee-save register --
557 // art::SlowPathCode::SaveLiveRegisters does not consider
558 // callee-save registers, as it has been designed with the
559 // assumption that callee-save registers are supposed to be
560 // handled by the called function. So, as a callee-save
561 // register, `index_reg` _would_ eventually be saved onto
562 // the stack, but it would be too late: we would have
563 // changed its value earlier. Therefore, we manually save
564 // it here into another freely available register,
565 // `free_reg`, chosen of course among the caller-save
566 // registers (as a callee-save `free_reg` register would
567 // exhibit the same problem).
568 //
569 // Note we could have requested a temporary register from
570 // the register allocator instead; but we prefer not to, as
571 // this is a slow path, and we know we can find a
572 // caller-save register that is available.
573 Register free_reg = FindAvailableCallerSaveRegister(codegen);
574 __ movl(free_reg, index_reg);
575 index_reg = free_reg;
576 index = Location::RegisterLocation(index_reg);
577 } else {
578 // The initial register stored in `index_` has already been
579 // saved in the call to art::SlowPathCode::SaveLiveRegisters
580 // (as it is not a callee-save register), so we can freely
581 // use it.
582 }
583 // Shifting the index value contained in `index_reg` by the scale
584 // factor (2) cannot overflow in practice, as the runtime is
585 // unable to allocate object arrays with a size larger than
586 // 2^26 - 1 (that is, 2^28 - 4 bytes).
587 __ shll(index_reg, Immediate(TIMES_4));
588 static_assert(
589 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
590 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
591 __ AddImmediate(index_reg, Immediate(offset_));
592 } else {
Roland Levillain3d312422016-06-23 13:53:42 +0100593 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
594 // intrinsics, `index_` is not shifted by a scale factor of 2
595 // (as in the case of ArrayGet), as it is actually an offset
596 // to an object field within an object.
597 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000598 DCHECK(instruction_->GetLocations()->Intrinsified());
599 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
600 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
601 << instruction_->AsInvoke()->GetIntrinsic();
602 DCHECK_EQ(offset_, 0U);
603 DCHECK(index_.IsRegisterPair());
604 // UnsafeGet's offset location is a register pair, the low
605 // part contains the correct offset.
606 index = index_.ToLow();
607 }
608 }
609
610 // We're moving two or three locations to locations that could
611 // overlap, so we need a parallel move resolver.
612 InvokeRuntimeCallingConvention calling_convention;
613 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
614 parallel_move.AddMove(ref_,
615 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
616 Primitive::kPrimNot,
617 nullptr);
618 parallel_move.AddMove(obj_,
619 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
620 Primitive::kPrimNot,
621 nullptr);
622 if (index.IsValid()) {
623 parallel_move.AddMove(index,
624 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
625 Primitive::kPrimInt,
626 nullptr);
627 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
628 } else {
629 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
630 __ movl(calling_convention.GetRegisterAt(2), Immediate(offset_));
631 }
Serban Constantinescuba45db02016-07-12 22:53:02 +0100632 x86_codegen->InvokeRuntime(kQuickReadBarrierSlow, instruction_, instruction_->GetDexPc(), this);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000633 CheckEntrypointTypes<
634 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
635 x86_codegen->Move32(out_, Location::RegisterLocation(EAX));
636
637 RestoreLiveRegisters(codegen, locations);
638 __ jmp(GetExitLabel());
639 }
640
641 const char* GetDescription() const OVERRIDE { return "ReadBarrierForHeapReferenceSlowPathX86"; }
642
643 private:
644 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
645 size_t ref = static_cast<int>(ref_.AsRegister<Register>());
646 size_t obj = static_cast<int>(obj_.AsRegister<Register>());
647 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
648 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
649 return static_cast<Register>(i);
650 }
651 }
652 // We shall never fail to find a free caller-save register, as
653 // there are more than two core caller-save registers on x86
654 // (meaning it is possible to find one which is different from
655 // `ref` and `obj`).
656 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
657 LOG(FATAL) << "Could not find a free caller-save register";
658 UNREACHABLE();
659 }
660
Roland Levillain0d5a2812015-11-13 10:07:31 +0000661 const Location out_;
662 const Location ref_;
663 const Location obj_;
664 const uint32_t offset_;
665 // An additional location containing an index to an array.
666 // Only used for HArrayGet and the UnsafeGetObject &
667 // UnsafeGetObjectVolatile intrinsics.
668 const Location index_;
669
670 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathX86);
671};
672
673// Slow path generating a read barrier for a GC root.
674class ReadBarrierForRootSlowPathX86 : public SlowPathCode {
675 public:
676 ReadBarrierForRootSlowPathX86(HInstruction* instruction, Location out, Location root)
David Srbecky9cd6d372016-02-09 15:24:47 +0000677 : SlowPathCode(instruction), out_(out), root_(root) {
Roland Levillain7c1559a2015-12-15 10:55:36 +0000678 DCHECK(kEmitCompilerReadBarrier);
679 }
Roland Levillain0d5a2812015-11-13 10:07:31 +0000680
681 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
682 LocationSummary* locations = instruction_->GetLocations();
683 Register reg_out = out_.AsRegister<Register>();
684 DCHECK(locations->CanCall());
685 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
Roland Levillain7c1559a2015-12-15 10:55:36 +0000686 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
687 << "Unexpected instruction in read barrier for GC root slow path: "
688 << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000689
690 __ Bind(GetEntryLabel());
691 SaveLiveRegisters(codegen, locations);
692
693 InvokeRuntimeCallingConvention calling_convention;
694 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
695 x86_codegen->Move32(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), root_);
Serban Constantinescuba45db02016-07-12 22:53:02 +0100696 x86_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000697 instruction_,
698 instruction_->GetDexPc(),
699 this);
700 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
701 x86_codegen->Move32(out_, Location::RegisterLocation(EAX));
702
703 RestoreLiveRegisters(codegen, locations);
704 __ jmp(GetExitLabel());
705 }
706
707 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathX86"; }
708
709 private:
Roland Levillain0d5a2812015-11-13 10:07:31 +0000710 const Location out_;
711 const Location root_;
712
713 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathX86);
714};
715
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100716#undef __
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100717// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
718#define __ down_cast<X86Assembler*>(GetAssembler())-> // NOLINT
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100719
Aart Bike9f37602015-10-09 11:15:55 -0700720inline Condition X86Condition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -0700721 switch (cond) {
722 case kCondEQ: return kEqual;
723 case kCondNE: return kNotEqual;
724 case kCondLT: return kLess;
725 case kCondLE: return kLessEqual;
726 case kCondGT: return kGreater;
727 case kCondGE: return kGreaterEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700728 case kCondB: return kBelow;
729 case kCondBE: return kBelowEqual;
730 case kCondA: return kAbove;
731 case kCondAE: return kAboveEqual;
Dave Allison20dfc792014-06-16 20:44:29 -0700732 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100733 LOG(FATAL) << "Unreachable";
734 UNREACHABLE();
735}
736
Aart Bike9f37602015-10-09 11:15:55 -0700737// Maps signed condition to unsigned condition and FP condition to x86 name.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100738inline Condition X86UnsignedOrFPCondition(IfCondition cond) {
739 switch (cond) {
740 case kCondEQ: return kEqual;
741 case kCondNE: return kNotEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700742 // Signed to unsigned, and FP to x86 name.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100743 case kCondLT: return kBelow;
744 case kCondLE: return kBelowEqual;
745 case kCondGT: return kAbove;
746 case kCondGE: return kAboveEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700747 // Unsigned remain unchanged.
748 case kCondB: return kBelow;
749 case kCondBE: return kBelowEqual;
750 case kCondA: return kAbove;
751 case kCondAE: return kAboveEqual;
Roland Levillain4fa13f62015-07-06 18:11:54 +0100752 }
753 LOG(FATAL) << "Unreachable";
754 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -0700755}
756
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100757void CodeGeneratorX86::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100758 stream << Register(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100759}
760
761void CodeGeneratorX86::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100762 stream << XmmRegister(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100763}
764
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100765size_t CodeGeneratorX86::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
766 __ movl(Address(ESP, stack_index), static_cast<Register>(reg_id));
767 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100768}
769
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100770size_t CodeGeneratorX86::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
771 __ movl(static_cast<Register>(reg_id), Address(ESP, stack_index));
772 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100773}
774
Mark Mendell7c8d0092015-01-26 11:21:33 -0500775size_t CodeGeneratorX86::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
776 __ movsd(Address(ESP, stack_index), XmmRegister(reg_id));
777 return GetFloatingPointSpillSlotSize();
778}
779
780size_t CodeGeneratorX86::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
781 __ movsd(XmmRegister(reg_id), Address(ESP, stack_index));
782 return GetFloatingPointSpillSlotSize();
783}
784
Calin Juravle175dc732015-08-25 15:42:32 +0100785void CodeGeneratorX86::InvokeRuntime(QuickEntrypointEnum entrypoint,
786 HInstruction* instruction,
787 uint32_t dex_pc,
788 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +0100789 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Serban Constantinescuba45db02016-07-12 22:53:02 +0100790 GenerateInvokeRuntime(GetThreadOffset<kX86PointerSize>(entrypoint).Int32Value());
791 if (EntrypointRequiresStackMap(entrypoint)) {
792 RecordPcInfo(instruction, dex_pc, slow_path);
793 }
Alexandre Rames8158f282015-08-07 10:26:17 +0100794}
795
Roland Levillaindec8f632016-07-22 17:10:06 +0100796void CodeGeneratorX86::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
797 HInstruction* instruction,
798 SlowPathCode* slow_path) {
799 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
Serban Constantinescuba45db02016-07-12 22:53:02 +0100800 GenerateInvokeRuntime(entry_point_offset);
801}
802
803void CodeGeneratorX86::GenerateInvokeRuntime(int32_t entry_point_offset) {
Roland Levillaindec8f632016-07-22 17:10:06 +0100804 __ fs()->call(Address::Absolute(entry_point_offset));
805}
806
Mark Mendellfb8d2792015-03-31 22:16:59 -0400807CodeGeneratorX86::CodeGeneratorX86(HGraph* graph,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000808 const X86InstructionSetFeatures& isa_features,
809 const CompilerOptions& compiler_options,
810 OptimizingCompilerStats* stats)
Mark Mendell5f874182015-03-04 15:42:45 -0500811 : CodeGenerator(graph,
812 kNumberOfCpuRegisters,
813 kNumberOfXmmRegisters,
814 kNumberOfRegisterPairs,
815 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
816 arraysize(kCoreCalleeSaves))
817 | (1 << kFakeReturnRegister),
Serban Constantinescuecc43662015-08-13 13:33:12 +0100818 0,
819 compiler_options,
820 stats),
Vladimir Marko225b6462015-09-28 12:17:40 +0100821 block_labels_(nullptr),
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100822 location_builder_(graph, this),
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100823 instruction_visitor_(graph, this),
Mark Mendellfb8d2792015-03-31 22:16:59 -0400824 move_resolver_(graph->GetArena(), this),
Vladimir Marko93205e32016-04-13 11:59:46 +0100825 assembler_(graph->GetArena()),
Vladimir Marko58155012015-08-19 12:49:41 +0000826 isa_features_(isa_features),
Vladimir Marko5233f932015-09-29 19:01:15 +0100827 method_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Mark Mendell805b3b52015-09-18 14:10:29 -0400828 relative_call_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000829 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000830 simple_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
831 string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100832 type_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko93205e32016-04-13 11:59:46 +0100833 constant_area_start_(-1),
834 fixups_to_jump_tables_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
835 method_address_offset_(-1) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000836 // Use a fake return address register to mimic Quick.
837 AddAllocatedRegister(Location::RegisterLocation(kFakeReturnRegister));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100838}
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100839
David Brazdil58282f42016-01-14 12:45:10 +0000840void CodeGeneratorX86::SetupBlockedRegisters() const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100841 // Don't allocate the dalvik style register pair passing.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100842 blocked_register_pairs_[ECX_EDX] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100843
844 // 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
Calin Juravle34bacdf2014-10-07 20:23:36 +0100847 UpdateBlockedPairRegisters();
848}
849
850void CodeGeneratorX86::UpdateBlockedPairRegisters() const {
851 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
852 X86ManagedRegister current =
853 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
854 if (blocked_core_registers_[current.AsRegisterPairLow()]
855 || blocked_core_registers_[current.AsRegisterPairHigh()]) {
856 blocked_register_pairs_[i] = true;
857 }
858 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100859}
860
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100861InstructionCodeGeneratorX86::InstructionCodeGeneratorX86(HGraph* graph, CodeGeneratorX86* codegen)
Aart Bik42249c32016-01-07 15:33:50 -0800862 : InstructionCodeGenerator(graph, codegen),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100863 assembler_(codegen->GetAssembler()),
864 codegen_(codegen) {}
865
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100866static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +0100867 return dwarf::Reg::X86Core(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100868}
869
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000870void CodeGeneratorX86::GenerateFrameEntry() {
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100871 __ cfi().SetCurrentCFAOffset(kX86WordSize); // return address
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000872 __ Bind(&frame_entry_label_);
Roland Levillain199f3362014-11-27 17:15:16 +0000873 bool skip_overflow_check =
874 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000875 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Calin Juravle93edf732015-01-20 20:14:07 +0000876
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000877 if (!skip_overflow_check) {
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100878 __ testl(EAX, Address(ESP, -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86))));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100879 RecordPcInfo(nullptr, 0);
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100880 }
881
Mark Mendell5f874182015-03-04 15:42:45 -0500882 if (HasEmptyFrame()) {
883 return;
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000884 }
Mark Mendell5f874182015-03-04 15:42:45 -0500885
886 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
887 Register reg = kCoreCalleeSaves[i];
888 if (allocated_registers_.ContainsCoreRegister(reg)) {
889 __ pushl(reg);
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100890 __ cfi().AdjustCFAOffset(kX86WordSize);
891 __ cfi().RelOffset(DWARFReg(reg), 0);
Mark Mendell5f874182015-03-04 15:42:45 -0500892 }
893 }
894
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100895 int adjust = GetFrameSize() - FrameEntrySpillSize();
896 __ subl(ESP, Immediate(adjust));
897 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100898 __ movl(Address(ESP, kCurrentMethodStackOffset), kMethodRegisterArgument);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000899}
900
901void CodeGeneratorX86::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +0100902 __ cfi().RememberState();
903 if (!HasEmptyFrame()) {
904 int adjust = GetFrameSize() - FrameEntrySpillSize();
905 __ addl(ESP, Immediate(adjust));
906 __ cfi().AdjustCFAOffset(-adjust);
Mark Mendell5f874182015-03-04 15:42:45 -0500907
David Srbeckyc34dc932015-04-12 09:27:43 +0100908 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
909 Register reg = kCoreCalleeSaves[i];
910 if (allocated_registers_.ContainsCoreRegister(reg)) {
911 __ popl(reg);
912 __ cfi().AdjustCFAOffset(-static_cast<int>(kX86WordSize));
913 __ cfi().Restore(DWARFReg(reg));
914 }
Mark Mendell5f874182015-03-04 15:42:45 -0500915 }
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000916 }
David Srbeckyc34dc932015-04-12 09:27:43 +0100917 __ ret();
918 __ cfi().RestoreState();
919 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000920}
921
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100922void CodeGeneratorX86::Bind(HBasicBlock* block) {
923 __ Bind(GetLabelOf(block));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000924}
925
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100926Location InvokeDexCallingConventionVisitorX86::GetReturnLocation(Primitive::Type type) const {
927 switch (type) {
928 case Primitive::kPrimBoolean:
929 case Primitive::kPrimByte:
930 case Primitive::kPrimChar:
931 case Primitive::kPrimShort:
932 case Primitive::kPrimInt:
933 case Primitive::kPrimNot:
934 return Location::RegisterLocation(EAX);
935
936 case Primitive::kPrimLong:
937 return Location::RegisterPairLocation(EAX, EDX);
938
939 case Primitive::kPrimVoid:
940 return Location::NoLocation();
941
942 case Primitive::kPrimDouble:
943 case Primitive::kPrimFloat:
944 return Location::FpuRegisterLocation(XMM0);
945 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +0100946
947 UNREACHABLE();
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100948}
949
950Location InvokeDexCallingConventionVisitorX86::GetMethodLocation() const {
951 return Location::RegisterLocation(kMethodRegisterArgument);
952}
953
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100954Location InvokeDexCallingConventionVisitorX86::GetNextLocation(Primitive::Type type) {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100955 switch (type) {
956 case Primitive::kPrimBoolean:
957 case Primitive::kPrimByte:
958 case Primitive::kPrimChar:
959 case Primitive::kPrimShort:
960 case Primitive::kPrimInt:
961 case Primitive::kPrimNot: {
962 uint32_t index = gp_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000963 stack_index_++;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100964 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100965 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100966 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000967 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100968 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100969 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100970
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000971 case Primitive::kPrimLong: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100972 uint32_t index = gp_index_;
973 gp_index_ += 2;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000974 stack_index_ += 2;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100975 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100976 X86ManagedRegister pair = X86ManagedRegister::FromRegisterPair(
977 calling_convention.GetRegisterPairAt(index));
978 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100979 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000980 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
981 }
982 }
983
984 case Primitive::kPrimFloat: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100985 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000986 stack_index_++;
987 if (index < calling_convention.GetNumberOfFpuRegisters()) {
988 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
989 } else {
990 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
991 }
992 }
993
994 case Primitive::kPrimDouble: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100995 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000996 stack_index_ += 2;
997 if (index < calling_convention.GetNumberOfFpuRegisters()) {
998 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
999 } else {
1000 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001001 }
1002 }
1003
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001004 case Primitive::kPrimVoid:
1005 LOG(FATAL) << "Unexpected parameter type " << type;
1006 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001007 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00001008 return Location::NoLocation();
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001009}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001010
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001011void CodeGeneratorX86::Move32(Location destination, Location source) {
1012 if (source.Equals(destination)) {
1013 return;
1014 }
1015 if (destination.IsRegister()) {
1016 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001017 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001018 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001019 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001020 } else {
1021 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001022 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001023 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001024 } else if (destination.IsFpuRegister()) {
1025 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001026 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001027 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001028 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001029 } else {
1030 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001031 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001032 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001033 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001034 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001035 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001036 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001037 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001038 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Mark Mendell7c8d0092015-01-26 11:21:33 -05001039 } else if (source.IsConstant()) {
1040 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001041 int32_t value = GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05001042 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001043 } else {
1044 DCHECK(source.IsStackSlot());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001045 __ pushl(Address(ESP, source.GetStackIndex()));
1046 __ popl(Address(ESP, destination.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001047 }
1048 }
1049}
1050
1051void CodeGeneratorX86::Move64(Location destination, Location source) {
1052 if (source.Equals(destination)) {
1053 return;
1054 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001055 if (destination.IsRegisterPair()) {
1056 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001057 EmitParallelMoves(
1058 Location::RegisterLocation(source.AsRegisterPairHigh<Register>()),
1059 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001060 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001061 Location::RegisterLocation(source.AsRegisterPairLow<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001062 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()),
1063 Primitive::kPrimInt);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001064 } else if (source.IsFpuRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001065 XmmRegister src_reg = source.AsFpuRegister<XmmRegister>();
1066 __ movd(destination.AsRegisterPairLow<Register>(), src_reg);
1067 __ psrlq(src_reg, Immediate(32));
1068 __ movd(destination.AsRegisterPairHigh<Register>(), src_reg);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001069 } else {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001070 // No conflict possible, so just do the moves.
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001071 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001072 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
1073 __ movl(destination.AsRegisterPairHigh<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001074 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
1075 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001076 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05001077 if (source.IsFpuRegister()) {
1078 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
1079 } else if (source.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001080 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01001081 } else if (source.IsRegisterPair()) {
1082 size_t elem_size = Primitive::ComponentSize(Primitive::kPrimInt);
1083 // Create stack space for 2 elements.
1084 __ subl(ESP, Immediate(2 * elem_size));
1085 __ movl(Address(ESP, 0), source.AsRegisterPairLow<Register>());
1086 __ movl(Address(ESP, elem_size), source.AsRegisterPairHigh<Register>());
1087 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
1088 // And remove the temporary stack space we allocated.
1089 __ addl(ESP, Immediate(2 * elem_size));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001090 } else {
1091 LOG(FATAL) << "Unimplemented";
1092 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001093 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001094 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001095 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001096 // No conflict possible, so just do the moves.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001097 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegisterPairLow<Register>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001098 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001099 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001100 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001101 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001102 } else if (source.IsConstant()) {
1103 HConstant* constant = source.GetConstant();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001104 DCHECK(constant->IsLongConstant() || constant->IsDoubleConstant());
1105 int64_t value = GetInt64ValueOf(constant);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001106 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(Low32Bits(value)));
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001107 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
1108 Immediate(High32Bits(value)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001109 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001110 DCHECK(source.IsDoubleStackSlot()) << source;
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001111 EmitParallelMoves(
1112 Location::StackSlot(source.GetStackIndex()),
1113 Location::StackSlot(destination.GetStackIndex()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001114 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001115 Location::StackSlot(source.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001116 Location::StackSlot(destination.GetHighStackIndex(kX86WordSize)),
1117 Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001118 }
1119 }
1120}
1121
Calin Juravle175dc732015-08-25 15:42:32 +01001122void CodeGeneratorX86::MoveConstant(Location location, int32_t value) {
1123 DCHECK(location.IsRegister());
1124 __ movl(location.AsRegister<Register>(), Immediate(value));
1125}
1126
Calin Juravlee460d1d2015-09-29 04:52:17 +01001127void CodeGeneratorX86::MoveLocation(Location dst, Location src, Primitive::Type dst_type) {
David Brazdil74eb1b22015-12-14 11:44:01 +00001128 HParallelMove move(GetGraph()->GetArena());
1129 if (dst_type == Primitive::kPrimLong && !src.IsConstant() && !src.IsFpuRegister()) {
1130 move.AddMove(src.ToLow(), dst.ToLow(), Primitive::kPrimInt, nullptr);
1131 move.AddMove(src.ToHigh(), dst.ToHigh(), Primitive::kPrimInt, nullptr);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001132 } else {
David Brazdil74eb1b22015-12-14 11:44:01 +00001133 move.AddMove(src, dst, dst_type, nullptr);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001134 }
David Brazdil74eb1b22015-12-14 11:44:01 +00001135 GetMoveResolver()->EmitNativeCode(&move);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001136}
1137
1138void CodeGeneratorX86::AddLocationAsTemp(Location location, LocationSummary* locations) {
1139 if (location.IsRegister()) {
1140 locations->AddTemp(location);
1141 } else if (location.IsRegisterPair()) {
1142 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
1143 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
1144 } else {
1145 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1146 }
1147}
1148
David Brazdilfc6a86a2015-06-26 10:33:45 +00001149void InstructionCodeGeneratorX86::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001150 DCHECK(!successor->IsExitBlock());
1151
1152 HBasicBlock* block = got->GetBlock();
1153 HInstruction* previous = got->GetPrevious();
1154
1155 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +00001156 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001157 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1158 return;
1159 }
1160
1161 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1162 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1163 }
1164 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001165 __ jmp(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001166 }
1167}
1168
David Brazdilfc6a86a2015-06-26 10:33:45 +00001169void LocationsBuilderX86::VisitGoto(HGoto* got) {
1170 got->SetLocations(nullptr);
1171}
1172
1173void InstructionCodeGeneratorX86::VisitGoto(HGoto* got) {
1174 HandleGoto(got, got->GetSuccessor());
1175}
1176
1177void LocationsBuilderX86::VisitTryBoundary(HTryBoundary* try_boundary) {
1178 try_boundary->SetLocations(nullptr);
1179}
1180
1181void InstructionCodeGeneratorX86::VisitTryBoundary(HTryBoundary* try_boundary) {
1182 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
1183 if (!successor->IsExitBlock()) {
1184 HandleGoto(try_boundary, successor);
1185 }
1186}
1187
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001188void LocationsBuilderX86::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001189 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001190}
1191
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001192void InstructionCodeGeneratorX86::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001193}
1194
Mark Mendell152408f2015-12-31 12:28:50 -05001195template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001196void InstructionCodeGeneratorX86::GenerateFPJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001197 LabelType* true_label,
1198 LabelType* false_label) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001199 if (cond->IsFPConditionTrueIfNaN()) {
1200 __ j(kUnordered, true_label);
1201 } else if (cond->IsFPConditionFalseIfNaN()) {
1202 __ j(kUnordered, false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001203 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001204 __ j(X86UnsignedOrFPCondition(cond->GetCondition()), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001205}
1206
Mark Mendell152408f2015-12-31 12:28:50 -05001207template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001208void InstructionCodeGeneratorX86::GenerateLongComparesAndJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001209 LabelType* true_label,
1210 LabelType* false_label) {
Mark Mendellc4701932015-04-10 13:18:51 -04001211 LocationSummary* locations = cond->GetLocations();
1212 Location left = locations->InAt(0);
1213 Location right = locations->InAt(1);
1214 IfCondition if_cond = cond->GetCondition();
1215
Mark Mendellc4701932015-04-10 13:18:51 -04001216 Register left_high = left.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001217 Register left_low = left.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001218 IfCondition true_high_cond = if_cond;
1219 IfCondition false_high_cond = cond->GetOppositeCondition();
Aart Bike9f37602015-10-09 11:15:55 -07001220 Condition final_condition = X86UnsignedOrFPCondition(if_cond); // unsigned on lower part
Mark Mendellc4701932015-04-10 13:18:51 -04001221
1222 // Set the conditions for the test, remembering that == needs to be
1223 // decided using the low words.
1224 switch (if_cond) {
1225 case kCondEQ:
Mark Mendellc4701932015-04-10 13:18:51 -04001226 case kCondNE:
Roland Levillain4fa13f62015-07-06 18:11:54 +01001227 // Nothing to do.
Mark Mendellc4701932015-04-10 13:18:51 -04001228 break;
1229 case kCondLT:
1230 false_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -04001231 break;
1232 case kCondLE:
1233 true_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -04001234 break;
1235 case kCondGT:
1236 false_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -04001237 break;
1238 case kCondGE:
1239 true_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -04001240 break;
Aart Bike9f37602015-10-09 11:15:55 -07001241 case kCondB:
1242 false_high_cond = kCondA;
1243 break;
1244 case kCondBE:
1245 true_high_cond = kCondB;
1246 break;
1247 case kCondA:
1248 false_high_cond = kCondB;
1249 break;
1250 case kCondAE:
1251 true_high_cond = kCondA;
1252 break;
Mark Mendellc4701932015-04-10 13:18:51 -04001253 }
1254
1255 if (right.IsConstant()) {
1256 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
Mark Mendellc4701932015-04-10 13:18:51 -04001257 int32_t val_high = High32Bits(value);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001258 int32_t val_low = Low32Bits(value);
Mark Mendellc4701932015-04-10 13:18:51 -04001259
Aart Bika19616e2016-02-01 18:57:58 -08001260 codegen_->Compare32BitValue(left_high, val_high);
Mark Mendellc4701932015-04-10 13:18:51 -04001261 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001262 __ j(X86Condition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001263 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001264 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001265 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001266 __ j(X86Condition(true_high_cond), true_label);
1267 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001268 }
1269 // Must be equal high, so compare the lows.
Aart Bika19616e2016-02-01 18:57:58 -08001270 codegen_->Compare32BitValue(left_low, val_low);
Mark Mendell8659e842016-02-16 10:41:46 -05001271 } else if (right.IsRegisterPair()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001272 Register right_high = right.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001273 Register right_low = right.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001274
1275 __ cmpl(left_high, right_high);
1276 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001277 __ j(X86Condition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001278 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001279 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001280 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001281 __ j(X86Condition(true_high_cond), true_label);
1282 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001283 }
1284 // Must be equal high, so compare the lows.
1285 __ cmpl(left_low, right_low);
Mark Mendell8659e842016-02-16 10:41:46 -05001286 } else {
1287 DCHECK(right.IsDoubleStackSlot());
1288 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
1289 if (if_cond == kCondNE) {
1290 __ j(X86Condition(true_high_cond), true_label);
1291 } else if (if_cond == kCondEQ) {
1292 __ j(X86Condition(false_high_cond), false_label);
1293 } else {
1294 __ j(X86Condition(true_high_cond), true_label);
1295 __ j(X86Condition(false_high_cond), false_label);
1296 }
1297 // Must be equal high, so compare the lows.
1298 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Mark Mendellc4701932015-04-10 13:18:51 -04001299 }
1300 // The last comparison might be unsigned.
1301 __ j(final_condition, true_label);
1302}
1303
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001304void InstructionCodeGeneratorX86::GenerateFPCompare(Location lhs,
1305 Location rhs,
1306 HInstruction* insn,
1307 bool is_double) {
1308 HX86LoadFromConstantTable* const_area = insn->InputAt(1)->AsX86LoadFromConstantTable();
1309 if (is_double) {
1310 if (rhs.IsFpuRegister()) {
1311 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1312 } else if (const_area != nullptr) {
1313 DCHECK(const_area->IsEmittedAtUseSite());
1314 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(),
1315 codegen_->LiteralDoubleAddress(
1316 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
1317 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
1318 } else {
1319 DCHECK(rhs.IsDoubleStackSlot());
1320 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(), Address(ESP, rhs.GetStackIndex()));
1321 }
1322 } else {
1323 if (rhs.IsFpuRegister()) {
1324 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1325 } else if (const_area != nullptr) {
1326 DCHECK(const_area->IsEmittedAtUseSite());
1327 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(),
1328 codegen_->LiteralFloatAddress(
1329 const_area->GetConstant()->AsFloatConstant()->GetValue(),
1330 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
1331 } else {
1332 DCHECK(rhs.IsStackSlot());
1333 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(), Address(ESP, rhs.GetStackIndex()));
1334 }
1335 }
1336}
1337
Mark Mendell152408f2015-12-31 12:28:50 -05001338template<class LabelType>
David Brazdil0debae72015-11-12 18:37:00 +00001339void InstructionCodeGeneratorX86::GenerateCompareTestAndBranch(HCondition* condition,
Mark Mendell152408f2015-12-31 12:28:50 -05001340 LabelType* true_target_in,
1341 LabelType* false_target_in) {
David Brazdil0debae72015-11-12 18:37:00 +00001342 // Generated branching requires both targets to be explicit. If either of the
1343 // targets is nullptr (fallthrough) use and bind `fallthrough_target` instead.
Mark Mendell152408f2015-12-31 12:28:50 -05001344 LabelType fallthrough_target;
1345 LabelType* true_target = true_target_in == nullptr ? &fallthrough_target : true_target_in;
1346 LabelType* false_target = false_target_in == nullptr ? &fallthrough_target : false_target_in;
David Brazdil0debae72015-11-12 18:37:00 +00001347
Mark Mendellc4701932015-04-10 13:18:51 -04001348 LocationSummary* locations = condition->GetLocations();
1349 Location left = locations->InAt(0);
1350 Location right = locations->InAt(1);
1351
Mark Mendellc4701932015-04-10 13:18:51 -04001352 Primitive::Type type = condition->InputAt(0)->GetType();
1353 switch (type) {
1354 case Primitive::kPrimLong:
1355 GenerateLongComparesAndJumps(condition, true_target, false_target);
1356 break;
1357 case Primitive::kPrimFloat:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001358 GenerateFPCompare(left, right, condition, false);
Mark Mendellc4701932015-04-10 13:18:51 -04001359 GenerateFPJumps(condition, true_target, false_target);
1360 break;
1361 case Primitive::kPrimDouble:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001362 GenerateFPCompare(left, right, condition, true);
Mark Mendellc4701932015-04-10 13:18:51 -04001363 GenerateFPJumps(condition, true_target, false_target);
1364 break;
1365 default:
1366 LOG(FATAL) << "Unexpected compare type " << type;
1367 }
1368
David Brazdil0debae72015-11-12 18:37:00 +00001369 if (false_target != &fallthrough_target) {
Mark Mendellc4701932015-04-10 13:18:51 -04001370 __ jmp(false_target);
1371 }
David Brazdil0debae72015-11-12 18:37:00 +00001372
1373 if (fallthrough_target.IsLinked()) {
1374 __ Bind(&fallthrough_target);
1375 }
Mark Mendellc4701932015-04-10 13:18:51 -04001376}
1377
David Brazdil0debae72015-11-12 18:37:00 +00001378static bool AreEflagsSetFrom(HInstruction* cond, HInstruction* branch) {
1379 // Moves may affect the eflags register (move zero uses xorl), so the EFLAGS
1380 // are set only strictly before `branch`. We can't use the eflags on long/FP
1381 // conditions if they are materialized due to the complex branching.
1382 return cond->IsCondition() &&
1383 cond->GetNext() == branch &&
1384 cond->InputAt(0)->GetType() != Primitive::kPrimLong &&
1385 !Primitive::IsFloatingPointType(cond->InputAt(0)->GetType());
1386}
1387
Mark Mendell152408f2015-12-31 12:28:50 -05001388template<class LabelType>
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001389void InstructionCodeGeneratorX86::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00001390 size_t condition_input_index,
Mark Mendell152408f2015-12-31 12:28:50 -05001391 LabelType* true_target,
1392 LabelType* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00001393 HInstruction* cond = instruction->InputAt(condition_input_index);
1394
1395 if (true_target == nullptr && false_target == nullptr) {
1396 // Nothing to do. The code always falls through.
1397 return;
1398 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00001399 // Constant condition, statically compared against "true" (integer value 1).
1400 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00001401 if (true_target != nullptr) {
1402 __ jmp(true_target);
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001403 }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001404 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00001405 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00001406 if (false_target != nullptr) {
1407 __ jmp(false_target);
1408 }
1409 }
1410 return;
1411 }
1412
1413 // The following code generates these patterns:
1414 // (1) true_target == nullptr && false_target != nullptr
1415 // - opposite condition true => branch to false_target
1416 // (2) true_target != nullptr && false_target == nullptr
1417 // - condition true => branch to true_target
1418 // (3) true_target != nullptr && false_target != nullptr
1419 // - condition true => branch to true_target
1420 // - branch to false_target
1421 if (IsBooleanValueOrMaterializedCondition(cond)) {
1422 if (AreEflagsSetFrom(cond, instruction)) {
1423 if (true_target == nullptr) {
1424 __ j(X86Condition(cond->AsCondition()->GetOppositeCondition()), false_target);
1425 } else {
1426 __ j(X86Condition(cond->AsCondition()->GetCondition()), true_target);
1427 }
1428 } else {
1429 // Materialized condition, compare against 0.
1430 Location lhs = instruction->GetLocations()->InAt(condition_input_index);
1431 if (lhs.IsRegister()) {
1432 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
1433 } else {
1434 __ cmpl(Address(ESP, lhs.GetStackIndex()), Immediate(0));
1435 }
1436 if (true_target == nullptr) {
1437 __ j(kEqual, false_target);
1438 } else {
1439 __ j(kNotEqual, true_target);
1440 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001441 }
1442 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001443 // Condition has not been materialized, use its inputs as the comparison and
1444 // its condition as the branch condition.
Mark Mendellb8b97692015-05-22 16:58:19 -04001445 HCondition* condition = cond->AsCondition();
David Brazdil0debae72015-11-12 18:37:00 +00001446
1447 // If this is a long or FP comparison that has been folded into
1448 // the HCondition, generate the comparison directly.
1449 Primitive::Type type = condition->InputAt(0)->GetType();
1450 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
1451 GenerateCompareTestAndBranch(condition, true_target, false_target);
1452 return;
1453 }
1454
1455 Location lhs = condition->GetLocations()->InAt(0);
1456 Location rhs = condition->GetLocations()->InAt(1);
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001457 // LHS is guaranteed to be in a register (see LocationsBuilderX86::HandleCondition).
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001458 codegen_->GenerateIntCompare(lhs, rhs);
David Brazdil0debae72015-11-12 18:37:00 +00001459 if (true_target == nullptr) {
1460 __ j(X86Condition(condition->GetOppositeCondition()), false_target);
1461 } else {
Mark Mendellb8b97692015-05-22 16:58:19 -04001462 __ j(X86Condition(condition->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -07001463 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001464 }
David Brazdil0debae72015-11-12 18:37:00 +00001465
1466 // If neither branch falls through (case 3), the conditional branch to `true_target`
1467 // was already emitted (case 2) and we need to emit a jump to `false_target`.
1468 if (true_target != nullptr && false_target != nullptr) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001469 __ jmp(false_target);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001470 }
1471}
1472
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001473void LocationsBuilderX86::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001474 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
1475 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001476 locations->SetInAt(0, Location::Any());
1477 }
1478}
1479
1480void InstructionCodeGeneratorX86::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001481 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
1482 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
1483 Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
1484 nullptr : codegen_->GetLabelOf(true_successor);
1485 Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
1486 nullptr : codegen_->GetLabelOf(false_successor);
1487 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001488}
1489
1490void LocationsBuilderX86::VisitDeoptimize(HDeoptimize* deoptimize) {
1491 LocationSummary* locations = new (GetGraph()->GetArena())
1492 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01001493 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
David Brazdil0debae72015-11-12 18:37:00 +00001494 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001495 locations->SetInAt(0, Location::Any());
1496 }
1497}
1498
1499void InstructionCodeGeneratorX86::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08001500 SlowPathCode* slow_path = deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathX86>(deoptimize);
David Brazdil74eb1b22015-12-14 11:44:01 +00001501 GenerateTestAndBranch<Label>(deoptimize,
1502 /* condition_input_index */ 0,
1503 slow_path->GetEntryLabel(),
1504 /* false_target */ nullptr);
1505}
1506
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001507static bool SelectCanUseCMOV(HSelect* select) {
1508 // There are no conditional move instructions for XMMs.
1509 if (Primitive::IsFloatingPointType(select->GetType())) {
1510 return false;
1511 }
1512
1513 // A FP condition doesn't generate the single CC that we need.
1514 // In 32 bit mode, a long condition doesn't generate a single CC either.
1515 HInstruction* condition = select->GetCondition();
1516 if (condition->IsCondition()) {
1517 Primitive::Type compare_type = condition->InputAt(0)->GetType();
1518 if (compare_type == Primitive::kPrimLong ||
1519 Primitive::IsFloatingPointType(compare_type)) {
1520 return false;
1521 }
1522 }
1523
1524 // We can generate a CMOV for this Select.
1525 return true;
1526}
1527
David Brazdil74eb1b22015-12-14 11:44:01 +00001528void LocationsBuilderX86::VisitSelect(HSelect* select) {
1529 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001530 if (Primitive::IsFloatingPointType(select->GetType())) {
David Brazdil74eb1b22015-12-14 11:44:01 +00001531 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001532 locations->SetInAt(1, Location::Any());
David Brazdil74eb1b22015-12-14 11:44:01 +00001533 } else {
1534 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001535 if (SelectCanUseCMOV(select)) {
1536 if (select->InputAt(1)->IsConstant()) {
1537 // Cmov can't handle a constant value.
1538 locations->SetInAt(1, Location::RequiresRegister());
1539 } else {
1540 locations->SetInAt(1, Location::Any());
1541 }
1542 } else {
1543 locations->SetInAt(1, Location::Any());
1544 }
David Brazdil74eb1b22015-12-14 11:44:01 +00001545 }
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001546 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
1547 locations->SetInAt(2, Location::RequiresRegister());
David Brazdil74eb1b22015-12-14 11:44:01 +00001548 }
1549 locations->SetOut(Location::SameAsFirstInput());
1550}
1551
1552void InstructionCodeGeneratorX86::VisitSelect(HSelect* select) {
1553 LocationSummary* locations = select->GetLocations();
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001554 DCHECK(locations->InAt(0).Equals(locations->Out()));
1555 if (SelectCanUseCMOV(select)) {
1556 // If both the condition and the source types are integer, we can generate
1557 // a CMOV to implement Select.
1558
1559 HInstruction* select_condition = select->GetCondition();
1560 Condition cond = kNotEqual;
1561
1562 // Figure out how to test the 'condition'.
1563 if (select_condition->IsCondition()) {
1564 HCondition* condition = select_condition->AsCondition();
1565 if (!condition->IsEmittedAtUseSite()) {
1566 // This was a previously materialized condition.
1567 // Can we use the existing condition code?
1568 if (AreEflagsSetFrom(condition, select)) {
1569 // Materialization was the previous instruction. Condition codes are right.
1570 cond = X86Condition(condition->GetCondition());
1571 } else {
1572 // No, we have to recreate the condition code.
1573 Register cond_reg = locations->InAt(2).AsRegister<Register>();
1574 __ testl(cond_reg, cond_reg);
1575 }
1576 } else {
1577 // We can't handle FP or long here.
1578 DCHECK_NE(condition->InputAt(0)->GetType(), Primitive::kPrimLong);
1579 DCHECK(!Primitive::IsFloatingPointType(condition->InputAt(0)->GetType()));
1580 LocationSummary* cond_locations = condition->GetLocations();
Roland Levillain0b671c02016-08-19 12:02:34 +01001581 codegen_->GenerateIntCompare(cond_locations->InAt(0), cond_locations->InAt(1));
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001582 cond = X86Condition(condition->GetCondition());
1583 }
1584 } else {
1585 // Must be a boolean condition, which needs to be compared to 0.
1586 Register cond_reg = locations->InAt(2).AsRegister<Register>();
1587 __ testl(cond_reg, cond_reg);
1588 }
1589
1590 // If the condition is true, overwrite the output, which already contains false.
1591 Location false_loc = locations->InAt(0);
1592 Location true_loc = locations->InAt(1);
1593 if (select->GetType() == Primitive::kPrimLong) {
1594 // 64 bit conditional move.
1595 Register false_high = false_loc.AsRegisterPairHigh<Register>();
1596 Register false_low = false_loc.AsRegisterPairLow<Register>();
1597 if (true_loc.IsRegisterPair()) {
1598 __ cmovl(cond, false_high, true_loc.AsRegisterPairHigh<Register>());
1599 __ cmovl(cond, false_low, true_loc.AsRegisterPairLow<Register>());
1600 } else {
1601 __ cmovl(cond, false_high, Address(ESP, true_loc.GetHighStackIndex(kX86WordSize)));
1602 __ cmovl(cond, false_low, Address(ESP, true_loc.GetStackIndex()));
1603 }
1604 } else {
1605 // 32 bit conditional move.
1606 Register false_reg = false_loc.AsRegister<Register>();
1607 if (true_loc.IsRegister()) {
1608 __ cmovl(cond, false_reg, true_loc.AsRegister<Register>());
1609 } else {
1610 __ cmovl(cond, false_reg, Address(ESP, true_loc.GetStackIndex()));
1611 }
1612 }
1613 } else {
1614 NearLabel false_target;
1615 GenerateTestAndBranch<NearLabel>(
1616 select, /* condition_input_index */ 2, /* true_target */ nullptr, &false_target);
1617 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
1618 __ Bind(&false_target);
1619 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001620}
1621
David Srbecky0cf44932015-12-09 14:09:59 +00001622void LocationsBuilderX86::VisitNativeDebugInfo(HNativeDebugInfo* info) {
1623 new (GetGraph()->GetArena()) LocationSummary(info);
1624}
1625
David Srbeckyd28f4a02016-03-14 17:14:24 +00001626void InstructionCodeGeneratorX86::VisitNativeDebugInfo(HNativeDebugInfo*) {
1627 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00001628}
1629
1630void CodeGeneratorX86::GenerateNop() {
1631 __ nop();
David Srbecky0cf44932015-12-09 14:09:59 +00001632}
1633
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001634void LocationsBuilderX86::HandleCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001635 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +01001636 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Mark Mendellc4701932015-04-10 13:18:51 -04001637 // Handle the long/FP comparisons made in instruction simplification.
1638 switch (cond->InputAt(0)->GetType()) {
1639 case Primitive::kPrimLong: {
1640 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell8659e842016-02-16 10:41:46 -05001641 locations->SetInAt(1, Location::Any());
David Brazdilb3e773e2016-01-26 11:28:37 +00001642 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001643 locations->SetOut(Location::RequiresRegister());
1644 }
1645 break;
1646 }
1647 case Primitive::kPrimFloat:
1648 case Primitive::kPrimDouble: {
1649 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001650 if (cond->InputAt(1)->IsX86LoadFromConstantTable()) {
1651 DCHECK(cond->InputAt(1)->IsEmittedAtUseSite());
1652 } else if (cond->InputAt(1)->IsConstant()) {
1653 locations->SetInAt(1, Location::RequiresFpuRegister());
1654 } else {
1655 locations->SetInAt(1, Location::Any());
1656 }
David Brazdilb3e773e2016-01-26 11:28:37 +00001657 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001658 locations->SetOut(Location::RequiresRegister());
1659 }
1660 break;
1661 }
1662 default:
1663 locations->SetInAt(0, Location::RequiresRegister());
1664 locations->SetInAt(1, Location::Any());
David Brazdilb3e773e2016-01-26 11:28:37 +00001665 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001666 // We need a byte register.
1667 locations->SetOut(Location::RegisterLocation(ECX));
1668 }
1669 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001670 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001671}
1672
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001673void InstructionCodeGeneratorX86::HandleCondition(HCondition* cond) {
David Brazdilb3e773e2016-01-26 11:28:37 +00001674 if (cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001675 return;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001676 }
Mark Mendellc4701932015-04-10 13:18:51 -04001677
1678 LocationSummary* locations = cond->GetLocations();
1679 Location lhs = locations->InAt(0);
1680 Location rhs = locations->InAt(1);
1681 Register reg = locations->Out().AsRegister<Register>();
Mark Mendell152408f2015-12-31 12:28:50 -05001682 NearLabel true_label, false_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001683
1684 switch (cond->InputAt(0)->GetType()) {
1685 default: {
1686 // Integer case.
1687
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01001688 // Clear output register: setb only sets the low byte.
Mark Mendellc4701932015-04-10 13:18:51 -04001689 __ xorl(reg, reg);
Roland Levillain0b671c02016-08-19 12:02:34 +01001690 codegen_->GenerateIntCompare(lhs, rhs);
Aart Bike9f37602015-10-09 11:15:55 -07001691 __ setb(X86Condition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001692 return;
1693 }
1694 case Primitive::kPrimLong:
1695 GenerateLongComparesAndJumps(cond, &true_label, &false_label);
1696 break;
1697 case Primitive::kPrimFloat:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001698 GenerateFPCompare(lhs, rhs, cond, false);
Mark Mendellc4701932015-04-10 13:18:51 -04001699 GenerateFPJumps(cond, &true_label, &false_label);
1700 break;
1701 case Primitive::kPrimDouble:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001702 GenerateFPCompare(lhs, rhs, cond, true);
Mark Mendellc4701932015-04-10 13:18:51 -04001703 GenerateFPJumps(cond, &true_label, &false_label);
1704 break;
1705 }
1706
1707 // Convert the jumps into the result.
Mark Mendell0c9497d2015-08-21 09:30:05 -04001708 NearLabel done_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001709
Roland Levillain4fa13f62015-07-06 18:11:54 +01001710 // False case: result = 0.
Mark Mendellc4701932015-04-10 13:18:51 -04001711 __ Bind(&false_label);
1712 __ xorl(reg, reg);
1713 __ jmp(&done_label);
1714
Roland Levillain4fa13f62015-07-06 18:11:54 +01001715 // True case: result = 1.
Mark Mendellc4701932015-04-10 13:18:51 -04001716 __ Bind(&true_label);
1717 __ movl(reg, Immediate(1));
1718 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07001719}
1720
1721void LocationsBuilderX86::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001722 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001723}
1724
1725void InstructionCodeGeneratorX86::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001726 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001727}
1728
1729void LocationsBuilderX86::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001730 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001731}
1732
1733void InstructionCodeGeneratorX86::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001734 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001735}
1736
1737void LocationsBuilderX86::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001738 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001739}
1740
1741void InstructionCodeGeneratorX86::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001742 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001743}
1744
1745void LocationsBuilderX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001746 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001747}
1748
1749void InstructionCodeGeneratorX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001750 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001751}
1752
1753void LocationsBuilderX86::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001754 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001755}
1756
1757void InstructionCodeGeneratorX86::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001758 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001759}
1760
1761void LocationsBuilderX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001762 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001763}
1764
1765void InstructionCodeGeneratorX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001766 HandleCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001767}
1768
Aart Bike9f37602015-10-09 11:15:55 -07001769void LocationsBuilderX86::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001770 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001771}
1772
1773void InstructionCodeGeneratorX86::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001774 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001775}
1776
1777void LocationsBuilderX86::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001778 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001779}
1780
1781void InstructionCodeGeneratorX86::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001782 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001783}
1784
1785void LocationsBuilderX86::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001786 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001787}
1788
1789void InstructionCodeGeneratorX86::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001790 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001791}
1792
1793void LocationsBuilderX86::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001794 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001795}
1796
1797void InstructionCodeGeneratorX86::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001798 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001799}
1800
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001801void LocationsBuilderX86::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001802 LocationSummary* locations =
1803 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001804 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001805}
1806
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001807void InstructionCodeGeneratorX86::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001808 // Will be generated at use site.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001809}
1810
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001811void LocationsBuilderX86::VisitNullConstant(HNullConstant* constant) {
1812 LocationSummary* locations =
1813 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1814 locations->SetOut(Location::ConstantLocation(constant));
1815}
1816
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001817void InstructionCodeGeneratorX86::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001818 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001819}
1820
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001821void LocationsBuilderX86::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001822 LocationSummary* locations =
1823 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001824 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001825}
1826
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001827void InstructionCodeGeneratorX86::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001828 // Will be generated at use site.
1829}
1830
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001831void LocationsBuilderX86::VisitFloatConstant(HFloatConstant* constant) {
1832 LocationSummary* locations =
1833 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1834 locations->SetOut(Location::ConstantLocation(constant));
1835}
1836
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001837void InstructionCodeGeneratorX86::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001838 // Will be generated at use site.
1839}
1840
1841void LocationsBuilderX86::VisitDoubleConstant(HDoubleConstant* constant) {
1842 LocationSummary* locations =
1843 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1844 locations->SetOut(Location::ConstantLocation(constant));
1845}
1846
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001847void InstructionCodeGeneratorX86::VisitDoubleConstant(HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001848 // Will be generated at use site.
1849}
1850
Calin Juravle27df7582015-04-17 19:12:31 +01001851void LocationsBuilderX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1852 memory_barrier->SetLocations(nullptr);
1853}
1854
1855void InstructionCodeGeneratorX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00001856 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01001857}
1858
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001859void LocationsBuilderX86::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001860 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001861}
1862
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001863void InstructionCodeGeneratorX86::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001864 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001865}
1866
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001867void LocationsBuilderX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001868 LocationSummary* locations =
1869 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001870 switch (ret->InputAt(0)->GetType()) {
1871 case Primitive::kPrimBoolean:
1872 case Primitive::kPrimByte:
1873 case Primitive::kPrimChar:
1874 case Primitive::kPrimShort:
1875 case Primitive::kPrimInt:
1876 case Primitive::kPrimNot:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001877 locations->SetInAt(0, Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001878 break;
1879
1880 case Primitive::kPrimLong:
1881 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001882 0, Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001883 break;
1884
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001885 case Primitive::kPrimFloat:
1886 case Primitive::kPrimDouble:
1887 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001888 0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001889 break;
1890
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001891 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001892 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001893 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001894}
1895
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001896void InstructionCodeGeneratorX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001897 if (kIsDebugBuild) {
1898 switch (ret->InputAt(0)->GetType()) {
1899 case Primitive::kPrimBoolean:
1900 case Primitive::kPrimByte:
1901 case Primitive::kPrimChar:
1902 case Primitive::kPrimShort:
1903 case Primitive::kPrimInt:
1904 case Primitive::kPrimNot:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001905 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<Register>(), EAX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001906 break;
1907
1908 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001909 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairLow<Register>(), EAX);
1910 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairHigh<Register>(), EDX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001911 break;
1912
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001913 case Primitive::kPrimFloat:
1914 case Primitive::kPrimDouble:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001915 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>(), XMM0);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001916 break;
1917
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001918 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001919 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001920 }
1921 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001922 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001923}
1924
Calin Juravle175dc732015-08-25 15:42:32 +01001925void LocationsBuilderX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
1926 // The trampoline uses the same calling convention as dex calling conventions,
1927 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
1928 // the method_idx.
1929 HandleInvoke(invoke);
1930}
1931
1932void InstructionCodeGeneratorX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
1933 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
1934}
1935
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001936void LocationsBuilderX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00001937 // Explicit clinit checks triggered by static invokes must have been pruned by
1938 // art::PrepareForRegisterAllocation.
1939 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001940
Mark Mendellfb8d2792015-03-31 22:16:59 -04001941 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001942 if (intrinsic.TryDispatch(invoke)) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00001943 if (invoke->GetLocations()->CanCall() && invoke->HasPcRelativeDexCache()) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00001944 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00001945 }
Mark Mendell09ed1a32015-03-25 08:30:06 -04001946 return;
1947 }
1948
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001949 HandleInvoke(invoke);
Nicolas Geoffray94015b92015-06-04 18:21:04 +01001950
Vladimir Marko0f7dca42015-11-02 14:36:43 +00001951 // For PC-relative dex cache the invoke has an extra input, the PC-relative address base.
1952 if (invoke->HasPcRelativeDexCache()) {
Vladimir Markob4536b72015-11-24 13:45:23 +00001953 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00001954 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001955}
1956
Mark Mendell09ed1a32015-03-25 08:30:06 -04001957static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86* codegen) {
1958 if (invoke->GetLocations()->Intrinsified()) {
1959 IntrinsicCodeGeneratorX86 intrinsic(codegen);
1960 intrinsic.Dispatch(invoke);
1961 return true;
1962 }
1963 return false;
1964}
1965
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001966void InstructionCodeGeneratorX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00001967 // Explicit clinit checks triggered by static invokes must have been pruned by
1968 // art::PrepareForRegisterAllocation.
1969 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001970
Mark Mendell09ed1a32015-03-25 08:30:06 -04001971 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1972 return;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001973 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001974
Nicolas Geoffray94015b92015-06-04 18:21:04 +01001975 LocationSummary* locations = invoke->GetLocations();
Mark Mendell09ed1a32015-03-25 08:30:06 -04001976 codegen_->GenerateStaticOrDirectCall(
Nicolas Geoffray94015b92015-06-04 18:21:04 +01001977 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Mingyao Yang8693fe12015-04-17 16:51:08 -07001978 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001979}
1980
1981void LocationsBuilderX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00001982 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
1983 if (intrinsic.TryDispatch(invoke)) {
1984 return;
1985 }
1986
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001987 HandleInvoke(invoke);
1988}
1989
1990void LocationsBuilderX86::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001991 InvokeDexCallingConventionVisitorX86 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001992 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001993}
1994
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001995void InstructionCodeGeneratorX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampebfb5ba92015-09-01 15:45:02 +00001996 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1997 return;
1998 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001999
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002000 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002001 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01002002 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002003}
2004
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002005void LocationsBuilderX86::VisitInvokeInterface(HInvokeInterface* invoke) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002006 // This call to HandleInvoke allocates a temporary (core) register
2007 // which is also used to transfer the hidden argument from FP to
2008 // core register.
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002009 HandleInvoke(invoke);
2010 // Add the hidden argument.
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002011 invoke->GetLocations()->AddTemp(Location::FpuRegisterLocation(XMM7));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002012}
2013
2014void InstructionCodeGeneratorX86::VisitInvokeInterface(HInvokeInterface* invoke) {
2015 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain0d5a2812015-11-13 10:07:31 +00002016 LocationSummary* locations = invoke->GetLocations();
2017 Register temp = locations->GetTemp(0).AsRegister<Register>();
2018 XmmRegister hidden_reg = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002019 Location receiver = locations->InAt(0);
2020 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2021
Roland Levillain0d5a2812015-11-13 10:07:31 +00002022 // Set the hidden argument. This is safe to do this here, as XMM7
2023 // won't be modified thereafter, before the `call` instruction.
2024 DCHECK_EQ(XMM7, hidden_reg);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002025 __ movl(temp, Immediate(invoke->GetDexMethodIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002026 __ movd(hidden_reg, temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002027
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002028 if (receiver.IsStackSlot()) {
2029 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002030 // /* HeapReference<Class> */ temp = temp->klass_
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002031 __ movl(temp, Address(temp, class_offset));
2032 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002033 // /* HeapReference<Class> */ temp = receiver->klass_
Roland Levillain271ab9c2014-11-27 15:23:57 +00002034 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002035 }
Roland Levillain4d027112015-07-01 15:41:14 +01002036 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00002037 // Instead of simply (possibly) unpoisoning `temp` here, we should
2038 // emit a read barrier for the previous class reference load.
2039 // However this is not required in practice, as this is an
2040 // intermediate/temporary reference and because the current
2041 // concurrent copying collector keeps the from-space memory
2042 // intact/accessible until the end of the marking phase (the
2043 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01002044 __ MaybeUnpoisonHeapReference(temp);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002045 // temp = temp->GetAddressOfIMT()
2046 __ movl(temp,
2047 Address(temp, mirror::Class::ImtPtrOffset(kX86PointerSize).Uint32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002048 // temp = temp->GetImtEntryAt(method_offset);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002049 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00002050 invoke->GetImtIndex(), kX86PointerSize));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002051 __ movl(temp, Address(temp, method_offset));
2052 // call temp->GetEntryPoint();
Roland Levillain0d5a2812015-11-13 10:07:31 +00002053 __ call(Address(temp,
Andreas Gampe542451c2016-07-26 09:02:02 -07002054 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86PointerSize).Int32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002055
2056 DCHECK(!codegen_->IsLeafMethod());
2057 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2058}
2059
Roland Levillain88cb1752014-10-20 16:36:47 +01002060void LocationsBuilderX86::VisitNeg(HNeg* neg) {
2061 LocationSummary* locations =
2062 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2063 switch (neg->GetResultType()) {
2064 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002065 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01002066 locations->SetInAt(0, Location::RequiresRegister());
2067 locations->SetOut(Location::SameAsFirstInput());
2068 break;
2069
Roland Levillain88cb1752014-10-20 16:36:47 +01002070 case Primitive::kPrimFloat:
Roland Levillain5368c212014-11-27 15:03:41 +00002071 locations->SetInAt(0, Location::RequiresFpuRegister());
2072 locations->SetOut(Location::SameAsFirstInput());
2073 locations->AddTemp(Location::RequiresRegister());
2074 locations->AddTemp(Location::RequiresFpuRegister());
2075 break;
2076
Roland Levillain88cb1752014-10-20 16:36:47 +01002077 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002078 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00002079 locations->SetOut(Location::SameAsFirstInput());
2080 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01002081 break;
2082
2083 default:
2084 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2085 }
2086}
2087
2088void InstructionCodeGeneratorX86::VisitNeg(HNeg* neg) {
2089 LocationSummary* locations = neg->GetLocations();
2090 Location out = locations->Out();
2091 Location in = locations->InAt(0);
2092 switch (neg->GetResultType()) {
2093 case Primitive::kPrimInt:
2094 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002095 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002096 __ negl(out.AsRegister<Register>());
Roland Levillain88cb1752014-10-20 16:36:47 +01002097 break;
2098
2099 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002100 DCHECK(in.IsRegisterPair());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002101 DCHECK(in.Equals(out));
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002102 __ negl(out.AsRegisterPairLow<Register>());
2103 // Negation is similar to subtraction from zero. The least
2104 // significant byte triggers a borrow when it is different from
2105 // zero; to take it into account, add 1 to the most significant
2106 // byte if the carry flag (CF) is set to 1 after the first NEGL
2107 // operation.
2108 __ adcl(out.AsRegisterPairHigh<Register>(), Immediate(0));
2109 __ negl(out.AsRegisterPairHigh<Register>());
2110 break;
2111
Roland Levillain5368c212014-11-27 15:03:41 +00002112 case Primitive::kPrimFloat: {
2113 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002114 Register constant = locations->GetTemp(0).AsRegister<Register>();
2115 XmmRegister mask = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002116 // Implement float negation with an exclusive or with value
2117 // 0x80000000 (mask for bit 31, representing the sign of a
2118 // single-precision floating-point number).
2119 __ movl(constant, Immediate(INT32_C(0x80000000)));
2120 __ movd(mask, constant);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002121 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00002122 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002123 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00002124
Roland Levillain5368c212014-11-27 15:03:41 +00002125 case Primitive::kPrimDouble: {
2126 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002127 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002128 // Implement double negation with an exclusive or with value
2129 // 0x8000000000000000 (mask for bit 63, representing the sign of
2130 // a double-precision floating-point number).
2131 __ LoadLongConstant(mask, INT64_C(0x8000000000000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002132 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01002133 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002134 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002135
2136 default:
2137 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2138 }
2139}
2140
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002141void LocationsBuilderX86::VisitX86FPNeg(HX86FPNeg* neg) {
2142 LocationSummary* locations =
2143 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2144 DCHECK(Primitive::IsFloatingPointType(neg->GetType()));
2145 locations->SetInAt(0, Location::RequiresFpuRegister());
2146 locations->SetInAt(1, Location::RequiresRegister());
2147 locations->SetOut(Location::SameAsFirstInput());
2148 locations->AddTemp(Location::RequiresFpuRegister());
2149}
2150
2151void InstructionCodeGeneratorX86::VisitX86FPNeg(HX86FPNeg* neg) {
2152 LocationSummary* locations = neg->GetLocations();
2153 Location out = locations->Out();
2154 DCHECK(locations->InAt(0).Equals(out));
2155
2156 Register constant_area = locations->InAt(1).AsRegister<Register>();
2157 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
2158 if (neg->GetType() == Primitive::kPrimFloat) {
2159 __ movss(mask, codegen_->LiteralInt32Address(INT32_C(0x80000000), constant_area));
2160 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
2161 } else {
2162 __ movsd(mask, codegen_->LiteralInt64Address(INT64_C(0x8000000000000000), constant_area));
2163 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
2164 }
2165}
2166
Roland Levillaindff1f282014-11-05 14:15:05 +00002167void LocationsBuilderX86::VisitTypeConversion(HTypeConversion* conversion) {
Roland Levillaindff1f282014-11-05 14:15:05 +00002168 Primitive::Type result_type = conversion->GetResultType();
2169 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002170 DCHECK_NE(result_type, input_type);
Roland Levillain624279f2014-12-04 11:54:28 +00002171
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002172 // The float-to-long and double-to-long type conversions rely on a
2173 // call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00002174 LocationSummary::CallKind call_kind =
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002175 ((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
2176 && result_type == Primitive::kPrimLong)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01002177 ? LocationSummary::kCallOnMainOnly
Roland Levillain624279f2014-12-04 11:54:28 +00002178 : LocationSummary::kNoCall;
2179 LocationSummary* locations =
2180 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
2181
David Brazdilb2bd1c52015-03-25 11:17:37 +00002182 // The Java language does not allow treating boolean as an integral type but
2183 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00002184
Roland Levillaindff1f282014-11-05 14:15:05 +00002185 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002186 case Primitive::kPrimByte:
2187 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002188 case Primitive::kPrimLong: {
2189 // Type conversion from long to byte is a result of code transformations.
2190 HInstruction* input = conversion->InputAt(0);
2191 Location input_location = input->IsConstant()
2192 ? Location::ConstantLocation(input->AsConstant())
2193 : Location::RegisterPairLocation(EAX, EDX);
2194 locations->SetInAt(0, input_location);
2195 // Make the output overlap to please the register allocator. This greatly simplifies
2196 // the validation of the linear scan implementation
2197 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2198 break;
2199 }
David Brazdil46e2a392015-03-16 17:31:52 +00002200 case Primitive::kPrimBoolean:
2201 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002202 case Primitive::kPrimShort:
2203 case Primitive::kPrimInt:
2204 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002205 // Processing a Dex `int-to-byte' instruction.
Mark Mendell5f874182015-03-04 15:42:45 -05002206 locations->SetInAt(0, Location::ByteRegisterOrConstant(ECX, conversion->InputAt(0)));
2207 // Make the output overlap to please the register allocator. This greatly simplifies
2208 // the validation of the linear scan implementation
2209 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Roland Levillain51d3fc42014-11-13 14:11:42 +00002210 break;
2211
2212 default:
2213 LOG(FATAL) << "Unexpected type conversion from " << input_type
2214 << " to " << result_type;
2215 }
2216 break;
2217
Roland Levillain01a8d712014-11-14 16:27:39 +00002218 case Primitive::kPrimShort:
2219 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002220 case Primitive::kPrimLong:
2221 // Type conversion from long to short is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002222 case Primitive::kPrimBoolean:
2223 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002224 case Primitive::kPrimByte:
2225 case Primitive::kPrimInt:
2226 case Primitive::kPrimChar:
2227 // Processing a Dex `int-to-short' instruction.
2228 locations->SetInAt(0, Location::Any());
2229 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2230 break;
2231
2232 default:
2233 LOG(FATAL) << "Unexpected type conversion from " << input_type
2234 << " to " << result_type;
2235 }
2236 break;
2237
Roland Levillain946e1432014-11-11 17:35:19 +00002238 case Primitive::kPrimInt:
2239 switch (input_type) {
2240 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002241 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002242 locations->SetInAt(0, Location::Any());
2243 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2244 break;
2245
2246 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00002247 // Processing a Dex `float-to-int' instruction.
2248 locations->SetInAt(0, Location::RequiresFpuRegister());
2249 locations->SetOut(Location::RequiresRegister());
2250 locations->AddTemp(Location::RequiresFpuRegister());
2251 break;
2252
Roland Levillain946e1432014-11-11 17:35:19 +00002253 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002254 // Processing a Dex `double-to-int' instruction.
2255 locations->SetInAt(0, Location::RequiresFpuRegister());
2256 locations->SetOut(Location::RequiresRegister());
2257 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00002258 break;
2259
2260 default:
2261 LOG(FATAL) << "Unexpected type conversion from " << input_type
2262 << " to " << result_type;
2263 }
2264 break;
2265
Roland Levillaindff1f282014-11-05 14:15:05 +00002266 case Primitive::kPrimLong:
2267 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002268 case Primitive::kPrimBoolean:
2269 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002270 case Primitive::kPrimByte:
2271 case Primitive::kPrimShort:
2272 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002273 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002274 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002275 locations->SetInAt(0, Location::RegisterLocation(EAX));
2276 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
2277 break;
2278
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002279 case Primitive::kPrimFloat:
Vladimir Marko949c91f2015-01-27 10:48:44 +00002280 case Primitive::kPrimDouble: {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002281 // Processing a Dex `float-to-long' or 'double-to-long' instruction.
Vladimir Marko949c91f2015-01-27 10:48:44 +00002282 InvokeRuntimeCallingConvention calling_convention;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002283 XmmRegister parameter = calling_convention.GetFpuRegisterAt(0);
2284 locations->SetInAt(0, Location::FpuRegisterLocation(parameter));
2285
Vladimir Marko949c91f2015-01-27 10:48:44 +00002286 // The runtime helper puts the result in EAX, EDX.
2287 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Vladimir Marko949c91f2015-01-27 10:48:44 +00002288 }
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002289 break;
Roland Levillaindff1f282014-11-05 14:15:05 +00002290
2291 default:
2292 LOG(FATAL) << "Unexpected type conversion from " << input_type
2293 << " to " << result_type;
2294 }
2295 break;
2296
Roland Levillain981e4542014-11-14 11:47:14 +00002297 case Primitive::kPrimChar:
2298 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002299 case Primitive::kPrimLong:
2300 // Type conversion from long to char is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002301 case Primitive::kPrimBoolean:
2302 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002303 case Primitive::kPrimByte:
2304 case Primitive::kPrimShort:
2305 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002306 // Processing a Dex `int-to-char' instruction.
2307 locations->SetInAt(0, Location::Any());
2308 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2309 break;
2310
2311 default:
2312 LOG(FATAL) << "Unexpected type conversion from " << input_type
2313 << " to " << result_type;
2314 }
2315 break;
2316
Roland Levillaindff1f282014-11-05 14:15:05 +00002317 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002318 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002319 case Primitive::kPrimBoolean:
2320 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002321 case Primitive::kPrimByte:
2322 case Primitive::kPrimShort:
2323 case Primitive::kPrimInt:
2324 case Primitive::kPrimChar:
2325 // Processing a Dex `int-to-float' instruction.
2326 locations->SetInAt(0, Location::RequiresRegister());
2327 locations->SetOut(Location::RequiresFpuRegister());
2328 break;
2329
2330 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002331 // Processing a Dex `long-to-float' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002332 locations->SetInAt(0, Location::Any());
2333 locations->SetOut(Location::Any());
Roland Levillain6d0e4832014-11-27 18:31:21 +00002334 break;
2335
Roland Levillaincff13742014-11-17 14:32:17 +00002336 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002337 // Processing a Dex `double-to-float' instruction.
2338 locations->SetInAt(0, Location::RequiresFpuRegister());
2339 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002340 break;
2341
2342 default:
2343 LOG(FATAL) << "Unexpected type conversion from " << input_type
2344 << " to " << result_type;
2345 };
2346 break;
2347
Roland Levillaindff1f282014-11-05 14:15:05 +00002348 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002349 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002350 case Primitive::kPrimBoolean:
2351 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002352 case Primitive::kPrimByte:
2353 case Primitive::kPrimShort:
2354 case Primitive::kPrimInt:
2355 case Primitive::kPrimChar:
2356 // Processing a Dex `int-to-double' instruction.
2357 locations->SetInAt(0, Location::RequiresRegister());
2358 locations->SetOut(Location::RequiresFpuRegister());
2359 break;
2360
2361 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002362 // Processing a Dex `long-to-double' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002363 locations->SetInAt(0, Location::Any());
2364 locations->SetOut(Location::Any());
Roland Levillain647b9ed2014-11-27 12:06:00 +00002365 break;
2366
Roland Levillaincff13742014-11-17 14:32:17 +00002367 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002368 // Processing a Dex `float-to-double' instruction.
2369 locations->SetInAt(0, Location::RequiresFpuRegister());
2370 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002371 break;
2372
2373 default:
2374 LOG(FATAL) << "Unexpected type conversion from " << input_type
2375 << " to " << result_type;
2376 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002377 break;
2378
2379 default:
2380 LOG(FATAL) << "Unexpected type conversion from " << input_type
2381 << " to " << result_type;
2382 }
2383}
2384
2385void InstructionCodeGeneratorX86::VisitTypeConversion(HTypeConversion* conversion) {
2386 LocationSummary* locations = conversion->GetLocations();
2387 Location out = locations->Out();
2388 Location in = locations->InAt(0);
2389 Primitive::Type result_type = conversion->GetResultType();
2390 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002391 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00002392 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002393 case Primitive::kPrimByte:
2394 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002395 case Primitive::kPrimLong:
2396 // Type conversion from long to byte is a result of code transformations.
2397 if (in.IsRegisterPair()) {
2398 __ movsxb(out.AsRegister<Register>(), in.AsRegisterPairLow<ByteRegister>());
2399 } else {
2400 DCHECK(in.GetConstant()->IsLongConstant());
2401 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2402 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
2403 }
2404 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002405 case Primitive::kPrimBoolean:
2406 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002407 case Primitive::kPrimShort:
2408 case Primitive::kPrimInt:
2409 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002410 // Processing a Dex `int-to-byte' instruction.
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002411 if (in.IsRegister()) {
2412 __ movsxb(out.AsRegister<Register>(), in.AsRegister<ByteRegister>());
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002413 } else {
2414 DCHECK(in.GetConstant()->IsIntConstant());
2415 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
2416 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
2417 }
Roland Levillain51d3fc42014-11-13 14:11:42 +00002418 break;
2419
2420 default:
2421 LOG(FATAL) << "Unexpected type conversion from " << input_type
2422 << " to " << result_type;
2423 }
2424 break;
2425
Roland Levillain01a8d712014-11-14 16:27:39 +00002426 case Primitive::kPrimShort:
2427 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002428 case Primitive::kPrimLong:
2429 // Type conversion from long to short is a result of code transformations.
2430 if (in.IsRegisterPair()) {
2431 __ movsxw(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
2432 } else if (in.IsDoubleStackSlot()) {
2433 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
2434 } else {
2435 DCHECK(in.GetConstant()->IsLongConstant());
2436 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2437 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
2438 }
2439 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002440 case Primitive::kPrimBoolean:
2441 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002442 case Primitive::kPrimByte:
2443 case Primitive::kPrimInt:
2444 case Primitive::kPrimChar:
2445 // Processing a Dex `int-to-short' instruction.
2446 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002447 __ movsxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain01a8d712014-11-14 16:27:39 +00002448 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002449 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain01a8d712014-11-14 16:27:39 +00002450 } else {
2451 DCHECK(in.GetConstant()->IsIntConstant());
2452 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002453 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
Roland Levillain01a8d712014-11-14 16:27:39 +00002454 }
2455 break;
2456
2457 default:
2458 LOG(FATAL) << "Unexpected type conversion from " << input_type
2459 << " to " << result_type;
2460 }
2461 break;
2462
Roland Levillain946e1432014-11-11 17:35:19 +00002463 case Primitive::kPrimInt:
2464 switch (input_type) {
2465 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002466 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002467 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002468 __ movl(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00002469 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002470 __ movl(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain946e1432014-11-11 17:35:19 +00002471 } else {
2472 DCHECK(in.IsConstant());
2473 DCHECK(in.GetConstant()->IsLongConstant());
2474 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002475 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00002476 }
2477 break;
2478
Roland Levillain3f8f9362014-12-02 17:45:01 +00002479 case Primitive::kPrimFloat: {
2480 // Processing a Dex `float-to-int' instruction.
2481 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2482 Register output = out.AsRegister<Register>();
2483 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002484 NearLabel done, nan;
Roland Levillain3f8f9362014-12-02 17:45:01 +00002485
2486 __ movl(output, Immediate(kPrimIntMax));
2487 // temp = int-to-float(output)
2488 __ cvtsi2ss(temp, output);
2489 // if input >= temp goto done
2490 __ comiss(input, temp);
2491 __ j(kAboveEqual, &done);
2492 // if input == NaN goto nan
2493 __ j(kUnordered, &nan);
2494 // output = float-to-int-truncate(input)
2495 __ cvttss2si(output, input);
2496 __ jmp(&done);
2497 __ Bind(&nan);
2498 // output = 0
2499 __ xorl(output, output);
2500 __ Bind(&done);
2501 break;
2502 }
2503
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002504 case Primitive::kPrimDouble: {
2505 // Processing a Dex `double-to-int' instruction.
2506 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2507 Register output = out.AsRegister<Register>();
2508 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002509 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002510
2511 __ movl(output, Immediate(kPrimIntMax));
2512 // temp = int-to-double(output)
2513 __ cvtsi2sd(temp, output);
2514 // if input >= temp goto done
2515 __ comisd(input, temp);
2516 __ j(kAboveEqual, &done);
2517 // if input == NaN goto nan
2518 __ j(kUnordered, &nan);
2519 // output = double-to-int-truncate(input)
2520 __ cvttsd2si(output, input);
2521 __ jmp(&done);
2522 __ Bind(&nan);
2523 // output = 0
2524 __ xorl(output, output);
2525 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00002526 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002527 }
Roland Levillain946e1432014-11-11 17:35:19 +00002528
2529 default:
2530 LOG(FATAL) << "Unexpected type conversion from " << input_type
2531 << " to " << result_type;
2532 }
2533 break;
2534
Roland Levillaindff1f282014-11-05 14:15:05 +00002535 case Primitive::kPrimLong:
2536 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002537 case Primitive::kPrimBoolean:
2538 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002539 case Primitive::kPrimByte:
2540 case Primitive::kPrimShort:
2541 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002542 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002543 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002544 DCHECK_EQ(out.AsRegisterPairLow<Register>(), EAX);
2545 DCHECK_EQ(out.AsRegisterPairHigh<Register>(), EDX);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002546 DCHECK_EQ(in.AsRegister<Register>(), EAX);
Roland Levillaindff1f282014-11-05 14:15:05 +00002547 __ cdq();
2548 break;
2549
2550 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00002551 // Processing a Dex `float-to-long' instruction.
Serban Constantinescuba45db02016-07-12 22:53:02 +01002552 codegen_->InvokeRuntime(kQuickF2l, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00002553 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
Roland Levillain624279f2014-12-04 11:54:28 +00002554 break;
2555
Roland Levillaindff1f282014-11-05 14:15:05 +00002556 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002557 // Processing a Dex `double-to-long' instruction.
Serban Constantinescuba45db02016-07-12 22:53:02 +01002558 codegen_->InvokeRuntime(kQuickD2l, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00002559 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
Roland Levillaindff1f282014-11-05 14:15:05 +00002560 break;
2561
2562 default:
2563 LOG(FATAL) << "Unexpected type conversion from " << input_type
2564 << " to " << result_type;
2565 }
2566 break;
2567
Roland Levillain981e4542014-11-14 11:47:14 +00002568 case Primitive::kPrimChar:
2569 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002570 case Primitive::kPrimLong:
2571 // Type conversion from long to short is a result of code transformations.
2572 if (in.IsRegisterPair()) {
2573 __ movzxw(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
2574 } else if (in.IsDoubleStackSlot()) {
2575 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
2576 } else {
2577 DCHECK(in.GetConstant()->IsLongConstant());
2578 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2579 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
2580 }
2581 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002582 case Primitive::kPrimBoolean:
2583 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002584 case Primitive::kPrimByte:
2585 case Primitive::kPrimShort:
2586 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002587 // Processing a Dex `Process a Dex `int-to-char'' instruction.
2588 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002589 __ movzxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain981e4542014-11-14 11:47:14 +00002590 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002591 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain981e4542014-11-14 11:47:14 +00002592 } else {
2593 DCHECK(in.GetConstant()->IsIntConstant());
2594 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002595 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
Roland Levillain981e4542014-11-14 11:47:14 +00002596 }
2597 break;
2598
2599 default:
2600 LOG(FATAL) << "Unexpected type conversion from " << input_type
2601 << " to " << result_type;
2602 }
2603 break;
2604
Roland Levillaindff1f282014-11-05 14:15:05 +00002605 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002606 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002607 case Primitive::kPrimBoolean:
2608 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002609 case Primitive::kPrimByte:
2610 case Primitive::kPrimShort:
2611 case Primitive::kPrimInt:
2612 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002613 // Processing a Dex `int-to-float' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002614 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002615 break;
2616
Roland Levillain6d0e4832014-11-27 18:31:21 +00002617 case Primitive::kPrimLong: {
2618 // Processing a Dex `long-to-float' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002619 size_t adjustment = 0;
Roland Levillain6d0e4832014-11-27 18:31:21 +00002620
Roland Levillain232ade02015-04-20 15:14:36 +01002621 // Create stack space for the call to
2622 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstps below.
2623 // TODO: enhance register allocator to ask for stack temporaries.
2624 if (!in.IsDoubleStackSlot() || !out.IsStackSlot()) {
2625 adjustment = Primitive::ComponentSize(Primitive::kPrimLong);
2626 __ subl(ESP, Immediate(adjustment));
2627 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002628
Roland Levillain232ade02015-04-20 15:14:36 +01002629 // Load the value to the FP stack, using temporaries if needed.
2630 PushOntoFPStack(in, 0, adjustment, false, true);
2631
2632 if (out.IsStackSlot()) {
2633 __ fstps(Address(ESP, out.GetStackIndex() + adjustment));
2634 } else {
2635 __ fstps(Address(ESP, 0));
2636 Location stack_temp = Location::StackSlot(0);
2637 codegen_->Move32(out, stack_temp);
2638 }
2639
2640 // Remove the temporary stack space we allocated.
2641 if (adjustment != 0) {
2642 __ addl(ESP, Immediate(adjustment));
2643 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002644 break;
2645 }
2646
Roland Levillaincff13742014-11-17 14:32:17 +00002647 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002648 // Processing a Dex `double-to-float' instruction.
2649 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002650 break;
2651
2652 default:
2653 LOG(FATAL) << "Unexpected type conversion from " << input_type
2654 << " to " << result_type;
2655 };
2656 break;
2657
Roland Levillaindff1f282014-11-05 14:15:05 +00002658 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002659 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002660 case Primitive::kPrimBoolean:
2661 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002662 case Primitive::kPrimByte:
2663 case Primitive::kPrimShort:
2664 case Primitive::kPrimInt:
2665 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002666 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002667 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002668 break;
2669
Roland Levillain647b9ed2014-11-27 12:06:00 +00002670 case Primitive::kPrimLong: {
2671 // Processing a Dex `long-to-double' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002672 size_t adjustment = 0;
Roland Levillain647b9ed2014-11-27 12:06:00 +00002673
Roland Levillain232ade02015-04-20 15:14:36 +01002674 // Create stack space for the call to
2675 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstpl below.
2676 // TODO: enhance register allocator to ask for stack temporaries.
2677 if (!in.IsDoubleStackSlot() || !out.IsDoubleStackSlot()) {
2678 adjustment = Primitive::ComponentSize(Primitive::kPrimLong);
2679 __ subl(ESP, Immediate(adjustment));
2680 }
2681
2682 // Load the value to the FP stack, using temporaries if needed.
2683 PushOntoFPStack(in, 0, adjustment, false, true);
2684
2685 if (out.IsDoubleStackSlot()) {
2686 __ fstpl(Address(ESP, out.GetStackIndex() + adjustment));
2687 } else {
2688 __ fstpl(Address(ESP, 0));
2689 Location stack_temp = Location::DoubleStackSlot(0);
2690 codegen_->Move64(out, stack_temp);
2691 }
2692
2693 // Remove the temporary stack space we allocated.
2694 if (adjustment != 0) {
2695 __ addl(ESP, Immediate(adjustment));
2696 }
Roland Levillain647b9ed2014-11-27 12:06:00 +00002697 break;
2698 }
2699
Roland Levillaincff13742014-11-17 14:32:17 +00002700 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002701 // Processing a Dex `float-to-double' instruction.
2702 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002703 break;
2704
2705 default:
2706 LOG(FATAL) << "Unexpected type conversion from " << input_type
2707 << " to " << result_type;
2708 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002709 break;
2710
2711 default:
2712 LOG(FATAL) << "Unexpected type conversion from " << input_type
2713 << " to " << result_type;
2714 }
2715}
2716
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002717void LocationsBuilderX86::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002718 LocationSummary* locations =
2719 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002720 switch (add->GetResultType()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002721 case Primitive::kPrimInt: {
2722 locations->SetInAt(0, Location::RequiresRegister());
2723 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
2724 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2725 break;
2726 }
2727
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002728 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002729 locations->SetInAt(0, Location::RequiresRegister());
2730 locations->SetInAt(1, Location::Any());
2731 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002732 break;
2733 }
2734
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002735 case Primitive::kPrimFloat:
2736 case Primitive::kPrimDouble: {
2737 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002738 if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
2739 DCHECK(add->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00002740 } else if (add->InputAt(1)->IsConstant()) {
2741 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002742 } else {
2743 locations->SetInAt(1, Location::Any());
2744 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002745 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002746 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002747 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002748
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002749 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002750 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
2751 break;
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002752 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002753}
2754
2755void InstructionCodeGeneratorX86::VisitAdd(HAdd* add) {
2756 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002757 Location first = locations->InAt(0);
2758 Location second = locations->InAt(1);
Mark Mendell09b84632015-02-13 17:48:38 -05002759 Location out = locations->Out();
2760
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002761 switch (add->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002762 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002763 if (second.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002764 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2765 __ addl(out.AsRegister<Register>(), second.AsRegister<Register>());
Mark Mendell33bf2452015-05-27 10:08:24 -04002766 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
2767 __ addl(out.AsRegister<Register>(), first.AsRegister<Register>());
Mark Mendell09b84632015-02-13 17:48:38 -05002768 } else {
2769 __ leal(out.AsRegister<Register>(), Address(
2770 first.AsRegister<Register>(), second.AsRegister<Register>(), TIMES_1, 0));
2771 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002772 } else if (second.IsConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002773 int32_t value = second.GetConstant()->AsIntConstant()->GetValue();
2774 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2775 __ addl(out.AsRegister<Register>(), Immediate(value));
2776 } else {
2777 __ leal(out.AsRegister<Register>(), Address(first.AsRegister<Register>(), value));
2778 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002779 } else {
Mark Mendell09b84632015-02-13 17:48:38 -05002780 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002781 __ addl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002782 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002783 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002784 }
2785
2786 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002787 if (second.IsRegisterPair()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002788 __ addl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
2789 __ adcl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002790 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002791 __ addl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
2792 __ adcl(first.AsRegisterPairHigh<Register>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002793 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002794 } else {
2795 DCHECK(second.IsConstant()) << second;
2796 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2797 __ addl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
2798 __ adcl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002799 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002800 break;
2801 }
2802
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002803 case Primitive::kPrimFloat: {
2804 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002805 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04002806 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
2807 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00002808 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04002809 __ addss(first.AsFpuRegister<XmmRegister>(),
2810 codegen_->LiteralFloatAddress(
2811 const_area->GetConstant()->AsFloatConstant()->GetValue(),
2812 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2813 } else {
2814 DCHECK(second.IsStackSlot());
2815 __ addss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002816 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002817 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002818 }
2819
2820 case Primitive::kPrimDouble: {
2821 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002822 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04002823 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
2824 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00002825 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04002826 __ addsd(first.AsFpuRegister<XmmRegister>(),
2827 codegen_->LiteralDoubleAddress(
2828 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
2829 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2830 } else {
2831 DCHECK(second.IsDoubleStackSlot());
2832 __ addsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002833 }
2834 break;
2835 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002836
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002837 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002838 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002839 }
2840}
2841
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002842void LocationsBuilderX86::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002843 LocationSummary* locations =
2844 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002845 switch (sub->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002846 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002847 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002848 locations->SetInAt(0, Location::RequiresRegister());
2849 locations->SetInAt(1, Location::Any());
2850 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002851 break;
2852 }
Calin Juravle11351682014-10-23 15:38:15 +01002853 case Primitive::kPrimFloat:
2854 case Primitive::kPrimDouble: {
2855 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002856 if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
2857 DCHECK(sub->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00002858 } else if (sub->InputAt(1)->IsConstant()) {
2859 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002860 } else {
2861 locations->SetInAt(1, Location::Any());
2862 }
Calin Juravle11351682014-10-23 15:38:15 +01002863 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002864 break;
Calin Juravle11351682014-10-23 15:38:15 +01002865 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002866
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002867 default:
Calin Juravle11351682014-10-23 15:38:15 +01002868 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002869 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002870}
2871
2872void InstructionCodeGeneratorX86::VisitSub(HSub* sub) {
2873 LocationSummary* locations = sub->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002874 Location first = locations->InAt(0);
2875 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01002876 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002877 switch (sub->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002878 case Primitive::kPrimInt: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002879 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002880 __ subl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002881 } else if (second.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002882 __ subl(first.AsRegister<Register>(),
2883 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002884 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002885 __ subl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002886 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002887 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002888 }
2889
2890 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002891 if (second.IsRegisterPair()) {
Calin Juravle11351682014-10-23 15:38:15 +01002892 __ subl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
2893 __ sbbl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002894 } else if (second.IsDoubleStackSlot()) {
Calin Juravle11351682014-10-23 15:38:15 +01002895 __ subl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002896 __ sbbl(first.AsRegisterPairHigh<Register>(),
2897 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002898 } else {
2899 DCHECK(second.IsConstant()) << second;
2900 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2901 __ subl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
2902 __ sbbl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002903 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002904 break;
2905 }
2906
Calin Juravle11351682014-10-23 15:38:15 +01002907 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04002908 if (second.IsFpuRegister()) {
2909 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2910 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
2911 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00002912 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04002913 __ subss(first.AsFpuRegister<XmmRegister>(),
2914 codegen_->LiteralFloatAddress(
2915 const_area->GetConstant()->AsFloatConstant()->GetValue(),
2916 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2917 } else {
2918 DCHECK(second.IsStackSlot());
2919 __ subss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
2920 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002921 break;
Calin Juravle11351682014-10-23 15:38:15 +01002922 }
2923
2924 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04002925 if (second.IsFpuRegister()) {
2926 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2927 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
2928 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00002929 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04002930 __ subsd(first.AsFpuRegister<XmmRegister>(),
2931 codegen_->LiteralDoubleAddress(
2932 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
2933 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2934 } else {
2935 DCHECK(second.IsDoubleStackSlot());
2936 __ subsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
2937 }
Calin Juravle11351682014-10-23 15:38:15 +01002938 break;
2939 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002940
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002941 default:
Calin Juravle11351682014-10-23 15:38:15 +01002942 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002943 }
2944}
2945
Calin Juravle34bacdf2014-10-07 20:23:36 +01002946void LocationsBuilderX86::VisitMul(HMul* mul) {
2947 LocationSummary* locations =
2948 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
2949 switch (mul->GetResultType()) {
2950 case Primitive::kPrimInt:
2951 locations->SetInAt(0, Location::RequiresRegister());
2952 locations->SetInAt(1, Location::Any());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002953 if (mul->InputAt(1)->IsIntConstant()) {
2954 // Can use 3 operand multiply.
2955 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2956 } else {
2957 locations->SetOut(Location::SameAsFirstInput());
2958 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002959 break;
2960 case Primitive::kPrimLong: {
2961 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002962 locations->SetInAt(1, Location::Any());
2963 locations->SetOut(Location::SameAsFirstInput());
2964 // Needed for imul on 32bits with 64bits output.
2965 locations->AddTemp(Location::RegisterLocation(EAX));
2966 locations->AddTemp(Location::RegisterLocation(EDX));
2967 break;
2968 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002969 case Primitive::kPrimFloat:
2970 case Primitive::kPrimDouble: {
2971 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002972 if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
2973 DCHECK(mul->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00002974 } else if (mul->InputAt(1)->IsConstant()) {
2975 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002976 } else {
2977 locations->SetInAt(1, Location::Any());
2978 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002979 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002980 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002981 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002982
2983 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002984 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002985 }
2986}
2987
2988void InstructionCodeGeneratorX86::VisitMul(HMul* mul) {
2989 LocationSummary* locations = mul->GetLocations();
2990 Location first = locations->InAt(0);
2991 Location second = locations->InAt(1);
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002992 Location out = locations->Out();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002993
2994 switch (mul->GetResultType()) {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002995 case Primitive::kPrimInt:
2996 // The constant may have ended up in a register, so test explicitly to avoid
2997 // problems where the output may not be the same as the first operand.
2998 if (mul->InputAt(1)->IsIntConstant()) {
2999 Immediate imm(mul->InputAt(1)->AsIntConstant()->GetValue());
3000 __ imull(out.AsRegister<Register>(), first.AsRegister<Register>(), imm);
3001 } else if (second.IsRegister()) {
3002 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003003 __ imull(first.AsRegister<Register>(), second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003004 } else {
3005 DCHECK(second.IsStackSlot());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003006 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003007 __ imull(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01003008 }
3009 break;
Calin Juravle34bacdf2014-10-07 20:23:36 +01003010
3011 case Primitive::kPrimLong: {
Calin Juravle34bacdf2014-10-07 20:23:36 +01003012 Register in1_hi = first.AsRegisterPairHigh<Register>();
3013 Register in1_lo = first.AsRegisterPairLow<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003014 Register eax = locations->GetTemp(0).AsRegister<Register>();
3015 Register edx = locations->GetTemp(1).AsRegister<Register>();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003016
3017 DCHECK_EQ(EAX, eax);
3018 DCHECK_EQ(EDX, edx);
3019
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003020 // input: in1 - 64 bits, in2 - 64 bits.
Calin Juravle34bacdf2014-10-07 20:23:36 +01003021 // output: in1
3022 // formula: in1.hi : in1.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
3023 // parts: in1.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
3024 // parts: in1.lo = (in1.lo * in2.lo)[31:0]
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003025 if (second.IsConstant()) {
3026 DCHECK(second.GetConstant()->IsLongConstant());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003027
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003028 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3029 int32_t low_value = Low32Bits(value);
3030 int32_t high_value = High32Bits(value);
3031 Immediate low(low_value);
3032 Immediate high(high_value);
3033
3034 __ movl(eax, high);
3035 // eax <- in1.lo * in2.hi
3036 __ imull(eax, in1_lo);
3037 // in1.hi <- in1.hi * in2.lo
3038 __ imull(in1_hi, low);
3039 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3040 __ addl(in1_hi, eax);
3041 // move in2_lo to eax to prepare for double precision
3042 __ movl(eax, low);
3043 // edx:eax <- in1.lo * in2.lo
3044 __ mull(in1_lo);
3045 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3046 __ addl(in1_hi, edx);
3047 // in1.lo <- (in1.lo * in2.lo)[31:0];
3048 __ movl(in1_lo, eax);
3049 } else if (second.IsRegisterPair()) {
3050 Register in2_hi = second.AsRegisterPairHigh<Register>();
3051 Register in2_lo = second.AsRegisterPairLow<Register>();
3052
3053 __ movl(eax, in2_hi);
3054 // eax <- in1.lo * in2.hi
3055 __ imull(eax, in1_lo);
3056 // in1.hi <- in1.hi * in2.lo
3057 __ imull(in1_hi, in2_lo);
3058 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3059 __ addl(in1_hi, eax);
3060 // move in1_lo to eax to prepare for double precision
3061 __ movl(eax, in1_lo);
3062 // edx:eax <- in1.lo * in2.lo
3063 __ mull(in2_lo);
3064 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3065 __ addl(in1_hi, edx);
3066 // in1.lo <- (in1.lo * in2.lo)[31:0];
3067 __ movl(in1_lo, eax);
3068 } else {
3069 DCHECK(second.IsDoubleStackSlot()) << second;
3070 Address in2_hi(ESP, second.GetHighStackIndex(kX86WordSize));
3071 Address in2_lo(ESP, second.GetStackIndex());
3072
3073 __ movl(eax, in2_hi);
3074 // eax <- in1.lo * in2.hi
3075 __ imull(eax, in1_lo);
3076 // in1.hi <- in1.hi * in2.lo
3077 __ imull(in1_hi, in2_lo);
3078 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3079 __ addl(in1_hi, eax);
3080 // move in1_lo to eax to prepare for double precision
3081 __ movl(eax, in1_lo);
3082 // edx:eax <- in1.lo * in2.lo
3083 __ mull(in2_lo);
3084 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3085 __ addl(in1_hi, edx);
3086 // in1.lo <- (in1.lo * in2.lo)[31:0];
3087 __ movl(in1_lo, eax);
3088 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003089
3090 break;
3091 }
3092
Calin Juravleb5bfa962014-10-21 18:02:24 +01003093 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003094 DCHECK(first.Equals(locations->Out()));
3095 if (second.IsFpuRegister()) {
3096 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3097 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3098 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003099 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003100 __ mulss(first.AsFpuRegister<XmmRegister>(),
3101 codegen_->LiteralFloatAddress(
3102 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3103 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3104 } else {
3105 DCHECK(second.IsStackSlot());
3106 __ mulss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3107 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003108 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003109 }
3110
3111 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003112 DCHECK(first.Equals(locations->Out()));
3113 if (second.IsFpuRegister()) {
3114 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3115 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3116 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003117 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003118 __ mulsd(first.AsFpuRegister<XmmRegister>(),
3119 codegen_->LiteralDoubleAddress(
3120 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3121 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3122 } else {
3123 DCHECK(second.IsDoubleStackSlot());
3124 __ mulsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3125 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003126 break;
3127 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003128
3129 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003130 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003131 }
3132}
3133
Roland Levillain232ade02015-04-20 15:14:36 +01003134void InstructionCodeGeneratorX86::PushOntoFPStack(Location source,
3135 uint32_t temp_offset,
3136 uint32_t stack_adjustment,
3137 bool is_fp,
3138 bool is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003139 if (source.IsStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01003140 DCHECK(!is_wide);
3141 if (is_fp) {
3142 __ flds(Address(ESP, source.GetStackIndex() + stack_adjustment));
3143 } else {
3144 __ filds(Address(ESP, source.GetStackIndex() + stack_adjustment));
3145 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003146 } else if (source.IsDoubleStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01003147 DCHECK(is_wide);
3148 if (is_fp) {
3149 __ fldl(Address(ESP, source.GetStackIndex() + stack_adjustment));
3150 } else {
3151 __ fildl(Address(ESP, source.GetStackIndex() + stack_adjustment));
3152 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003153 } else {
3154 // Write the value to the temporary location on the stack and load to FP stack.
Roland Levillain232ade02015-04-20 15:14:36 +01003155 if (!is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003156 Location stack_temp = Location::StackSlot(temp_offset);
3157 codegen_->Move32(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01003158 if (is_fp) {
3159 __ flds(Address(ESP, temp_offset));
3160 } else {
3161 __ filds(Address(ESP, temp_offset));
3162 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003163 } else {
3164 Location stack_temp = Location::DoubleStackSlot(temp_offset);
3165 codegen_->Move64(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01003166 if (is_fp) {
3167 __ fldl(Address(ESP, temp_offset));
3168 } else {
3169 __ fildl(Address(ESP, temp_offset));
3170 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003171 }
3172 }
3173}
3174
3175void InstructionCodeGeneratorX86::GenerateRemFP(HRem *rem) {
3176 Primitive::Type type = rem->GetResultType();
3177 bool is_float = type == Primitive::kPrimFloat;
3178 size_t elem_size = Primitive::ComponentSize(type);
3179 LocationSummary* locations = rem->GetLocations();
3180 Location first = locations->InAt(0);
3181 Location second = locations->InAt(1);
3182 Location out = locations->Out();
3183
3184 // Create stack space for 2 elements.
3185 // TODO: enhance register allocator to ask for stack temporaries.
3186 __ subl(ESP, Immediate(2 * elem_size));
3187
3188 // Load the values to the FP stack in reverse order, using temporaries if needed.
Roland Levillain232ade02015-04-20 15:14:36 +01003189 const bool is_wide = !is_float;
3190 PushOntoFPStack(second, elem_size, 2 * elem_size, /* is_fp */ true, is_wide);
3191 PushOntoFPStack(first, 0, 2 * elem_size, /* is_fp */ true, is_wide);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003192
3193 // Loop doing FPREM until we stabilize.
Mark Mendell0c9497d2015-08-21 09:30:05 -04003194 NearLabel retry;
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003195 __ Bind(&retry);
3196 __ fprem();
3197
3198 // Move FP status to AX.
3199 __ fstsw();
3200
3201 // And see if the argument reduction is complete. This is signaled by the
3202 // C2 FPU flag bit set to 0.
3203 __ andl(EAX, Immediate(kC2ConditionMask));
3204 __ j(kNotEqual, &retry);
3205
3206 // We have settled on the final value. Retrieve it into an XMM register.
3207 // Store FP top of stack to real stack.
3208 if (is_float) {
3209 __ fsts(Address(ESP, 0));
3210 } else {
3211 __ fstl(Address(ESP, 0));
3212 }
3213
3214 // Pop the 2 items from the FP stack.
3215 __ fucompp();
3216
3217 // Load the value from the stack into an XMM register.
3218 DCHECK(out.IsFpuRegister()) << out;
3219 if (is_float) {
3220 __ movss(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
3221 } else {
3222 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
3223 }
3224
3225 // And remove the temporary stack space we allocated.
3226 __ addl(ESP, Immediate(2 * elem_size));
3227}
3228
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003229
3230void InstructionCodeGeneratorX86::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3231 DCHECK(instruction->IsDiv() || instruction->IsRem());
3232
3233 LocationSummary* locations = instruction->GetLocations();
3234 DCHECK(locations->InAt(1).IsConstant());
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003235 DCHECK(locations->InAt(1).GetConstant()->IsIntConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003236
3237 Register out_register = locations->Out().AsRegister<Register>();
3238 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003239 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003240
3241 DCHECK(imm == 1 || imm == -1);
3242
3243 if (instruction->IsRem()) {
3244 __ xorl(out_register, out_register);
3245 } else {
3246 __ movl(out_register, input_register);
3247 if (imm == -1) {
3248 __ negl(out_register);
3249 }
3250 }
3251}
3252
3253
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003254void InstructionCodeGeneratorX86::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003255 LocationSummary* locations = instruction->GetLocations();
3256
3257 Register out_register = locations->Out().AsRegister<Register>();
3258 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003259 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003260 DCHECK(IsPowerOfTwo(AbsOrMin(imm)));
3261 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003262
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003263 Register num = locations->GetTemp(0).AsRegister<Register>();
3264
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003265 __ leal(num, Address(input_register, abs_imm - 1));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003266 __ testl(input_register, input_register);
3267 __ cmovl(kGreaterEqual, num, input_register);
3268 int shift = CTZ(imm);
3269 __ sarl(num, Immediate(shift));
3270
3271 if (imm < 0) {
3272 __ negl(num);
3273 }
3274
3275 __ movl(out_register, num);
3276}
3277
3278void InstructionCodeGeneratorX86::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3279 DCHECK(instruction->IsDiv() || instruction->IsRem());
3280
3281 LocationSummary* locations = instruction->GetLocations();
3282 int imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
3283
3284 Register eax = locations->InAt(0).AsRegister<Register>();
3285 Register out = locations->Out().AsRegister<Register>();
3286 Register num;
3287 Register edx;
3288
3289 if (instruction->IsDiv()) {
3290 edx = locations->GetTemp(0).AsRegister<Register>();
3291 num = locations->GetTemp(1).AsRegister<Register>();
3292 } else {
3293 edx = locations->Out().AsRegister<Register>();
3294 num = locations->GetTemp(0).AsRegister<Register>();
3295 }
3296
3297 DCHECK_EQ(EAX, eax);
3298 DCHECK_EQ(EDX, edx);
3299 if (instruction->IsDiv()) {
3300 DCHECK_EQ(EAX, out);
3301 } else {
3302 DCHECK_EQ(EDX, out);
3303 }
3304
3305 int64_t magic;
3306 int shift;
3307 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
3308
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003309 // Save the numerator.
3310 __ movl(num, eax);
3311
3312 // EAX = magic
3313 __ movl(eax, Immediate(magic));
3314
3315 // EDX:EAX = magic * numerator
3316 __ imull(num);
3317
3318 if (imm > 0 && magic < 0) {
3319 // EDX += num
3320 __ addl(edx, num);
3321 } else if (imm < 0 && magic > 0) {
3322 __ subl(edx, num);
3323 }
3324
3325 // Shift if needed.
3326 if (shift != 0) {
3327 __ sarl(edx, Immediate(shift));
3328 }
3329
3330 // EDX += 1 if EDX < 0
3331 __ movl(eax, edx);
3332 __ shrl(edx, Immediate(31));
3333 __ addl(edx, eax);
3334
3335 if (instruction->IsRem()) {
3336 __ movl(eax, num);
3337 __ imull(edx, Immediate(imm));
3338 __ subl(eax, edx);
3339 __ movl(edx, eax);
3340 } else {
3341 __ movl(eax, edx);
3342 }
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003343}
3344
Calin Juravlebacfec32014-11-14 15:54:36 +00003345void InstructionCodeGeneratorX86::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3346 DCHECK(instruction->IsDiv() || instruction->IsRem());
3347
3348 LocationSummary* locations = instruction->GetLocations();
3349 Location out = locations->Out();
3350 Location first = locations->InAt(0);
3351 Location second = locations->InAt(1);
3352 bool is_div = instruction->IsDiv();
3353
3354 switch (instruction->GetResultType()) {
3355 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003356 DCHECK_EQ(EAX, first.AsRegister<Register>());
3357 DCHECK_EQ(is_div ? EAX : EDX, out.AsRegister<Register>());
Calin Juravlebacfec32014-11-14 15:54:36 +00003358
Vladimir Marko13c86fd2015-11-11 12:37:46 +00003359 if (second.IsConstant()) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003360 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003361
3362 if (imm == 0) {
3363 // Do not generate anything for 0. DivZeroCheck would forbid any generated code.
3364 } else if (imm == 1 || imm == -1) {
3365 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003366 } else if (is_div && IsPowerOfTwo(AbsOrMin(imm))) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003367 DivByPowerOfTwo(instruction->AsDiv());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003368 } else {
3369 DCHECK(imm <= -2 || imm >= 2);
3370 GenerateDivRemWithAnyConstant(instruction);
3371 }
3372 } else {
David Srbecky9cd6d372016-02-09 15:24:47 +00003373 SlowPathCode* slow_path = new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86(
3374 instruction, out.AsRegister<Register>(), is_div);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003375 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00003376
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003377 Register second_reg = second.AsRegister<Register>();
3378 // 0x80000000/-1 triggers an arithmetic exception!
3379 // Dividing by -1 is actually negation and -0x800000000 = 0x80000000 so
3380 // it's safe to just use negl instead of more complex comparisons.
Calin Juravlebacfec32014-11-14 15:54:36 +00003381
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003382 __ cmpl(second_reg, Immediate(-1));
3383 __ j(kEqual, slow_path->GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +00003384
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003385 // edx:eax <- sign-extended of eax
3386 __ cdq();
3387 // eax = quotient, edx = remainder
3388 __ idivl(second_reg);
3389 __ Bind(slow_path->GetExitLabel());
3390 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003391 break;
3392 }
3393
3394 case Primitive::kPrimLong: {
3395 InvokeRuntimeCallingConvention calling_convention;
3396 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
3397 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
3398 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
3399 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
3400 DCHECK_EQ(EAX, out.AsRegisterPairLow<Register>());
3401 DCHECK_EQ(EDX, out.AsRegisterPairHigh<Register>());
3402
3403 if (is_div) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01003404 codegen_->InvokeRuntime(kQuickLdiv, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003405 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003406 } else {
Serban Constantinescuba45db02016-07-12 22:53:02 +01003407 codegen_->InvokeRuntime(kQuickLmod, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003408 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003409 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003410 break;
3411 }
3412
3413 default:
3414 LOG(FATAL) << "Unexpected type for GenerateDivRemIntegral " << instruction->GetResultType();
3415 }
3416}
3417
Calin Juravle7c4954d2014-10-28 16:57:40 +00003418void LocationsBuilderX86::VisitDiv(HDiv* div) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003419 LocationSummary::CallKind call_kind = (div->GetResultType() == Primitive::kPrimLong)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003420 ? LocationSummary::kCallOnMainOnly
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003421 : LocationSummary::kNoCall;
3422 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
3423
Calin Juravle7c4954d2014-10-28 16:57:40 +00003424 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00003425 case Primitive::kPrimInt: {
3426 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003427 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00003428 locations->SetOut(Location::SameAsFirstInput());
3429 // Intel uses edx:eax as the dividend.
3430 locations->AddTemp(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003431 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3432 // which enforces results to be in EAX and EDX, things are simpler if we use EAX also as
3433 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003434 if (div->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003435 locations->AddTemp(Location::RequiresRegister());
3436 }
Calin Juravled0d48522014-11-04 16:40:20 +00003437 break;
3438 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003439 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003440 InvokeRuntimeCallingConvention calling_convention;
3441 locations->SetInAt(0, Location::RegisterPairLocation(
3442 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3443 locations->SetInAt(1, Location::RegisterPairLocation(
3444 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3445 // Runtime helper puts the result in EAX, EDX.
3446 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Calin Juravle7c4954d2014-10-28 16:57:40 +00003447 break;
3448 }
3449 case Primitive::kPrimFloat:
3450 case Primitive::kPrimDouble: {
3451 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003452 if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3453 DCHECK(div->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003454 } else if (div->InputAt(1)->IsConstant()) {
3455 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003456 } else {
3457 locations->SetInAt(1, Location::Any());
3458 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003459 locations->SetOut(Location::SameAsFirstInput());
3460 break;
3461 }
3462
3463 default:
3464 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3465 }
3466}
3467
3468void InstructionCodeGeneratorX86::VisitDiv(HDiv* div) {
3469 LocationSummary* locations = div->GetLocations();
3470 Location first = locations->InAt(0);
3471 Location second = locations->InAt(1);
Calin Juravle7c4954d2014-10-28 16:57:40 +00003472
3473 switch (div->GetResultType()) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003474 case Primitive::kPrimInt:
Calin Juravle7c4954d2014-10-28 16:57:40 +00003475 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003476 GenerateDivRemIntegral(div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00003477 break;
3478 }
3479
3480 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003481 if (second.IsFpuRegister()) {
3482 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3483 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3484 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003485 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003486 __ divss(first.AsFpuRegister<XmmRegister>(),
3487 codegen_->LiteralFloatAddress(
3488 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3489 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3490 } else {
3491 DCHECK(second.IsStackSlot());
3492 __ divss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3493 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003494 break;
3495 }
3496
3497 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003498 if (second.IsFpuRegister()) {
3499 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3500 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3501 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003502 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003503 __ divsd(first.AsFpuRegister<XmmRegister>(),
3504 codegen_->LiteralDoubleAddress(
3505 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3506 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3507 } else {
3508 DCHECK(second.IsDoubleStackSlot());
3509 __ divsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3510 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003511 break;
3512 }
3513
3514 default:
3515 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3516 }
3517}
3518
Calin Juravlebacfec32014-11-14 15:54:36 +00003519void LocationsBuilderX86::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00003520 Primitive::Type type = rem->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003521
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003522 LocationSummary::CallKind call_kind = (rem->GetResultType() == Primitive::kPrimLong)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003523 ? LocationSummary::kCallOnMainOnly
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003524 : LocationSummary::kNoCall;
3525 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
Calin Juravlebacfec32014-11-14 15:54:36 +00003526
Calin Juravled2ec87d2014-12-08 14:24:46 +00003527 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003528 case Primitive::kPrimInt: {
3529 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003530 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00003531 locations->SetOut(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003532 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3533 // which enforces results to be in EAX and EDX, things are simpler if we use EDX also as
3534 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003535 if (rem->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003536 locations->AddTemp(Location::RequiresRegister());
3537 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003538 break;
3539 }
3540 case Primitive::kPrimLong: {
3541 InvokeRuntimeCallingConvention calling_convention;
3542 locations->SetInAt(0, Location::RegisterPairLocation(
3543 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3544 locations->SetInAt(1, Location::RegisterPairLocation(
3545 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3546 // Runtime helper puts the result in EAX, EDX.
3547 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
3548 break;
3549 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003550 case Primitive::kPrimDouble:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003551 case Primitive::kPrimFloat: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003552 locations->SetInAt(0, Location::Any());
3553 locations->SetInAt(1, Location::Any());
3554 locations->SetOut(Location::RequiresFpuRegister());
3555 locations->AddTemp(Location::RegisterLocation(EAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00003556 break;
3557 }
3558
3559 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003560 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003561 }
3562}
3563
3564void InstructionCodeGeneratorX86::VisitRem(HRem* rem) {
3565 Primitive::Type type = rem->GetResultType();
3566 switch (type) {
3567 case Primitive::kPrimInt:
3568 case Primitive::kPrimLong: {
3569 GenerateDivRemIntegral(rem);
3570 break;
3571 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003572 case Primitive::kPrimFloat:
Calin Juravlebacfec32014-11-14 15:54:36 +00003573 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003574 GenerateRemFP(rem);
Calin Juravlebacfec32014-11-14 15:54:36 +00003575 break;
3576 }
3577 default:
3578 LOG(FATAL) << "Unexpected rem type " << type;
3579 }
3580}
3581
Calin Juravled0d48522014-11-04 16:40:20 +00003582void LocationsBuilderX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003583 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003584 switch (instruction->GetType()) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003585 case Primitive::kPrimBoolean:
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003586 case Primitive::kPrimByte:
3587 case Primitive::kPrimChar:
3588 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003589 case Primitive::kPrimInt: {
3590 locations->SetInAt(0, Location::Any());
3591 break;
3592 }
3593 case Primitive::kPrimLong: {
3594 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
3595 if (!instruction->IsConstant()) {
3596 locations->AddTemp(Location::RequiresRegister());
3597 }
3598 break;
3599 }
3600 default:
3601 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
3602 }
Calin Juravled0d48522014-11-04 16:40:20 +00003603}
3604
3605void InstructionCodeGeneratorX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003606 SlowPathCode* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86(instruction);
Calin Juravled0d48522014-11-04 16:40:20 +00003607 codegen_->AddSlowPath(slow_path);
3608
3609 LocationSummary* locations = instruction->GetLocations();
3610 Location value = locations->InAt(0);
3611
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003612 switch (instruction->GetType()) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003613 case Primitive::kPrimBoolean:
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003614 case Primitive::kPrimByte:
3615 case Primitive::kPrimChar:
3616 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003617 case Primitive::kPrimInt: {
3618 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003619 __ testl(value.AsRegister<Register>(), value.AsRegister<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003620 __ j(kEqual, slow_path->GetEntryLabel());
3621 } else if (value.IsStackSlot()) {
3622 __ cmpl(Address(ESP, value.GetStackIndex()), Immediate(0));
3623 __ j(kEqual, slow_path->GetEntryLabel());
3624 } else {
3625 DCHECK(value.IsConstant()) << value;
3626 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01003627 __ jmp(slow_path->GetEntryLabel());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003628 }
3629 }
3630 break;
Calin Juravled0d48522014-11-04 16:40:20 +00003631 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003632 case Primitive::kPrimLong: {
3633 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003634 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003635 __ movl(temp, value.AsRegisterPairLow<Register>());
3636 __ orl(temp, value.AsRegisterPairHigh<Register>());
3637 __ j(kEqual, slow_path->GetEntryLabel());
3638 } else {
3639 DCHECK(value.IsConstant()) << value;
3640 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
3641 __ jmp(slow_path->GetEntryLabel());
3642 }
3643 }
3644 break;
3645 }
3646 default:
3647 LOG(FATAL) << "Unexpected type for HDivZeroCheck" << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00003648 }
Calin Juravled0d48522014-11-04 16:40:20 +00003649}
3650
Calin Juravle9aec02f2014-11-18 23:06:35 +00003651void LocationsBuilderX86::HandleShift(HBinaryOperation* op) {
3652 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3653
3654 LocationSummary* locations =
3655 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
3656
3657 switch (op->GetResultType()) {
Mark P Mendell73945692015-04-29 14:56:17 +00003658 case Primitive::kPrimInt:
Calin Juravle9aec02f2014-11-18 23:06:35 +00003659 case Primitive::kPrimLong: {
Mark P Mendell73945692015-04-29 14:56:17 +00003660 // Can't have Location::Any() and output SameAsFirstInput()
Calin Juravle9aec02f2014-11-18 23:06:35 +00003661 locations->SetInAt(0, Location::RequiresRegister());
Mark P Mendell73945692015-04-29 14:56:17 +00003662 // The shift count needs to be in CL or a constant.
3663 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, op->InputAt(1)));
Calin Juravle9aec02f2014-11-18 23:06:35 +00003664 locations->SetOut(Location::SameAsFirstInput());
3665 break;
3666 }
3667 default:
3668 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
3669 }
3670}
3671
3672void InstructionCodeGeneratorX86::HandleShift(HBinaryOperation* op) {
3673 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3674
3675 LocationSummary* locations = op->GetLocations();
3676 Location first = locations->InAt(0);
3677 Location second = locations->InAt(1);
3678 DCHECK(first.Equals(locations->Out()));
3679
3680 switch (op->GetResultType()) {
3681 case Primitive::kPrimInt: {
Mark P Mendell73945692015-04-29 14:56:17 +00003682 DCHECK(first.IsRegister());
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003683 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003684 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003685 Register second_reg = second.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003686 DCHECK_EQ(ECX, second_reg);
3687 if (op->IsShl()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003688 __ shll(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003689 } else if (op->IsShr()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003690 __ sarl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003691 } else {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003692 __ shrl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003693 }
3694 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003695 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance;
Mark P Mendell73945692015-04-29 14:56:17 +00003696 if (shift == 0) {
3697 return;
3698 }
3699 Immediate imm(shift);
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003700 if (op->IsShl()) {
3701 __ shll(first_reg, imm);
3702 } else if (op->IsShr()) {
3703 __ sarl(first_reg, imm);
3704 } else {
3705 __ shrl(first_reg, imm);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003706 }
3707 }
3708 break;
3709 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003710 case Primitive::kPrimLong: {
Mark P Mendell73945692015-04-29 14:56:17 +00003711 if (second.IsRegister()) {
3712 Register second_reg = second.AsRegister<Register>();
3713 DCHECK_EQ(ECX, second_reg);
3714 if (op->IsShl()) {
3715 GenerateShlLong(first, second_reg);
3716 } else if (op->IsShr()) {
3717 GenerateShrLong(first, second_reg);
3718 } else {
3719 GenerateUShrLong(first, second_reg);
3720 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003721 } else {
Mark P Mendell73945692015-04-29 14:56:17 +00003722 // Shift by a constant.
Roland Levillain5b5b9312016-03-22 14:57:31 +00003723 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance;
Mark P Mendell73945692015-04-29 14:56:17 +00003724 // Nothing to do if the shift is 0, as the input is already the output.
3725 if (shift != 0) {
3726 if (op->IsShl()) {
3727 GenerateShlLong(first, shift);
3728 } else if (op->IsShr()) {
3729 GenerateShrLong(first, shift);
3730 } else {
3731 GenerateUShrLong(first, shift);
3732 }
3733 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003734 }
3735 break;
3736 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003737 default:
3738 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
3739 }
3740}
3741
Mark P Mendell73945692015-04-29 14:56:17 +00003742void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, int shift) {
3743 Register low = loc.AsRegisterPairLow<Register>();
3744 Register high = loc.AsRegisterPairHigh<Register>();
Mark Mendellba56d062015-05-05 21:34:03 -04003745 if (shift == 1) {
3746 // This is just an addition.
3747 __ addl(low, low);
3748 __ adcl(high, high);
3749 } else if (shift == 32) {
Mark P Mendell73945692015-04-29 14:56:17 +00003750 // Shift by 32 is easy. High gets low, and low gets 0.
3751 codegen_->EmitParallelMoves(
3752 loc.ToLow(),
3753 loc.ToHigh(),
3754 Primitive::kPrimInt,
3755 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
3756 loc.ToLow(),
3757 Primitive::kPrimInt);
3758 } else if (shift > 32) {
3759 // Low part becomes 0. High part is low part << (shift-32).
3760 __ movl(high, low);
3761 __ shll(high, Immediate(shift - 32));
3762 __ xorl(low, low);
3763 } else {
3764 // Between 1 and 31.
3765 __ shld(high, low, Immediate(shift));
3766 __ shll(low, Immediate(shift));
3767 }
3768}
3769
Calin Juravle9aec02f2014-11-18 23:06:35 +00003770void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04003771 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00003772 __ shld(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>(), shifter);
3773 __ shll(loc.AsRegisterPairLow<Register>(), shifter);
3774 __ testl(shifter, Immediate(32));
3775 __ j(kEqual, &done);
3776 __ movl(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>());
3777 __ movl(loc.AsRegisterPairLow<Register>(), Immediate(0));
3778 __ Bind(&done);
3779}
3780
Mark P Mendell73945692015-04-29 14:56:17 +00003781void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, int shift) {
3782 Register low = loc.AsRegisterPairLow<Register>();
3783 Register high = loc.AsRegisterPairHigh<Register>();
3784 if (shift == 32) {
3785 // Need to copy the sign.
3786 DCHECK_NE(low, high);
3787 __ movl(low, high);
3788 __ sarl(high, Immediate(31));
3789 } else if (shift > 32) {
3790 DCHECK_NE(low, high);
3791 // High part becomes sign. Low part is shifted by shift - 32.
3792 __ movl(low, high);
3793 __ sarl(high, Immediate(31));
3794 __ sarl(low, Immediate(shift - 32));
3795 } else {
3796 // Between 1 and 31.
3797 __ shrd(low, high, Immediate(shift));
3798 __ sarl(high, Immediate(shift));
3799 }
3800}
3801
Calin Juravle9aec02f2014-11-18 23:06:35 +00003802void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04003803 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00003804 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
3805 __ sarl(loc.AsRegisterPairHigh<Register>(), shifter);
3806 __ testl(shifter, Immediate(32));
3807 __ j(kEqual, &done);
3808 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
3809 __ sarl(loc.AsRegisterPairHigh<Register>(), Immediate(31));
3810 __ Bind(&done);
3811}
3812
Mark P Mendell73945692015-04-29 14:56:17 +00003813void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, int shift) {
3814 Register low = loc.AsRegisterPairLow<Register>();
3815 Register high = loc.AsRegisterPairHigh<Register>();
3816 if (shift == 32) {
3817 // Shift by 32 is easy. Low gets high, and high gets 0.
3818 codegen_->EmitParallelMoves(
3819 loc.ToHigh(),
3820 loc.ToLow(),
3821 Primitive::kPrimInt,
3822 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
3823 loc.ToHigh(),
3824 Primitive::kPrimInt);
3825 } else if (shift > 32) {
3826 // Low part is high >> (shift - 32). High part becomes 0.
3827 __ movl(low, high);
3828 __ shrl(low, Immediate(shift - 32));
3829 __ xorl(high, high);
3830 } else {
3831 // Between 1 and 31.
3832 __ shrd(low, high, Immediate(shift));
3833 __ shrl(high, Immediate(shift));
3834 }
3835}
3836
Calin Juravle9aec02f2014-11-18 23:06:35 +00003837void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04003838 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00003839 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
3840 __ shrl(loc.AsRegisterPairHigh<Register>(), shifter);
3841 __ testl(shifter, Immediate(32));
3842 __ j(kEqual, &done);
3843 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
3844 __ movl(loc.AsRegisterPairHigh<Register>(), Immediate(0));
3845 __ Bind(&done);
3846}
3847
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003848void LocationsBuilderX86::VisitRor(HRor* ror) {
3849 LocationSummary* locations =
3850 new (GetGraph()->GetArena()) LocationSummary(ror, LocationSummary::kNoCall);
3851
3852 switch (ror->GetResultType()) {
3853 case Primitive::kPrimLong:
3854 // Add the temporary needed.
3855 locations->AddTemp(Location::RequiresRegister());
3856 FALLTHROUGH_INTENDED;
3857 case Primitive::kPrimInt:
3858 locations->SetInAt(0, Location::RequiresRegister());
3859 // The shift count needs to be in CL (unless it is a constant).
3860 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, ror->InputAt(1)));
3861 locations->SetOut(Location::SameAsFirstInput());
3862 break;
3863 default:
3864 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
3865 UNREACHABLE();
3866 }
3867}
3868
3869void InstructionCodeGeneratorX86::VisitRor(HRor* ror) {
3870 LocationSummary* locations = ror->GetLocations();
3871 Location first = locations->InAt(0);
3872 Location second = locations->InAt(1);
3873
3874 if (ror->GetResultType() == Primitive::kPrimInt) {
3875 Register first_reg = first.AsRegister<Register>();
3876 if (second.IsRegister()) {
3877 Register second_reg = second.AsRegister<Register>();
3878 __ rorl(first_reg, second_reg);
3879 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003880 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003881 __ rorl(first_reg, imm);
3882 }
3883 return;
3884 }
3885
3886 DCHECK_EQ(ror->GetResultType(), Primitive::kPrimLong);
3887 Register first_reg_lo = first.AsRegisterPairLow<Register>();
3888 Register first_reg_hi = first.AsRegisterPairHigh<Register>();
3889 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
3890 if (second.IsRegister()) {
3891 Register second_reg = second.AsRegister<Register>();
3892 DCHECK_EQ(second_reg, ECX);
3893 __ movl(temp_reg, first_reg_hi);
3894 __ shrd(first_reg_hi, first_reg_lo, second_reg);
3895 __ shrd(first_reg_lo, temp_reg, second_reg);
3896 __ movl(temp_reg, first_reg_hi);
3897 __ testl(second_reg, Immediate(32));
3898 __ cmovl(kNotEqual, first_reg_hi, first_reg_lo);
3899 __ cmovl(kNotEqual, first_reg_lo, temp_reg);
3900 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003901 int32_t shift_amt = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance;
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003902 if (shift_amt == 0) {
3903 // Already fine.
3904 return;
3905 }
3906 if (shift_amt == 32) {
3907 // Just swap.
3908 __ movl(temp_reg, first_reg_lo);
3909 __ movl(first_reg_lo, first_reg_hi);
3910 __ movl(first_reg_hi, temp_reg);
3911 return;
3912 }
3913
3914 Immediate imm(shift_amt);
3915 // Save the constents of the low value.
3916 __ movl(temp_reg, first_reg_lo);
3917
3918 // Shift right into low, feeding bits from high.
3919 __ shrd(first_reg_lo, first_reg_hi, imm);
3920
3921 // Shift right into high, feeding bits from the original low.
3922 __ shrd(first_reg_hi, temp_reg, imm);
3923
3924 // Swap if needed.
3925 if (shift_amt > 32) {
3926 __ movl(temp_reg, first_reg_lo);
3927 __ movl(first_reg_lo, first_reg_hi);
3928 __ movl(first_reg_hi, temp_reg);
3929 }
3930 }
3931}
3932
Calin Juravle9aec02f2014-11-18 23:06:35 +00003933void LocationsBuilderX86::VisitShl(HShl* shl) {
3934 HandleShift(shl);
3935}
3936
3937void InstructionCodeGeneratorX86::VisitShl(HShl* shl) {
3938 HandleShift(shl);
3939}
3940
3941void LocationsBuilderX86::VisitShr(HShr* shr) {
3942 HandleShift(shr);
3943}
3944
3945void InstructionCodeGeneratorX86::VisitShr(HShr* shr) {
3946 HandleShift(shr);
3947}
3948
3949void LocationsBuilderX86::VisitUShr(HUShr* ushr) {
3950 HandleShift(ushr);
3951}
3952
3953void InstructionCodeGeneratorX86::VisitUShr(HUShr* ushr) {
3954 HandleShift(ushr);
3955}
3956
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003957void LocationsBuilderX86::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003958 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003959 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003960 locations->SetOut(Location::RegisterLocation(EAX));
David Brazdil6de19382016-01-08 17:37:10 +00003961 if (instruction->IsStringAlloc()) {
3962 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
3963 } else {
3964 InvokeRuntimeCallingConvention calling_convention;
3965 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3966 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3967 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003968}
3969
3970void InstructionCodeGeneratorX86::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01003971 // Note: if heap poisoning is enabled, the entry point takes cares
3972 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00003973 if (instruction->IsStringAlloc()) {
3974 // String is allocated through StringFactory. Call NewEmptyString entry point.
3975 Register temp = instruction->GetLocations()->GetTemp(0).AsRegister<Register>();
Andreas Gampe542451c2016-07-26 09:02:02 -07003976 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86PointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00003977 __ fs()->movl(temp, Address::Absolute(QUICK_ENTRY_POINT(pNewEmptyString)));
3978 __ call(Address(temp, code_offset.Int32Value()));
3979 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3980 } else {
Serban Constantinescuba45db02016-07-12 22:53:02 +01003981 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
David Brazdil6de19382016-01-08 17:37:10 +00003982 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
3983 DCHECK(!codegen_->IsLeafMethod());
3984 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003985}
3986
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003987void LocationsBuilderX86::VisitNewArray(HNewArray* instruction) {
3988 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003989 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003990 locations->SetOut(Location::RegisterLocation(EAX));
3991 InvokeRuntimeCallingConvention calling_convention;
3992 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003993 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003994 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003995}
3996
3997void InstructionCodeGeneratorX86::VisitNewArray(HNewArray* instruction) {
3998 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003999 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
Roland Levillain4d027112015-07-01 15:41:14 +01004000 // Note: if heap poisoning is enabled, the entry point takes cares
4001 // of poisoning the reference.
Serban Constantinescuba45db02016-07-12 22:53:02 +01004002 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00004003 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004004 DCHECK(!codegen_->IsLeafMethod());
4005}
4006
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004007void LocationsBuilderX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004008 LocationSummary* locations =
4009 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01004010 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
4011 if (location.IsStackSlot()) {
4012 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4013 } else if (location.IsDoubleStackSlot()) {
4014 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004015 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01004016 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004017}
4018
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004019void InstructionCodeGeneratorX86::VisitParameterValue(
4020 HParameterValue* instruction ATTRIBUTE_UNUSED) {
4021}
4022
4023void LocationsBuilderX86::VisitCurrentMethod(HCurrentMethod* instruction) {
4024 LocationSummary* locations =
4025 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4026 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
4027}
4028
4029void InstructionCodeGeneratorX86::VisitCurrentMethod(HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004030}
4031
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004032void LocationsBuilderX86::VisitClassTableGet(HClassTableGet* instruction) {
4033 LocationSummary* locations =
4034 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4035 locations->SetInAt(0, Location::RequiresRegister());
4036 locations->SetOut(Location::RequiresRegister());
4037}
4038
4039void InstructionCodeGeneratorX86::VisitClassTableGet(HClassTableGet* instruction) {
4040 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004041 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004042 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004043 instruction->GetIndex(), kX86PointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004044 __ movl(locations->Out().AsRegister<Register>(),
4045 Address(locations->InAt(0).AsRegister<Register>(), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004046 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004047 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00004048 instruction->GetIndex(), kX86PointerSize));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004049 __ movl(locations->Out().AsRegister<Register>(),
4050 Address(locations->InAt(0).AsRegister<Register>(),
4051 mirror::Class::ImtPtrOffset(kX86PointerSize).Uint32Value()));
4052 // temp = temp->GetImtEntryAt(method_offset);
4053 __ movl(locations->Out().AsRegister<Register>(),
4054 Address(locations->Out().AsRegister<Register>(), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004055 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004056}
4057
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004058void LocationsBuilderX86::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004059 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004060 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01004061 locations->SetInAt(0, Location::RequiresRegister());
4062 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004063}
4064
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004065void InstructionCodeGeneratorX86::VisitNot(HNot* not_) {
4066 LocationSummary* locations = not_->GetLocations();
Roland Levillain70566432014-10-24 16:20:17 +01004067 Location in = locations->InAt(0);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01004068 Location out = locations->Out();
Roland Levillain70566432014-10-24 16:20:17 +01004069 DCHECK(in.Equals(out));
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00004070 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004071 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00004072 __ notl(out.AsRegister<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004073 break;
4074
4075 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01004076 __ notl(out.AsRegisterPairLow<Register>());
4077 __ notl(out.AsRegisterPairHigh<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004078 break;
4079
4080 default:
4081 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
4082 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004083}
4084
David Brazdil66d126e2015-04-03 16:02:44 +01004085void LocationsBuilderX86::VisitBooleanNot(HBooleanNot* bool_not) {
4086 LocationSummary* locations =
4087 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
4088 locations->SetInAt(0, Location::RequiresRegister());
4089 locations->SetOut(Location::SameAsFirstInput());
4090}
4091
4092void InstructionCodeGeneratorX86::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01004093 LocationSummary* locations = bool_not->GetLocations();
4094 Location in = locations->InAt(0);
4095 Location out = locations->Out();
4096 DCHECK(in.Equals(out));
4097 __ xorl(out.AsRegister<Register>(), Immediate(1));
4098}
4099
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004100void LocationsBuilderX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004101 LocationSummary* locations =
4102 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00004103 switch (compare->InputAt(0)->GetType()) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00004104 case Primitive::kPrimBoolean:
4105 case Primitive::kPrimByte:
4106 case Primitive::kPrimShort:
4107 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08004108 case Primitive::kPrimInt:
Calin Juravleddb7df22014-11-25 20:56:51 +00004109 case Primitive::kPrimLong: {
4110 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravleddb7df22014-11-25 20:56:51 +00004111 locations->SetInAt(1, Location::Any());
4112 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4113 break;
4114 }
4115 case Primitive::kPrimFloat:
4116 case Primitive::kPrimDouble: {
4117 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004118 if (compare->InputAt(1)->IsX86LoadFromConstantTable()) {
4119 DCHECK(compare->InputAt(1)->IsEmittedAtUseSite());
4120 } else if (compare->InputAt(1)->IsConstant()) {
4121 locations->SetInAt(1, Location::RequiresFpuRegister());
4122 } else {
4123 locations->SetInAt(1, Location::Any());
4124 }
Calin Juravleddb7df22014-11-25 20:56:51 +00004125 locations->SetOut(Location::RequiresRegister());
4126 break;
4127 }
4128 default:
4129 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
4130 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004131}
4132
4133void InstructionCodeGeneratorX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004134 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004135 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00004136 Location left = locations->InAt(0);
4137 Location right = locations->InAt(1);
4138
Mark Mendell0c9497d2015-08-21 09:30:05 -04004139 NearLabel less, greater, done;
Aart Bika19616e2016-02-01 18:57:58 -08004140 Condition less_cond = kLess;
4141
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004142 switch (compare->InputAt(0)->GetType()) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00004143 case Primitive::kPrimBoolean:
4144 case Primitive::kPrimByte:
4145 case Primitive::kPrimShort:
4146 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08004147 case Primitive::kPrimInt: {
Roland Levillain0b671c02016-08-19 12:02:34 +01004148 codegen_->GenerateIntCompare(left, right);
Aart Bika19616e2016-02-01 18:57:58 -08004149 break;
4150 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004151 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004152 Register left_low = left.AsRegisterPairLow<Register>();
4153 Register left_high = left.AsRegisterPairHigh<Register>();
4154 int32_t val_low = 0;
4155 int32_t val_high = 0;
4156 bool right_is_const = false;
4157
4158 if (right.IsConstant()) {
4159 DCHECK(right.GetConstant()->IsLongConstant());
4160 right_is_const = true;
4161 int64_t val = right.GetConstant()->AsLongConstant()->GetValue();
4162 val_low = Low32Bits(val);
4163 val_high = High32Bits(val);
4164 }
4165
Calin Juravleddb7df22014-11-25 20:56:51 +00004166 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004167 __ cmpl(left_high, right.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004168 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004169 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004170 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004171 DCHECK(right_is_const) << right;
Aart Bika19616e2016-02-01 18:57:58 -08004172 codegen_->Compare32BitValue(left_high, val_high);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004173 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004174 __ j(kLess, &less); // Signed compare.
4175 __ j(kGreater, &greater); // Signed compare.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004176 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004177 __ cmpl(left_low, right.AsRegisterPairLow<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004178 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004179 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004180 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004181 DCHECK(right_is_const) << right;
Aart Bika19616e2016-02-01 18:57:58 -08004182 codegen_->Compare32BitValue(left_low, val_low);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004183 }
Aart Bika19616e2016-02-01 18:57:58 -08004184 less_cond = kBelow; // for CF (unsigned).
Calin Juravleddb7df22014-11-25 20:56:51 +00004185 break;
4186 }
4187 case Primitive::kPrimFloat: {
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004188 GenerateFPCompare(left, right, compare, false);
Calin Juravleddb7df22014-11-25 20:56:51 +00004189 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08004190 less_cond = kBelow; // for CF (floats).
Calin Juravleddb7df22014-11-25 20:56:51 +00004191 break;
4192 }
4193 case Primitive::kPrimDouble: {
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004194 GenerateFPCompare(left, right, compare, true);
Calin Juravleddb7df22014-11-25 20:56:51 +00004195 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08004196 less_cond = kBelow; // for CF (floats).
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004197 break;
4198 }
4199 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00004200 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004201 }
Aart Bika19616e2016-02-01 18:57:58 -08004202
Calin Juravleddb7df22014-11-25 20:56:51 +00004203 __ movl(out, Immediate(0));
4204 __ j(kEqual, &done);
Aart Bika19616e2016-02-01 18:57:58 -08004205 __ j(less_cond, &less);
Calin Juravleddb7df22014-11-25 20:56:51 +00004206
4207 __ Bind(&greater);
4208 __ movl(out, Immediate(1));
4209 __ jmp(&done);
4210
4211 __ Bind(&less);
4212 __ movl(out, Immediate(-1));
4213
4214 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004215}
4216
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004217void LocationsBuilderX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004218 LocationSummary* locations =
4219 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko372f10e2016-05-17 16:30:10 +01004220 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01004221 locations->SetInAt(i, Location::Any());
4222 }
4223 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004224}
4225
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004226void InstructionCodeGeneratorX86::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004227 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004228}
4229
Roland Levillain7c1559a2015-12-15 10:55:36 +00004230void CodeGeneratorX86::GenerateMemoryBarrier(MemBarrierKind kind) {
Calin Juravle52c48962014-12-16 17:02:57 +00004231 /*
4232 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
4233 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
4234 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
4235 */
4236 switch (kind) {
4237 case MemBarrierKind::kAnyAny: {
Mark P Mendell17077d82015-12-16 19:15:59 +00004238 MemoryFence();
Calin Juravle52c48962014-12-16 17:02:57 +00004239 break;
4240 }
4241 case MemBarrierKind::kAnyStore:
4242 case MemBarrierKind::kLoadAny:
4243 case MemBarrierKind::kStoreStore: {
4244 // nop
4245 break;
4246 }
Mark Mendell7aa04a12016-01-27 22:39:07 -05004247 case MemBarrierKind::kNTStoreStore:
4248 // Non-Temporal Store/Store needs an explicit fence.
4249 MemoryFence(/* non-temporal */ true);
4250 break;
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004251 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004252}
4253
Vladimir Markodc151b22015-10-15 18:02:30 +01004254HInvokeStaticOrDirect::DispatchInfo CodeGeneratorX86::GetSupportedInvokeStaticOrDirectDispatch(
4255 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004256 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00004257 HInvokeStaticOrDirect::DispatchInfo dispatch_info = desired_dispatch_info;
4258
4259 // We disable pc-relative load when there is an irreducible loop, as the optimization
4260 // is incompatible with it.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004261 // TODO: Create as many X86ComputeBaseMethodAddress instructions
4262 // as needed for methods with irreducible loops.
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00004263 if (GetGraph()->HasIrreducibleLoops() &&
4264 (dispatch_info.method_load_kind ==
4265 HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative)) {
4266 dispatch_info.method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod;
4267 }
4268 switch (dispatch_info.code_ptr_location) {
Vladimir Markodc151b22015-10-15 18:02:30 +01004269 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
4270 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
4271 // For direct code, we actually prefer to call via the code pointer from ArtMethod*.
4272 // (Though the direct CALL ptr16:32 is available for consideration).
4273 return HInvokeStaticOrDirect::DispatchInfo {
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00004274 dispatch_info.method_load_kind,
Vladimir Markodc151b22015-10-15 18:02:30 +01004275 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00004276 dispatch_info.method_load_data,
Vladimir Markodc151b22015-10-15 18:02:30 +01004277 0u
4278 };
4279 default:
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00004280 return dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01004281 }
4282}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004283
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004284Register CodeGeneratorX86::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
4285 Register temp) {
4286 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
Vladimir Markoc53c0792015-11-19 15:48:33 +00004287 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004288 if (!invoke->GetLocations()->Intrinsified()) {
4289 return location.AsRegister<Register>();
4290 }
4291 // For intrinsics we allow any location, so it may be on the stack.
4292 if (!location.IsRegister()) {
4293 __ movl(temp, Address(ESP, location.GetStackIndex()));
4294 return temp;
4295 }
4296 // For register locations, check if the register was saved. If so, get it from the stack.
4297 // Note: There is a chance that the register was saved but not overwritten, so we could
4298 // save one load. However, since this is just an intrinsic slow path we prefer this
4299 // simple and more robust approach rather that trying to determine if that's the case.
4300 SlowPathCode* slow_path = GetCurrentSlowPath();
Serguei Katkov288c7a82016-05-16 11:53:15 +06004301 if (slow_path != nullptr) {
4302 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
4303 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
4304 __ movl(temp, Address(ESP, stack_offset));
4305 return temp;
4306 }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004307 }
4308 return location.AsRegister<Register>();
4309}
4310
Serguei Katkov288c7a82016-05-16 11:53:15 +06004311Location CodeGeneratorX86::GenerateCalleeMethodStaticOrDirectCall(HInvokeStaticOrDirect* invoke,
4312 Location temp) {
Vladimir Marko58155012015-08-19 12:49:41 +00004313 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
4314 switch (invoke->GetMethodLoadKind()) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004315 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
Vladimir Marko58155012015-08-19 12:49:41 +00004316 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004317 uint32_t offset =
4318 GetThreadOffset<kX86PointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
4319 __ fs()->movl(temp.AsRegister<Register>(), Address::Absolute(offset));
Vladimir Marko58155012015-08-19 12:49:41 +00004320 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004321 }
Vladimir Marko58155012015-08-19 12:49:41 +00004322 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00004323 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004324 break;
4325 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
4326 __ movl(temp.AsRegister<Register>(), Immediate(invoke->GetMethodAddress()));
4327 break;
4328 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004329 __ movl(temp.AsRegister<Register>(), Immediate(/* placeholder */ 0));
Vladimir Marko63dccbbe2016-09-21 13:51:10 +01004330 method_patches_.emplace_back(*invoke->GetTargetMethod().dex_file,
4331 invoke->GetTargetMethod().dex_method_index);
Vladimir Marko58155012015-08-19 12:49:41 +00004332 __ Bind(&method_patches_.back().label); // Bind the label at the end of the "movl" insn.
4333 break;
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004334 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
4335 Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
4336 temp.AsRegister<Register>());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004337 __ movl(temp.AsRegister<Register>(), Address(base_reg, kDummy32BitOffset));
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004338 // Bind a new fixup label at the end of the "movl" insn.
4339 uint32_t offset = invoke->GetDexCacheArrayOffset();
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004340 __ Bind(NewPcRelativeDexCacheArrayPatch(invoke->GetDexFile(), offset));
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004341 break;
4342 }
Vladimir Marko58155012015-08-19 12:49:41 +00004343 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004344 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004345 Register method_reg;
4346 Register reg = temp.AsRegister<Register>();
4347 if (current_method.IsRegister()) {
4348 method_reg = current_method.AsRegister<Register>();
4349 } else {
David Brazdil58282f42016-01-14 12:45:10 +00004350 DCHECK(invoke->GetLocations()->Intrinsified());
Vladimir Marko58155012015-08-19 12:49:41 +00004351 DCHECK(!current_method.IsValid());
4352 method_reg = reg;
4353 __ movl(reg, Address(ESP, kCurrentMethodStackOffset));
4354 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00004355 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +01004356 __ movl(reg, Address(method_reg,
4357 ArtMethod::DexCacheResolvedMethodsOffset(kX86PointerSize).Int32Value()));
Vladimir Marko40ecb122016-04-06 17:33:41 +01004358 // temp = temp[index_in_cache];
4359 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
4360 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Vladimir Marko58155012015-08-19 12:49:41 +00004361 __ movl(reg, Address(reg, CodeGenerator::GetCachePointerOffset(index_in_cache)));
4362 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +01004363 }
Vladimir Marko58155012015-08-19 12:49:41 +00004364 }
Serguei Katkov288c7a82016-05-16 11:53:15 +06004365 return callee_method;
4366}
4367
4368void CodeGeneratorX86::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
4369 Location callee_method = GenerateCalleeMethodStaticOrDirectCall(invoke, temp);
Vladimir Marko58155012015-08-19 12:49:41 +00004370
4371 switch (invoke->GetCodePtrLocation()) {
4372 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
4373 __ call(GetFrameEntryLabel());
4374 break;
4375 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative: {
Vladimir Marko63dccbbe2016-09-21 13:51:10 +01004376 relative_call_patches_.emplace_back(*invoke->GetTargetMethod().dex_file,
4377 invoke->GetTargetMethod().dex_method_index);
Vladimir Marko58155012015-08-19 12:49:41 +00004378 Label* label = &relative_call_patches_.back().label;
4379 __ call(label); // Bind to the patch label, override at link time.
4380 __ Bind(label); // Bind the label at the end of the "call" insn.
4381 break;
4382 }
4383 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
4384 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
Vladimir Markodc151b22015-10-15 18:02:30 +01004385 // Filtered out by GetSupportedInvokeStaticOrDirectDispatch().
4386 LOG(FATAL) << "Unsupported";
4387 UNREACHABLE();
Vladimir Marko58155012015-08-19 12:49:41 +00004388 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
4389 // (callee_method + offset_of_quick_compiled_code)()
4390 __ call(Address(callee_method.AsRegister<Register>(),
4391 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07004392 kX86PointerSize).Int32Value()));
Vladimir Marko58155012015-08-19 12:49:41 +00004393 break;
Mark Mendell09ed1a32015-03-25 08:30:06 -04004394 }
4395
4396 DCHECK(!IsLeafMethod());
Mark Mendell09ed1a32015-03-25 08:30:06 -04004397}
4398
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004399void CodeGeneratorX86::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_in) {
4400 Register temp = temp_in.AsRegister<Register>();
4401 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
4402 invoke->GetVTableIndex(), kX86PointerSize).Uint32Value();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004403
4404 // Use the calling convention instead of the location of the receiver, as
4405 // intrinsics may have put the receiver in a different register. In the intrinsics
4406 // slow path, the arguments have been moved to the right place, so here we are
4407 // guaranteed that the receiver is the first register of the calling convention.
4408 InvokeDexCallingConvention calling_convention;
4409 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004410 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004411 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004412 __ movl(temp, Address(receiver, class_offset));
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004413 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00004414 // Instead of simply (possibly) unpoisoning `temp` here, we should
4415 // emit a read barrier for the previous class reference load.
4416 // However this is not required in practice, as this is an
4417 // intermediate/temporary reference and because the current
4418 // concurrent copying collector keeps the from-space memory
4419 // intact/accessible until the end of the marking phase (the
4420 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004421 __ MaybeUnpoisonHeapReference(temp);
4422 // temp = temp->GetMethodAt(method_offset);
4423 __ movl(temp, Address(temp, method_offset));
4424 // call temp->GetEntryPoint();
4425 __ call(Address(
Andreas Gampe542451c2016-07-26 09:02:02 -07004426 temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86PointerSize).Int32Value()));
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004427}
4428
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004429void CodeGeneratorX86::RecordSimplePatch() {
4430 if (GetCompilerOptions().GetIncludePatchInformation()) {
4431 simple_patches_.emplace_back();
4432 __ Bind(&simple_patches_.back());
4433 }
4434}
4435
Vladimir Marko63dccbbe2016-09-21 13:51:10 +01004436void CodeGeneratorX86::RecordBootStringPatch(HLoadString* load_string) {
4437 DCHECK(GetCompilerOptions().IsBootImage());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004438 string_patches_.emplace_back(load_string->GetDexFile(), load_string->GetStringIndex());
4439 __ Bind(&string_patches_.back().label);
4440}
4441
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004442void CodeGeneratorX86::RecordTypePatch(HLoadClass* load_class) {
4443 type_patches_.emplace_back(load_class->GetDexFile(), load_class->GetTypeIndex());
4444 __ Bind(&type_patches_.back().label);
4445}
4446
Vladimir Marko63dccbbe2016-09-21 13:51:10 +01004447Label* CodeGeneratorX86::NewStringBssEntryPatch(HLoadString* load_string) {
4448 DCHECK(!GetCompilerOptions().IsBootImage());
4449 string_patches_.emplace_back(load_string->GetDexFile(), load_string->GetStringIndex());
4450 return &string_patches_.back().label;
4451}
4452
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004453Label* CodeGeneratorX86::NewPcRelativeDexCacheArrayPatch(const DexFile& dex_file,
4454 uint32_t element_offset) {
4455 // Add the patch entry and bind its label at the end of the instruction.
4456 pc_relative_dex_cache_patches_.emplace_back(dex_file, element_offset);
4457 return &pc_relative_dex_cache_patches_.back().label;
4458}
4459
Vladimir Marko63dccbbe2016-09-21 13:51:10 +01004460// The label points to the end of the "movl" or another instruction but the literal offset
4461// for method patch needs to point to the embedded constant which occupies the last 4 bytes.
4462constexpr uint32_t kLabelPositionToLiteralOffsetAdjustment = 4u;
4463
4464template <LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
4465inline void CodeGeneratorX86::EmitPcRelativeLinkerPatches(
4466 const ArenaDeque<PatchInfo<Label>>& infos,
4467 ArenaVector<LinkerPatch>* linker_patches) {
4468 for (const PatchInfo<Label>& info : infos) {
4469 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
4470 linker_patches->push_back(
4471 Factory(literal_offset, &info.dex_file, GetMethodAddressOffset(), info.index));
4472 }
4473}
4474
Vladimir Marko58155012015-08-19 12:49:41 +00004475void CodeGeneratorX86::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
4476 DCHECK(linker_patches->empty());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004477 size_t size =
4478 method_patches_.size() +
4479 relative_call_patches_.size() +
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004480 pc_relative_dex_cache_patches_.size() +
4481 simple_patches_.size() +
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004482 string_patches_.size() +
4483 type_patches_.size();
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004484 linker_patches->reserve(size);
Vladimir Marko63dccbbe2016-09-21 13:51:10 +01004485 for (const PatchInfo<Label>& info : method_patches_) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004486 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Marko63dccbbe2016-09-21 13:51:10 +01004487 linker_patches->push_back(LinkerPatch::MethodPatch(literal_offset, &info.dex_file, info.index));
Vladimir Marko58155012015-08-19 12:49:41 +00004488 }
Vladimir Marko63dccbbe2016-09-21 13:51:10 +01004489 for (const PatchInfo<Label>& info : relative_call_patches_) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004490 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Marko63dccbbe2016-09-21 13:51:10 +01004491 linker_patches->push_back(
4492 LinkerPatch::RelativeCodePatch(literal_offset, &info.dex_file, info.index));
Vladimir Marko58155012015-08-19 12:49:41 +00004493 }
Vladimir Marko63dccbbe2016-09-21 13:51:10 +01004494 EmitPcRelativeLinkerPatches<LinkerPatch::DexCacheArrayPatch>(pc_relative_dex_cache_patches_,
4495 linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004496 for (const Label& label : simple_patches_) {
4497 uint32_t literal_offset = label.Position() - kLabelPositionToLiteralOffsetAdjustment;
4498 linker_patches->push_back(LinkerPatch::RecordPosition(literal_offset));
4499 }
Vladimir Marko63dccbbe2016-09-21 13:51:10 +01004500 if (!GetCompilerOptions().IsBootImage()) {
4501 EmitPcRelativeLinkerPatches<LinkerPatch::StringBssEntryPatch>(string_patches_, linker_patches);
4502 } else if (GetCompilerOptions().GetCompilePic()) {
4503 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeStringPatch>(string_patches_, linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004504 } else {
Vladimir Marko63dccbbe2016-09-21 13:51:10 +01004505 for (const PatchInfo<Label>& info : string_patches_) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004506 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Marko63dccbbe2016-09-21 13:51:10 +01004507 linker_patches->push_back(
4508 LinkerPatch::StringPatch(literal_offset, &info.dex_file, info.index));
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004509 }
Vladimir Marko63dccbbe2016-09-21 13:51:10 +01004510 }
4511 if (GetCompilerOptions().GetCompilePic()) {
4512 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeTypePatch>(type_patches_, linker_patches);
4513 } else {
4514 for (const PatchInfo<Label>& info : type_patches_) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004515 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Marko63dccbbe2016-09-21 13:51:10 +01004516 linker_patches->push_back(LinkerPatch::TypePatch(literal_offset, &info.dex_file, info.index));
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004517 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004518 }
Vladimir Marko58155012015-08-19 12:49:41 +00004519}
4520
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004521void CodeGeneratorX86::MarkGCCard(Register temp,
4522 Register card,
4523 Register object,
4524 Register value,
4525 bool value_can_be_null) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004526 NearLabel is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004527 if (value_can_be_null) {
4528 __ testl(value, value);
4529 __ j(kEqual, &is_null);
4530 }
Andreas Gampe542451c2016-07-26 09:02:02 -07004531 __ fs()->movl(card, Address::Absolute(Thread::CardTableOffset<kX86PointerSize>().Int32Value()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004532 __ movl(temp, object);
4533 __ shrl(temp, Immediate(gc::accounting::CardTable::kCardShift));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00004534 __ movb(Address(temp, card, TIMES_1, 0),
4535 X86ManagedRegister::FromCpuRegister(card).AsByteRegister());
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004536 if (value_can_be_null) {
4537 __ Bind(&is_null);
4538 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004539}
4540
Calin Juravle52c48962014-12-16 17:02:57 +00004541void LocationsBuilderX86::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
4542 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain0d5a2812015-11-13 10:07:31 +00004543
4544 bool object_field_get_with_read_barrier =
4545 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004546 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00004547 new (GetGraph()->GetArena()) LocationSummary(instruction,
4548 kEmitCompilerReadBarrier ?
4549 LocationSummary::kCallOnSlowPath :
4550 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01004551 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004552 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01004553 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004554 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004555
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004556 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4557 locations->SetOut(Location::RequiresFpuRegister());
4558 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004559 // The output overlaps in case of long: we don't want the low move
4560 // to overwrite the object's location. Likewise, in the case of
4561 // an object field get with read barriers enabled, we do not want
4562 // the move to overwrite the object's location, as we need it to emit
4563 // the read barrier.
4564 locations->SetOut(
4565 Location::RequiresRegister(),
4566 (object_field_get_with_read_barrier || instruction->GetType() == Primitive::kPrimLong) ?
4567 Location::kOutputOverlap :
4568 Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004569 }
Calin Juravle52c48962014-12-16 17:02:57 +00004570
4571 if (field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong)) {
4572 // Long values can be loaded atomically into an XMM using movsd.
Roland Levillain7c1559a2015-12-15 10:55:36 +00004573 // So we use an XMM register as a temp to achieve atomicity (first
4574 // load the temp into the XMM and then copy the XMM into the
4575 // output, 32 bits at a time).
Calin Juravle52c48962014-12-16 17:02:57 +00004576 locations->AddTemp(Location::RequiresFpuRegister());
4577 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004578}
4579
Calin Juravle52c48962014-12-16 17:02:57 +00004580void InstructionCodeGeneratorX86::HandleFieldGet(HInstruction* instruction,
4581 const FieldInfo& field_info) {
4582 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004583
Calin Juravle52c48962014-12-16 17:02:57 +00004584 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004585 Location base_loc = locations->InAt(0);
4586 Register base = base_loc.AsRegister<Register>();
Calin Juravle52c48962014-12-16 17:02:57 +00004587 Location out = locations->Out();
4588 bool is_volatile = field_info.IsVolatile();
4589 Primitive::Type field_type = field_info.GetFieldType();
4590 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4591
4592 switch (field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004593 case Primitive::kPrimBoolean: {
Calin Juravle52c48962014-12-16 17:02:57 +00004594 __ movzxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004595 break;
4596 }
4597
4598 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00004599 __ movsxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004600 break;
4601 }
4602
4603 case Primitive::kPrimShort: {
Calin Juravle52c48962014-12-16 17:02:57 +00004604 __ movsxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004605 break;
4606 }
4607
4608 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00004609 __ movzxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004610 break;
4611 }
4612
4613 case Primitive::kPrimInt:
Calin Juravle52c48962014-12-16 17:02:57 +00004614 __ movl(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004615 break;
Roland Levillain7c1559a2015-12-15 10:55:36 +00004616
4617 case Primitive::kPrimNot: {
4618 // /* HeapReference<Object> */ out = *(base + offset)
4619 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004620 // Note that a potential implicit null check is handled in this
4621 // CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier call.
4622 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00004623 instruction, out, base, offset, /* needs_null_check */ true);
Roland Levillain7c1559a2015-12-15 10:55:36 +00004624 if (is_volatile) {
4625 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4626 }
4627 } else {
4628 __ movl(out.AsRegister<Register>(), Address(base, offset));
4629 codegen_->MaybeRecordImplicitNullCheck(instruction);
4630 if (is_volatile) {
4631 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4632 }
4633 // If read barriers are enabled, emit read barriers other than
4634 // Baker's using a slow path (and also unpoison the loaded
4635 // reference, if heap poisoning is enabled).
4636 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
4637 }
4638 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004639 }
4640
4641 case Primitive::kPrimLong: {
Calin Juravle52c48962014-12-16 17:02:57 +00004642 if (is_volatile) {
4643 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4644 __ movsd(temp, Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004645 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004646 __ movd(out.AsRegisterPairLow<Register>(), temp);
4647 __ psrlq(temp, Immediate(32));
4648 __ movd(out.AsRegisterPairHigh<Register>(), temp);
4649 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004650 DCHECK_NE(base, out.AsRegisterPairLow<Register>());
Calin Juravle52c48962014-12-16 17:02:57 +00004651 __ movl(out.AsRegisterPairLow<Register>(), Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004652 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004653 __ movl(out.AsRegisterPairHigh<Register>(), Address(base, kX86WordSize + offset));
4654 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004655 break;
4656 }
4657
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004658 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00004659 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004660 break;
4661 }
4662
4663 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00004664 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004665 break;
4666 }
4667
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004668 case Primitive::kPrimVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00004669 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004670 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004671 }
Calin Juravle52c48962014-12-16 17:02:57 +00004672
Roland Levillain7c1559a2015-12-15 10:55:36 +00004673 if (field_type == Primitive::kPrimNot || field_type == Primitive::kPrimLong) {
4674 // Potential implicit null checks, in the case of reference or
4675 // long fields, are handled in the previous switch statement.
4676 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00004677 codegen_->MaybeRecordImplicitNullCheck(instruction);
4678 }
4679
Calin Juravle52c48962014-12-16 17:02:57 +00004680 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004681 if (field_type == Primitive::kPrimNot) {
4682 // Memory barriers, in the case of references, are also handled
4683 // in the previous switch statement.
4684 } else {
4685 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4686 }
Roland Levillain4d027112015-07-01 15:41:14 +01004687 }
Calin Juravle52c48962014-12-16 17:02:57 +00004688}
4689
4690void LocationsBuilderX86::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
4691 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4692
4693 LocationSummary* locations =
4694 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4695 locations->SetInAt(0, Location::RequiresRegister());
4696 bool is_volatile = field_info.IsVolatile();
4697 Primitive::Type field_type = field_info.GetFieldType();
4698 bool is_byte_type = (field_type == Primitive::kPrimBoolean)
4699 || (field_type == Primitive::kPrimByte);
4700
4701 // The register allocator does not support multiple
4702 // inputs that die at entry with one in a specific register.
4703 if (is_byte_type) {
4704 // Ensure the value is in a byte register.
4705 locations->SetInAt(1, Location::RegisterLocation(EAX));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004706 } else if (Primitive::IsFloatingPointType(field_type)) {
Mark Mendell81489372015-11-04 11:30:41 -05004707 if (is_volatile && field_type == Primitive::kPrimDouble) {
4708 // In order to satisfy the semantics of volatile, this must be a single instruction store.
4709 locations->SetInAt(1, Location::RequiresFpuRegister());
4710 } else {
4711 locations->SetInAt(1, Location::FpuRegisterOrConstant(instruction->InputAt(1)));
4712 }
4713 } else if (is_volatile && field_type == Primitive::kPrimLong) {
4714 // In order to satisfy the semantics of volatile, this must be a single instruction store.
Calin Juravle52c48962014-12-16 17:02:57 +00004715 locations->SetInAt(1, Location::RequiresRegister());
Mark Mendell81489372015-11-04 11:30:41 -05004716
Calin Juravle52c48962014-12-16 17:02:57 +00004717 // 64bits value can be atomically written to an address with movsd and an XMM register.
4718 // We need two XMM registers because there's no easier way to (bit) copy a register pair
4719 // into a single XMM register (we copy each pair part into the XMMs and then interleave them).
4720 // NB: We could make the register allocator understand fp_reg <-> core_reg moves but given the
4721 // isolated cases when we need this it isn't worth adding the extra complexity.
4722 locations->AddTemp(Location::RequiresFpuRegister());
4723 locations->AddTemp(Location::RequiresFpuRegister());
Mark Mendell81489372015-11-04 11:30:41 -05004724 } else {
4725 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4726
4727 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
4728 // Temporary registers for the write barrier.
4729 locations->AddTemp(Location::RequiresRegister()); // May be used for reference poisoning too.
4730 // Ensure the card is in a byte register.
4731 locations->AddTemp(Location::RegisterLocation(ECX));
4732 }
Calin Juravle52c48962014-12-16 17:02:57 +00004733 }
4734}
4735
4736void InstructionCodeGeneratorX86::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004737 const FieldInfo& field_info,
4738 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00004739 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4740
4741 LocationSummary* locations = instruction->GetLocations();
4742 Register base = locations->InAt(0).AsRegister<Register>();
4743 Location value = locations->InAt(1);
4744 bool is_volatile = field_info.IsVolatile();
4745 Primitive::Type field_type = field_info.GetFieldType();
4746 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01004747 bool needs_write_barrier =
4748 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00004749
4750 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004751 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
Calin Juravle52c48962014-12-16 17:02:57 +00004752 }
4753
Mark Mendell81489372015-11-04 11:30:41 -05004754 bool maybe_record_implicit_null_check_done = false;
4755
Calin Juravle52c48962014-12-16 17:02:57 +00004756 switch (field_type) {
4757 case Primitive::kPrimBoolean:
4758 case Primitive::kPrimByte: {
4759 __ movb(Address(base, offset), value.AsRegister<ByteRegister>());
4760 break;
4761 }
4762
4763 case Primitive::kPrimShort:
4764 case Primitive::kPrimChar: {
Mark Mendell81489372015-11-04 11:30:41 -05004765 if (value.IsConstant()) {
4766 int16_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4767 __ movw(Address(base, offset), Immediate(v));
4768 } else {
4769 __ movw(Address(base, offset), value.AsRegister<Register>());
4770 }
Calin Juravle52c48962014-12-16 17:02:57 +00004771 break;
4772 }
4773
4774 case Primitive::kPrimInt:
4775 case Primitive::kPrimNot: {
Roland Levillain4d027112015-07-01 15:41:14 +01004776 if (kPoisonHeapReferences && needs_write_barrier) {
4777 // Note that in the case where `value` is a null reference,
4778 // we do not enter this block, as the reference does not
4779 // need poisoning.
4780 DCHECK_EQ(field_type, Primitive::kPrimNot);
4781 Register temp = locations->GetTemp(0).AsRegister<Register>();
4782 __ movl(temp, value.AsRegister<Register>());
4783 __ PoisonHeapReference(temp);
4784 __ movl(Address(base, offset), temp);
Mark Mendell81489372015-11-04 11:30:41 -05004785 } else if (value.IsConstant()) {
4786 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4787 __ movl(Address(base, offset), Immediate(v));
Roland Levillain4d027112015-07-01 15:41:14 +01004788 } else {
Nicolas Geoffray03971632016-03-17 10:44:24 +00004789 DCHECK(value.IsRegister()) << value;
Roland Levillain4d027112015-07-01 15:41:14 +01004790 __ movl(Address(base, offset), value.AsRegister<Register>());
4791 }
Calin Juravle52c48962014-12-16 17:02:57 +00004792 break;
4793 }
4794
4795 case Primitive::kPrimLong: {
4796 if (is_volatile) {
4797 XmmRegister temp1 = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4798 XmmRegister temp2 = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
4799 __ movd(temp1, value.AsRegisterPairLow<Register>());
4800 __ movd(temp2, value.AsRegisterPairHigh<Register>());
4801 __ punpckldq(temp1, temp2);
4802 __ movsd(Address(base, offset), temp1);
Calin Juravle77520bc2015-01-12 18:45:46 +00004803 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell81489372015-11-04 11:30:41 -05004804 } else if (value.IsConstant()) {
4805 int64_t v = CodeGenerator::GetInt64ValueOf(value.GetConstant());
4806 __ movl(Address(base, offset), Immediate(Low32Bits(v)));
4807 codegen_->MaybeRecordImplicitNullCheck(instruction);
4808 __ movl(Address(base, kX86WordSize + offset), Immediate(High32Bits(v)));
Calin Juravle52c48962014-12-16 17:02:57 +00004809 } else {
4810 __ movl(Address(base, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00004811 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004812 __ movl(Address(base, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
4813 }
Mark Mendell81489372015-11-04 11:30:41 -05004814 maybe_record_implicit_null_check_done = true;
Calin Juravle52c48962014-12-16 17:02:57 +00004815 break;
4816 }
4817
4818 case Primitive::kPrimFloat: {
Mark Mendell81489372015-11-04 11:30:41 -05004819 if (value.IsConstant()) {
4820 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4821 __ movl(Address(base, offset), Immediate(v));
4822 } else {
4823 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4824 }
Calin Juravle52c48962014-12-16 17:02:57 +00004825 break;
4826 }
4827
4828 case Primitive::kPrimDouble: {
Mark Mendell81489372015-11-04 11:30:41 -05004829 if (value.IsConstant()) {
4830 int64_t v = CodeGenerator::GetInt64ValueOf(value.GetConstant());
4831 __ movl(Address(base, offset), Immediate(Low32Bits(v)));
4832 codegen_->MaybeRecordImplicitNullCheck(instruction);
4833 __ movl(Address(base, kX86WordSize + offset), Immediate(High32Bits(v)));
4834 maybe_record_implicit_null_check_done = true;
4835 } else {
4836 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4837 }
Calin Juravle52c48962014-12-16 17:02:57 +00004838 break;
4839 }
4840
4841 case Primitive::kPrimVoid:
4842 LOG(FATAL) << "Unreachable type " << field_type;
4843 UNREACHABLE();
4844 }
4845
Mark Mendell81489372015-11-04 11:30:41 -05004846 if (!maybe_record_implicit_null_check_done) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004847 codegen_->MaybeRecordImplicitNullCheck(instruction);
4848 }
4849
Roland Levillain4d027112015-07-01 15:41:14 +01004850 if (needs_write_barrier) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004851 Register temp = locations->GetTemp(0).AsRegister<Register>();
4852 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004853 codegen_->MarkGCCard(temp, card, base, value.AsRegister<Register>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00004854 }
4855
Calin Juravle52c48962014-12-16 17:02:57 +00004856 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004857 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
Calin Juravle52c48962014-12-16 17:02:57 +00004858 }
4859}
4860
4861void LocationsBuilderX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4862 HandleFieldGet(instruction, instruction->GetFieldInfo());
4863}
4864
4865void InstructionCodeGeneratorX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4866 HandleFieldGet(instruction, instruction->GetFieldInfo());
4867}
4868
4869void LocationsBuilderX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
4870 HandleFieldSet(instruction, instruction->GetFieldInfo());
4871}
4872
4873void InstructionCodeGeneratorX86::VisitStaticFieldSet(HStaticFieldSet* 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::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
4878 HandleFieldSet(instruction, instruction->GetFieldInfo());
4879}
4880
4881void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004882 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00004883}
4884
4885void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4886 HandleFieldGet(instruction, instruction->GetFieldInfo());
4887}
4888
4889void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4890 HandleFieldGet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004891}
4892
Calin Juravlee460d1d2015-09-29 04:52:17 +01004893void LocationsBuilderX86::VisitUnresolvedInstanceFieldGet(
4894 HUnresolvedInstanceFieldGet* instruction) {
4895 FieldAccessCallingConventionX86 calling_convention;
4896 codegen_->CreateUnresolvedFieldLocationSummary(
4897 instruction, instruction->GetFieldType(), calling_convention);
4898}
4899
4900void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldGet(
4901 HUnresolvedInstanceFieldGet* instruction) {
4902 FieldAccessCallingConventionX86 calling_convention;
4903 codegen_->GenerateUnresolvedFieldAccess(instruction,
4904 instruction->GetFieldType(),
4905 instruction->GetFieldIndex(),
4906 instruction->GetDexPc(),
4907 calling_convention);
4908}
4909
4910void LocationsBuilderX86::VisitUnresolvedInstanceFieldSet(
4911 HUnresolvedInstanceFieldSet* instruction) {
4912 FieldAccessCallingConventionX86 calling_convention;
4913 codegen_->CreateUnresolvedFieldLocationSummary(
4914 instruction, instruction->GetFieldType(), calling_convention);
4915}
4916
4917void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldSet(
4918 HUnresolvedInstanceFieldSet* instruction) {
4919 FieldAccessCallingConventionX86 calling_convention;
4920 codegen_->GenerateUnresolvedFieldAccess(instruction,
4921 instruction->GetFieldType(),
4922 instruction->GetFieldIndex(),
4923 instruction->GetDexPc(),
4924 calling_convention);
4925}
4926
4927void LocationsBuilderX86::VisitUnresolvedStaticFieldGet(
4928 HUnresolvedStaticFieldGet* instruction) {
4929 FieldAccessCallingConventionX86 calling_convention;
4930 codegen_->CreateUnresolvedFieldLocationSummary(
4931 instruction, instruction->GetFieldType(), calling_convention);
4932}
4933
4934void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldGet(
4935 HUnresolvedStaticFieldGet* instruction) {
4936 FieldAccessCallingConventionX86 calling_convention;
4937 codegen_->GenerateUnresolvedFieldAccess(instruction,
4938 instruction->GetFieldType(),
4939 instruction->GetFieldIndex(),
4940 instruction->GetDexPc(),
4941 calling_convention);
4942}
4943
4944void LocationsBuilderX86::VisitUnresolvedStaticFieldSet(
4945 HUnresolvedStaticFieldSet* instruction) {
4946 FieldAccessCallingConventionX86 calling_convention;
4947 codegen_->CreateUnresolvedFieldLocationSummary(
4948 instruction, instruction->GetFieldType(), calling_convention);
4949}
4950
4951void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldSet(
4952 HUnresolvedStaticFieldSet* instruction) {
4953 FieldAccessCallingConventionX86 calling_convention;
4954 codegen_->GenerateUnresolvedFieldAccess(instruction,
4955 instruction->GetFieldType(),
4956 instruction->GetFieldIndex(),
4957 instruction->GetDexPc(),
4958 calling_convention);
4959}
4960
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004961void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004962 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
4963 Location loc = codegen_->GetCompilerOptions().GetImplicitNullChecks()
4964 ? Location::RequiresRegister()
4965 : Location::Any();
4966 locations->SetInAt(0, loc);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004967}
4968
Calin Juravle2ae48182016-03-16 14:05:09 +00004969void CodeGeneratorX86::GenerateImplicitNullCheck(HNullCheck* instruction) {
4970 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004971 return;
4972 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004973 LocationSummary* locations = instruction->GetLocations();
4974 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00004975
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004976 __ testl(EAX, Address(obj.AsRegister<Register>(), 0));
Calin Juravle2ae48182016-03-16 14:05:09 +00004977 RecordPcInfo(instruction, instruction->GetDexPc());
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004978}
4979
Calin Juravle2ae48182016-03-16 14:05:09 +00004980void CodeGeneratorX86::GenerateExplicitNullCheck(HNullCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07004981 SlowPathCode* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00004982 AddSlowPath(slow_path);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004983
4984 LocationSummary* locations = instruction->GetLocations();
4985 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004986
4987 if (obj.IsRegister()) {
Mark Mendell42514f62015-03-31 11:34:22 -04004988 __ testl(obj.AsRegister<Register>(), obj.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004989 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004990 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004991 } else {
4992 DCHECK(obj.IsConstant()) << obj;
David Brazdil77a48ae2015-09-15 12:34:04 +00004993 DCHECK(obj.GetConstant()->IsNullConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004994 __ jmp(slow_path->GetEntryLabel());
4995 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004996 }
4997 __ j(kEqual, slow_path->GetEntryLabel());
4998}
4999
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005000void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00005001 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005002}
5003
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005004void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005005 bool object_array_get_with_read_barrier =
5006 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005007 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00005008 new (GetGraph()->GetArena()) LocationSummary(instruction,
5009 object_array_get_with_read_barrier ?
5010 LocationSummary::kCallOnSlowPath :
5011 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01005012 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005013 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01005014 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005015 locations->SetInAt(0, Location::RequiresRegister());
5016 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005017 if (Primitive::IsFloatingPointType(instruction->GetType())) {
5018 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5019 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005020 // The output overlaps in case of long: we don't want the low move
5021 // to overwrite the array's location. Likewise, in the case of an
5022 // object array get with read barriers enabled, we do not want the
5023 // move to overwrite the array's location, as we need it to emit
5024 // the read barrier.
5025 locations->SetOut(
5026 Location::RequiresRegister(),
5027 (instruction->GetType() == Primitive::kPrimLong || object_array_get_with_read_barrier) ?
5028 Location::kOutputOverlap :
5029 Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005030 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005031}
5032
5033void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
5034 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005035 Location obj_loc = locations->InAt(0);
5036 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005037 Location index = locations->InAt(1);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005038 Location out_loc = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005039 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005040
Calin Juravle77520bc2015-01-12 18:45:46 +00005041 Primitive::Type type = instruction->GetType();
5042 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005043 case Primitive::kPrimBoolean: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005044 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005045 __ movzxb(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005046 break;
5047 }
5048
5049 case Primitive::kPrimByte: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005050 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005051 __ movsxb(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005052 break;
5053 }
5054
5055 case Primitive::kPrimShort: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005056 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005057 __ movsxw(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005058 break;
5059 }
5060
5061 case Primitive::kPrimChar: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005062 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005063 __ movzxw(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005064 break;
5065 }
5066
Roland Levillain7c1559a2015-12-15 10:55:36 +00005067 case Primitive::kPrimInt: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005068 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005069 __ movl(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005070 break;
5071 }
5072
Roland Levillain7c1559a2015-12-15 10:55:36 +00005073 case Primitive::kPrimNot: {
5074 static_assert(
5075 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
5076 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain7c1559a2015-12-15 10:55:36 +00005077 // /* HeapReference<Object> */ out =
5078 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
5079 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005080 // Note that a potential implicit null check is handled in this
5081 // CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier call.
5082 codegen_->GenerateArrayLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00005083 instruction, out_loc, obj, data_offset, index, /* needs_null_check */ true);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005084 } else {
5085 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005086 __ movl(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset));
5087 codegen_->MaybeRecordImplicitNullCheck(instruction);
5088 // If read barriers are enabled, emit read barriers other than
5089 // Baker's using a slow path (and also unpoison the loaded
5090 // reference, if heap poisoning is enabled).
Roland Levillain7c1559a2015-12-15 10:55:36 +00005091 if (index.IsConstant()) {
5092 uint32_t offset =
5093 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Roland Levillain7c1559a2015-12-15 10:55:36 +00005094 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
5095 } else {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005096 codegen_->MaybeGenerateReadBarrierSlow(
5097 instruction, out_loc, out_loc, obj_loc, data_offset, index);
5098 }
5099 }
5100 break;
5101 }
5102
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005103 case Primitive::kPrimLong: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005104 DCHECK_NE(obj, out_loc.AsRegisterPairLow<Register>());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005105 __ movl(out_loc.AsRegisterPairLow<Register>(),
5106 CodeGeneratorX86::ArrayAddress(obj, index, TIMES_8, data_offset));
5107 codegen_->MaybeRecordImplicitNullCheck(instruction);
5108 __ movl(out_loc.AsRegisterPairHigh<Register>(),
5109 CodeGeneratorX86::ArrayAddress(obj, index, TIMES_8, data_offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005110 break;
5111 }
5112
Mark Mendell7c8d0092015-01-26 11:21:33 -05005113 case Primitive::kPrimFloat: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005114 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005115 __ movss(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005116 break;
5117 }
5118
5119 case Primitive::kPrimDouble: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005120 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005121 __ movsd(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_8, data_offset));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005122 break;
5123 }
5124
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005125 case Primitive::kPrimVoid:
Calin Juravle77520bc2015-01-12 18:45:46 +00005126 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07005127 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005128 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005129
Roland Levillain7c1559a2015-12-15 10:55:36 +00005130 if (type == Primitive::kPrimNot || type == Primitive::kPrimLong) {
5131 // Potential implicit null checks, in the case of reference or
5132 // long arrays, are handled in the previous switch statement.
5133 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00005134 codegen_->MaybeRecordImplicitNullCheck(instruction);
5135 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005136}
5137
5138void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01005139 Primitive::Type value_type = instruction->GetComponentType();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005140
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005141 bool needs_write_barrier =
5142 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Roland Levillain0d5a2812015-11-13 10:07:31 +00005143 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005144
Nicolas Geoffray39468442014-09-02 15:17:15 +01005145 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
5146 instruction,
Vladimir Marko8d49fd72016-08-25 15:20:47 +01005147 may_need_runtime_call_for_type_check ?
Roland Levillain0d5a2812015-11-13 10:07:31 +00005148 LocationSummary::kCallOnSlowPath :
5149 LocationSummary::kNoCall);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005150
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005151 bool is_byte_type = (value_type == Primitive::kPrimBoolean)
5152 || (value_type == Primitive::kPrimByte);
5153 // We need the inputs to be different than the output in case of long operation.
5154 // In case of a byte operation, the register allocator does not support multiple
5155 // inputs that die at entry with one in a specific register.
5156 locations->SetInAt(0, Location::RequiresRegister());
5157 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
5158 if (is_byte_type) {
5159 // Ensure the value is in a byte register.
5160 locations->SetInAt(2, Location::ByteRegisterOrConstant(EAX, instruction->InputAt(2)));
5161 } else if (Primitive::IsFloatingPointType(value_type)) {
Mark Mendell81489372015-11-04 11:30:41 -05005162 locations->SetInAt(2, Location::FpuRegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005163 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005164 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
5165 }
5166 if (needs_write_barrier) {
5167 // Temporary registers for the write barrier.
5168 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
5169 // Ensure the card is in a byte register.
Roland Levillain4f6b0b52015-11-23 19:29:22 +00005170 locations->AddTemp(Location::RegisterLocation(ECX));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005171 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005172}
5173
5174void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
5175 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005176 Location array_loc = locations->InAt(0);
5177 Register array = array_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005178 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005179 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005180 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005181 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
5182 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5183 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005184 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005185 bool needs_write_barrier =
5186 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005187
5188 switch (value_type) {
5189 case Primitive::kPrimBoolean:
5190 case Primitive::kPrimByte: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005191 uint32_t offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005192 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_1, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005193 if (value.IsRegister()) {
5194 __ movb(address, value.AsRegister<ByteRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005195 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005196 __ movb(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005197 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005198 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005199 break;
5200 }
5201
5202 case Primitive::kPrimShort:
5203 case Primitive::kPrimChar: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005204 uint32_t offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005205 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_2, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005206 if (value.IsRegister()) {
5207 __ movw(address, value.AsRegister<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005208 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005209 __ movw(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005210 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005211 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005212 break;
5213 }
5214
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005215 case Primitive::kPrimNot: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005216 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005217 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_4, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005218
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005219 if (!value.IsRegister()) {
5220 // Just setting null.
5221 DCHECK(instruction->InputAt(2)->IsNullConstant());
5222 DCHECK(value.IsConstant()) << value;
5223 __ movl(address, Immediate(0));
Calin Juravle77520bc2015-01-12 18:45:46 +00005224 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005225 DCHECK(!needs_write_barrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005226 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005227 break;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005228 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005229
5230 DCHECK(needs_write_barrier);
5231 Register register_value = value.AsRegister<Register>();
Roland Levillain16d9f942016-08-25 17:27:56 +01005232 // We cannot use a NearLabel for `done`, as its range may be too
5233 // short when Baker read barriers are enabled.
5234 Label done;
5235 NearLabel not_null, do_put;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005236 SlowPathCode* slow_path = nullptr;
Roland Levillain16d9f942016-08-25 17:27:56 +01005237 Location temp_loc = locations->GetTemp(0);
5238 Register temp = temp_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005239 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005240 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathX86(instruction);
5241 codegen_->AddSlowPath(slow_path);
5242 if (instruction->GetValueCanBeNull()) {
5243 __ testl(register_value, register_value);
5244 __ j(kNotEqual, &not_null);
5245 __ movl(address, Immediate(0));
5246 codegen_->MaybeRecordImplicitNullCheck(instruction);
5247 __ jmp(&done);
5248 __ Bind(&not_null);
5249 }
5250
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005251 // Note that when Baker read barriers are enabled, the type
5252 // checks are performed without read barriers. This is fine,
5253 // even in the case where a class object is in the from-space
5254 // after the flip, as a comparison involving such a type would
5255 // not produce a false positive; it may of course produce a
5256 // false negative, in which case we would take the ArraySet
5257 // slow path.
Roland Levillain16d9f942016-08-25 17:27:56 +01005258
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005259 // /* HeapReference<Class> */ temp = array->klass_
5260 __ movl(temp, Address(array, class_offset));
5261 codegen_->MaybeRecordImplicitNullCheck(instruction);
5262 __ MaybeUnpoisonHeapReference(temp);
Roland Levillain16d9f942016-08-25 17:27:56 +01005263
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005264 // /* HeapReference<Class> */ temp = temp->component_type_
5265 __ movl(temp, Address(temp, component_offset));
5266 // If heap poisoning is enabled, no need to unpoison `temp`
5267 // nor the object reference in `register_value->klass`, as
5268 // we are comparing two poisoned references.
5269 __ cmpl(temp, Address(register_value, class_offset));
Roland Levillain16d9f942016-08-25 17:27:56 +01005270
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005271 if (instruction->StaticTypeOfArrayIsObjectArray()) {
5272 __ j(kEqual, &do_put);
5273 // If heap poisoning is enabled, the `temp` reference has
5274 // not been unpoisoned yet; unpoison it now.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005275 __ MaybeUnpoisonHeapReference(temp);
5276
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005277 // If heap poisoning is enabled, no need to unpoison the
5278 // heap reference loaded below, as it is only used for a
5279 // comparison with null.
5280 __ cmpl(Address(temp, super_offset), Immediate(0));
5281 __ j(kNotEqual, slow_path->GetEntryLabel());
5282 __ Bind(&do_put);
5283 } else {
5284 __ j(kNotEqual, slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005285 }
5286 }
5287
5288 if (kPoisonHeapReferences) {
5289 __ movl(temp, register_value);
5290 __ PoisonHeapReference(temp);
5291 __ movl(address, temp);
5292 } else {
5293 __ movl(address, register_value);
5294 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005295 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005296 codegen_->MaybeRecordImplicitNullCheck(instruction);
5297 }
5298
5299 Register card = locations->GetTemp(1).AsRegister<Register>();
5300 codegen_->MarkGCCard(
5301 temp, card, array, value.AsRegister<Register>(), instruction->GetValueCanBeNull());
5302 __ Bind(&done);
5303
5304 if (slow_path != nullptr) {
5305 __ Bind(slow_path->GetExitLabel());
5306 }
5307
5308 break;
5309 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005310
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005311 case Primitive::kPrimInt: {
5312 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005313 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_4, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005314 if (value.IsRegister()) {
5315 __ movl(address, value.AsRegister<Register>());
5316 } else {
5317 DCHECK(value.IsConstant()) << value;
5318 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
5319 __ movl(address, Immediate(v));
5320 }
5321 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005322 break;
5323 }
5324
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005325 case Primitive::kPrimLong: {
5326 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005327 if (value.IsRegisterPair()) {
5328 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset),
5329 value.AsRegisterPairLow<Register>());
5330 codegen_->MaybeRecordImplicitNullCheck(instruction);
5331 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset + kX86WordSize),
5332 value.AsRegisterPairHigh<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005333 } else {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005334 DCHECK(value.IsConstant());
5335 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
5336 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset),
5337 Immediate(Low32Bits(val)));
5338 codegen_->MaybeRecordImplicitNullCheck(instruction);
5339 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset + kX86WordSize),
5340 Immediate(High32Bits(val)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005341 }
5342 break;
5343 }
5344
Mark Mendell7c8d0092015-01-26 11:21:33 -05005345 case Primitive::kPrimFloat: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005346 uint32_t offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005347 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_4, offset);
Mark Mendell81489372015-11-04 11:30:41 -05005348 if (value.IsFpuRegister()) {
5349 __ movss(address, value.AsFpuRegister<XmmRegister>());
5350 } else {
5351 DCHECK(value.IsConstant());
5352 int32_t v = bit_cast<int32_t, float>(value.GetConstant()->AsFloatConstant()->GetValue());
5353 __ movl(address, Immediate(v));
5354 }
5355 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005356 break;
5357 }
5358
5359 case Primitive::kPrimDouble: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005360 uint32_t offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005361 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, offset);
Mark Mendell81489372015-11-04 11:30:41 -05005362 if (value.IsFpuRegister()) {
5363 __ movsd(address, value.AsFpuRegister<XmmRegister>());
5364 } else {
5365 DCHECK(value.IsConstant());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005366 Address address_hi =
5367 CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, offset + kX86WordSize);
Mark Mendell81489372015-11-04 11:30:41 -05005368 int64_t v = bit_cast<int64_t, double>(value.GetConstant()->AsDoubleConstant()->GetValue());
5369 __ movl(address, Immediate(Low32Bits(v)));
5370 codegen_->MaybeRecordImplicitNullCheck(instruction);
5371 __ movl(address_hi, Immediate(High32Bits(v)));
5372 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005373 break;
5374 }
5375
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005376 case Primitive::kPrimVoid:
5377 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07005378 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005379 }
5380}
5381
5382void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
5383 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005384 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellee8d9712016-07-12 11:13:15 -04005385 if (!instruction->IsEmittedAtUseSite()) {
5386 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5387 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005388}
5389
5390void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
Mark Mendellee8d9712016-07-12 11:13:15 -04005391 if (instruction->IsEmittedAtUseSite()) {
5392 return;
5393 }
5394
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005395 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01005396 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00005397 Register obj = locations->InAt(0).AsRegister<Register>();
5398 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005399 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005400 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005401}
5402
5403void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005404 RegisterSet caller_saves = RegisterSet::Empty();
5405 InvokeRuntimeCallingConvention calling_convention;
5406 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5407 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
5408 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Mark Mendellf60c90b2015-03-04 15:12:59 -05005409 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendellee8d9712016-07-12 11:13:15 -04005410 HInstruction* length = instruction->InputAt(1);
5411 if (!length->IsEmittedAtUseSite()) {
5412 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
5413 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005414}
5415
5416void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
5417 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05005418 Location index_loc = locations->InAt(0);
5419 Location length_loc = locations->InAt(1);
Andreas Gampe85b62f22015-09-09 13:15:38 -07005420 SlowPathCode* slow_path =
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01005421 new (GetGraph()->GetArena()) BoundsCheckSlowPathX86(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005422
Mark Mendell99dbd682015-04-22 16:18:52 -04005423 if (length_loc.IsConstant()) {
5424 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
5425 if (index_loc.IsConstant()) {
5426 // BCE will remove the bounds check if we are guarenteed to pass.
5427 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5428 if (index < 0 || index >= length) {
5429 codegen_->AddSlowPath(slow_path);
5430 __ jmp(slow_path->GetEntryLabel());
5431 } else {
5432 // Some optimization after BCE may have generated this, and we should not
5433 // generate a bounds check if it is a valid range.
5434 }
5435 return;
5436 }
5437
5438 // We have to reverse the jump condition because the length is the constant.
5439 Register index_reg = index_loc.AsRegister<Register>();
5440 __ cmpl(index_reg, Immediate(length));
5441 codegen_->AddSlowPath(slow_path);
5442 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005443 } else {
Mark Mendellee8d9712016-07-12 11:13:15 -04005444 HInstruction* array_length = instruction->InputAt(1);
5445 if (array_length->IsEmittedAtUseSite()) {
5446 // Address the length field in the array.
5447 DCHECK(array_length->IsArrayLength());
5448 uint32_t len_offset = CodeGenerator::GetArrayLengthOffset(array_length->AsArrayLength());
5449 Location array_loc = array_length->GetLocations()->InAt(0);
5450 Address array_len(array_loc.AsRegister<Register>(), len_offset);
5451 if (index_loc.IsConstant()) {
5452 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5453 __ cmpl(array_len, Immediate(value));
5454 } else {
5455 __ cmpl(array_len, index_loc.AsRegister<Register>());
5456 }
5457 codegen_->MaybeRecordImplicitNullCheck(array_length);
Mark Mendell99dbd682015-04-22 16:18:52 -04005458 } else {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005459 codegen_->GenerateIntCompare(length_loc, index_loc);
Mark Mendell99dbd682015-04-22 16:18:52 -04005460 }
5461 codegen_->AddSlowPath(slow_path);
5462 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005463 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005464}
5465
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005466void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005467 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005468}
5469
5470void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005471 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5472}
5473
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005474void LocationsBuilderX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Marko70e97462016-08-09 11:04:26 +01005475 LocationSummary* locations =
5476 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01005477 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005478}
5479
5480void InstructionCodeGeneratorX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005481 HBasicBlock* block = instruction->GetBlock();
5482 if (block->GetLoopInformation() != nullptr) {
5483 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5484 // The back edge will generate the suspend check.
5485 return;
5486 }
5487 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5488 // The goto will generate the suspend check.
5489 return;
5490 }
5491 GenerateSuspendCheck(instruction, nullptr);
5492}
5493
5494void InstructionCodeGeneratorX86::GenerateSuspendCheck(HSuspendCheck* instruction,
5495 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005496 SuspendCheckSlowPathX86* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005497 down_cast<SuspendCheckSlowPathX86*>(instruction->GetSlowPath());
5498 if (slow_path == nullptr) {
5499 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathX86(instruction, successor);
5500 instruction->SetSlowPath(slow_path);
5501 codegen_->AddSlowPath(slow_path);
5502 if (successor != nullptr) {
5503 DCHECK(successor->IsLoopHeader());
5504 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
5505 }
5506 } else {
5507 DCHECK_EQ(slow_path->GetSuccessor(), successor);
5508 }
5509
Andreas Gampe542451c2016-07-26 09:02:02 -07005510 __ fs()->cmpw(Address::Absolute(Thread::ThreadFlagsOffset<kX86PointerSize>().Int32Value()),
Roland Levillain7c1559a2015-12-15 10:55:36 +00005511 Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005512 if (successor == nullptr) {
5513 __ j(kNotEqual, slow_path->GetEntryLabel());
5514 __ Bind(slow_path->GetReturnLabel());
5515 } else {
5516 __ j(kEqual, codegen_->GetLabelOf(successor));
5517 __ jmp(slow_path->GetEntryLabel());
5518 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005519}
5520
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005521X86Assembler* ParallelMoveResolverX86::GetAssembler() const {
5522 return codegen_->GetAssembler();
5523}
5524
Mark Mendell7c8d0092015-01-26 11:21:33 -05005525void ParallelMoveResolverX86::MoveMemoryToMemory32(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005526 ScratchRegisterScope ensure_scratch(
5527 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5528 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5529 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5530 __ movl(temp_reg, Address(ESP, src + stack_offset));
5531 __ movl(Address(ESP, dst + stack_offset), temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005532}
5533
5534void ParallelMoveResolverX86::MoveMemoryToMemory64(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005535 ScratchRegisterScope ensure_scratch(
5536 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5537 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5538 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5539 __ movl(temp_reg, Address(ESP, src + stack_offset));
5540 __ movl(Address(ESP, dst + stack_offset), temp_reg);
5541 __ movl(temp_reg, Address(ESP, src + stack_offset + kX86WordSize));
5542 __ movl(Address(ESP, dst + stack_offset + kX86WordSize), temp_reg);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005543}
5544
5545void ParallelMoveResolverX86::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005546 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005547 Location source = move->GetSource();
5548 Location destination = move->GetDestination();
5549
5550 if (source.IsRegister()) {
5551 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005552 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
David Brazdil74eb1b22015-12-14 11:44:01 +00005553 } else if (destination.IsFpuRegister()) {
5554 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005555 } else {
5556 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00005557 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005558 }
David Brazdil74eb1b22015-12-14 11:44:01 +00005559 } else if (source.IsRegisterPair()) {
5560 size_t elem_size = Primitive::ComponentSize(Primitive::kPrimInt);
5561 // Create stack space for 2 elements.
5562 __ subl(ESP, Immediate(2 * elem_size));
5563 __ movl(Address(ESP, 0), source.AsRegisterPairLow<Register>());
5564 __ movl(Address(ESP, elem_size), source.AsRegisterPairHigh<Register>());
5565 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
5566 // And remove the temporary stack space we allocated.
5567 __ addl(ESP, Immediate(2 * elem_size));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005568 } else if (source.IsFpuRegister()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00005569 if (destination.IsRegister()) {
5570 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
5571 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005572 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
David Brazdil74eb1b22015-12-14 11:44:01 +00005573 } else if (destination.IsRegisterPair()) {
5574 XmmRegister src_reg = source.AsFpuRegister<XmmRegister>();
5575 __ movd(destination.AsRegisterPairLow<Register>(), src_reg);
5576 __ psrlq(src_reg, Immediate(32));
5577 __ movd(destination.AsRegisterPairHigh<Register>(), src_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005578 } else if (destination.IsStackSlot()) {
5579 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
5580 } else {
5581 DCHECK(destination.IsDoubleStackSlot());
5582 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
5583 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005584 } else if (source.IsStackSlot()) {
5585 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005586 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005587 } else if (destination.IsFpuRegister()) {
5588 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005589 } else {
5590 DCHECK(destination.IsStackSlot());
Mark Mendell7c8d0092015-01-26 11:21:33 -05005591 MoveMemoryToMemory32(destination.GetStackIndex(), source.GetStackIndex());
5592 }
5593 } else if (source.IsDoubleStackSlot()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00005594 if (destination.IsRegisterPair()) {
5595 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
5596 __ movl(destination.AsRegisterPairHigh<Register>(),
5597 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
5598 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005599 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
5600 } else {
5601 DCHECK(destination.IsDoubleStackSlot()) << destination;
5602 MoveMemoryToMemory64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005603 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005604 } else if (source.IsConstant()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005605 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00005606 if (constant->IsIntConstant() || constant->IsNullConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05005607 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005608 if (destination.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05005609 if (value == 0) {
5610 __ xorl(destination.AsRegister<Register>(), destination.AsRegister<Register>());
5611 } else {
5612 __ movl(destination.AsRegister<Register>(), Immediate(value));
5613 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005614 } else {
5615 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell09b84632015-02-13 17:48:38 -05005616 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005617 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005618 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005619 float fp_value = constant->AsFloatConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00005620 int32_t value = bit_cast<int32_t, float>(fp_value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005621 Immediate imm(value);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005622 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005623 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
5624 if (value == 0) {
5625 // Easy handling of 0.0.
5626 __ xorps(dest, dest);
5627 } else {
5628 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005629 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5630 Register temp = static_cast<Register>(ensure_scratch.GetRegister());
5631 __ movl(temp, Immediate(value));
5632 __ movd(dest, temp);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005633 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005634 } else {
5635 DCHECK(destination.IsStackSlot()) << destination;
5636 __ movl(Address(ESP, destination.GetStackIndex()), imm);
5637 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005638 } else if (constant->IsLongConstant()) {
5639 int64_t value = constant->AsLongConstant()->GetValue();
5640 int32_t low_value = Low32Bits(value);
5641 int32_t high_value = High32Bits(value);
5642 Immediate low(low_value);
5643 Immediate high(high_value);
5644 if (destination.IsDoubleStackSlot()) {
5645 __ movl(Address(ESP, destination.GetStackIndex()), low);
5646 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
5647 } else {
5648 __ movl(destination.AsRegisterPairLow<Register>(), low);
5649 __ movl(destination.AsRegisterPairHigh<Register>(), high);
5650 }
5651 } else {
5652 DCHECK(constant->IsDoubleConstant());
5653 double dbl_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00005654 int64_t value = bit_cast<int64_t, double>(dbl_value);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005655 int32_t low_value = Low32Bits(value);
5656 int32_t high_value = High32Bits(value);
5657 Immediate low(low_value);
5658 Immediate high(high_value);
5659 if (destination.IsFpuRegister()) {
5660 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
5661 if (value == 0) {
5662 // Easy handling of 0.0.
5663 __ xorpd(dest, dest);
5664 } else {
5665 __ pushl(high);
5666 __ pushl(low);
5667 __ movsd(dest, Address(ESP, 0));
5668 __ addl(ESP, Immediate(8));
5669 }
5670 } else {
5671 DCHECK(destination.IsDoubleStackSlot()) << destination;
5672 __ movl(Address(ESP, destination.GetStackIndex()), low);
5673 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
5674 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005675 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005676 } else {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00005677 LOG(FATAL) << "Unimplemented move: " << destination << " <- " << source;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005678 }
5679}
5680
Mark Mendella5c19ce2015-04-01 12:51:05 -04005681void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005682 Register suggested_scratch = reg == EAX ? EBX : EAX;
5683 ScratchRegisterScope ensure_scratch(
5684 this, reg, suggested_scratch, codegen_->GetNumberOfCoreRegisters());
5685
5686 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5687 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset));
5688 __ movl(Address(ESP, mem + stack_offset), reg);
5689 __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005690}
5691
Mark Mendell7c8d0092015-01-26 11:21:33 -05005692void ParallelMoveResolverX86::Exchange32(XmmRegister reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005693 ScratchRegisterScope ensure_scratch(
5694 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5695
5696 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5697 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5698 __ movl(temp_reg, Address(ESP, mem + stack_offset));
5699 __ movss(Address(ESP, mem + stack_offset), reg);
5700 __ movd(reg, temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005701}
5702
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005703void ParallelMoveResolverX86::Exchange(int mem1, int mem2) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005704 ScratchRegisterScope ensure_scratch1(
5705 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005706
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005707 Register suggested_scratch = ensure_scratch1.GetRegister() == EAX ? EBX : EAX;
5708 ScratchRegisterScope ensure_scratch2(
5709 this, ensure_scratch1.GetRegister(), suggested_scratch, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005710
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005711 int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0;
5712 stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0;
5713 __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset));
5714 __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset));
5715 __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister()));
5716 __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005717}
5718
5719void ParallelMoveResolverX86::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005720 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005721 Location source = move->GetSource();
5722 Location destination = move->GetDestination();
5723
5724 if (source.IsRegister() && destination.IsRegister()) {
Mark Mendell90979812015-07-28 16:41:21 -04005725 // Use XOR swap algorithm to avoid serializing XCHG instruction or using a temporary.
5726 DCHECK_NE(destination.AsRegister<Register>(), source.AsRegister<Register>());
5727 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
5728 __ xorl(source.AsRegister<Register>(), destination.AsRegister<Register>());
5729 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005730 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005731 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005732 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005733 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005734 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
5735 Exchange(destination.GetStackIndex(), source.GetStackIndex());
Mark Mendell7c8d0092015-01-26 11:21:33 -05005736 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
5737 // Use XOR Swap algorithm to avoid a temporary.
5738 DCHECK_NE(source.reg(), destination.reg());
5739 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
5740 __ xorpd(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
5741 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
5742 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
5743 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
5744 } else if (destination.IsFpuRegister() && source.IsStackSlot()) {
5745 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005746 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
5747 // Take advantage of the 16 bytes in the XMM register.
5748 XmmRegister reg = source.AsFpuRegister<XmmRegister>();
5749 Address stack(ESP, destination.GetStackIndex());
5750 // Load the double into the high doubleword.
5751 __ movhpd(reg, stack);
5752
5753 // Store the low double into the destination.
5754 __ movsd(stack, reg);
5755
5756 // Move the high double to the low double.
5757 __ psrldq(reg, Immediate(8));
5758 } else if (destination.IsFpuRegister() && source.IsDoubleStackSlot()) {
5759 // Take advantage of the 16 bytes in the XMM register.
5760 XmmRegister reg = destination.AsFpuRegister<XmmRegister>();
5761 Address stack(ESP, source.GetStackIndex());
5762 // Load the double into the high doubleword.
5763 __ movhpd(reg, stack);
5764
5765 // Store the low double into the destination.
5766 __ movsd(stack, reg);
5767
5768 // Move the high double to the low double.
5769 __ psrldq(reg, Immediate(8));
5770 } else if (destination.IsDoubleStackSlot() && source.IsDoubleStackSlot()) {
5771 Exchange(destination.GetStackIndex(), source.GetStackIndex());
5772 Exchange(destination.GetHighStackIndex(kX86WordSize), source.GetHighStackIndex(kX86WordSize));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005773 } else {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005774 LOG(FATAL) << "Unimplemented: source: " << source << ", destination: " << destination;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005775 }
5776}
5777
5778void ParallelMoveResolverX86::SpillScratch(int reg) {
5779 __ pushl(static_cast<Register>(reg));
5780}
5781
5782void ParallelMoveResolverX86::RestoreScratch(int reg) {
5783 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005784}
5785
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005786HLoadClass::LoadKind CodeGeneratorX86::GetSupportedLoadClassKind(
5787 HLoadClass::LoadKind desired_class_load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005788 switch (desired_class_load_kind) {
5789 case HLoadClass::LoadKind::kReferrersClass:
5790 break;
5791 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
5792 DCHECK(!GetCompilerOptions().GetCompilePic());
5793 break;
5794 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
5795 DCHECK(GetCompilerOptions().GetCompilePic());
5796 FALLTHROUGH_INTENDED;
5797 case HLoadClass::LoadKind::kDexCachePcRelative:
5798 DCHECK(!Runtime::Current()->UseJitCompilation()); // Note: boot image is also non-JIT.
5799 // We disable pc-relative load when there is an irreducible loop, as the optimization
5800 // is incompatible with it.
5801 // TODO: Create as many X86ComputeBaseMethodAddress instructions as needed for methods
5802 // with irreducible loops.
5803 if (GetGraph()->HasIrreducibleLoops()) {
5804 return HLoadClass::LoadKind::kDexCacheViaMethod;
5805 }
5806 break;
5807 case HLoadClass::LoadKind::kBootImageAddress:
5808 break;
5809 case HLoadClass::LoadKind::kDexCacheAddress:
5810 DCHECK(Runtime::Current()->UseJitCompilation());
5811 break;
5812 case HLoadClass::LoadKind::kDexCacheViaMethod:
5813 break;
5814 }
5815 return desired_class_load_kind;
5816}
5817
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005818void LocationsBuilderX86::VisitLoadClass(HLoadClass* cls) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005819 if (cls->NeedsAccessCheck()) {
5820 InvokeRuntimeCallingConvention calling_convention;
5821 CodeGenerator::CreateLoadClassLocationSummary(
5822 cls,
5823 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
5824 Location::RegisterLocation(EAX),
5825 /* code_generator_supports_read_barrier */ true);
5826 return;
5827 }
5828
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005829 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
5830 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005831 ? LocationSummary::kCallOnSlowPath
5832 : LocationSummary::kNoCall;
5833 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005834 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005835 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01005836 }
5837
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005838 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
5839 if (load_kind == HLoadClass::LoadKind::kReferrersClass ||
5840 load_kind == HLoadClass::LoadKind::kDexCacheViaMethod ||
5841 load_kind == HLoadClass::LoadKind::kBootImageLinkTimePcRelative ||
5842 load_kind == HLoadClass::LoadKind::kDexCachePcRelative) {
5843 locations->SetInAt(0, Location::RequiresRegister());
5844 }
5845 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005846}
5847
5848void InstructionCodeGeneratorX86::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005849 LocationSummary* locations = cls->GetLocations();
Calin Juravle98893e12015-10-02 21:05:03 +01005850 if (cls->NeedsAccessCheck()) {
5851 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex());
Serban Constantinescuba45db02016-07-12 22:53:02 +01005852 codegen_->InvokeRuntime(kQuickInitializeTypeAndVerifyAccess, cls, cls->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00005853 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Calin Juravle580b6092015-10-06 17:35:58 +01005854 return;
5855 }
5856
Roland Levillain0d5a2812015-11-13 10:07:31 +00005857 Location out_loc = locations->Out();
5858 Register out = out_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005859
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005860 bool generate_null_check = false;
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005861 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005862 switch (cls->GetLoadKind()) {
5863 case HLoadClass::LoadKind::kReferrersClass: {
5864 DCHECK(!cls->CanCallRuntime());
5865 DCHECK(!cls->MustGenerateClinitCheck());
5866 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5867 Register current_method = locations->InAt(0).AsRegister<Register>();
5868 GenerateGcRootFieldLoad(
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005869 cls,
5870 out_loc,
5871 Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()),
5872 /*fixup_label*/ nullptr,
5873 requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005874 break;
5875 }
5876 case HLoadClass::LoadKind::kBootImageLinkTimeAddress: {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005877 DCHECK(!requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005878 __ movl(out, Immediate(/* placeholder */ 0));
5879 codegen_->RecordTypePatch(cls);
5880 break;
5881 }
5882 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005883 DCHECK(!requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005884 Register method_address = locations->InAt(0).AsRegister<Register>();
5885 __ leal(out, Address(method_address, CodeGeneratorX86::kDummy32BitOffset));
5886 codegen_->RecordTypePatch(cls);
5887 break;
5888 }
5889 case HLoadClass::LoadKind::kBootImageAddress: {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005890 DCHECK(!requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005891 DCHECK_NE(cls->GetAddress(), 0u);
5892 uint32_t address = dchecked_integral_cast<uint32_t>(cls->GetAddress());
5893 __ movl(out, Immediate(address));
5894 codegen_->RecordSimplePatch();
5895 break;
5896 }
5897 case HLoadClass::LoadKind::kDexCacheAddress: {
5898 DCHECK_NE(cls->GetAddress(), 0u);
5899 uint32_t address = dchecked_integral_cast<uint32_t>(cls->GetAddress());
5900 // /* GcRoot<mirror::Class> */ out = *address
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005901 GenerateGcRootFieldLoad(cls,
5902 out_loc,
5903 Address::Absolute(address),
5904 /*fixup_label*/ nullptr,
5905 requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005906 generate_null_check = !cls->IsInDexCache();
5907 break;
5908 }
5909 case HLoadClass::LoadKind::kDexCachePcRelative: {
5910 Register base_reg = locations->InAt(0).AsRegister<Register>();
5911 uint32_t offset = cls->GetDexCacheElementOffset();
5912 Label* fixup_label = codegen_->NewPcRelativeDexCacheArrayPatch(cls->GetDexFile(), offset);
5913 // /* GcRoot<mirror::Class> */ out = *(base + offset) /* PC-relative */
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005914 GenerateGcRootFieldLoad(cls,
5915 out_loc,
5916 Address(base_reg, CodeGeneratorX86::kDummy32BitOffset),
5917 fixup_label,
5918 requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005919 generate_null_check = !cls->IsInDexCache();
5920 break;
5921 }
5922 case HLoadClass::LoadKind::kDexCacheViaMethod: {
5923 // /* GcRoot<mirror::Class>[] */ out =
5924 // current_method.ptr_sized_fields_->dex_cache_resolved_types_
5925 Register current_method = locations->InAt(0).AsRegister<Register>();
5926 __ movl(out, Address(current_method,
5927 ArtMethod::DexCacheResolvedTypesOffset(kX86PointerSize).Int32Value()));
5928 // /* GcRoot<mirror::Class> */ out = out[type_index]
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005929 GenerateGcRootFieldLoad(cls,
5930 out_loc,
5931 Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())),
5932 /*fixup_label*/ nullptr,
5933 requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005934 generate_null_check = !cls->IsInDexCache();
5935 break;
5936 }
5937 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005938
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005939 if (generate_null_check || cls->MustGenerateClinitCheck()) {
5940 DCHECK(cls->CanCallRuntime());
5941 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
5942 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
5943 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005944
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005945 if (generate_null_check) {
5946 __ testl(out, out);
5947 __ j(kEqual, slow_path->GetEntryLabel());
5948 }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005949
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005950 if (cls->MustGenerateClinitCheck()) {
5951 GenerateClassInitializationCheck(slow_path, out);
5952 } else {
5953 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005954 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005955 }
5956}
5957
5958void LocationsBuilderX86::VisitClinitCheck(HClinitCheck* check) {
5959 LocationSummary* locations =
5960 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
5961 locations->SetInAt(0, Location::RequiresRegister());
5962 if (check->HasUses()) {
5963 locations->SetOut(Location::SameAsFirstInput());
5964 }
5965}
5966
5967void InstructionCodeGeneratorX86::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005968 // We assume the class to not be null.
Andreas Gampe85b62f22015-09-09 13:15:38 -07005969 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005970 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005971 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00005972 GenerateClassInitializationCheck(slow_path,
5973 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005974}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005975
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005976void InstructionCodeGeneratorX86::GenerateClassInitializationCheck(
Andreas Gampe85b62f22015-09-09 13:15:38 -07005977 SlowPathCode* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005978 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
5979 Immediate(mirror::Class::kStatusInitialized));
5980 __ j(kLess, slow_path->GetEntryLabel());
5981 __ Bind(slow_path->GetExitLabel());
5982 // No need for memory fence, thanks to the X86 memory model.
5983}
5984
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005985HLoadString::LoadKind CodeGeneratorX86::GetSupportedLoadStringKind(
5986 HLoadString::LoadKind desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005987 switch (desired_string_load_kind) {
5988 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
5989 DCHECK(!GetCompilerOptions().GetCompilePic());
5990 break;
5991 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
5992 DCHECK(GetCompilerOptions().GetCompilePic());
5993 FALLTHROUGH_INTENDED;
Vladimir Marko63dccbbe2016-09-21 13:51:10 +01005994 case HLoadString::LoadKind::kBssEntry:
Calin Juravleffc87072016-04-20 14:22:09 +01005995 DCHECK(!Runtime::Current()->UseJitCompilation()); // Note: boot image is also non-JIT.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005996 // We disable pc-relative load when there is an irreducible loop, as the optimization
5997 // is incompatible with it.
5998 // TODO: Create as many X86ComputeBaseMethodAddress instructions as needed for methods
5999 // with irreducible loops.
6000 if (GetGraph()->HasIrreducibleLoops()) {
6001 return HLoadString::LoadKind::kDexCacheViaMethod;
6002 }
6003 break;
6004 case HLoadString::LoadKind::kBootImageAddress:
6005 break;
6006 case HLoadString::LoadKind::kDexCacheAddress:
Calin Juravleffc87072016-04-20 14:22:09 +01006007 DCHECK(Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006008 break;
6009 case HLoadString::LoadKind::kDexCacheViaMethod:
6010 break;
6011 }
6012 return desired_string_load_kind;
6013}
6014
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006015void LocationsBuilderX86::VisitLoadString(HLoadString* load) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006016 LocationSummary::CallKind call_kind = (load->NeedsEnvironment() || kEmitCompilerReadBarrier)
Vladimir Marko63dccbbe2016-09-21 13:51:10 +01006017 ? ((load->GetLoadKind() == HLoadString::LoadKind::kDexCacheViaMethod)
6018 ? LocationSummary::kCallOnMainOnly
6019 : LocationSummary::kCallOnSlowPath)
Nicolas Geoffray917d0162015-11-24 18:25:35 +00006020 : LocationSummary::kNoCall;
6021 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006022 HLoadString::LoadKind load_kind = load->GetLoadKind();
6023 if (load_kind == HLoadString::LoadKind::kDexCacheViaMethod ||
6024 load_kind == HLoadString::LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Marko63dccbbe2016-09-21 13:51:10 +01006025 load_kind == HLoadString::LoadKind::kBssEntry) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006026 locations->SetInAt(0, Location::RequiresRegister());
6027 }
Christina Wadsworth175d09b2016-08-31 16:26:01 -07006028 if (load_kind == HLoadString::LoadKind::kDexCacheViaMethod) {
6029 locations->SetOut(Location::RegisterLocation(EAX));
6030 } else {
6031 locations->SetOut(Location::RequiresRegister());
6032 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006033}
6034
6035void InstructionCodeGeneratorX86::VisitLoadString(HLoadString* load) {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01006036 LocationSummary* locations = load->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006037 Location out_loc = locations->Out();
6038 Register out = out_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006039
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006040 switch (load->GetLoadKind()) {
6041 case HLoadString::LoadKind::kBootImageLinkTimeAddress: {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006042 __ movl(out, Immediate(/* placeholder */ 0));
Vladimir Marko63dccbbe2016-09-21 13:51:10 +01006043 codegen_->RecordBootStringPatch(load);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006044 return; // No dex cache slow path.
6045 }
6046 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006047 Register method_address = locations->InAt(0).AsRegister<Register>();
6048 __ leal(out, Address(method_address, CodeGeneratorX86::kDummy32BitOffset));
Vladimir Marko63dccbbe2016-09-21 13:51:10 +01006049 codegen_->RecordBootStringPatch(load);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006050 return; // No dex cache slow path.
6051 }
6052 case HLoadString::LoadKind::kBootImageAddress: {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006053 DCHECK_NE(load->GetAddress(), 0u);
6054 uint32_t address = dchecked_integral_cast<uint32_t>(load->GetAddress());
6055 __ movl(out, Immediate(address));
6056 codegen_->RecordSimplePatch();
6057 return; // No dex cache slow path.
6058 }
Vladimir Marko63dccbbe2016-09-21 13:51:10 +01006059 case HLoadString::LoadKind::kBssEntry: {
6060 Register method_address = locations->InAt(0).AsRegister<Register>();
6061 Address address = Address(method_address, CodeGeneratorX86::kDummy32BitOffset);
6062 Label* fixup_label = codegen_->NewStringBssEntryPatch(load);
6063 // /* GcRoot<mirror::Class> */ out = *address /* PC-relative */
6064 GenerateGcRootFieldLoad(load, out_loc, address, fixup_label);
6065 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86(load);
6066 codegen_->AddSlowPath(slow_path);
6067 __ testl(out, out);
6068 __ j(kEqual, slow_path->GetEntryLabel());
6069 __ Bind(slow_path->GetExitLabel());
6070 return;
6071 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006072 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07006073 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006074 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006075
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07006076 // TODO: Re-add the compiler code to do string dex cache lookup again.
Christina Wadsworth175d09b2016-08-31 16:26:01 -07006077 InvokeRuntimeCallingConvention calling_convention;
6078 __ movl(calling_convention.GetRegisterAt(0), Immediate(load->GetStringIndex()));
6079 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
6080 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006081}
6082
David Brazdilcb1c0552015-08-04 16:22:25 +01006083static Address GetExceptionTlsAddress() {
Andreas Gampe542451c2016-07-26 09:02:02 -07006084 return Address::Absolute(Thread::ExceptionOffset<kX86PointerSize>().Int32Value());
David Brazdilcb1c0552015-08-04 16:22:25 +01006085}
6086
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006087void LocationsBuilderX86::VisitLoadException(HLoadException* load) {
6088 LocationSummary* locations =
6089 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
6090 locations->SetOut(Location::RequiresRegister());
6091}
6092
6093void InstructionCodeGeneratorX86::VisitLoadException(HLoadException* load) {
David Brazdilcb1c0552015-08-04 16:22:25 +01006094 __ fs()->movl(load->GetLocations()->Out().AsRegister<Register>(), GetExceptionTlsAddress());
6095}
6096
6097void LocationsBuilderX86::VisitClearException(HClearException* clear) {
6098 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
6099}
6100
6101void InstructionCodeGeneratorX86::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
6102 __ fs()->movl(GetExceptionTlsAddress(), Immediate(0));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006103}
6104
6105void LocationsBuilderX86::VisitThrow(HThrow* instruction) {
6106 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006107 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006108 InvokeRuntimeCallingConvention calling_convention;
6109 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6110}
6111
6112void InstructionCodeGeneratorX86::VisitThrow(HThrow* instruction) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01006113 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006114 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006115}
6116
Roland Levillain7c1559a2015-12-15 10:55:36 +00006117static bool TypeCheckNeedsATemporary(TypeCheckKind type_check_kind) {
6118 return kEmitCompilerReadBarrier &&
Vladimir Marko953437b2016-08-24 08:30:46 +00006119 !kUseBakerReadBarrier &&
6120 (type_check_kind == TypeCheckKind::kAbstractClassCheck ||
Roland Levillain7c1559a2015-12-15 10:55:36 +00006121 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
6122 type_check_kind == TypeCheckKind::kArrayObjectCheck);
6123}
6124
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006125void LocationsBuilderX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006126 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain0d5a2812015-11-13 10:07:31 +00006127 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01006128 bool baker_read_barrier_slow_path = false;
Roland Levillain0d5a2812015-11-13 10:07:31 +00006129 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006130 case TypeCheckKind::kExactCheck:
6131 case TypeCheckKind::kAbstractClassCheck:
6132 case TypeCheckKind::kClassHierarchyCheck:
6133 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006134 call_kind =
6135 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Vladimir Marko70e97462016-08-09 11:04:26 +01006136 baker_read_barrier_slow_path = kUseBakerReadBarrier;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006137 break;
6138 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006139 case TypeCheckKind::kUnresolvedCheck:
6140 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006141 call_kind = LocationSummary::kCallOnSlowPath;
6142 break;
6143 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006144
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006145 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01006146 if (baker_read_barrier_slow_path) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006147 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01006148 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006149 locations->SetInAt(0, Location::RequiresRegister());
6150 locations->SetInAt(1, Location::Any());
6151 // Note that TypeCheckSlowPathX86 uses this "out" register too.
6152 locations->SetOut(Location::RequiresRegister());
6153 // When read barriers are enabled, we need a temporary register for
6154 // some cases.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006155 if (TypeCheckNeedsATemporary(type_check_kind)) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006156 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006157 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006158}
6159
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006160void InstructionCodeGeneratorX86::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006161 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006162 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006163 Location obj_loc = locations->InAt(0);
6164 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006165 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006166 Location out_loc = locations->Out();
6167 Register out = out_loc.AsRegister<Register>();
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006168 Location maybe_temp_loc = TypeCheckNeedsATemporary(type_check_kind) ?
Roland Levillain7c1559a2015-12-15 10:55:36 +00006169 locations->GetTemp(0) :
6170 Location::NoLocation();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006171 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006172 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6173 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6174 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Andreas Gampe85b62f22015-09-09 13:15:38 -07006175 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006176 NearLabel done, zero;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006177
6178 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006179 // Avoid null check if we know obj is not null.
6180 if (instruction->MustDoNullCheck()) {
6181 __ testl(obj, obj);
6182 __ j(kEqual, &zero);
6183 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006184
Roland Levillain0d5a2812015-11-13 10:07:31 +00006185 // /* HeapReference<Class> */ out = obj->klass_
Vladimir Marko953437b2016-08-24 08:30:46 +00006186 GenerateReferenceLoadTwoRegisters(instruction, out_loc, obj_loc, class_offset);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006187
Roland Levillain7c1559a2015-12-15 10:55:36 +00006188 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006189 case TypeCheckKind::kExactCheck: {
6190 if (cls.IsRegister()) {
6191 __ cmpl(out, cls.AsRegister<Register>());
6192 } else {
6193 DCHECK(cls.IsStackSlot()) << cls;
6194 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6195 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006196
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006197 // Classes must be equal for the instanceof to succeed.
6198 __ j(kNotEqual, &zero);
6199 __ movl(out, Immediate(1));
6200 __ jmp(&done);
6201 break;
6202 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006203
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006204 case TypeCheckKind::kAbstractClassCheck: {
6205 // If the class is abstract, we eagerly fetch the super class of the
6206 // object to avoid doing a comparison we know will fail.
6207 NearLabel loop;
6208 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006209 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006210 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006211 __ testl(out, out);
6212 // If `out` is null, we use it for the result, and jump to `done`.
6213 __ j(kEqual, &done);
6214 if (cls.IsRegister()) {
6215 __ cmpl(out, cls.AsRegister<Register>());
6216 } else {
6217 DCHECK(cls.IsStackSlot()) << cls;
6218 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6219 }
6220 __ j(kNotEqual, &loop);
6221 __ movl(out, Immediate(1));
6222 if (zero.IsLinked()) {
6223 __ jmp(&done);
6224 }
6225 break;
6226 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006227
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006228 case TypeCheckKind::kClassHierarchyCheck: {
6229 // Walk over the class hierarchy to find a match.
6230 NearLabel loop, success;
6231 __ Bind(&loop);
6232 if (cls.IsRegister()) {
6233 __ cmpl(out, cls.AsRegister<Register>());
6234 } else {
6235 DCHECK(cls.IsStackSlot()) << cls;
6236 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6237 }
6238 __ j(kEqual, &success);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006239 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006240 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006241 __ testl(out, out);
6242 __ j(kNotEqual, &loop);
6243 // If `out` is null, we use it for the result, and jump to `done`.
6244 __ jmp(&done);
6245 __ Bind(&success);
6246 __ movl(out, Immediate(1));
6247 if (zero.IsLinked()) {
6248 __ jmp(&done);
6249 }
6250 break;
6251 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006252
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006253 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006254 // Do an exact check.
6255 NearLabel exact_check;
6256 if (cls.IsRegister()) {
6257 __ cmpl(out, cls.AsRegister<Register>());
6258 } else {
6259 DCHECK(cls.IsStackSlot()) << cls;
6260 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6261 }
6262 __ j(kEqual, &exact_check);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006263 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006264 // /* HeapReference<Class> */ out = out->component_type_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006265 GenerateReferenceLoadOneRegister(instruction, out_loc, component_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006266 __ testl(out, out);
6267 // If `out` is null, we use it for the result, and jump to `done`.
6268 __ j(kEqual, &done);
6269 __ cmpw(Address(out, primitive_offset), Immediate(Primitive::kPrimNot));
6270 __ j(kNotEqual, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006271 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006272 __ movl(out, Immediate(1));
6273 __ jmp(&done);
6274 break;
6275 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006276
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006277 case TypeCheckKind::kArrayCheck: {
6278 if (cls.IsRegister()) {
6279 __ cmpl(out, cls.AsRegister<Register>());
6280 } else {
6281 DCHECK(cls.IsStackSlot()) << cls;
6282 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6283 }
6284 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain0d5a2812015-11-13 10:07:31 +00006285 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
6286 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006287 codegen_->AddSlowPath(slow_path);
6288 __ j(kNotEqual, slow_path->GetEntryLabel());
6289 __ movl(out, Immediate(1));
6290 if (zero.IsLinked()) {
6291 __ jmp(&done);
6292 }
6293 break;
6294 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006295
Calin Juravle98893e12015-10-02 21:05:03 +01006296 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006297 case TypeCheckKind::kInterfaceCheck: {
6298 // Note that we indeed only call on slow path, but we always go
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006299 // into the slow path for the unresolved and interface check
Roland Levillain0d5a2812015-11-13 10:07:31 +00006300 // cases.
6301 //
6302 // We cannot directly call the InstanceofNonTrivial runtime
6303 // entry point without resorting to a type checking slow path
6304 // here (i.e. by calling InvokeRuntime directly), as it would
6305 // require to assign fixed registers for the inputs of this
6306 // HInstanceOf instruction (following the runtime calling
6307 // convention), which might be cluttered by the potential first
6308 // read barrier emission at the beginning of this method.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006309 //
6310 // TODO: Introduce a new runtime entry point taking the object
6311 // to test (instead of its class) as argument, and let it deal
6312 // with the read barrier issues. This will let us refactor this
6313 // case of the `switch` code as it was previously (with a direct
6314 // call to the runtime not using a type checking slow path).
6315 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006316 DCHECK(locations->OnlyCallsOnSlowPath());
6317 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
6318 /* is_fatal */ false);
6319 codegen_->AddSlowPath(slow_path);
6320 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006321 if (zero.IsLinked()) {
6322 __ jmp(&done);
6323 }
6324 break;
6325 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006326 }
6327
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006328 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006329 __ Bind(&zero);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006330 __ xorl(out, out);
6331 }
6332
6333 if (done.IsLinked()) {
6334 __ Bind(&done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006335 }
6336
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006337 if (slow_path != nullptr) {
6338 __ Bind(slow_path->GetExitLabel());
6339 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006340}
6341
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006342void LocationsBuilderX86::VisitCheckCast(HCheckCast* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006343 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
6344 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006345 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
6346 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006347 case TypeCheckKind::kExactCheck:
6348 case TypeCheckKind::kAbstractClassCheck:
6349 case TypeCheckKind::kClassHierarchyCheck:
6350 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006351 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
6352 LocationSummary::kCallOnSlowPath :
6353 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006354 break;
6355 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006356 case TypeCheckKind::kUnresolvedCheck:
6357 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006358 call_kind = LocationSummary::kCallOnSlowPath;
6359 break;
6360 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006361 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
6362 locations->SetInAt(0, Location::RequiresRegister());
6363 locations->SetInAt(1, Location::Any());
6364 // Note that TypeCheckSlowPathX86 uses this "temp" register too.
6365 locations->AddTemp(Location::RequiresRegister());
6366 // When read barriers are enabled, we need an additional temporary
6367 // register for some cases.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006368 if (TypeCheckNeedsATemporary(type_check_kind)) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006369 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006370 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006371}
6372
6373void InstructionCodeGeneratorX86::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006374 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006375 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006376 Location obj_loc = locations->InAt(0);
6377 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006378 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006379 Location temp_loc = locations->GetTemp(0);
6380 Register temp = temp_loc.AsRegister<Register>();
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006381 Location maybe_temp2_loc = TypeCheckNeedsATemporary(type_check_kind) ?
Roland Levillain7c1559a2015-12-15 10:55:36 +00006382 locations->GetTemp(1) :
6383 Location::NoLocation();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006384 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
6385 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6386 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6387 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006388
Roland Levillain0d5a2812015-11-13 10:07:31 +00006389 bool is_type_check_slow_path_fatal =
6390 (type_check_kind == TypeCheckKind::kExactCheck ||
6391 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
6392 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
6393 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
6394 !instruction->CanThrowIntoCatchBlock();
6395 SlowPathCode* type_check_slow_path =
6396 new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
6397 is_type_check_slow_path_fatal);
6398 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006399
Roland Levillain0d5a2812015-11-13 10:07:31 +00006400 NearLabel done;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006401 // Avoid null check if we know obj is not null.
6402 if (instruction->MustDoNullCheck()) {
6403 __ testl(obj, obj);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006404 __ j(kEqual, &done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006405 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006406
Roland Levillain0d5a2812015-11-13 10:07:31 +00006407 // /* HeapReference<Class> */ temp = obj->klass_
Vladimir Marko953437b2016-08-24 08:30:46 +00006408 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006409
Roland Levillain0d5a2812015-11-13 10:07:31 +00006410 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006411 case TypeCheckKind::kExactCheck:
6412 case TypeCheckKind::kArrayCheck: {
6413 if (cls.IsRegister()) {
6414 __ cmpl(temp, cls.AsRegister<Register>());
6415 } else {
6416 DCHECK(cls.IsStackSlot()) << cls;
6417 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6418 }
6419 // Jump to slow path for throwing the exception or doing a
6420 // more involved array check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006421 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006422 break;
6423 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006424
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006425 case TypeCheckKind::kAbstractClassCheck: {
6426 // If the class is abstract, we eagerly fetch the super class of the
6427 // object to avoid doing a comparison we know will fail.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006428 NearLabel loop, compare_classes;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006429 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006430 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006431 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006432
6433 // If the class reference currently in `temp` is not null, jump
6434 // to the `compare_classes` label to compare it with the checked
6435 // class.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006436 __ testl(temp, temp);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006437 __ j(kNotEqual, &compare_classes);
6438 // Otherwise, jump to the slow path to throw the exception.
6439 //
6440 // But before, move back the object's class into `temp` before
6441 // going into the slow path, as it has been overwritten in the
6442 // meantime.
6443 // /* HeapReference<Class> */ temp = obj->klass_
Vladimir Marko953437b2016-08-24 08:30:46 +00006444 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006445 __ jmp(type_check_slow_path->GetEntryLabel());
6446
6447 __ Bind(&compare_classes);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006448 if (cls.IsRegister()) {
6449 __ cmpl(temp, cls.AsRegister<Register>());
6450 } else {
6451 DCHECK(cls.IsStackSlot()) << cls;
6452 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6453 }
6454 __ j(kNotEqual, &loop);
6455 break;
6456 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006457
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006458 case TypeCheckKind::kClassHierarchyCheck: {
6459 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006460 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006461 __ Bind(&loop);
6462 if (cls.IsRegister()) {
6463 __ cmpl(temp, cls.AsRegister<Register>());
6464 } else {
6465 DCHECK(cls.IsStackSlot()) << cls;
6466 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6467 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006468 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006469
Roland Levillain0d5a2812015-11-13 10:07:31 +00006470 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006471 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006472
6473 // If the class reference currently in `temp` is not null, jump
6474 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006475 __ testl(temp, temp);
6476 __ j(kNotEqual, &loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006477 // 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());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006485 break;
6486 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006487
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006488 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006489 // Do an exact check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006490 NearLabel check_non_primitive_component_type;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006491 if (cls.IsRegister()) {
6492 __ cmpl(temp, cls.AsRegister<Register>());
6493 } else {
6494 DCHECK(cls.IsStackSlot()) << cls;
6495 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6496 }
6497 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006498
6499 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006500 // /* HeapReference<Class> */ temp = temp->component_type_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006501 GenerateReferenceLoadOneRegister(instruction, temp_loc, component_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006502
6503 // If the component type is not null (i.e. the object is indeed
6504 // an array), jump to label `check_non_primitive_component_type`
6505 // to further check that this component type is not a primitive
6506 // type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006507 __ testl(temp, temp);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006508 __ j(kNotEqual, &check_non_primitive_component_type);
6509 // Otherwise, jump to the slow path to throw the exception.
6510 //
6511 // But before, move back the object's class into `temp` before
6512 // going into the slow path, as it has been overwritten in the
6513 // meantime.
6514 // /* HeapReference<Class> */ temp = obj->klass_
Vladimir Marko953437b2016-08-24 08:30:46 +00006515 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006516 __ jmp(type_check_slow_path->GetEntryLabel());
6517
6518 __ Bind(&check_non_primitive_component_type);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006519 __ cmpw(Address(temp, primitive_offset), Immediate(Primitive::kPrimNot));
Roland Levillain0d5a2812015-11-13 10:07:31 +00006520 __ j(kEqual, &done);
6521 // Same comment as above regarding `temp` and the slow path.
6522 // /* HeapReference<Class> */ temp = obj->klass_
Vladimir Marko953437b2016-08-24 08:30:46 +00006523 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006524 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006525 break;
6526 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006527
Calin Juravle98893e12015-10-02 21:05:03 +01006528 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006529 case TypeCheckKind::kInterfaceCheck:
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006530 // We always go into the type check slow path for the unresolved
6531 // and interface check cases.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006532 //
6533 // We cannot directly call the CheckCast runtime entry point
6534 // without resorting to a type checking slow path here (i.e. by
6535 // calling InvokeRuntime directly), as it would require to
6536 // assign fixed registers for the inputs of this HInstanceOf
6537 // instruction (following the runtime calling convention), which
6538 // might be cluttered by the potential first read barrier
6539 // emission at the beginning of this method.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006540 //
6541 // TODO: Introduce a new runtime entry point taking the object
6542 // to test (instead of its class) as argument, and let it deal
6543 // with the read barrier issues. This will let us refactor this
6544 // case of the `switch` code as it was previously (with a direct
6545 // call to the runtime not using a type checking slow path).
6546 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006547 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006548 break;
6549 }
6550 __ Bind(&done);
6551
Roland Levillain0d5a2812015-11-13 10:07:31 +00006552 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006553}
6554
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006555void LocationsBuilderX86::VisitMonitorOperation(HMonitorOperation* instruction) {
6556 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006557 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006558 InvokeRuntimeCallingConvention calling_convention;
6559 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6560}
6561
6562void InstructionCodeGeneratorX86::VisitMonitorOperation(HMonitorOperation* instruction) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01006563 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject
6564 : kQuickUnlockObject,
Alexandre Rames8158f282015-08-07 10:26:17 +01006565 instruction,
Serban Constantinescuba45db02016-07-12 22:53:02 +01006566 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006567 if (instruction->IsEnter()) {
6568 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
6569 } else {
6570 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
6571 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006572}
6573
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006574void LocationsBuilderX86::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
6575void LocationsBuilderX86::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
6576void LocationsBuilderX86::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
6577
6578void LocationsBuilderX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
6579 LocationSummary* locations =
6580 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6581 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
6582 || instruction->GetResultType() == Primitive::kPrimLong);
6583 locations->SetInAt(0, Location::RequiresRegister());
6584 locations->SetInAt(1, Location::Any());
6585 locations->SetOut(Location::SameAsFirstInput());
6586}
6587
6588void InstructionCodeGeneratorX86::VisitAnd(HAnd* instruction) {
6589 HandleBitwiseOperation(instruction);
6590}
6591
6592void InstructionCodeGeneratorX86::VisitOr(HOr* instruction) {
6593 HandleBitwiseOperation(instruction);
6594}
6595
6596void InstructionCodeGeneratorX86::VisitXor(HXor* instruction) {
6597 HandleBitwiseOperation(instruction);
6598}
6599
6600void InstructionCodeGeneratorX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
6601 LocationSummary* locations = instruction->GetLocations();
6602 Location first = locations->InAt(0);
6603 Location second = locations->InAt(1);
6604 DCHECK(first.Equals(locations->Out()));
6605
6606 if (instruction->GetResultType() == Primitive::kPrimInt) {
6607 if (second.IsRegister()) {
6608 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006609 __ andl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006610 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006611 __ orl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006612 } else {
6613 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006614 __ xorl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006615 }
6616 } else if (second.IsConstant()) {
6617 if (instruction->IsAnd()) {
Roland Levillain199f3362014-11-27 17:15:16 +00006618 __ andl(first.AsRegister<Register>(),
6619 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006620 } else if (instruction->IsOr()) {
Roland Levillain199f3362014-11-27 17:15:16 +00006621 __ orl(first.AsRegister<Register>(),
6622 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006623 } else {
6624 DCHECK(instruction->IsXor());
Roland Levillain199f3362014-11-27 17:15:16 +00006625 __ xorl(first.AsRegister<Register>(),
6626 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006627 }
6628 } else {
6629 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006630 __ andl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006631 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006632 __ orl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006633 } else {
6634 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006635 __ xorl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006636 }
6637 }
6638 } else {
6639 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
6640 if (second.IsRegisterPair()) {
6641 if (instruction->IsAnd()) {
6642 __ andl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
6643 __ andl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
6644 } else if (instruction->IsOr()) {
6645 __ orl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
6646 __ orl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
6647 } else {
6648 DCHECK(instruction->IsXor());
6649 __ xorl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
6650 __ xorl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
6651 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006652 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006653 if (instruction->IsAnd()) {
6654 __ andl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
6655 __ andl(first.AsRegisterPairHigh<Register>(),
6656 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
6657 } else if (instruction->IsOr()) {
6658 __ orl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
6659 __ orl(first.AsRegisterPairHigh<Register>(),
6660 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
6661 } else {
6662 DCHECK(instruction->IsXor());
6663 __ xorl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
6664 __ xorl(first.AsRegisterPairHigh<Register>(),
6665 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
6666 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006667 } else {
6668 DCHECK(second.IsConstant()) << second;
6669 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006670 int32_t low_value = Low32Bits(value);
6671 int32_t high_value = High32Bits(value);
6672 Immediate low(low_value);
6673 Immediate high(high_value);
6674 Register first_low = first.AsRegisterPairLow<Register>();
6675 Register first_high = first.AsRegisterPairHigh<Register>();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006676 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006677 if (low_value == 0) {
6678 __ xorl(first_low, first_low);
6679 } else if (low_value != -1) {
6680 __ andl(first_low, low);
6681 }
6682 if (high_value == 0) {
6683 __ xorl(first_high, first_high);
6684 } else if (high_value != -1) {
6685 __ andl(first_high, high);
6686 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006687 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006688 if (low_value != 0) {
6689 __ orl(first_low, low);
6690 }
6691 if (high_value != 0) {
6692 __ orl(first_high, high);
6693 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006694 } else {
6695 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006696 if (low_value != 0) {
6697 __ xorl(first_low, low);
6698 }
6699 if (high_value != 0) {
6700 __ xorl(first_high, high);
6701 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006702 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006703 }
6704 }
6705}
6706
Roland Levillain7c1559a2015-12-15 10:55:36 +00006707void InstructionCodeGeneratorX86::GenerateReferenceLoadOneRegister(HInstruction* instruction,
6708 Location out,
6709 uint32_t offset,
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006710 Location maybe_temp) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006711 Register out_reg = out.AsRegister<Register>();
6712 if (kEmitCompilerReadBarrier) {
6713 if (kUseBakerReadBarrier) {
6714 // Load with fast path based Baker's read barrier.
6715 // /* HeapReference<Object> */ out = *(out + offset)
6716 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00006717 instruction, out, out_reg, offset, /* needs_null_check */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006718 } else {
6719 // Load with slow path based read barrier.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006720 // Save the value of `out` into `maybe_temp` before overwriting it
Roland Levillain7c1559a2015-12-15 10:55:36 +00006721 // in the following move operation, as we will need it for the
6722 // read barrier below.
Vladimir Marko953437b2016-08-24 08:30:46 +00006723 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006724 __ movl(maybe_temp.AsRegister<Register>(), out_reg);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006725 // /* HeapReference<Object> */ out = *(out + offset)
6726 __ movl(out_reg, Address(out_reg, offset));
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006727 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006728 }
6729 } else {
6730 // Plain load with no read barrier.
6731 // /* HeapReference<Object> */ out = *(out + offset)
6732 __ movl(out_reg, Address(out_reg, offset));
6733 __ MaybeUnpoisonHeapReference(out_reg);
6734 }
6735}
6736
6737void InstructionCodeGeneratorX86::GenerateReferenceLoadTwoRegisters(HInstruction* instruction,
6738 Location out,
6739 Location obj,
Vladimir Marko953437b2016-08-24 08:30:46 +00006740 uint32_t offset) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006741 Register out_reg = out.AsRegister<Register>();
6742 Register obj_reg = obj.AsRegister<Register>();
6743 if (kEmitCompilerReadBarrier) {
6744 if (kUseBakerReadBarrier) {
6745 // Load with fast path based Baker's read barrier.
6746 // /* HeapReference<Object> */ out = *(obj + offset)
6747 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00006748 instruction, out, obj_reg, offset, /* needs_null_check */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006749 } else {
6750 // Load with slow path based read barrier.
6751 // /* HeapReference<Object> */ out = *(obj + offset)
6752 __ movl(out_reg, Address(obj_reg, offset));
6753 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
6754 }
6755 } else {
6756 // Plain load with no read barrier.
6757 // /* HeapReference<Object> */ out = *(obj + offset)
6758 __ movl(out_reg, Address(obj_reg, offset));
6759 __ MaybeUnpoisonHeapReference(out_reg);
6760 }
6761}
6762
6763void InstructionCodeGeneratorX86::GenerateGcRootFieldLoad(HInstruction* instruction,
6764 Location root,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006765 const Address& address,
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006766 Label* fixup_label,
6767 bool requires_read_barrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006768 Register root_reg = root.AsRegister<Register>();
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006769 if (requires_read_barrier) {
6770 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006771 if (kUseBakerReadBarrier) {
6772 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
6773 // Baker's read barrier are used:
6774 //
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006775 // root = *address;
Roland Levillain7c1559a2015-12-15 10:55:36 +00006776 // if (Thread::Current()->GetIsGcMarking()) {
6777 // root = ReadBarrier::Mark(root)
6778 // }
6779
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006780 // /* GcRoot<mirror::Object> */ root = *address
6781 __ movl(root_reg, address);
6782 if (fixup_label != nullptr) {
6783 __ Bind(fixup_label);
6784 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00006785 static_assert(
6786 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
6787 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
6788 "have different sizes.");
6789 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
6790 "art::mirror::CompressedReference<mirror::Object> and int32_t "
6791 "have different sizes.");
6792
Vladimir Marko953437b2016-08-24 08:30:46 +00006793 // Slow path marking the GC root `root`.
6794 SlowPathCode* slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathX86(
6795 instruction, root, /* unpoison */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006796 codegen_->AddSlowPath(slow_path);
6797
Andreas Gampe542451c2016-07-26 09:02:02 -07006798 __ fs()->cmpl(Address::Absolute(Thread::IsGcMarkingOffset<kX86PointerSize>().Int32Value()),
Roland Levillain7c1559a2015-12-15 10:55:36 +00006799 Immediate(0));
6800 __ j(kNotEqual, slow_path->GetEntryLabel());
6801 __ Bind(slow_path->GetExitLabel());
6802 } else {
6803 // GC root loaded through a slow path for read barriers other
6804 // than Baker's.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006805 // /* GcRoot<mirror::Object>* */ root = address
6806 __ leal(root_reg, address);
6807 if (fixup_label != nullptr) {
6808 __ Bind(fixup_label);
6809 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00006810 // /* mirror::Object* */ root = root->Read()
6811 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
6812 }
6813 } else {
6814 // Plain GC root load with no read barrier.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006815 // /* GcRoot<mirror::Object> */ root = *address
6816 __ movl(root_reg, address);
6817 if (fixup_label != nullptr) {
6818 __ Bind(fixup_label);
6819 }
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006820 // Note that GC roots are not affected by heap poisoning, thus we
6821 // do not have to unpoison `root_reg` here.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006822 }
6823}
6824
6825void CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
6826 Location ref,
6827 Register obj,
6828 uint32_t offset,
Roland Levillain7c1559a2015-12-15 10:55:36 +00006829 bool needs_null_check) {
6830 DCHECK(kEmitCompilerReadBarrier);
6831 DCHECK(kUseBakerReadBarrier);
6832
6833 // /* HeapReference<Object> */ ref = *(obj + offset)
6834 Address src(obj, offset);
Vladimir Marko953437b2016-08-24 08:30:46 +00006835 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, needs_null_check);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006836}
6837
6838void CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
6839 Location ref,
6840 Register obj,
6841 uint32_t data_offset,
6842 Location index,
Roland Levillain7c1559a2015-12-15 10:55:36 +00006843 bool needs_null_check) {
6844 DCHECK(kEmitCompilerReadBarrier);
6845 DCHECK(kUseBakerReadBarrier);
6846
Roland Levillain3d312422016-06-23 13:53:42 +01006847 static_assert(
6848 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
6849 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain7c1559a2015-12-15 10:55:36 +00006850 // /* HeapReference<Object> */ ref =
6851 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006852 Address src = CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset);
Vladimir Marko953437b2016-08-24 08:30:46 +00006853 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, needs_null_check);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006854}
6855
6856void CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
6857 Location ref,
6858 Register obj,
6859 const Address& src,
Roland Levillain7c1559a2015-12-15 10:55:36 +00006860 bool needs_null_check) {
6861 DCHECK(kEmitCompilerReadBarrier);
6862 DCHECK(kUseBakerReadBarrier);
6863
6864 // In slow path based read barriers, the read barrier call is
6865 // inserted after the original load. However, in fast path based
6866 // Baker's read barriers, we need to perform the load of
6867 // mirror::Object::monitor_ *before* the original reference load.
6868 // This load-load ordering is required by the read barrier.
6869 // The fast path/slow path (for Baker's algorithm) should look like:
6870 //
6871 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
6872 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
6873 // HeapReference<Object> ref = *src; // Original reference load.
6874 // bool is_gray = (rb_state == ReadBarrier::gray_ptr_);
6875 // if (is_gray) {
6876 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
6877 // }
6878 //
6879 // Note: the original implementation in ReadBarrier::Barrier is
6880 // slightly more complex as:
6881 // - it implements the load-load fence using a data dependency on
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006882 // the high-bits of rb_state, which are expected to be all zeroes
6883 // (we use CodeGeneratorX86::GenerateMemoryBarrier instead here,
6884 // which is a no-op thanks to the x86 memory model);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006885 // - it performs additional checks that we do not do here for
6886 // performance reasons.
6887
6888 Register ref_reg = ref.AsRegister<Register>();
Roland Levillain7c1559a2015-12-15 10:55:36 +00006889 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
6890
Vladimir Marko953437b2016-08-24 08:30:46 +00006891 // Given the numeric representation, it's enough to check the low bit of the rb_state.
6892 static_assert(ReadBarrier::white_ptr_ == 0, "Expecting white to have value 0");
6893 static_assert(ReadBarrier::gray_ptr_ == 1, "Expecting gray to have value 1");
6894 static_assert(ReadBarrier::black_ptr_ == 2, "Expecting black to have value 2");
6895 constexpr uint32_t gray_byte_position = LockWord::kReadBarrierStateShift / kBitsPerByte;
6896 constexpr uint32_t gray_bit_position = LockWord::kReadBarrierStateShift % kBitsPerByte;
6897 constexpr int32_t test_value = static_cast<int8_t>(1 << gray_bit_position);
6898
6899 // if (rb_state == ReadBarrier::gray_ptr_)
6900 // ref = ReadBarrier::Mark(ref);
6901 // At this point, just do the "if" and make sure that flags are preserved until the branch.
6902 __ testb(Address(obj, monitor_offset + gray_byte_position), Immediate(test_value));
Roland Levillain7c1559a2015-12-15 10:55:36 +00006903 if (needs_null_check) {
6904 MaybeRecordImplicitNullCheck(instruction);
6905 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00006906
6907 // Load fence to prevent load-load reordering.
6908 // Note that this is a no-op, thanks to the x86 memory model.
6909 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
6910
6911 // The actual reference load.
6912 // /* HeapReference<Object> */ ref = *src
Vladimir Marko953437b2016-08-24 08:30:46 +00006913 __ movl(ref_reg, src); // Flags are unaffected.
6914
6915 // Note: Reference unpoisoning modifies the flags, so we need to delay it after the branch.
6916 // Slow path marking the object `ref` when it is gray.
6917 SlowPathCode* slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathX86(
6918 instruction, ref, /* unpoison */ true);
6919 AddSlowPath(slow_path);
6920
6921 // We have done the "if" of the gray bit check above, now branch based on the flags.
6922 __ j(kNotZero, slow_path->GetEntryLabel());
Roland Levillain7c1559a2015-12-15 10:55:36 +00006923
6924 // Object* ref = ref_addr->AsMirrorPtr()
6925 __ MaybeUnpoisonHeapReference(ref_reg);
6926
Roland Levillain7c1559a2015-12-15 10:55:36 +00006927 __ Bind(slow_path->GetExitLabel());
6928}
6929
6930void CodeGeneratorX86::GenerateReadBarrierSlow(HInstruction* instruction,
6931 Location out,
6932 Location ref,
6933 Location obj,
6934 uint32_t offset,
6935 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006936 DCHECK(kEmitCompilerReadBarrier);
6937
Roland Levillain7c1559a2015-12-15 10:55:36 +00006938 // Insert a slow path based read barrier *after* the reference load.
6939 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00006940 // If heap poisoning is enabled, the unpoisoning of the loaded
6941 // reference will be carried out by the runtime within the slow
6942 // path.
6943 //
6944 // Note that `ref` currently does not get unpoisoned (when heap
6945 // poisoning is enabled), which is alright as the `ref` argument is
6946 // not used by the artReadBarrierSlow entry point.
6947 //
6948 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
6949 SlowPathCode* slow_path = new (GetGraph()->GetArena())
6950 ReadBarrierForHeapReferenceSlowPathX86(instruction, out, ref, obj, offset, index);
6951 AddSlowPath(slow_path);
6952
Roland Levillain0d5a2812015-11-13 10:07:31 +00006953 __ jmp(slow_path->GetEntryLabel());
6954 __ Bind(slow_path->GetExitLabel());
6955}
6956
Roland Levillain7c1559a2015-12-15 10:55:36 +00006957void CodeGeneratorX86::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
6958 Location out,
6959 Location ref,
6960 Location obj,
6961 uint32_t offset,
6962 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006963 if (kEmitCompilerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006964 // Baker's read barriers shall be handled by the fast path
6965 // (CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier).
6966 DCHECK(!kUseBakerReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006967 // If heap poisoning is enabled, unpoisoning will be taken care of
6968 // by the runtime within the slow path.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006969 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006970 } else if (kPoisonHeapReferences) {
6971 __ UnpoisonHeapReference(out.AsRegister<Register>());
6972 }
6973}
6974
Roland Levillain7c1559a2015-12-15 10:55:36 +00006975void CodeGeneratorX86::GenerateReadBarrierForRootSlow(HInstruction* instruction,
6976 Location out,
6977 Location root) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006978 DCHECK(kEmitCompilerReadBarrier);
6979
Roland Levillain7c1559a2015-12-15 10:55:36 +00006980 // Insert a slow path based read barrier *after* the GC root load.
6981 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00006982 // Note that GC roots are not affected by heap poisoning, so we do
6983 // not need to do anything special for this here.
6984 SlowPathCode* slow_path =
6985 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathX86(instruction, out, root);
6986 AddSlowPath(slow_path);
6987
Roland Levillain0d5a2812015-11-13 10:07:31 +00006988 __ jmp(slow_path->GetEntryLabel());
6989 __ Bind(slow_path->GetExitLabel());
6990}
6991
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006992void LocationsBuilderX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00006993 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00006994 LOG(FATAL) << "Unreachable";
6995}
6996
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006997void InstructionCodeGeneratorX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00006998 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00006999 LOG(FATAL) << "Unreachable";
7000}
7001
Mark Mendellfe57faa2015-09-18 09:26:15 -04007002// Simple implementation of packed switch - generate cascaded compare/jumps.
7003void LocationsBuilderX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7004 LocationSummary* locations =
7005 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
7006 locations->SetInAt(0, Location::RequiresRegister());
7007}
7008
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007009void InstructionCodeGeneratorX86::GenPackedSwitchWithCompares(Register value_reg,
7010 int32_t lower_bound,
7011 uint32_t num_entries,
7012 HBasicBlock* switch_block,
7013 HBasicBlock* default_block) {
7014 // Figure out the correct compare values and jump conditions.
7015 // Handle the first compare/branch as a special case because it might
7016 // jump to the default case.
7017 DCHECK_GT(num_entries, 2u);
7018 Condition first_condition;
7019 uint32_t index;
7020 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
7021 if (lower_bound != 0) {
7022 first_condition = kLess;
7023 __ cmpl(value_reg, Immediate(lower_bound));
7024 __ j(first_condition, codegen_->GetLabelOf(default_block));
7025 __ j(kEqual, codegen_->GetLabelOf(successors[0]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007026
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007027 index = 1;
7028 } else {
7029 // Handle all the compare/jumps below.
7030 first_condition = kBelow;
7031 index = 0;
7032 }
7033
7034 // Handle the rest of the compare/jumps.
7035 for (; index + 1 < num_entries; index += 2) {
7036 int32_t compare_to_value = lower_bound + index + 1;
7037 __ cmpl(value_reg, Immediate(compare_to_value));
7038 // Jump to successors[index] if value < case_value[index].
7039 __ j(first_condition, codegen_->GetLabelOf(successors[index]));
7040 // Jump to successors[index + 1] if value == case_value[index + 1].
7041 __ j(kEqual, codegen_->GetLabelOf(successors[index + 1]));
7042 }
7043
7044 if (index != num_entries) {
7045 // There are an odd number of entries. Handle the last one.
7046 DCHECK_EQ(index + 1, num_entries);
7047 __ cmpl(value_reg, Immediate(lower_bound + index));
7048 __ j(kEqual, codegen_->GetLabelOf(successors[index]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007049 }
7050
7051 // And the default for any other value.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007052 if (!codegen_->GoesToNextBlock(switch_block, default_block)) {
7053 __ jmp(codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007054 }
7055}
7056
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007057void InstructionCodeGeneratorX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7058 int32_t lower_bound = switch_instr->GetStartValue();
7059 uint32_t num_entries = switch_instr->GetNumEntries();
7060 LocationSummary* locations = switch_instr->GetLocations();
7061 Register value_reg = locations->InAt(0).AsRegister<Register>();
7062
7063 GenPackedSwitchWithCompares(value_reg,
7064 lower_bound,
7065 num_entries,
7066 switch_instr->GetBlock(),
7067 switch_instr->GetDefaultBlock());
7068}
7069
Mark Mendell805b3b52015-09-18 14:10:29 -04007070void LocationsBuilderX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
7071 LocationSummary* locations =
7072 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
7073 locations->SetInAt(0, Location::RequiresRegister());
7074
7075 // Constant area pointer.
7076 locations->SetInAt(1, Location::RequiresRegister());
7077
7078 // And the temporary we need.
7079 locations->AddTemp(Location::RequiresRegister());
7080}
7081
7082void InstructionCodeGeneratorX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
7083 int32_t lower_bound = switch_instr->GetStartValue();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007084 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendell805b3b52015-09-18 14:10:29 -04007085 LocationSummary* locations = switch_instr->GetLocations();
7086 Register value_reg = locations->InAt(0).AsRegister<Register>();
7087 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
7088
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007089 if (num_entries <= kPackedSwitchJumpTableThreshold) {
7090 GenPackedSwitchWithCompares(value_reg,
7091 lower_bound,
7092 num_entries,
7093 switch_instr->GetBlock(),
7094 default_block);
7095 return;
7096 }
7097
Mark Mendell805b3b52015-09-18 14:10:29 -04007098 // Optimizing has a jump area.
7099 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
7100 Register constant_area = locations->InAt(1).AsRegister<Register>();
7101
7102 // Remove the bias, if needed.
7103 if (lower_bound != 0) {
7104 __ leal(temp_reg, Address(value_reg, -lower_bound));
7105 value_reg = temp_reg;
7106 }
7107
7108 // Is the value in range?
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007109 DCHECK_GE(num_entries, 1u);
Mark Mendell805b3b52015-09-18 14:10:29 -04007110 __ cmpl(value_reg, Immediate(num_entries - 1));
7111 __ j(kAbove, codegen_->GetLabelOf(default_block));
7112
7113 // We are in the range of the table.
7114 // Load (target-constant_area) from the jump table, indexing by the value.
7115 __ movl(temp_reg, codegen_->LiteralCaseTable(switch_instr, constant_area, value_reg));
7116
7117 // Compute the actual target address by adding in constant_area.
7118 __ addl(temp_reg, constant_area);
7119
7120 // And jump.
7121 __ jmp(temp_reg);
7122}
7123
Mark Mendell0616ae02015-04-17 12:49:27 -04007124void LocationsBuilderX86::VisitX86ComputeBaseMethodAddress(
7125 HX86ComputeBaseMethodAddress* insn) {
7126 LocationSummary* locations =
7127 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
7128 locations->SetOut(Location::RequiresRegister());
7129}
7130
7131void InstructionCodeGeneratorX86::VisitX86ComputeBaseMethodAddress(
7132 HX86ComputeBaseMethodAddress* insn) {
7133 LocationSummary* locations = insn->GetLocations();
7134 Register reg = locations->Out().AsRegister<Register>();
7135
7136 // Generate call to next instruction.
7137 Label next_instruction;
7138 __ call(&next_instruction);
7139 __ Bind(&next_instruction);
7140
7141 // Remember this offset for later use with constant area.
7142 codegen_->SetMethodAddressOffset(GetAssembler()->CodeSize());
7143
7144 // Grab the return address off the stack.
7145 __ popl(reg);
7146}
7147
7148void LocationsBuilderX86::VisitX86LoadFromConstantTable(
7149 HX86LoadFromConstantTable* insn) {
7150 LocationSummary* locations =
7151 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
7152
7153 locations->SetInAt(0, Location::RequiresRegister());
7154 locations->SetInAt(1, Location::ConstantLocation(insn->GetConstant()));
7155
7156 // If we don't need to be materialized, we only need the inputs to be set.
David Brazdilb3e773e2016-01-26 11:28:37 +00007157 if (insn->IsEmittedAtUseSite()) {
Mark Mendell0616ae02015-04-17 12:49:27 -04007158 return;
7159 }
7160
7161 switch (insn->GetType()) {
7162 case Primitive::kPrimFloat:
7163 case Primitive::kPrimDouble:
7164 locations->SetOut(Location::RequiresFpuRegister());
7165 break;
7166
7167 case Primitive::kPrimInt:
7168 locations->SetOut(Location::RequiresRegister());
7169 break;
7170
7171 default:
7172 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
7173 }
7174}
7175
7176void InstructionCodeGeneratorX86::VisitX86LoadFromConstantTable(HX86LoadFromConstantTable* insn) {
David Brazdilb3e773e2016-01-26 11:28:37 +00007177 if (insn->IsEmittedAtUseSite()) {
Mark Mendell0616ae02015-04-17 12:49:27 -04007178 return;
7179 }
7180
7181 LocationSummary* locations = insn->GetLocations();
7182 Location out = locations->Out();
7183 Register const_area = locations->InAt(0).AsRegister<Register>();
7184 HConstant *value = insn->GetConstant();
7185
7186 switch (insn->GetType()) {
7187 case Primitive::kPrimFloat:
7188 __ movss(out.AsFpuRegister<XmmRegister>(),
7189 codegen_->LiteralFloatAddress(value->AsFloatConstant()->GetValue(), const_area));
7190 break;
7191
7192 case Primitive::kPrimDouble:
7193 __ movsd(out.AsFpuRegister<XmmRegister>(),
7194 codegen_->LiteralDoubleAddress(value->AsDoubleConstant()->GetValue(), const_area));
7195 break;
7196
7197 case Primitive::kPrimInt:
7198 __ movl(out.AsRegister<Register>(),
7199 codegen_->LiteralInt32Address(value->AsIntConstant()->GetValue(), const_area));
7200 break;
7201
7202 default:
7203 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
7204 }
7205}
7206
Mark Mendell0616ae02015-04-17 12:49:27 -04007207/**
7208 * Class to handle late fixup of offsets into constant area.
7209 */
Vladimir Marko5233f932015-09-29 19:01:15 +01007210class RIPFixup : public AssemblerFixup, public ArenaObject<kArenaAllocCodeGenerator> {
Mark Mendell0616ae02015-04-17 12:49:27 -04007211 public:
Mark Mendell805b3b52015-09-18 14:10:29 -04007212 RIPFixup(CodeGeneratorX86& codegen, size_t offset)
7213 : codegen_(&codegen), offset_into_constant_area_(offset) {}
7214
7215 protected:
7216 void SetOffset(size_t offset) { offset_into_constant_area_ = offset; }
7217
7218 CodeGeneratorX86* codegen_;
Mark Mendell0616ae02015-04-17 12:49:27 -04007219
7220 private:
7221 void Process(const MemoryRegion& region, int pos) OVERRIDE {
7222 // Patch the correct offset for the instruction. The place to patch is the
7223 // last 4 bytes of the instruction.
7224 // The value to patch is the distance from the offset in the constant area
7225 // from the address computed by the HX86ComputeBaseMethodAddress instruction.
Mark Mendell805b3b52015-09-18 14:10:29 -04007226 int32_t constant_offset = codegen_->ConstantAreaStart() + offset_into_constant_area_;
7227 int32_t relative_position = constant_offset - codegen_->GetMethodAddressOffset();;
Mark Mendell0616ae02015-04-17 12:49:27 -04007228
7229 // Patch in the right value.
7230 region.StoreUnaligned<int32_t>(pos - 4, relative_position);
7231 }
7232
Mark Mendell0616ae02015-04-17 12:49:27 -04007233 // Location in constant area that the fixup refers to.
Mark Mendell805b3b52015-09-18 14:10:29 -04007234 int32_t offset_into_constant_area_;
Mark Mendell0616ae02015-04-17 12:49:27 -04007235};
7236
Mark Mendell805b3b52015-09-18 14:10:29 -04007237/**
7238 * Class to handle late fixup of offsets to a jump table that will be created in the
7239 * constant area.
7240 */
7241class JumpTableRIPFixup : public RIPFixup {
7242 public:
7243 JumpTableRIPFixup(CodeGeneratorX86& codegen, HX86PackedSwitch* switch_instr)
7244 : RIPFixup(codegen, static_cast<size_t>(-1)), switch_instr_(switch_instr) {}
7245
7246 void CreateJumpTable() {
7247 X86Assembler* assembler = codegen_->GetAssembler();
7248
7249 // Ensure that the reference to the jump table has the correct offset.
7250 const int32_t offset_in_constant_table = assembler->ConstantAreaSize();
7251 SetOffset(offset_in_constant_table);
7252
7253 // The label values in the jump table are computed relative to the
7254 // instruction addressing the constant area.
7255 const int32_t relative_offset = codegen_->GetMethodAddressOffset();
7256
7257 // Populate the jump table with the correct values for the jump table.
7258 int32_t num_entries = switch_instr_->GetNumEntries();
7259 HBasicBlock* block = switch_instr_->GetBlock();
7260 const ArenaVector<HBasicBlock*>& successors = block->GetSuccessors();
7261 // The value that we want is the target offset - the position of the table.
7262 for (int32_t i = 0; i < num_entries; i++) {
7263 HBasicBlock* b = successors[i];
7264 Label* l = codegen_->GetLabelOf(b);
7265 DCHECK(l->IsBound());
7266 int32_t offset_to_block = l->Position() - relative_offset;
7267 assembler->AppendInt32(offset_to_block);
7268 }
7269 }
7270
7271 private:
7272 const HX86PackedSwitch* switch_instr_;
7273};
7274
7275void CodeGeneratorX86::Finalize(CodeAllocator* allocator) {
7276 // Generate the constant area if needed.
7277 X86Assembler* assembler = GetAssembler();
7278 if (!assembler->IsConstantAreaEmpty() || !fixups_to_jump_tables_.empty()) {
7279 // Align to 4 byte boundary to reduce cache misses, as the data is 4 and 8
7280 // byte values.
7281 assembler->Align(4, 0);
7282 constant_area_start_ = assembler->CodeSize();
7283
7284 // Populate any jump tables.
7285 for (auto jump_table : fixups_to_jump_tables_) {
7286 jump_table->CreateJumpTable();
7287 }
7288
7289 // And now add the constant area to the generated code.
7290 assembler->AddConstantArea();
7291 }
7292
7293 // And finish up.
7294 CodeGenerator::Finalize(allocator);
7295}
7296
Mark Mendell0616ae02015-04-17 12:49:27 -04007297Address CodeGeneratorX86::LiteralDoubleAddress(double v, Register reg) {
7298 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddDouble(v));
7299 return Address(reg, kDummy32BitOffset, fixup);
7300}
7301
7302Address CodeGeneratorX86::LiteralFloatAddress(float v, Register reg) {
7303 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddFloat(v));
7304 return Address(reg, kDummy32BitOffset, fixup);
7305}
7306
7307Address CodeGeneratorX86::LiteralInt32Address(int32_t v, Register reg) {
7308 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt32(v));
7309 return Address(reg, kDummy32BitOffset, fixup);
7310}
7311
7312Address CodeGeneratorX86::LiteralInt64Address(int64_t v, Register reg) {
7313 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt64(v));
7314 return Address(reg, kDummy32BitOffset, fixup);
7315}
7316
Aart Bika19616e2016-02-01 18:57:58 -08007317void CodeGeneratorX86::Load32BitValue(Register dest, int32_t value) {
7318 if (value == 0) {
7319 __ xorl(dest, dest);
7320 } else {
7321 __ movl(dest, Immediate(value));
7322 }
7323}
7324
7325void CodeGeneratorX86::Compare32BitValue(Register dest, int32_t value) {
7326 if (value == 0) {
7327 __ testl(dest, dest);
7328 } else {
7329 __ cmpl(dest, Immediate(value));
7330 }
7331}
7332
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007333void CodeGeneratorX86::GenerateIntCompare(Location lhs, Location rhs) {
7334 Register lhs_reg = lhs.AsRegister<Register>();
7335 if (rhs.IsConstant()) {
7336 int32_t value = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
7337 Compare32BitValue(lhs_reg, value);
7338 } else if (rhs.IsStackSlot()) {
7339 __ cmpl(lhs_reg, Address(ESP, rhs.GetStackIndex()));
7340 } else {
7341 __ cmpl(lhs_reg, rhs.AsRegister<Register>());
7342 }
7343}
7344
7345Address CodeGeneratorX86::ArrayAddress(Register obj,
7346 Location index,
7347 ScaleFactor scale,
7348 uint32_t data_offset) {
7349 return index.IsConstant() ?
7350 Address(obj, (index.GetConstant()->AsIntConstant()->GetValue() << scale) + data_offset) :
7351 Address(obj, index.AsRegister<Register>(), scale, data_offset);
7352}
7353
Mark Mendell805b3b52015-09-18 14:10:29 -04007354Address CodeGeneratorX86::LiteralCaseTable(HX86PackedSwitch* switch_instr,
7355 Register reg,
7356 Register value) {
7357 // Create a fixup to be used to create and address the jump table.
7358 JumpTableRIPFixup* table_fixup =
7359 new (GetGraph()->GetArena()) JumpTableRIPFixup(*this, switch_instr);
7360
7361 // We have to populate the jump tables.
7362 fixups_to_jump_tables_.push_back(table_fixup);
7363
7364 // We want a scaled address, as we are extracting the correct offset from the table.
7365 return Address(reg, value, TIMES_4, kDummy32BitOffset, table_fixup);
7366}
7367
Andreas Gampe85b62f22015-09-09 13:15:38 -07007368// TODO: target as memory.
7369void CodeGeneratorX86::MoveFromReturnRegister(Location target, Primitive::Type type) {
7370 if (!target.IsValid()) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007371 DCHECK_EQ(type, Primitive::kPrimVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07007372 return;
7373 }
7374
7375 DCHECK_NE(type, Primitive::kPrimVoid);
7376
7377 Location return_loc = InvokeDexCallingConventionVisitorX86().GetReturnLocation(type);
7378 if (target.Equals(return_loc)) {
7379 return;
7380 }
7381
7382 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
7383 // with the else branch.
7384 if (type == Primitive::kPrimLong) {
7385 HParallelMove parallel_move(GetGraph()->GetArena());
7386 parallel_move.AddMove(return_loc.ToLow(), target.ToLow(), Primitive::kPrimInt, nullptr);
7387 parallel_move.AddMove(return_loc.ToHigh(), target.ToHigh(), Primitive::kPrimInt, nullptr);
7388 GetMoveResolver()->EmitNativeCode(&parallel_move);
7389 } else {
7390 // Let the parallel move resolver take care of all of this.
7391 HParallelMove parallel_move(GetGraph()->GetArena());
7392 parallel_move.AddMove(return_loc, target, type, nullptr);
7393 GetMoveResolver()->EmitNativeCode(&parallel_move);
7394 }
7395}
7396
Roland Levillain4d027112015-07-01 15:41:14 +01007397#undef __
7398
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00007399} // namespace x86
7400} // namespace art