blob: a7051aeeb13c7c8008062067c193989d564f1c24 [file] [log] [blame]
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "code_generator_x86.h"
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +010018
Mathieu Chartiere401d142015-04-22 13:56:20 -070019#include "art_method.h"
Guillaume Sanchez0f88e872015-03-30 17:55:45 +010020#include "code_generator_utils.h"
Vladimir Marko58155012015-08-19 12:49:41 +000021#include "compiled_method.h"
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +010022#include "entrypoints/quick/quick_entrypoints.h"
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +000023#include "entrypoints/quick/quick_entrypoints_enum.h"
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +010024#include "gc/accounting/card_table.h"
Mark Mendell09ed1a32015-03-25 08:30:06 -040025#include "intrinsics.h"
26#include "intrinsics_x86.h"
Ian Rogers7e70b002014-10-08 11:47:24 -070027#include "mirror/array-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070028#include "mirror/class-inl.h"
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +010029#include "thread.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000030#include "utils/assembler.h"
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010031#include "utils/stack_checks.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000032#include "utils/x86/assembler_x86.h"
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010033#include "utils/x86/managed_register_x86.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000034
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000035namespace art {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010036
Roland Levillain0d5a2812015-11-13 10:07:31 +000037template<class MirrorType>
38class GcRoot;
39
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000040namespace x86 {
41
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010042static constexpr int kCurrentMethodStackOffset = 0;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +010043static constexpr Register kMethodRegisterArgument = EAX;
Mark Mendell5f874182015-03-04 15:42:45 -050044static constexpr Register kCoreCalleeSaves[] = { EBP, ESI, EDI };
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010045
Mark Mendell24f2dfa2015-01-14 19:51:45 -050046static constexpr int kC2ConditionMask = 0x400;
47
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +000048static constexpr int kFakeReturnRegister = Register(8);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +000049
Roland Levillain7cbd27f2016-08-11 23:53:33 +010050// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
51#define __ down_cast<X86Assembler*>(codegen->GetAssembler())-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -070052#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kX86PointerSize, x).Int32Value()
Nicolas Geoffraye5038322014-07-04 09:41:32 +010053
Andreas Gampe85b62f22015-09-09 13:15:38 -070054class NullCheckSlowPathX86 : public SlowPathCode {
Nicolas Geoffraye5038322014-07-04 09:41:32 +010055 public:
David Srbecky9cd6d372016-02-09 15:24:47 +000056 explicit NullCheckSlowPathX86(HNullCheck* instruction) : SlowPathCode(instruction) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +010057
Alexandre Rames2ed20af2015-03-06 13:55:35 +000058 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames8158f282015-08-07 10:26:17 +010059 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffraye5038322014-07-04 09:41:32 +010060 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +000061 if (instruction_->CanThrowIntoCatchBlock()) {
62 // Live registers will be restored in the catch block if caught.
63 SaveLiveRegisters(codegen, instruction_->GetLocations());
64 }
Serban Constantinescuba45db02016-07-12 22:53:02 +010065 x86_codegen->InvokeRuntime(kQuickThrowNullPointer,
Alexandre Rames8158f282015-08-07 10:26:17 +010066 instruction_,
67 instruction_->GetDexPc(),
68 this);
Roland Levillain888d0672015-11-23 18:53:50 +000069 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +010070 }
71
Alexandre Rames8158f282015-08-07 10:26:17 +010072 bool IsFatal() const OVERRIDE { return true; }
73
Alexandre Rames9931f312015-06-19 14:47:01 +010074 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathX86"; }
75
Nicolas Geoffraye5038322014-07-04 09:41:32 +010076 private:
Nicolas Geoffraye5038322014-07-04 09:41:32 +010077 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathX86);
78};
79
Andreas Gampe85b62f22015-09-09 13:15:38 -070080class DivZeroCheckSlowPathX86 : public SlowPathCode {
Calin Juravled0d48522014-11-04 16:40:20 +000081 public:
David Srbecky9cd6d372016-02-09 15:24:47 +000082 explicit DivZeroCheckSlowPathX86(HDivZeroCheck* instruction) : SlowPathCode(instruction) {}
Calin Juravled0d48522014-11-04 16:40:20 +000083
Alexandre Rames2ed20af2015-03-06 13:55:35 +000084 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames8158f282015-08-07 10:26:17 +010085 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Calin Juravled0d48522014-11-04 16:40:20 +000086 __ Bind(GetEntryLabel());
Serban Constantinescuba45db02016-07-12 22:53:02 +010087 x86_codegen->InvokeRuntime(kQuickThrowDivZero, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +000088 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
Calin Juravled0d48522014-11-04 16:40:20 +000089 }
90
Alexandre Rames8158f282015-08-07 10:26:17 +010091 bool IsFatal() const OVERRIDE { return true; }
92
Alexandre Rames9931f312015-06-19 14:47:01 +010093 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathX86"; }
94
Calin Juravled0d48522014-11-04 16:40:20 +000095 private:
Calin Juravled0d48522014-11-04 16:40:20 +000096 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathX86);
97};
98
Andreas Gampe85b62f22015-09-09 13:15:38 -070099class DivRemMinusOneSlowPathX86 : public SlowPathCode {
Calin Juravled0d48522014-11-04 16:40:20 +0000100 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000101 DivRemMinusOneSlowPathX86(HInstruction* instruction, Register reg, bool is_div)
102 : SlowPathCode(instruction), reg_(reg), is_div_(is_div) {}
Calin Juravled0d48522014-11-04 16:40:20 +0000103
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000104 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +0000105 __ Bind(GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +0000106 if (is_div_) {
107 __ negl(reg_);
108 } else {
109 __ movl(reg_, Immediate(0));
110 }
Calin Juravled0d48522014-11-04 16:40:20 +0000111 __ jmp(GetExitLabel());
112 }
113
Alexandre Rames9931f312015-06-19 14:47:01 +0100114 const char* GetDescription() const OVERRIDE { return "DivRemMinusOneSlowPathX86"; }
115
Calin Juravled0d48522014-11-04 16:40:20 +0000116 private:
117 Register reg_;
Calin Juravlebacfec32014-11-14 15:54:36 +0000118 bool is_div_;
119 DISALLOW_COPY_AND_ASSIGN(DivRemMinusOneSlowPathX86);
Calin Juravled0d48522014-11-04 16:40:20 +0000120};
121
Andreas Gampe85b62f22015-09-09 13:15:38 -0700122class BoundsCheckSlowPathX86 : public SlowPathCode {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100123 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000124 explicit BoundsCheckSlowPathX86(HBoundsCheck* instruction) : SlowPathCode(instruction) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100125
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000126 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100127 LocationSummary* locations = instruction_->GetLocations();
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100128 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100129 __ Bind(GetEntryLabel());
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000130 // We're moving two locations to locations that could overlap, so we need a parallel
131 // move resolver.
David Brazdil77a48ae2015-09-15 12:34:04 +0000132 if (instruction_->CanThrowIntoCatchBlock()) {
133 // Live registers will be restored in the catch block if caught.
134 SaveLiveRegisters(codegen, instruction_->GetLocations());
135 }
Mark Mendellee8d9712016-07-12 11:13:15 -0400136
137 // Are we using an array length from memory?
138 HInstruction* array_length = instruction_->InputAt(1);
139 Location length_loc = locations->InAt(1);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100140 InvokeRuntimeCallingConvention calling_convention;
Mark Mendellee8d9712016-07-12 11:13:15 -0400141 if (array_length->IsArrayLength() && array_length->IsEmittedAtUseSite()) {
142 // Load the array length into our temporary.
143 uint32_t len_offset = CodeGenerator::GetArrayLengthOffset(array_length->AsArrayLength());
144 Location array_loc = array_length->GetLocations()->InAt(0);
145 Address array_len(array_loc.AsRegister<Register>(), len_offset);
146 length_loc = Location::RegisterLocation(calling_convention.GetRegisterAt(1));
147 // Check for conflicts with index.
148 if (length_loc.Equals(locations->InAt(0))) {
149 // We know we aren't using parameter 2.
150 length_loc = Location::RegisterLocation(calling_convention.GetRegisterAt(2));
151 }
152 __ movl(length_loc.AsRegister<Register>(), array_len);
jessicahandojo4877b792016-09-08 19:49:13 -0700153 if (mirror::kUseStringCompression) {
154 __ andl(length_loc.AsRegister<Register>(), Immediate(INT32_MAX));
155 }
Mark Mendellee8d9712016-07-12 11:13:15 -0400156 }
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000157 x86_codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100158 locations->InAt(0),
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000159 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100160 Primitive::kPrimInt,
Mark Mendellee8d9712016-07-12 11:13:15 -0400161 length_loc,
Nicolas Geoffray90218252015-04-15 11:56:51 +0100162 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
163 Primitive::kPrimInt);
Serban Constantinescuba45db02016-07-12 22:53:02 +0100164 QuickEntrypointEnum entrypoint = instruction_->AsBoundsCheck()->IsStringCharAt()
165 ? kQuickThrowStringBounds
166 : kQuickThrowArrayBounds;
167 x86_codegen->InvokeRuntime(entrypoint, instruction_, instruction_->GetDexPc(), this);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100168 CheckEntrypointTypes<kQuickThrowStringBounds, void, int32_t, int32_t>();
Roland Levillain888d0672015-11-23 18:53:50 +0000169 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100170 }
171
Alexandre Rames8158f282015-08-07 10:26:17 +0100172 bool IsFatal() const OVERRIDE { return true; }
173
Alexandre Rames9931f312015-06-19 14:47:01 +0100174 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathX86"; }
175
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100176 private:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100177 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathX86);
178};
179
Andreas Gampe85b62f22015-09-09 13:15:38 -0700180class SuspendCheckSlowPathX86 : public SlowPathCode {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000181 public:
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000182 SuspendCheckSlowPathX86(HSuspendCheck* instruction, HBasicBlock* successor)
David Srbecky9cd6d372016-02-09 15:24:47 +0000183 : SlowPathCode(instruction), successor_(successor) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000184
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000185 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100186 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000187 __ Bind(GetEntryLabel());
Serban Constantinescuba45db02016-07-12 22:53:02 +0100188 x86_codegen->InvokeRuntime(kQuickTestSuspend, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000189 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100190 if (successor_ == nullptr) {
191 __ jmp(GetReturnLabel());
192 } else {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100193 __ jmp(x86_codegen->GetLabelOf(successor_));
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100194 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000195 }
196
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100197 Label* GetReturnLabel() {
198 DCHECK(successor_ == nullptr);
199 return &return_label_;
200 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000201
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100202 HBasicBlock* GetSuccessor() const {
203 return successor_;
204 }
205
Alexandre Rames9931f312015-06-19 14:47:01 +0100206 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathX86"; }
207
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000208 private:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100209 HBasicBlock* const successor_;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000210 Label return_label_;
211
212 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathX86);
213};
214
Andreas Gampe85b62f22015-09-09 13:15:38 -0700215class LoadClassSlowPathX86 : public SlowPathCode {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000216 public:
217 LoadClassSlowPathX86(HLoadClass* cls,
218 HInstruction* at,
219 uint32_t dex_pc,
220 bool do_clinit)
David Srbecky9cd6d372016-02-09 15:24:47 +0000221 : SlowPathCode(at), cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000222 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
223 }
224
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000225 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000226 LocationSummary* locations = at_->GetLocations();
227 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
228 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000229 SaveLiveRegisters(codegen, locations);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000230
231 InvokeRuntimeCallingConvention calling_convention;
232 __ movl(calling_convention.GetRegisterAt(0), Immediate(cls_->GetTypeIndex()));
Serban Constantinescuba45db02016-07-12 22:53:02 +0100233 x86_codegen->InvokeRuntime(do_clinit_ ? kQuickInitializeStaticStorage
234 : kQuickInitializeType,
Alexandre Rames8158f282015-08-07 10:26:17 +0100235 at_, dex_pc_, this);
Roland Levillain888d0672015-11-23 18:53:50 +0000236 if (do_clinit_) {
237 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
238 } else {
239 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
240 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000241
242 // Move the class to the desired location.
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000243 Location out = locations->Out();
244 if (out.IsValid()) {
245 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
246 x86_codegen->Move32(out, Location::RegisterLocation(EAX));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000247 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000248
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000249 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000250 __ jmp(GetExitLabel());
251 }
252
Alexandre Rames9931f312015-06-19 14:47:01 +0100253 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathX86"; }
254
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000255 private:
256 // The class this slow path will load.
257 HLoadClass* const cls_;
258
259 // The instruction where this slow path is happening.
260 // (Might be the load class or an initialization check).
261 HInstruction* const at_;
262
263 // The dex PC of `at_`.
264 const uint32_t dex_pc_;
265
266 // Whether to initialize the class.
267 const bool do_clinit_;
268
269 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathX86);
270};
271
Andreas Gampe85b62f22015-09-09 13:15:38 -0700272class TypeCheckSlowPathX86 : public SlowPathCode {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000273 public:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000274 TypeCheckSlowPathX86(HInstruction* instruction, bool is_fatal)
David Srbecky9cd6d372016-02-09 15:24:47 +0000275 : SlowPathCode(instruction), is_fatal_(is_fatal) {}
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000276
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000277 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000278 LocationSummary* locations = instruction_->GetLocations();
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100279 Location object_class = instruction_->IsCheckCast() ? locations->GetTemp(0)
280 : locations->Out();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000281 DCHECK(instruction_->IsCheckCast()
282 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000283
284 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
285 __ Bind(GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000286
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000287 if (!is_fatal_) {
288 SaveLiveRegisters(codegen, locations);
289 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000290
291 // We're moving two locations to locations that could overlap, so we need a parallel
292 // move resolver.
293 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000294 x86_codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100295 locations->InAt(1),
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000296 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100297 Primitive::kPrimNot,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100298 object_class,
Nicolas Geoffray90218252015-04-15 11:56:51 +0100299 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
300 Primitive::kPrimNot);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000301
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000302 if (instruction_->IsInstanceOf()) {
Serban Constantinescuba45db02016-07-12 22:53:02 +0100303 x86_codegen->InvokeRuntime(kQuickInstanceofNonTrivial,
Alexandre Rames8158f282015-08-07 10:26:17 +0100304 instruction_,
305 instruction_->GetDexPc(),
306 this);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000307 CheckEntrypointTypes<
Andreas Gampe67409972016-07-19 22:34:53 -0700308 kQuickInstanceofNonTrivial, size_t, const mirror::Class*, const mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000309 } else {
310 DCHECK(instruction_->IsCheckCast());
Serban Constantinescuba45db02016-07-12 22:53:02 +0100311 x86_codegen->InvokeRuntime(kQuickCheckCast, instruction_, instruction_->GetDexPc(), this);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000312 CheckEntrypointTypes<kQuickCheckCast, void, const mirror::Class*, const mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000313 }
314
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000315 if (!is_fatal_) {
316 if (instruction_->IsInstanceOf()) {
317 x86_codegen->Move32(locations->Out(), Location::RegisterLocation(EAX));
318 }
319 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray75374372015-09-17 17:12:19 +0000320
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000321 __ jmp(GetExitLabel());
322 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000323 }
324
Alexandre Rames9931f312015-06-19 14:47:01 +0100325 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathX86"; }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000326 bool IsFatal() const OVERRIDE { return is_fatal_; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100327
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000328 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000329 const bool is_fatal_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000330
331 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathX86);
332};
333
Andreas Gampe85b62f22015-09-09 13:15:38 -0700334class DeoptimizationSlowPathX86 : public SlowPathCode {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700335 public:
Aart Bik42249c32016-01-07 15:33:50 -0800336 explicit DeoptimizationSlowPathX86(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000337 : SlowPathCode(instruction) {}
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700338
339 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames8158f282015-08-07 10:26:17 +0100340 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700341 __ Bind(GetEntryLabel());
Serban Constantinescuba45db02016-07-12 22:53:02 +0100342 x86_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000343 CheckEntrypointTypes<kQuickDeoptimize, void, void>();
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700344 }
345
Alexandre Rames9931f312015-06-19 14:47:01 +0100346 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathX86"; }
347
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700348 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700349 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathX86);
350};
351
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100352class ArraySetSlowPathX86 : public SlowPathCode {
353 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000354 explicit ArraySetSlowPathX86(HInstruction* instruction) : SlowPathCode(instruction) {}
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100355
356 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
357 LocationSummary* locations = instruction_->GetLocations();
358 __ Bind(GetEntryLabel());
359 SaveLiveRegisters(codegen, locations);
360
361 InvokeRuntimeCallingConvention calling_convention;
362 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
363 parallel_move.AddMove(
364 locations->InAt(0),
365 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
366 Primitive::kPrimNot,
367 nullptr);
368 parallel_move.AddMove(
369 locations->InAt(1),
370 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
371 Primitive::kPrimInt,
372 nullptr);
373 parallel_move.AddMove(
374 locations->InAt(2),
375 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
376 Primitive::kPrimNot,
377 nullptr);
378 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
379
380 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Serban Constantinescuba45db02016-07-12 22:53:02 +0100381 x86_codegen->InvokeRuntime(kQuickAputObject, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000382 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100383 RestoreLiveRegisters(codegen, locations);
384 __ jmp(GetExitLabel());
385 }
386
387 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathX86"; }
388
389 private:
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100390 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathX86);
391};
392
Roland Levillain7c1559a2015-12-15 10:55:36 +0000393// Slow path marking an object during a read barrier.
394class ReadBarrierMarkSlowPathX86 : public SlowPathCode {
395 public:
Vladimir Marko953437b2016-08-24 08:30:46 +0000396 ReadBarrierMarkSlowPathX86(HInstruction* instruction, Location obj, bool unpoison)
397 : SlowPathCode(instruction), obj_(obj), unpoison_(unpoison) {
Roland Levillain7c1559a2015-12-15 10:55:36 +0000398 DCHECK(kEmitCompilerReadBarrier);
399 }
400
401 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathX86"; }
402
403 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
404 LocationSummary* locations = instruction_->GetLocations();
Roland Levillain02b75802016-07-13 11:54:35 +0100405 Register reg = obj_.AsRegister<Register>();
Roland Levillain7c1559a2015-12-15 10:55:36 +0000406 DCHECK(locations->CanCall());
Roland Levillain02b75802016-07-13 11:54:35 +0100407 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg));
Roland Levillain7c1559a2015-12-15 10:55:36 +0000408 DCHECK(instruction_->IsInstanceFieldGet() ||
409 instruction_->IsStaticFieldGet() ||
410 instruction_->IsArrayGet() ||
Roland Levillain16d9f942016-08-25 17:27:56 +0100411 instruction_->IsArraySet() ||
Roland Levillain7c1559a2015-12-15 10:55:36 +0000412 instruction_->IsLoadClass() ||
413 instruction_->IsLoadString() ||
414 instruction_->IsInstanceOf() ||
Roland Levillain3d312422016-06-23 13:53:42 +0100415 instruction_->IsCheckCast() ||
Roland Levillain0b671c02016-08-19 12:02:34 +0100416 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()) ||
417 (instruction_->IsInvokeStaticOrDirect() && instruction_->GetLocations()->Intrinsified()))
Roland Levillain7c1559a2015-12-15 10:55:36 +0000418 << "Unexpected instruction in read barrier marking slow path: "
419 << instruction_->DebugName();
420
421 __ Bind(GetEntryLabel());
Vladimir Marko953437b2016-08-24 08:30:46 +0000422 if (unpoison_) {
423 // Object* ref = ref_addr->AsMirrorPtr()
424 __ MaybeUnpoisonHeapReference(reg);
425 }
Roland Levillain4359e612016-07-20 11:32:19 +0100426 // No need to save live registers; it's taken care of by the
427 // entrypoint. Also, there is no need to update the stack mask,
428 // as this runtime call will not trigger a garbage collection.
Roland Levillain7c1559a2015-12-15 10:55:36 +0000429 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Roland Levillain02b75802016-07-13 11:54:35 +0100430 DCHECK_NE(reg, ESP);
431 DCHECK(0 <= reg && reg < kNumberOfCpuRegisters) << reg;
432 // "Compact" slow path, saving two moves.
433 //
434 // Instead of using the standard runtime calling convention (input
435 // and output in EAX):
436 //
437 // EAX <- obj
438 // EAX <- ReadBarrierMark(EAX)
439 // obj <- EAX
440 //
441 // we just use rX (the register holding `obj`) as input and output
442 // of a dedicated entrypoint:
443 //
444 // rX <- ReadBarrierMarkRegX(rX)
445 //
446 int32_t entry_point_offset =
Andreas Gampe542451c2016-07-26 09:02:02 -0700447 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kX86PointerSize>(reg);
Roland Levillaindec8f632016-07-22 17:10:06 +0100448 // This runtime call does not require a stack map.
449 x86_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
Roland Levillain7c1559a2015-12-15 10:55:36 +0000450 __ jmp(GetExitLabel());
451 }
452
453 private:
Roland Levillain7c1559a2015-12-15 10:55:36 +0000454 const Location obj_;
Vladimir Marko953437b2016-08-24 08:30:46 +0000455 const bool unpoison_;
Roland Levillain7c1559a2015-12-15 10:55:36 +0000456
457 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathX86);
458};
459
Roland Levillain0d5a2812015-11-13 10:07:31 +0000460// Slow path generating a read barrier for a heap reference.
461class ReadBarrierForHeapReferenceSlowPathX86 : public SlowPathCode {
462 public:
463 ReadBarrierForHeapReferenceSlowPathX86(HInstruction* instruction,
464 Location out,
465 Location ref,
466 Location obj,
467 uint32_t offset,
468 Location index)
David Srbecky9cd6d372016-02-09 15:24:47 +0000469 : SlowPathCode(instruction),
Roland Levillain0d5a2812015-11-13 10:07:31 +0000470 out_(out),
471 ref_(ref),
472 obj_(obj),
473 offset_(offset),
474 index_(index) {
475 DCHECK(kEmitCompilerReadBarrier);
476 // If `obj` is equal to `out` or `ref`, it means the initial object
477 // has been overwritten by (or after) the heap object reference load
478 // to be instrumented, e.g.:
479 //
480 // __ movl(out, Address(out, offset));
Roland Levillain7c1559a2015-12-15 10:55:36 +0000481 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000482 //
483 // In that case, we have lost the information about the original
484 // object, and the emitted read barrier cannot work properly.
485 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
486 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
487 }
488
489 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
490 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
491 LocationSummary* locations = instruction_->GetLocations();
492 Register reg_out = out_.AsRegister<Register>();
493 DCHECK(locations->CanCall());
494 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
Roland Levillain3d312422016-06-23 13:53:42 +0100495 DCHECK(instruction_->IsInstanceFieldGet() ||
496 instruction_->IsStaticFieldGet() ||
497 instruction_->IsArrayGet() ||
498 instruction_->IsInstanceOf() ||
499 instruction_->IsCheckCast() ||
Roland Levillaindec8f632016-07-22 17:10:06 +0100500 (instruction_->IsInvokeVirtual()) && instruction_->GetLocations()->Intrinsified())
Roland Levillain7c1559a2015-12-15 10:55:36 +0000501 << "Unexpected instruction in read barrier for heap reference slow path: "
502 << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000503
504 __ Bind(GetEntryLabel());
505 SaveLiveRegisters(codegen, locations);
506
507 // We may have to change the index's value, but as `index_` is a
508 // constant member (like other "inputs" of this slow path),
509 // introduce a copy of it, `index`.
510 Location index = index_;
511 if (index_.IsValid()) {
Roland Levillain3d312422016-06-23 13:53:42 +0100512 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
Roland Levillain0d5a2812015-11-13 10:07:31 +0000513 if (instruction_->IsArrayGet()) {
514 // Compute the actual memory offset and store it in `index`.
515 Register index_reg = index_.AsRegister<Register>();
516 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg));
517 if (codegen->IsCoreCalleeSaveRegister(index_reg)) {
518 // We are about to change the value of `index_reg` (see the
519 // calls to art::x86::X86Assembler::shll and
520 // art::x86::X86Assembler::AddImmediate below), but it has
521 // not been saved by the previous call to
522 // art::SlowPathCode::SaveLiveRegisters, as it is a
523 // callee-save register --
524 // art::SlowPathCode::SaveLiveRegisters does not consider
525 // callee-save registers, as it has been designed with the
526 // assumption that callee-save registers are supposed to be
527 // handled by the called function. So, as a callee-save
528 // register, `index_reg` _would_ eventually be saved onto
529 // the stack, but it would be too late: we would have
530 // changed its value earlier. Therefore, we manually save
531 // it here into another freely available register,
532 // `free_reg`, chosen of course among the caller-save
533 // registers (as a callee-save `free_reg` register would
534 // exhibit the same problem).
535 //
536 // Note we could have requested a temporary register from
537 // the register allocator instead; but we prefer not to, as
538 // this is a slow path, and we know we can find a
539 // caller-save register that is available.
540 Register free_reg = FindAvailableCallerSaveRegister(codegen);
541 __ movl(free_reg, index_reg);
542 index_reg = free_reg;
543 index = Location::RegisterLocation(index_reg);
544 } else {
545 // The initial register stored in `index_` has already been
546 // saved in the call to art::SlowPathCode::SaveLiveRegisters
547 // (as it is not a callee-save register), so we can freely
548 // use it.
549 }
550 // Shifting the index value contained in `index_reg` by the scale
551 // factor (2) cannot overflow in practice, as the runtime is
552 // unable to allocate object arrays with a size larger than
553 // 2^26 - 1 (that is, 2^28 - 4 bytes).
554 __ shll(index_reg, Immediate(TIMES_4));
555 static_assert(
556 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
557 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
558 __ AddImmediate(index_reg, Immediate(offset_));
559 } else {
Roland Levillain3d312422016-06-23 13:53:42 +0100560 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
561 // intrinsics, `index_` is not shifted by a scale factor of 2
562 // (as in the case of ArrayGet), as it is actually an offset
563 // to an object field within an object.
564 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000565 DCHECK(instruction_->GetLocations()->Intrinsified());
566 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
567 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
568 << instruction_->AsInvoke()->GetIntrinsic();
569 DCHECK_EQ(offset_, 0U);
570 DCHECK(index_.IsRegisterPair());
571 // UnsafeGet's offset location is a register pair, the low
572 // part contains the correct offset.
573 index = index_.ToLow();
574 }
575 }
576
577 // We're moving two or three locations to locations that could
578 // overlap, so we need a parallel move resolver.
579 InvokeRuntimeCallingConvention calling_convention;
580 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
581 parallel_move.AddMove(ref_,
582 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
583 Primitive::kPrimNot,
584 nullptr);
585 parallel_move.AddMove(obj_,
586 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
587 Primitive::kPrimNot,
588 nullptr);
589 if (index.IsValid()) {
590 parallel_move.AddMove(index,
591 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
592 Primitive::kPrimInt,
593 nullptr);
594 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
595 } else {
596 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
597 __ movl(calling_convention.GetRegisterAt(2), Immediate(offset_));
598 }
Serban Constantinescuba45db02016-07-12 22:53:02 +0100599 x86_codegen->InvokeRuntime(kQuickReadBarrierSlow, instruction_, instruction_->GetDexPc(), this);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000600 CheckEntrypointTypes<
601 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
602 x86_codegen->Move32(out_, Location::RegisterLocation(EAX));
603
604 RestoreLiveRegisters(codegen, locations);
605 __ jmp(GetExitLabel());
606 }
607
608 const char* GetDescription() const OVERRIDE { return "ReadBarrierForHeapReferenceSlowPathX86"; }
609
610 private:
611 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
612 size_t ref = static_cast<int>(ref_.AsRegister<Register>());
613 size_t obj = static_cast<int>(obj_.AsRegister<Register>());
614 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
615 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
616 return static_cast<Register>(i);
617 }
618 }
619 // We shall never fail to find a free caller-save register, as
620 // there are more than two core caller-save registers on x86
621 // (meaning it is possible to find one which is different from
622 // `ref` and `obj`).
623 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
624 LOG(FATAL) << "Could not find a free caller-save register";
625 UNREACHABLE();
626 }
627
Roland Levillain0d5a2812015-11-13 10:07:31 +0000628 const Location out_;
629 const Location ref_;
630 const Location obj_;
631 const uint32_t offset_;
632 // An additional location containing an index to an array.
633 // Only used for HArrayGet and the UnsafeGetObject &
634 // UnsafeGetObjectVolatile intrinsics.
635 const Location index_;
636
637 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathX86);
638};
639
640// Slow path generating a read barrier for a GC root.
641class ReadBarrierForRootSlowPathX86 : public SlowPathCode {
642 public:
643 ReadBarrierForRootSlowPathX86(HInstruction* instruction, Location out, Location root)
David Srbecky9cd6d372016-02-09 15:24:47 +0000644 : SlowPathCode(instruction), out_(out), root_(root) {
Roland Levillain7c1559a2015-12-15 10:55:36 +0000645 DCHECK(kEmitCompilerReadBarrier);
646 }
Roland Levillain0d5a2812015-11-13 10:07:31 +0000647
648 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
649 LocationSummary* locations = instruction_->GetLocations();
650 Register reg_out = out_.AsRegister<Register>();
651 DCHECK(locations->CanCall());
652 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
Roland Levillain7c1559a2015-12-15 10:55:36 +0000653 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
654 << "Unexpected instruction in read barrier for GC root slow path: "
655 << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000656
657 __ Bind(GetEntryLabel());
658 SaveLiveRegisters(codegen, locations);
659
660 InvokeRuntimeCallingConvention calling_convention;
661 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
662 x86_codegen->Move32(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), root_);
Serban Constantinescuba45db02016-07-12 22:53:02 +0100663 x86_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000664 instruction_,
665 instruction_->GetDexPc(),
666 this);
667 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
668 x86_codegen->Move32(out_, Location::RegisterLocation(EAX));
669
670 RestoreLiveRegisters(codegen, locations);
671 __ jmp(GetExitLabel());
672 }
673
674 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathX86"; }
675
676 private:
Roland Levillain0d5a2812015-11-13 10:07:31 +0000677 const Location out_;
678 const Location root_;
679
680 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathX86);
681};
682
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100683#undef __
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100684// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
685#define __ down_cast<X86Assembler*>(GetAssembler())-> // NOLINT
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100686
Aart Bike9f37602015-10-09 11:15:55 -0700687inline Condition X86Condition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -0700688 switch (cond) {
689 case kCondEQ: return kEqual;
690 case kCondNE: return kNotEqual;
691 case kCondLT: return kLess;
692 case kCondLE: return kLessEqual;
693 case kCondGT: return kGreater;
694 case kCondGE: return kGreaterEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700695 case kCondB: return kBelow;
696 case kCondBE: return kBelowEqual;
697 case kCondA: return kAbove;
698 case kCondAE: return kAboveEqual;
Dave Allison20dfc792014-06-16 20:44:29 -0700699 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100700 LOG(FATAL) << "Unreachable";
701 UNREACHABLE();
702}
703
Aart Bike9f37602015-10-09 11:15:55 -0700704// Maps signed condition to unsigned condition and FP condition to x86 name.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100705inline Condition X86UnsignedOrFPCondition(IfCondition cond) {
706 switch (cond) {
707 case kCondEQ: return kEqual;
708 case kCondNE: return kNotEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700709 // Signed to unsigned, and FP to x86 name.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100710 case kCondLT: return kBelow;
711 case kCondLE: return kBelowEqual;
712 case kCondGT: return kAbove;
713 case kCondGE: return kAboveEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700714 // Unsigned remain unchanged.
715 case kCondB: return kBelow;
716 case kCondBE: return kBelowEqual;
717 case kCondA: return kAbove;
718 case kCondAE: return kAboveEqual;
Roland Levillain4fa13f62015-07-06 18:11:54 +0100719 }
720 LOG(FATAL) << "Unreachable";
721 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -0700722}
723
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100724void CodeGeneratorX86::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100725 stream << Register(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100726}
727
728void CodeGeneratorX86::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100729 stream << XmmRegister(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100730}
731
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100732size_t CodeGeneratorX86::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
733 __ movl(Address(ESP, stack_index), static_cast<Register>(reg_id));
734 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100735}
736
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100737size_t CodeGeneratorX86::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
738 __ movl(static_cast<Register>(reg_id), Address(ESP, stack_index));
739 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100740}
741
Mark Mendell7c8d0092015-01-26 11:21:33 -0500742size_t CodeGeneratorX86::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
743 __ movsd(Address(ESP, stack_index), XmmRegister(reg_id));
744 return GetFloatingPointSpillSlotSize();
745}
746
747size_t CodeGeneratorX86::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
748 __ movsd(XmmRegister(reg_id), Address(ESP, stack_index));
749 return GetFloatingPointSpillSlotSize();
750}
751
Calin Juravle175dc732015-08-25 15:42:32 +0100752void CodeGeneratorX86::InvokeRuntime(QuickEntrypointEnum entrypoint,
753 HInstruction* instruction,
754 uint32_t dex_pc,
755 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +0100756 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Serban Constantinescuba45db02016-07-12 22:53:02 +0100757 GenerateInvokeRuntime(GetThreadOffset<kX86PointerSize>(entrypoint).Int32Value());
758 if (EntrypointRequiresStackMap(entrypoint)) {
759 RecordPcInfo(instruction, dex_pc, slow_path);
760 }
Alexandre Rames8158f282015-08-07 10:26:17 +0100761}
762
Roland Levillaindec8f632016-07-22 17:10:06 +0100763void CodeGeneratorX86::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
764 HInstruction* instruction,
765 SlowPathCode* slow_path) {
766 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
Serban Constantinescuba45db02016-07-12 22:53:02 +0100767 GenerateInvokeRuntime(entry_point_offset);
768}
769
770void CodeGeneratorX86::GenerateInvokeRuntime(int32_t entry_point_offset) {
Roland Levillaindec8f632016-07-22 17:10:06 +0100771 __ fs()->call(Address::Absolute(entry_point_offset));
772}
773
Mark Mendellfb8d2792015-03-31 22:16:59 -0400774CodeGeneratorX86::CodeGeneratorX86(HGraph* graph,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000775 const X86InstructionSetFeatures& isa_features,
776 const CompilerOptions& compiler_options,
777 OptimizingCompilerStats* stats)
Mark Mendell5f874182015-03-04 15:42:45 -0500778 : CodeGenerator(graph,
779 kNumberOfCpuRegisters,
780 kNumberOfXmmRegisters,
781 kNumberOfRegisterPairs,
782 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
783 arraysize(kCoreCalleeSaves))
784 | (1 << kFakeReturnRegister),
Serban Constantinescuecc43662015-08-13 13:33:12 +0100785 0,
786 compiler_options,
787 stats),
Vladimir Marko225b6462015-09-28 12:17:40 +0100788 block_labels_(nullptr),
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100789 location_builder_(graph, this),
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100790 instruction_visitor_(graph, this),
Mark Mendellfb8d2792015-03-31 22:16:59 -0400791 move_resolver_(graph->GetArena(), this),
Vladimir Marko93205e32016-04-13 11:59:46 +0100792 assembler_(graph->GetArena()),
Vladimir Marko58155012015-08-19 12:49:41 +0000793 isa_features_(isa_features),
Vladimir Marko5233f932015-09-29 19:01:15 +0100794 method_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Mark Mendell805b3b52015-09-18 14:10:29 -0400795 relative_call_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000796 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000797 simple_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
798 string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100799 type_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko93205e32016-04-13 11:59:46 +0100800 constant_area_start_(-1),
801 fixups_to_jump_tables_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
802 method_address_offset_(-1) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000803 // Use a fake return address register to mimic Quick.
804 AddAllocatedRegister(Location::RegisterLocation(kFakeReturnRegister));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100805}
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100806
David Brazdil58282f42016-01-14 12:45:10 +0000807void CodeGeneratorX86::SetupBlockedRegisters() const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100808 // Don't allocate the dalvik style register pair passing.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100809 blocked_register_pairs_[ECX_EDX] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100810
811 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100812 blocked_core_registers_[ESP] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100813
Calin Juravle34bacdf2014-10-07 20:23:36 +0100814 UpdateBlockedPairRegisters();
815}
816
817void CodeGeneratorX86::UpdateBlockedPairRegisters() const {
818 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
819 X86ManagedRegister current =
820 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
821 if (blocked_core_registers_[current.AsRegisterPairLow()]
822 || blocked_core_registers_[current.AsRegisterPairHigh()]) {
823 blocked_register_pairs_[i] = true;
824 }
825 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100826}
827
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100828InstructionCodeGeneratorX86::InstructionCodeGeneratorX86(HGraph* graph, CodeGeneratorX86* codegen)
Aart Bik42249c32016-01-07 15:33:50 -0800829 : InstructionCodeGenerator(graph, codegen),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100830 assembler_(codegen->GetAssembler()),
831 codegen_(codegen) {}
832
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100833static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +0100834 return dwarf::Reg::X86Core(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100835}
836
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000837void CodeGeneratorX86::GenerateFrameEntry() {
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100838 __ cfi().SetCurrentCFAOffset(kX86WordSize); // return address
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000839 __ Bind(&frame_entry_label_);
Roland Levillain199f3362014-11-27 17:15:16 +0000840 bool skip_overflow_check =
841 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000842 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Calin Juravle93edf732015-01-20 20:14:07 +0000843
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000844 if (!skip_overflow_check) {
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100845 __ testl(EAX, Address(ESP, -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86))));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100846 RecordPcInfo(nullptr, 0);
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100847 }
848
Mark Mendell5f874182015-03-04 15:42:45 -0500849 if (HasEmptyFrame()) {
850 return;
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000851 }
Mark Mendell5f874182015-03-04 15:42:45 -0500852
853 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
854 Register reg = kCoreCalleeSaves[i];
855 if (allocated_registers_.ContainsCoreRegister(reg)) {
856 __ pushl(reg);
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100857 __ cfi().AdjustCFAOffset(kX86WordSize);
858 __ cfi().RelOffset(DWARFReg(reg), 0);
Mark Mendell5f874182015-03-04 15:42:45 -0500859 }
860 }
861
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100862 int adjust = GetFrameSize() - FrameEntrySpillSize();
863 __ subl(ESP, Immediate(adjust));
864 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100865 __ movl(Address(ESP, kCurrentMethodStackOffset), kMethodRegisterArgument);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000866}
867
868void CodeGeneratorX86::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +0100869 __ cfi().RememberState();
870 if (!HasEmptyFrame()) {
871 int adjust = GetFrameSize() - FrameEntrySpillSize();
872 __ addl(ESP, Immediate(adjust));
873 __ cfi().AdjustCFAOffset(-adjust);
Mark Mendell5f874182015-03-04 15:42:45 -0500874
David Srbeckyc34dc932015-04-12 09:27:43 +0100875 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
876 Register reg = kCoreCalleeSaves[i];
877 if (allocated_registers_.ContainsCoreRegister(reg)) {
878 __ popl(reg);
879 __ cfi().AdjustCFAOffset(-static_cast<int>(kX86WordSize));
880 __ cfi().Restore(DWARFReg(reg));
881 }
Mark Mendell5f874182015-03-04 15:42:45 -0500882 }
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000883 }
David Srbeckyc34dc932015-04-12 09:27:43 +0100884 __ ret();
885 __ cfi().RestoreState();
886 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000887}
888
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100889void CodeGeneratorX86::Bind(HBasicBlock* block) {
890 __ Bind(GetLabelOf(block));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000891}
892
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100893Location InvokeDexCallingConventionVisitorX86::GetReturnLocation(Primitive::Type type) const {
894 switch (type) {
895 case Primitive::kPrimBoolean:
896 case Primitive::kPrimByte:
897 case Primitive::kPrimChar:
898 case Primitive::kPrimShort:
899 case Primitive::kPrimInt:
900 case Primitive::kPrimNot:
901 return Location::RegisterLocation(EAX);
902
903 case Primitive::kPrimLong:
904 return Location::RegisterPairLocation(EAX, EDX);
905
906 case Primitive::kPrimVoid:
907 return Location::NoLocation();
908
909 case Primitive::kPrimDouble:
910 case Primitive::kPrimFloat:
911 return Location::FpuRegisterLocation(XMM0);
912 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +0100913
914 UNREACHABLE();
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100915}
916
917Location InvokeDexCallingConventionVisitorX86::GetMethodLocation() const {
918 return Location::RegisterLocation(kMethodRegisterArgument);
919}
920
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100921Location InvokeDexCallingConventionVisitorX86::GetNextLocation(Primitive::Type type) {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100922 switch (type) {
923 case Primitive::kPrimBoolean:
924 case Primitive::kPrimByte:
925 case Primitive::kPrimChar:
926 case Primitive::kPrimShort:
927 case Primitive::kPrimInt:
928 case Primitive::kPrimNot: {
929 uint32_t index = gp_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000930 stack_index_++;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100931 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100932 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100933 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000934 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100935 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100936 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100937
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000938 case Primitive::kPrimLong: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100939 uint32_t index = gp_index_;
940 gp_index_ += 2;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000941 stack_index_ += 2;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100942 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100943 X86ManagedRegister pair = X86ManagedRegister::FromRegisterPair(
944 calling_convention.GetRegisterPairAt(index));
945 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100946 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000947 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
948 }
949 }
950
951 case Primitive::kPrimFloat: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100952 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000953 stack_index_++;
954 if (index < calling_convention.GetNumberOfFpuRegisters()) {
955 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
956 } else {
957 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
958 }
959 }
960
961 case Primitive::kPrimDouble: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100962 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000963 stack_index_ += 2;
964 if (index < calling_convention.GetNumberOfFpuRegisters()) {
965 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
966 } else {
967 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100968 }
969 }
970
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100971 case Primitive::kPrimVoid:
972 LOG(FATAL) << "Unexpected parameter type " << type;
973 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100974 }
Roland Levillain0d5a2812015-11-13 10:07:31 +0000975 return Location::NoLocation();
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100976}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100977
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100978void CodeGeneratorX86::Move32(Location destination, Location source) {
979 if (source.Equals(destination)) {
980 return;
981 }
982 if (destination.IsRegister()) {
983 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000984 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100985 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000986 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100987 } else {
988 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000989 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100990 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100991 } else if (destination.IsFpuRegister()) {
992 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000993 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100994 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000995 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100996 } else {
997 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000998 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100999 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001000 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001001 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001002 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001003 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001004 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001005 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Mark Mendell7c8d0092015-01-26 11:21:33 -05001006 } else if (source.IsConstant()) {
1007 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001008 int32_t value = GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05001009 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001010 } else {
1011 DCHECK(source.IsStackSlot());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001012 __ pushl(Address(ESP, source.GetStackIndex()));
1013 __ popl(Address(ESP, destination.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001014 }
1015 }
1016}
1017
1018void CodeGeneratorX86::Move64(Location destination, Location source) {
1019 if (source.Equals(destination)) {
1020 return;
1021 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001022 if (destination.IsRegisterPair()) {
1023 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001024 EmitParallelMoves(
1025 Location::RegisterLocation(source.AsRegisterPairHigh<Register>()),
1026 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001027 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001028 Location::RegisterLocation(source.AsRegisterPairLow<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001029 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()),
1030 Primitive::kPrimInt);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001031 } else if (source.IsFpuRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001032 XmmRegister src_reg = source.AsFpuRegister<XmmRegister>();
1033 __ movd(destination.AsRegisterPairLow<Register>(), src_reg);
1034 __ psrlq(src_reg, Immediate(32));
1035 __ movd(destination.AsRegisterPairHigh<Register>(), src_reg);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001036 } else {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001037 // No conflict possible, so just do the moves.
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001038 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001039 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
1040 __ movl(destination.AsRegisterPairHigh<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001041 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
1042 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001043 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05001044 if (source.IsFpuRegister()) {
1045 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
1046 } else if (source.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001047 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01001048 } else if (source.IsRegisterPair()) {
1049 size_t elem_size = Primitive::ComponentSize(Primitive::kPrimInt);
1050 // Create stack space for 2 elements.
1051 __ subl(ESP, Immediate(2 * elem_size));
1052 __ movl(Address(ESP, 0), source.AsRegisterPairLow<Register>());
1053 __ movl(Address(ESP, elem_size), source.AsRegisterPairHigh<Register>());
1054 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
1055 // And remove the temporary stack space we allocated.
1056 __ addl(ESP, Immediate(2 * elem_size));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001057 } else {
1058 LOG(FATAL) << "Unimplemented";
1059 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001060 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001061 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001062 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001063 // No conflict possible, so just do the moves.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001064 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegisterPairLow<Register>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001065 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001066 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001067 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001068 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001069 } else if (source.IsConstant()) {
1070 HConstant* constant = source.GetConstant();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001071 DCHECK(constant->IsLongConstant() || constant->IsDoubleConstant());
1072 int64_t value = GetInt64ValueOf(constant);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001073 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(Low32Bits(value)));
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001074 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
1075 Immediate(High32Bits(value)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001076 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001077 DCHECK(source.IsDoubleStackSlot()) << source;
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001078 EmitParallelMoves(
1079 Location::StackSlot(source.GetStackIndex()),
1080 Location::StackSlot(destination.GetStackIndex()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001081 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001082 Location::StackSlot(source.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001083 Location::StackSlot(destination.GetHighStackIndex(kX86WordSize)),
1084 Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001085 }
1086 }
1087}
1088
Calin Juravle175dc732015-08-25 15:42:32 +01001089void CodeGeneratorX86::MoveConstant(Location location, int32_t value) {
1090 DCHECK(location.IsRegister());
1091 __ movl(location.AsRegister<Register>(), Immediate(value));
1092}
1093
Calin Juravlee460d1d2015-09-29 04:52:17 +01001094void CodeGeneratorX86::MoveLocation(Location dst, Location src, Primitive::Type dst_type) {
David Brazdil74eb1b22015-12-14 11:44:01 +00001095 HParallelMove move(GetGraph()->GetArena());
1096 if (dst_type == Primitive::kPrimLong && !src.IsConstant() && !src.IsFpuRegister()) {
1097 move.AddMove(src.ToLow(), dst.ToLow(), Primitive::kPrimInt, nullptr);
1098 move.AddMove(src.ToHigh(), dst.ToHigh(), Primitive::kPrimInt, nullptr);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001099 } else {
David Brazdil74eb1b22015-12-14 11:44:01 +00001100 move.AddMove(src, dst, dst_type, nullptr);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001101 }
David Brazdil74eb1b22015-12-14 11:44:01 +00001102 GetMoveResolver()->EmitNativeCode(&move);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001103}
1104
1105void CodeGeneratorX86::AddLocationAsTemp(Location location, LocationSummary* locations) {
1106 if (location.IsRegister()) {
1107 locations->AddTemp(location);
1108 } else if (location.IsRegisterPair()) {
1109 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
1110 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
1111 } else {
1112 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1113 }
1114}
1115
David Brazdilfc6a86a2015-06-26 10:33:45 +00001116void InstructionCodeGeneratorX86::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001117 DCHECK(!successor->IsExitBlock());
1118
1119 HBasicBlock* block = got->GetBlock();
1120 HInstruction* previous = got->GetPrevious();
1121
1122 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +00001123 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001124 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1125 return;
1126 }
1127
1128 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1129 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1130 }
1131 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001132 __ jmp(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001133 }
1134}
1135
David Brazdilfc6a86a2015-06-26 10:33:45 +00001136void LocationsBuilderX86::VisitGoto(HGoto* got) {
1137 got->SetLocations(nullptr);
1138}
1139
1140void InstructionCodeGeneratorX86::VisitGoto(HGoto* got) {
1141 HandleGoto(got, got->GetSuccessor());
1142}
1143
1144void LocationsBuilderX86::VisitTryBoundary(HTryBoundary* try_boundary) {
1145 try_boundary->SetLocations(nullptr);
1146}
1147
1148void InstructionCodeGeneratorX86::VisitTryBoundary(HTryBoundary* try_boundary) {
1149 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
1150 if (!successor->IsExitBlock()) {
1151 HandleGoto(try_boundary, successor);
1152 }
1153}
1154
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001155void LocationsBuilderX86::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001156 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001157}
1158
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001159void InstructionCodeGeneratorX86::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001160}
1161
Mark Mendell152408f2015-12-31 12:28:50 -05001162template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001163void InstructionCodeGeneratorX86::GenerateFPJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001164 LabelType* true_label,
1165 LabelType* false_label) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001166 if (cond->IsFPConditionTrueIfNaN()) {
1167 __ j(kUnordered, true_label);
1168 } else if (cond->IsFPConditionFalseIfNaN()) {
1169 __ j(kUnordered, false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001170 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001171 __ j(X86UnsignedOrFPCondition(cond->GetCondition()), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001172}
1173
Mark Mendell152408f2015-12-31 12:28:50 -05001174template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001175void InstructionCodeGeneratorX86::GenerateLongComparesAndJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001176 LabelType* true_label,
1177 LabelType* false_label) {
Mark Mendellc4701932015-04-10 13:18:51 -04001178 LocationSummary* locations = cond->GetLocations();
1179 Location left = locations->InAt(0);
1180 Location right = locations->InAt(1);
1181 IfCondition if_cond = cond->GetCondition();
1182
Mark Mendellc4701932015-04-10 13:18:51 -04001183 Register left_high = left.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001184 Register left_low = left.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001185 IfCondition true_high_cond = if_cond;
1186 IfCondition false_high_cond = cond->GetOppositeCondition();
Aart Bike9f37602015-10-09 11:15:55 -07001187 Condition final_condition = X86UnsignedOrFPCondition(if_cond); // unsigned on lower part
Mark Mendellc4701932015-04-10 13:18:51 -04001188
1189 // Set the conditions for the test, remembering that == needs to be
1190 // decided using the low words.
1191 switch (if_cond) {
1192 case kCondEQ:
Mark Mendellc4701932015-04-10 13:18:51 -04001193 case kCondNE:
Roland Levillain4fa13f62015-07-06 18:11:54 +01001194 // Nothing to do.
Mark Mendellc4701932015-04-10 13:18:51 -04001195 break;
1196 case kCondLT:
1197 false_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -04001198 break;
1199 case kCondLE:
1200 true_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -04001201 break;
1202 case kCondGT:
1203 false_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -04001204 break;
1205 case kCondGE:
1206 true_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -04001207 break;
Aart Bike9f37602015-10-09 11:15:55 -07001208 case kCondB:
1209 false_high_cond = kCondA;
1210 break;
1211 case kCondBE:
1212 true_high_cond = kCondB;
1213 break;
1214 case kCondA:
1215 false_high_cond = kCondB;
1216 break;
1217 case kCondAE:
1218 true_high_cond = kCondA;
1219 break;
Mark Mendellc4701932015-04-10 13:18:51 -04001220 }
1221
1222 if (right.IsConstant()) {
1223 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
Mark Mendellc4701932015-04-10 13:18:51 -04001224 int32_t val_high = High32Bits(value);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001225 int32_t val_low = Low32Bits(value);
Mark Mendellc4701932015-04-10 13:18:51 -04001226
Aart Bika19616e2016-02-01 18:57:58 -08001227 codegen_->Compare32BitValue(left_high, val_high);
Mark Mendellc4701932015-04-10 13:18:51 -04001228 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001229 __ j(X86Condition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001230 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001231 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001232 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001233 __ j(X86Condition(true_high_cond), true_label);
1234 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001235 }
1236 // Must be equal high, so compare the lows.
Aart Bika19616e2016-02-01 18:57:58 -08001237 codegen_->Compare32BitValue(left_low, val_low);
Mark Mendell8659e842016-02-16 10:41:46 -05001238 } else if (right.IsRegisterPair()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001239 Register right_high = right.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001240 Register right_low = right.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001241
1242 __ cmpl(left_high, right_high);
1243 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001244 __ j(X86Condition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001245 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001246 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001247 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001248 __ j(X86Condition(true_high_cond), true_label);
1249 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001250 }
1251 // Must be equal high, so compare the lows.
1252 __ cmpl(left_low, right_low);
Mark Mendell8659e842016-02-16 10:41:46 -05001253 } else {
1254 DCHECK(right.IsDoubleStackSlot());
1255 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
1256 if (if_cond == kCondNE) {
1257 __ j(X86Condition(true_high_cond), true_label);
1258 } else if (if_cond == kCondEQ) {
1259 __ j(X86Condition(false_high_cond), false_label);
1260 } else {
1261 __ j(X86Condition(true_high_cond), true_label);
1262 __ j(X86Condition(false_high_cond), false_label);
1263 }
1264 // Must be equal high, so compare the lows.
1265 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Mark Mendellc4701932015-04-10 13:18:51 -04001266 }
1267 // The last comparison might be unsigned.
1268 __ j(final_condition, true_label);
1269}
1270
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001271void InstructionCodeGeneratorX86::GenerateFPCompare(Location lhs,
1272 Location rhs,
1273 HInstruction* insn,
1274 bool is_double) {
1275 HX86LoadFromConstantTable* const_area = insn->InputAt(1)->AsX86LoadFromConstantTable();
1276 if (is_double) {
1277 if (rhs.IsFpuRegister()) {
1278 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1279 } else if (const_area != nullptr) {
1280 DCHECK(const_area->IsEmittedAtUseSite());
1281 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(),
1282 codegen_->LiteralDoubleAddress(
1283 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
1284 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
1285 } else {
1286 DCHECK(rhs.IsDoubleStackSlot());
1287 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(), Address(ESP, rhs.GetStackIndex()));
1288 }
1289 } else {
1290 if (rhs.IsFpuRegister()) {
1291 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1292 } else if (const_area != nullptr) {
1293 DCHECK(const_area->IsEmittedAtUseSite());
1294 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(),
1295 codegen_->LiteralFloatAddress(
1296 const_area->GetConstant()->AsFloatConstant()->GetValue(),
1297 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
1298 } else {
1299 DCHECK(rhs.IsStackSlot());
1300 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(), Address(ESP, rhs.GetStackIndex()));
1301 }
1302 }
1303}
1304
Mark Mendell152408f2015-12-31 12:28:50 -05001305template<class LabelType>
David Brazdil0debae72015-11-12 18:37:00 +00001306void InstructionCodeGeneratorX86::GenerateCompareTestAndBranch(HCondition* condition,
Mark Mendell152408f2015-12-31 12:28:50 -05001307 LabelType* true_target_in,
1308 LabelType* false_target_in) {
David Brazdil0debae72015-11-12 18:37:00 +00001309 // Generated branching requires both targets to be explicit. If either of the
1310 // targets is nullptr (fallthrough) use and bind `fallthrough_target` instead.
Mark Mendell152408f2015-12-31 12:28:50 -05001311 LabelType fallthrough_target;
1312 LabelType* true_target = true_target_in == nullptr ? &fallthrough_target : true_target_in;
1313 LabelType* false_target = false_target_in == nullptr ? &fallthrough_target : false_target_in;
David Brazdil0debae72015-11-12 18:37:00 +00001314
Mark Mendellc4701932015-04-10 13:18:51 -04001315 LocationSummary* locations = condition->GetLocations();
1316 Location left = locations->InAt(0);
1317 Location right = locations->InAt(1);
1318
Mark Mendellc4701932015-04-10 13:18:51 -04001319 Primitive::Type type = condition->InputAt(0)->GetType();
1320 switch (type) {
1321 case Primitive::kPrimLong:
1322 GenerateLongComparesAndJumps(condition, true_target, false_target);
1323 break;
1324 case Primitive::kPrimFloat:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001325 GenerateFPCompare(left, right, condition, false);
Mark Mendellc4701932015-04-10 13:18:51 -04001326 GenerateFPJumps(condition, true_target, false_target);
1327 break;
1328 case Primitive::kPrimDouble:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001329 GenerateFPCompare(left, right, condition, true);
Mark Mendellc4701932015-04-10 13:18:51 -04001330 GenerateFPJumps(condition, true_target, false_target);
1331 break;
1332 default:
1333 LOG(FATAL) << "Unexpected compare type " << type;
1334 }
1335
David Brazdil0debae72015-11-12 18:37:00 +00001336 if (false_target != &fallthrough_target) {
Mark Mendellc4701932015-04-10 13:18:51 -04001337 __ jmp(false_target);
1338 }
David Brazdil0debae72015-11-12 18:37:00 +00001339
1340 if (fallthrough_target.IsLinked()) {
1341 __ Bind(&fallthrough_target);
1342 }
Mark Mendellc4701932015-04-10 13:18:51 -04001343}
1344
David Brazdil0debae72015-11-12 18:37:00 +00001345static bool AreEflagsSetFrom(HInstruction* cond, HInstruction* branch) {
1346 // Moves may affect the eflags register (move zero uses xorl), so the EFLAGS
1347 // are set only strictly before `branch`. We can't use the eflags on long/FP
1348 // conditions if they are materialized due to the complex branching.
1349 return cond->IsCondition() &&
1350 cond->GetNext() == branch &&
1351 cond->InputAt(0)->GetType() != Primitive::kPrimLong &&
1352 !Primitive::IsFloatingPointType(cond->InputAt(0)->GetType());
1353}
1354
Mark Mendell152408f2015-12-31 12:28:50 -05001355template<class LabelType>
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001356void InstructionCodeGeneratorX86::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00001357 size_t condition_input_index,
Mark Mendell152408f2015-12-31 12:28:50 -05001358 LabelType* true_target,
1359 LabelType* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00001360 HInstruction* cond = instruction->InputAt(condition_input_index);
1361
1362 if (true_target == nullptr && false_target == nullptr) {
1363 // Nothing to do. The code always falls through.
1364 return;
1365 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00001366 // Constant condition, statically compared against "true" (integer value 1).
1367 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00001368 if (true_target != nullptr) {
1369 __ jmp(true_target);
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001370 }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001371 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00001372 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00001373 if (false_target != nullptr) {
1374 __ jmp(false_target);
1375 }
1376 }
1377 return;
1378 }
1379
1380 // The following code generates these patterns:
1381 // (1) true_target == nullptr && false_target != nullptr
1382 // - opposite condition true => branch to false_target
1383 // (2) true_target != nullptr && false_target == nullptr
1384 // - condition true => branch to true_target
1385 // (3) true_target != nullptr && false_target != nullptr
1386 // - condition true => branch to true_target
1387 // - branch to false_target
1388 if (IsBooleanValueOrMaterializedCondition(cond)) {
1389 if (AreEflagsSetFrom(cond, instruction)) {
1390 if (true_target == nullptr) {
1391 __ j(X86Condition(cond->AsCondition()->GetOppositeCondition()), false_target);
1392 } else {
1393 __ j(X86Condition(cond->AsCondition()->GetCondition()), true_target);
1394 }
1395 } else {
1396 // Materialized condition, compare against 0.
1397 Location lhs = instruction->GetLocations()->InAt(condition_input_index);
1398 if (lhs.IsRegister()) {
1399 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
1400 } else {
1401 __ cmpl(Address(ESP, lhs.GetStackIndex()), Immediate(0));
1402 }
1403 if (true_target == nullptr) {
1404 __ j(kEqual, false_target);
1405 } else {
1406 __ j(kNotEqual, true_target);
1407 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001408 }
1409 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001410 // Condition has not been materialized, use its inputs as the comparison and
1411 // its condition as the branch condition.
Mark Mendellb8b97692015-05-22 16:58:19 -04001412 HCondition* condition = cond->AsCondition();
David Brazdil0debae72015-11-12 18:37:00 +00001413
1414 // If this is a long or FP comparison that has been folded into
1415 // the HCondition, generate the comparison directly.
1416 Primitive::Type type = condition->InputAt(0)->GetType();
1417 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
1418 GenerateCompareTestAndBranch(condition, true_target, false_target);
1419 return;
1420 }
1421
1422 Location lhs = condition->GetLocations()->InAt(0);
1423 Location rhs = condition->GetLocations()->InAt(1);
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001424 // LHS is guaranteed to be in a register (see LocationsBuilderX86::HandleCondition).
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001425 codegen_->GenerateIntCompare(lhs, rhs);
David Brazdil0debae72015-11-12 18:37:00 +00001426 if (true_target == nullptr) {
1427 __ j(X86Condition(condition->GetOppositeCondition()), false_target);
1428 } else {
Mark Mendellb8b97692015-05-22 16:58:19 -04001429 __ j(X86Condition(condition->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -07001430 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001431 }
David Brazdil0debae72015-11-12 18:37:00 +00001432
1433 // If neither branch falls through (case 3), the conditional branch to `true_target`
1434 // was already emitted (case 2) and we need to emit a jump to `false_target`.
1435 if (true_target != nullptr && false_target != nullptr) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001436 __ jmp(false_target);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001437 }
1438}
1439
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001440void LocationsBuilderX86::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001441 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
1442 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001443 locations->SetInAt(0, Location::Any());
1444 }
1445}
1446
1447void InstructionCodeGeneratorX86::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001448 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
1449 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
1450 Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
1451 nullptr : codegen_->GetLabelOf(true_successor);
1452 Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
1453 nullptr : codegen_->GetLabelOf(false_successor);
1454 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001455}
1456
1457void LocationsBuilderX86::VisitDeoptimize(HDeoptimize* deoptimize) {
1458 LocationSummary* locations = new (GetGraph()->GetArena())
1459 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01001460 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
David Brazdil0debae72015-11-12 18:37:00 +00001461 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001462 locations->SetInAt(0, Location::Any());
1463 }
1464}
1465
1466void InstructionCodeGeneratorX86::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08001467 SlowPathCode* slow_path = deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathX86>(deoptimize);
David Brazdil74eb1b22015-12-14 11:44:01 +00001468 GenerateTestAndBranch<Label>(deoptimize,
1469 /* condition_input_index */ 0,
1470 slow_path->GetEntryLabel(),
1471 /* false_target */ nullptr);
1472}
1473
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001474static bool SelectCanUseCMOV(HSelect* select) {
1475 // There are no conditional move instructions for XMMs.
1476 if (Primitive::IsFloatingPointType(select->GetType())) {
1477 return false;
1478 }
1479
1480 // A FP condition doesn't generate the single CC that we need.
1481 // In 32 bit mode, a long condition doesn't generate a single CC either.
1482 HInstruction* condition = select->GetCondition();
1483 if (condition->IsCondition()) {
1484 Primitive::Type compare_type = condition->InputAt(0)->GetType();
1485 if (compare_type == Primitive::kPrimLong ||
1486 Primitive::IsFloatingPointType(compare_type)) {
1487 return false;
1488 }
1489 }
1490
1491 // We can generate a CMOV for this Select.
1492 return true;
1493}
1494
David Brazdil74eb1b22015-12-14 11:44:01 +00001495void LocationsBuilderX86::VisitSelect(HSelect* select) {
1496 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001497 if (Primitive::IsFloatingPointType(select->GetType())) {
David Brazdil74eb1b22015-12-14 11:44:01 +00001498 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001499 locations->SetInAt(1, Location::Any());
David Brazdil74eb1b22015-12-14 11:44:01 +00001500 } else {
1501 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001502 if (SelectCanUseCMOV(select)) {
1503 if (select->InputAt(1)->IsConstant()) {
1504 // Cmov can't handle a constant value.
1505 locations->SetInAt(1, Location::RequiresRegister());
1506 } else {
1507 locations->SetInAt(1, Location::Any());
1508 }
1509 } else {
1510 locations->SetInAt(1, Location::Any());
1511 }
David Brazdil74eb1b22015-12-14 11:44:01 +00001512 }
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001513 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
1514 locations->SetInAt(2, Location::RequiresRegister());
David Brazdil74eb1b22015-12-14 11:44:01 +00001515 }
1516 locations->SetOut(Location::SameAsFirstInput());
1517}
1518
1519void InstructionCodeGeneratorX86::VisitSelect(HSelect* select) {
1520 LocationSummary* locations = select->GetLocations();
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001521 DCHECK(locations->InAt(0).Equals(locations->Out()));
1522 if (SelectCanUseCMOV(select)) {
1523 // If both the condition and the source types are integer, we can generate
1524 // a CMOV to implement Select.
1525
1526 HInstruction* select_condition = select->GetCondition();
1527 Condition cond = kNotEqual;
1528
1529 // Figure out how to test the 'condition'.
1530 if (select_condition->IsCondition()) {
1531 HCondition* condition = select_condition->AsCondition();
1532 if (!condition->IsEmittedAtUseSite()) {
1533 // This was a previously materialized condition.
1534 // Can we use the existing condition code?
1535 if (AreEflagsSetFrom(condition, select)) {
1536 // Materialization was the previous instruction. Condition codes are right.
1537 cond = X86Condition(condition->GetCondition());
1538 } else {
1539 // No, we have to recreate the condition code.
1540 Register cond_reg = locations->InAt(2).AsRegister<Register>();
1541 __ testl(cond_reg, cond_reg);
1542 }
1543 } else {
1544 // We can't handle FP or long here.
1545 DCHECK_NE(condition->InputAt(0)->GetType(), Primitive::kPrimLong);
1546 DCHECK(!Primitive::IsFloatingPointType(condition->InputAt(0)->GetType()));
1547 LocationSummary* cond_locations = condition->GetLocations();
Roland Levillain0b671c02016-08-19 12:02:34 +01001548 codegen_->GenerateIntCompare(cond_locations->InAt(0), cond_locations->InAt(1));
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001549 cond = X86Condition(condition->GetCondition());
1550 }
1551 } else {
1552 // Must be a boolean condition, which needs to be compared to 0.
1553 Register cond_reg = locations->InAt(2).AsRegister<Register>();
1554 __ testl(cond_reg, cond_reg);
1555 }
1556
1557 // If the condition is true, overwrite the output, which already contains false.
1558 Location false_loc = locations->InAt(0);
1559 Location true_loc = locations->InAt(1);
1560 if (select->GetType() == Primitive::kPrimLong) {
1561 // 64 bit conditional move.
1562 Register false_high = false_loc.AsRegisterPairHigh<Register>();
1563 Register false_low = false_loc.AsRegisterPairLow<Register>();
1564 if (true_loc.IsRegisterPair()) {
1565 __ cmovl(cond, false_high, true_loc.AsRegisterPairHigh<Register>());
1566 __ cmovl(cond, false_low, true_loc.AsRegisterPairLow<Register>());
1567 } else {
1568 __ cmovl(cond, false_high, Address(ESP, true_loc.GetHighStackIndex(kX86WordSize)));
1569 __ cmovl(cond, false_low, Address(ESP, true_loc.GetStackIndex()));
1570 }
1571 } else {
1572 // 32 bit conditional move.
1573 Register false_reg = false_loc.AsRegister<Register>();
1574 if (true_loc.IsRegister()) {
1575 __ cmovl(cond, false_reg, true_loc.AsRegister<Register>());
1576 } else {
1577 __ cmovl(cond, false_reg, Address(ESP, true_loc.GetStackIndex()));
1578 }
1579 }
1580 } else {
1581 NearLabel false_target;
1582 GenerateTestAndBranch<NearLabel>(
1583 select, /* condition_input_index */ 2, /* true_target */ nullptr, &false_target);
1584 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
1585 __ Bind(&false_target);
1586 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001587}
1588
David Srbecky0cf44932015-12-09 14:09:59 +00001589void LocationsBuilderX86::VisitNativeDebugInfo(HNativeDebugInfo* info) {
1590 new (GetGraph()->GetArena()) LocationSummary(info);
1591}
1592
David Srbeckyd28f4a02016-03-14 17:14:24 +00001593void InstructionCodeGeneratorX86::VisitNativeDebugInfo(HNativeDebugInfo*) {
1594 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00001595}
1596
1597void CodeGeneratorX86::GenerateNop() {
1598 __ nop();
David Srbecky0cf44932015-12-09 14:09:59 +00001599}
1600
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001601void LocationsBuilderX86::HandleCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001602 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +01001603 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Mark Mendellc4701932015-04-10 13:18:51 -04001604 // Handle the long/FP comparisons made in instruction simplification.
1605 switch (cond->InputAt(0)->GetType()) {
1606 case Primitive::kPrimLong: {
1607 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell8659e842016-02-16 10:41:46 -05001608 locations->SetInAt(1, Location::Any());
David Brazdilb3e773e2016-01-26 11:28:37 +00001609 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001610 locations->SetOut(Location::RequiresRegister());
1611 }
1612 break;
1613 }
1614 case Primitive::kPrimFloat:
1615 case Primitive::kPrimDouble: {
1616 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001617 if (cond->InputAt(1)->IsX86LoadFromConstantTable()) {
1618 DCHECK(cond->InputAt(1)->IsEmittedAtUseSite());
1619 } else if (cond->InputAt(1)->IsConstant()) {
1620 locations->SetInAt(1, Location::RequiresFpuRegister());
1621 } else {
1622 locations->SetInAt(1, Location::Any());
1623 }
David Brazdilb3e773e2016-01-26 11:28:37 +00001624 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001625 locations->SetOut(Location::RequiresRegister());
1626 }
1627 break;
1628 }
1629 default:
1630 locations->SetInAt(0, Location::RequiresRegister());
1631 locations->SetInAt(1, Location::Any());
David Brazdilb3e773e2016-01-26 11:28:37 +00001632 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001633 // We need a byte register.
1634 locations->SetOut(Location::RegisterLocation(ECX));
1635 }
1636 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001637 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001638}
1639
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001640void InstructionCodeGeneratorX86::HandleCondition(HCondition* cond) {
David Brazdilb3e773e2016-01-26 11:28:37 +00001641 if (cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001642 return;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001643 }
Mark Mendellc4701932015-04-10 13:18:51 -04001644
1645 LocationSummary* locations = cond->GetLocations();
1646 Location lhs = locations->InAt(0);
1647 Location rhs = locations->InAt(1);
1648 Register reg = locations->Out().AsRegister<Register>();
Mark Mendell152408f2015-12-31 12:28:50 -05001649 NearLabel true_label, false_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001650
1651 switch (cond->InputAt(0)->GetType()) {
1652 default: {
1653 // Integer case.
1654
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01001655 // Clear output register: setb only sets the low byte.
Mark Mendellc4701932015-04-10 13:18:51 -04001656 __ xorl(reg, reg);
Roland Levillain0b671c02016-08-19 12:02:34 +01001657 codegen_->GenerateIntCompare(lhs, rhs);
Aart Bike9f37602015-10-09 11:15:55 -07001658 __ setb(X86Condition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001659 return;
1660 }
1661 case Primitive::kPrimLong:
1662 GenerateLongComparesAndJumps(cond, &true_label, &false_label);
1663 break;
1664 case Primitive::kPrimFloat:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001665 GenerateFPCompare(lhs, rhs, cond, false);
Mark Mendellc4701932015-04-10 13:18:51 -04001666 GenerateFPJumps(cond, &true_label, &false_label);
1667 break;
1668 case Primitive::kPrimDouble:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001669 GenerateFPCompare(lhs, rhs, cond, true);
Mark Mendellc4701932015-04-10 13:18:51 -04001670 GenerateFPJumps(cond, &true_label, &false_label);
1671 break;
1672 }
1673
1674 // Convert the jumps into the result.
Mark Mendell0c9497d2015-08-21 09:30:05 -04001675 NearLabel done_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001676
Roland Levillain4fa13f62015-07-06 18:11:54 +01001677 // False case: result = 0.
Mark Mendellc4701932015-04-10 13:18:51 -04001678 __ Bind(&false_label);
1679 __ xorl(reg, reg);
1680 __ jmp(&done_label);
1681
Roland Levillain4fa13f62015-07-06 18:11:54 +01001682 // True case: result = 1.
Mark Mendellc4701932015-04-10 13:18:51 -04001683 __ Bind(&true_label);
1684 __ movl(reg, Immediate(1));
1685 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07001686}
1687
1688void LocationsBuilderX86::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001689 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001690}
1691
1692void InstructionCodeGeneratorX86::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001693 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001694}
1695
1696void LocationsBuilderX86::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001697 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001698}
1699
1700void InstructionCodeGeneratorX86::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001701 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001702}
1703
1704void LocationsBuilderX86::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001705 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001706}
1707
1708void InstructionCodeGeneratorX86::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001709 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001710}
1711
1712void LocationsBuilderX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001713 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001714}
1715
1716void InstructionCodeGeneratorX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001717 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001718}
1719
1720void LocationsBuilderX86::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001721 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001722}
1723
1724void InstructionCodeGeneratorX86::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001725 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001726}
1727
1728void LocationsBuilderX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001729 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001730}
1731
1732void InstructionCodeGeneratorX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001733 HandleCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001734}
1735
Aart Bike9f37602015-10-09 11:15:55 -07001736void LocationsBuilderX86::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001737 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001738}
1739
1740void InstructionCodeGeneratorX86::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001741 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001742}
1743
1744void LocationsBuilderX86::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001745 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001746}
1747
1748void InstructionCodeGeneratorX86::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001749 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001750}
1751
1752void LocationsBuilderX86::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001753 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001754}
1755
1756void InstructionCodeGeneratorX86::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001757 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001758}
1759
1760void LocationsBuilderX86::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001761 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001762}
1763
1764void InstructionCodeGeneratorX86::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001765 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001766}
1767
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001768void LocationsBuilderX86::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001769 LocationSummary* locations =
1770 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001771 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001772}
1773
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001774void InstructionCodeGeneratorX86::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001775 // Will be generated at use site.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001776}
1777
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001778void LocationsBuilderX86::VisitNullConstant(HNullConstant* constant) {
1779 LocationSummary* locations =
1780 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1781 locations->SetOut(Location::ConstantLocation(constant));
1782}
1783
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001784void InstructionCodeGeneratorX86::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001785 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001786}
1787
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001788void LocationsBuilderX86::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001789 LocationSummary* locations =
1790 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001791 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001792}
1793
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001794void InstructionCodeGeneratorX86::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001795 // Will be generated at use site.
1796}
1797
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001798void LocationsBuilderX86::VisitFloatConstant(HFloatConstant* constant) {
1799 LocationSummary* locations =
1800 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1801 locations->SetOut(Location::ConstantLocation(constant));
1802}
1803
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001804void InstructionCodeGeneratorX86::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001805 // Will be generated at use site.
1806}
1807
1808void LocationsBuilderX86::VisitDoubleConstant(HDoubleConstant* constant) {
1809 LocationSummary* locations =
1810 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1811 locations->SetOut(Location::ConstantLocation(constant));
1812}
1813
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001814void InstructionCodeGeneratorX86::VisitDoubleConstant(HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001815 // Will be generated at use site.
1816}
1817
Calin Juravle27df7582015-04-17 19:12:31 +01001818void LocationsBuilderX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1819 memory_barrier->SetLocations(nullptr);
1820}
1821
1822void InstructionCodeGeneratorX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00001823 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01001824}
1825
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001826void LocationsBuilderX86::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001827 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001828}
1829
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001830void InstructionCodeGeneratorX86::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001831 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001832}
1833
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001834void LocationsBuilderX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001835 LocationSummary* locations =
1836 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001837 switch (ret->InputAt(0)->GetType()) {
1838 case Primitive::kPrimBoolean:
1839 case Primitive::kPrimByte:
1840 case Primitive::kPrimChar:
1841 case Primitive::kPrimShort:
1842 case Primitive::kPrimInt:
1843 case Primitive::kPrimNot:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001844 locations->SetInAt(0, Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001845 break;
1846
1847 case Primitive::kPrimLong:
1848 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001849 0, Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001850 break;
1851
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001852 case Primitive::kPrimFloat:
1853 case Primitive::kPrimDouble:
1854 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001855 0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001856 break;
1857
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001858 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001859 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001860 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001861}
1862
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001863void InstructionCodeGeneratorX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001864 if (kIsDebugBuild) {
1865 switch (ret->InputAt(0)->GetType()) {
1866 case Primitive::kPrimBoolean:
1867 case Primitive::kPrimByte:
1868 case Primitive::kPrimChar:
1869 case Primitive::kPrimShort:
1870 case Primitive::kPrimInt:
1871 case Primitive::kPrimNot:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001872 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<Register>(), EAX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001873 break;
1874
1875 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001876 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairLow<Register>(), EAX);
1877 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairHigh<Register>(), EDX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001878 break;
1879
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001880 case Primitive::kPrimFloat:
1881 case Primitive::kPrimDouble:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001882 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>(), XMM0);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001883 break;
1884
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001885 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001886 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001887 }
1888 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001889 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001890}
1891
Calin Juravle175dc732015-08-25 15:42:32 +01001892void LocationsBuilderX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
1893 // The trampoline uses the same calling convention as dex calling conventions,
1894 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
1895 // the method_idx.
1896 HandleInvoke(invoke);
1897}
1898
1899void InstructionCodeGeneratorX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
1900 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
1901}
1902
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001903void LocationsBuilderX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00001904 // Explicit clinit checks triggered by static invokes must have been pruned by
1905 // art::PrepareForRegisterAllocation.
1906 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001907
Mark Mendellfb8d2792015-03-31 22:16:59 -04001908 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001909 if (intrinsic.TryDispatch(invoke)) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00001910 if (invoke->GetLocations()->CanCall() && invoke->HasPcRelativeDexCache()) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00001911 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00001912 }
Mark Mendell09ed1a32015-03-25 08:30:06 -04001913 return;
1914 }
1915
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001916 HandleInvoke(invoke);
Nicolas Geoffray94015b92015-06-04 18:21:04 +01001917
Vladimir Marko0f7dca42015-11-02 14:36:43 +00001918 // For PC-relative dex cache the invoke has an extra input, the PC-relative address base.
1919 if (invoke->HasPcRelativeDexCache()) {
Vladimir Markob4536b72015-11-24 13:45:23 +00001920 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00001921 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001922}
1923
Mark Mendell09ed1a32015-03-25 08:30:06 -04001924static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86* codegen) {
1925 if (invoke->GetLocations()->Intrinsified()) {
1926 IntrinsicCodeGeneratorX86 intrinsic(codegen);
1927 intrinsic.Dispatch(invoke);
1928 return true;
1929 }
1930 return false;
1931}
1932
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001933void InstructionCodeGeneratorX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00001934 // Explicit clinit checks triggered by static invokes must have been pruned by
1935 // art::PrepareForRegisterAllocation.
1936 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001937
Mark Mendell09ed1a32015-03-25 08:30:06 -04001938 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1939 return;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001940 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001941
Nicolas Geoffray94015b92015-06-04 18:21:04 +01001942 LocationSummary* locations = invoke->GetLocations();
Mark Mendell09ed1a32015-03-25 08:30:06 -04001943 codegen_->GenerateStaticOrDirectCall(
Nicolas Geoffray94015b92015-06-04 18:21:04 +01001944 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Mingyao Yang8693fe12015-04-17 16:51:08 -07001945 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001946}
1947
1948void LocationsBuilderX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00001949 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
1950 if (intrinsic.TryDispatch(invoke)) {
1951 return;
1952 }
1953
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001954 HandleInvoke(invoke);
1955}
1956
1957void LocationsBuilderX86::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001958 InvokeDexCallingConventionVisitorX86 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001959 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001960}
1961
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001962void InstructionCodeGeneratorX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampebfb5ba92015-09-01 15:45:02 +00001963 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1964 return;
1965 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001966
Andreas Gampebfb5ba92015-09-01 15:45:02 +00001967 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001968 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001969 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001970}
1971
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001972void LocationsBuilderX86::VisitInvokeInterface(HInvokeInterface* invoke) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00001973 // This call to HandleInvoke allocates a temporary (core) register
1974 // which is also used to transfer the hidden argument from FP to
1975 // core register.
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001976 HandleInvoke(invoke);
1977 // Add the hidden argument.
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001978 invoke->GetLocations()->AddTemp(Location::FpuRegisterLocation(XMM7));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001979}
1980
1981void InstructionCodeGeneratorX86::VisitInvokeInterface(HInvokeInterface* invoke) {
1982 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain0d5a2812015-11-13 10:07:31 +00001983 LocationSummary* locations = invoke->GetLocations();
1984 Register temp = locations->GetTemp(0).AsRegister<Register>();
1985 XmmRegister hidden_reg = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001986 Location receiver = locations->InAt(0);
1987 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1988
Roland Levillain0d5a2812015-11-13 10:07:31 +00001989 // Set the hidden argument. This is safe to do this here, as XMM7
1990 // won't be modified thereafter, before the `call` instruction.
1991 DCHECK_EQ(XMM7, hidden_reg);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001992 __ movl(temp, Immediate(invoke->GetDexMethodIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00001993 __ movd(hidden_reg, temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001994
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001995 if (receiver.IsStackSlot()) {
1996 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00001997 // /* HeapReference<Class> */ temp = temp->klass_
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001998 __ movl(temp, Address(temp, class_offset));
1999 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002000 // /* HeapReference<Class> */ temp = receiver->klass_
Roland Levillain271ab9c2014-11-27 15:23:57 +00002001 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002002 }
Roland Levillain4d027112015-07-01 15:41:14 +01002003 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00002004 // Instead of simply (possibly) unpoisoning `temp` here, we should
2005 // emit a read barrier for the previous class reference load.
2006 // However this is not required in practice, as this is an
2007 // intermediate/temporary reference and because the current
2008 // concurrent copying collector keeps the from-space memory
2009 // intact/accessible until the end of the marking phase (the
2010 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01002011 __ MaybeUnpoisonHeapReference(temp);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002012 // temp = temp->GetAddressOfIMT()
2013 __ movl(temp,
2014 Address(temp, mirror::Class::ImtPtrOffset(kX86PointerSize).Uint32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002015 // temp = temp->GetImtEntryAt(method_offset);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002016 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00002017 invoke->GetImtIndex(), kX86PointerSize));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002018 __ movl(temp, Address(temp, method_offset));
2019 // call temp->GetEntryPoint();
Roland Levillain0d5a2812015-11-13 10:07:31 +00002020 __ call(Address(temp,
Andreas Gampe542451c2016-07-26 09:02:02 -07002021 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86PointerSize).Int32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002022
2023 DCHECK(!codegen_->IsLeafMethod());
2024 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2025}
2026
Roland Levillain88cb1752014-10-20 16:36:47 +01002027void LocationsBuilderX86::VisitNeg(HNeg* neg) {
2028 LocationSummary* locations =
2029 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2030 switch (neg->GetResultType()) {
2031 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002032 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01002033 locations->SetInAt(0, Location::RequiresRegister());
2034 locations->SetOut(Location::SameAsFirstInput());
2035 break;
2036
Roland Levillain88cb1752014-10-20 16:36:47 +01002037 case Primitive::kPrimFloat:
Roland Levillain5368c212014-11-27 15:03:41 +00002038 locations->SetInAt(0, Location::RequiresFpuRegister());
2039 locations->SetOut(Location::SameAsFirstInput());
2040 locations->AddTemp(Location::RequiresRegister());
2041 locations->AddTemp(Location::RequiresFpuRegister());
2042 break;
2043
Roland Levillain88cb1752014-10-20 16:36:47 +01002044 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002045 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00002046 locations->SetOut(Location::SameAsFirstInput());
2047 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01002048 break;
2049
2050 default:
2051 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2052 }
2053}
2054
2055void InstructionCodeGeneratorX86::VisitNeg(HNeg* neg) {
2056 LocationSummary* locations = neg->GetLocations();
2057 Location out = locations->Out();
2058 Location in = locations->InAt(0);
2059 switch (neg->GetResultType()) {
2060 case Primitive::kPrimInt:
2061 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002062 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002063 __ negl(out.AsRegister<Register>());
Roland Levillain88cb1752014-10-20 16:36:47 +01002064 break;
2065
2066 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002067 DCHECK(in.IsRegisterPair());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002068 DCHECK(in.Equals(out));
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002069 __ negl(out.AsRegisterPairLow<Register>());
2070 // Negation is similar to subtraction from zero. The least
2071 // significant byte triggers a borrow when it is different from
2072 // zero; to take it into account, add 1 to the most significant
2073 // byte if the carry flag (CF) is set to 1 after the first NEGL
2074 // operation.
2075 __ adcl(out.AsRegisterPairHigh<Register>(), Immediate(0));
2076 __ negl(out.AsRegisterPairHigh<Register>());
2077 break;
2078
Roland Levillain5368c212014-11-27 15:03:41 +00002079 case Primitive::kPrimFloat: {
2080 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002081 Register constant = locations->GetTemp(0).AsRegister<Register>();
2082 XmmRegister mask = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002083 // Implement float negation with an exclusive or with value
2084 // 0x80000000 (mask for bit 31, representing the sign of a
2085 // single-precision floating-point number).
2086 __ movl(constant, Immediate(INT32_C(0x80000000)));
2087 __ movd(mask, constant);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002088 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00002089 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002090 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00002091
Roland Levillain5368c212014-11-27 15:03:41 +00002092 case Primitive::kPrimDouble: {
2093 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002094 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002095 // Implement double negation with an exclusive or with value
2096 // 0x8000000000000000 (mask for bit 63, representing the sign of
2097 // a double-precision floating-point number).
2098 __ LoadLongConstant(mask, INT64_C(0x8000000000000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002099 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01002100 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002101 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002102
2103 default:
2104 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2105 }
2106}
2107
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002108void LocationsBuilderX86::VisitX86FPNeg(HX86FPNeg* neg) {
2109 LocationSummary* locations =
2110 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2111 DCHECK(Primitive::IsFloatingPointType(neg->GetType()));
2112 locations->SetInAt(0, Location::RequiresFpuRegister());
2113 locations->SetInAt(1, Location::RequiresRegister());
2114 locations->SetOut(Location::SameAsFirstInput());
2115 locations->AddTemp(Location::RequiresFpuRegister());
2116}
2117
2118void InstructionCodeGeneratorX86::VisitX86FPNeg(HX86FPNeg* neg) {
2119 LocationSummary* locations = neg->GetLocations();
2120 Location out = locations->Out();
2121 DCHECK(locations->InAt(0).Equals(out));
2122
2123 Register constant_area = locations->InAt(1).AsRegister<Register>();
2124 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
2125 if (neg->GetType() == Primitive::kPrimFloat) {
2126 __ movss(mask, codegen_->LiteralInt32Address(INT32_C(0x80000000), constant_area));
2127 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
2128 } else {
2129 __ movsd(mask, codegen_->LiteralInt64Address(INT64_C(0x8000000000000000), constant_area));
2130 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
2131 }
2132}
2133
Roland Levillaindff1f282014-11-05 14:15:05 +00002134void LocationsBuilderX86::VisitTypeConversion(HTypeConversion* conversion) {
Roland Levillaindff1f282014-11-05 14:15:05 +00002135 Primitive::Type result_type = conversion->GetResultType();
2136 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002137 DCHECK_NE(result_type, input_type);
Roland Levillain624279f2014-12-04 11:54:28 +00002138
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002139 // The float-to-long and double-to-long type conversions rely on a
2140 // call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00002141 LocationSummary::CallKind call_kind =
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002142 ((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
2143 && result_type == Primitive::kPrimLong)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01002144 ? LocationSummary::kCallOnMainOnly
Roland Levillain624279f2014-12-04 11:54:28 +00002145 : LocationSummary::kNoCall;
2146 LocationSummary* locations =
2147 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
2148
David Brazdilb2bd1c52015-03-25 11:17:37 +00002149 // The Java language does not allow treating boolean as an integral type but
2150 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00002151
Roland Levillaindff1f282014-11-05 14:15:05 +00002152 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002153 case Primitive::kPrimByte:
2154 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002155 case Primitive::kPrimLong: {
2156 // Type conversion from long to byte is a result of code transformations.
2157 HInstruction* input = conversion->InputAt(0);
2158 Location input_location = input->IsConstant()
2159 ? Location::ConstantLocation(input->AsConstant())
2160 : Location::RegisterPairLocation(EAX, EDX);
2161 locations->SetInAt(0, input_location);
2162 // Make the output overlap to please the register allocator. This greatly simplifies
2163 // the validation of the linear scan implementation
2164 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2165 break;
2166 }
David Brazdil46e2a392015-03-16 17:31:52 +00002167 case Primitive::kPrimBoolean:
2168 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002169 case Primitive::kPrimShort:
2170 case Primitive::kPrimInt:
2171 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002172 // Processing a Dex `int-to-byte' instruction.
Mark Mendell5f874182015-03-04 15:42:45 -05002173 locations->SetInAt(0, Location::ByteRegisterOrConstant(ECX, conversion->InputAt(0)));
2174 // Make the output overlap to please the register allocator. This greatly simplifies
2175 // the validation of the linear scan implementation
2176 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Roland Levillain51d3fc42014-11-13 14:11:42 +00002177 break;
2178
2179 default:
2180 LOG(FATAL) << "Unexpected type conversion from " << input_type
2181 << " to " << result_type;
2182 }
2183 break;
2184
Roland Levillain01a8d712014-11-14 16:27:39 +00002185 case Primitive::kPrimShort:
2186 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002187 case Primitive::kPrimLong:
2188 // Type conversion from long to short is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002189 case Primitive::kPrimBoolean:
2190 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002191 case Primitive::kPrimByte:
2192 case Primitive::kPrimInt:
2193 case Primitive::kPrimChar:
2194 // Processing a Dex `int-to-short' instruction.
2195 locations->SetInAt(0, Location::Any());
2196 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2197 break;
2198
2199 default:
2200 LOG(FATAL) << "Unexpected type conversion from " << input_type
2201 << " to " << result_type;
2202 }
2203 break;
2204
Roland Levillain946e1432014-11-11 17:35:19 +00002205 case Primitive::kPrimInt:
2206 switch (input_type) {
2207 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002208 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002209 locations->SetInAt(0, Location::Any());
2210 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2211 break;
2212
2213 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00002214 // Processing a Dex `float-to-int' instruction.
2215 locations->SetInAt(0, Location::RequiresFpuRegister());
2216 locations->SetOut(Location::RequiresRegister());
2217 locations->AddTemp(Location::RequiresFpuRegister());
2218 break;
2219
Roland Levillain946e1432014-11-11 17:35:19 +00002220 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002221 // Processing a Dex `double-to-int' instruction.
2222 locations->SetInAt(0, Location::RequiresFpuRegister());
2223 locations->SetOut(Location::RequiresRegister());
2224 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00002225 break;
2226
2227 default:
2228 LOG(FATAL) << "Unexpected type conversion from " << input_type
2229 << " to " << result_type;
2230 }
2231 break;
2232
Roland Levillaindff1f282014-11-05 14:15:05 +00002233 case Primitive::kPrimLong:
2234 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002235 case Primitive::kPrimBoolean:
2236 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002237 case Primitive::kPrimByte:
2238 case Primitive::kPrimShort:
2239 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002240 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002241 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002242 locations->SetInAt(0, Location::RegisterLocation(EAX));
2243 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
2244 break;
2245
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002246 case Primitive::kPrimFloat:
Vladimir Marko949c91f2015-01-27 10:48:44 +00002247 case Primitive::kPrimDouble: {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002248 // Processing a Dex `float-to-long' or 'double-to-long' instruction.
Vladimir Marko949c91f2015-01-27 10:48:44 +00002249 InvokeRuntimeCallingConvention calling_convention;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002250 XmmRegister parameter = calling_convention.GetFpuRegisterAt(0);
2251 locations->SetInAt(0, Location::FpuRegisterLocation(parameter));
2252
Vladimir Marko949c91f2015-01-27 10:48:44 +00002253 // The runtime helper puts the result in EAX, EDX.
2254 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Vladimir Marko949c91f2015-01-27 10:48:44 +00002255 }
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002256 break;
Roland Levillaindff1f282014-11-05 14:15:05 +00002257
2258 default:
2259 LOG(FATAL) << "Unexpected type conversion from " << input_type
2260 << " to " << result_type;
2261 }
2262 break;
2263
Roland Levillain981e4542014-11-14 11:47:14 +00002264 case Primitive::kPrimChar:
2265 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002266 case Primitive::kPrimLong:
2267 // Type conversion from long to char is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002268 case Primitive::kPrimBoolean:
2269 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002270 case Primitive::kPrimByte:
2271 case Primitive::kPrimShort:
2272 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002273 // Processing a Dex `int-to-char' instruction.
2274 locations->SetInAt(0, Location::Any());
2275 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2276 break;
2277
2278 default:
2279 LOG(FATAL) << "Unexpected type conversion from " << input_type
2280 << " to " << result_type;
2281 }
2282 break;
2283
Roland Levillaindff1f282014-11-05 14:15:05 +00002284 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002285 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002286 case Primitive::kPrimBoolean:
2287 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002288 case Primitive::kPrimByte:
2289 case Primitive::kPrimShort:
2290 case Primitive::kPrimInt:
2291 case Primitive::kPrimChar:
2292 // Processing a Dex `int-to-float' instruction.
2293 locations->SetInAt(0, Location::RequiresRegister());
2294 locations->SetOut(Location::RequiresFpuRegister());
2295 break;
2296
2297 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002298 // Processing a Dex `long-to-float' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002299 locations->SetInAt(0, Location::Any());
2300 locations->SetOut(Location::Any());
Roland Levillain6d0e4832014-11-27 18:31:21 +00002301 break;
2302
Roland Levillaincff13742014-11-17 14:32:17 +00002303 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002304 // Processing a Dex `double-to-float' instruction.
2305 locations->SetInAt(0, Location::RequiresFpuRegister());
2306 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002307 break;
2308
2309 default:
2310 LOG(FATAL) << "Unexpected type conversion from " << input_type
2311 << " to " << result_type;
2312 };
2313 break;
2314
Roland Levillaindff1f282014-11-05 14:15:05 +00002315 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002316 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002317 case Primitive::kPrimBoolean:
2318 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002319 case Primitive::kPrimByte:
2320 case Primitive::kPrimShort:
2321 case Primitive::kPrimInt:
2322 case Primitive::kPrimChar:
2323 // Processing a Dex `int-to-double' instruction.
2324 locations->SetInAt(0, Location::RequiresRegister());
2325 locations->SetOut(Location::RequiresFpuRegister());
2326 break;
2327
2328 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002329 // Processing a Dex `long-to-double' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002330 locations->SetInAt(0, Location::Any());
2331 locations->SetOut(Location::Any());
Roland Levillain647b9ed2014-11-27 12:06:00 +00002332 break;
2333
Roland Levillaincff13742014-11-17 14:32:17 +00002334 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002335 // Processing a Dex `float-to-double' instruction.
2336 locations->SetInAt(0, Location::RequiresFpuRegister());
2337 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002338 break;
2339
2340 default:
2341 LOG(FATAL) << "Unexpected type conversion from " << input_type
2342 << " to " << result_type;
2343 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002344 break;
2345
2346 default:
2347 LOG(FATAL) << "Unexpected type conversion from " << input_type
2348 << " to " << result_type;
2349 }
2350}
2351
2352void InstructionCodeGeneratorX86::VisitTypeConversion(HTypeConversion* conversion) {
2353 LocationSummary* locations = conversion->GetLocations();
2354 Location out = locations->Out();
2355 Location in = locations->InAt(0);
2356 Primitive::Type result_type = conversion->GetResultType();
2357 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002358 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00002359 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002360 case Primitive::kPrimByte:
2361 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002362 case Primitive::kPrimLong:
2363 // Type conversion from long to byte is a result of code transformations.
2364 if (in.IsRegisterPair()) {
2365 __ movsxb(out.AsRegister<Register>(), in.AsRegisterPairLow<ByteRegister>());
2366 } else {
2367 DCHECK(in.GetConstant()->IsLongConstant());
2368 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2369 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
2370 }
2371 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002372 case Primitive::kPrimBoolean:
2373 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002374 case Primitive::kPrimShort:
2375 case Primitive::kPrimInt:
2376 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002377 // Processing a Dex `int-to-byte' instruction.
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002378 if (in.IsRegister()) {
2379 __ movsxb(out.AsRegister<Register>(), in.AsRegister<ByteRegister>());
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002380 } else {
2381 DCHECK(in.GetConstant()->IsIntConstant());
2382 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
2383 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
2384 }
Roland Levillain51d3fc42014-11-13 14:11:42 +00002385 break;
2386
2387 default:
2388 LOG(FATAL) << "Unexpected type conversion from " << input_type
2389 << " to " << result_type;
2390 }
2391 break;
2392
Roland Levillain01a8d712014-11-14 16:27:39 +00002393 case Primitive::kPrimShort:
2394 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002395 case Primitive::kPrimLong:
2396 // Type conversion from long to short is a result of code transformations.
2397 if (in.IsRegisterPair()) {
2398 __ movsxw(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
2399 } else if (in.IsDoubleStackSlot()) {
2400 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
2401 } else {
2402 DCHECK(in.GetConstant()->IsLongConstant());
2403 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2404 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
2405 }
2406 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002407 case Primitive::kPrimBoolean:
2408 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002409 case Primitive::kPrimByte:
2410 case Primitive::kPrimInt:
2411 case Primitive::kPrimChar:
2412 // Processing a Dex `int-to-short' instruction.
2413 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002414 __ movsxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain01a8d712014-11-14 16:27:39 +00002415 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002416 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain01a8d712014-11-14 16:27:39 +00002417 } else {
2418 DCHECK(in.GetConstant()->IsIntConstant());
2419 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002420 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
Roland Levillain01a8d712014-11-14 16:27:39 +00002421 }
2422 break;
2423
2424 default:
2425 LOG(FATAL) << "Unexpected type conversion from " << input_type
2426 << " to " << result_type;
2427 }
2428 break;
2429
Roland Levillain946e1432014-11-11 17:35:19 +00002430 case Primitive::kPrimInt:
2431 switch (input_type) {
2432 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002433 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002434 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002435 __ movl(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00002436 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002437 __ movl(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain946e1432014-11-11 17:35:19 +00002438 } else {
2439 DCHECK(in.IsConstant());
2440 DCHECK(in.GetConstant()->IsLongConstant());
2441 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002442 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00002443 }
2444 break;
2445
Roland Levillain3f8f9362014-12-02 17:45:01 +00002446 case Primitive::kPrimFloat: {
2447 // Processing a Dex `float-to-int' instruction.
2448 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2449 Register output = out.AsRegister<Register>();
2450 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002451 NearLabel done, nan;
Roland Levillain3f8f9362014-12-02 17:45:01 +00002452
2453 __ movl(output, Immediate(kPrimIntMax));
2454 // temp = int-to-float(output)
2455 __ cvtsi2ss(temp, output);
2456 // if input >= temp goto done
2457 __ comiss(input, temp);
2458 __ j(kAboveEqual, &done);
2459 // if input == NaN goto nan
2460 __ j(kUnordered, &nan);
2461 // output = float-to-int-truncate(input)
2462 __ cvttss2si(output, input);
2463 __ jmp(&done);
2464 __ Bind(&nan);
2465 // output = 0
2466 __ xorl(output, output);
2467 __ Bind(&done);
2468 break;
2469 }
2470
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002471 case Primitive::kPrimDouble: {
2472 // Processing a Dex `double-to-int' instruction.
2473 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2474 Register output = out.AsRegister<Register>();
2475 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002476 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002477
2478 __ movl(output, Immediate(kPrimIntMax));
2479 // temp = int-to-double(output)
2480 __ cvtsi2sd(temp, output);
2481 // if input >= temp goto done
2482 __ comisd(input, temp);
2483 __ j(kAboveEqual, &done);
2484 // if input == NaN goto nan
2485 __ j(kUnordered, &nan);
2486 // output = double-to-int-truncate(input)
2487 __ cvttsd2si(output, input);
2488 __ jmp(&done);
2489 __ Bind(&nan);
2490 // output = 0
2491 __ xorl(output, output);
2492 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00002493 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002494 }
Roland Levillain946e1432014-11-11 17:35:19 +00002495
2496 default:
2497 LOG(FATAL) << "Unexpected type conversion from " << input_type
2498 << " to " << result_type;
2499 }
2500 break;
2501
Roland Levillaindff1f282014-11-05 14:15:05 +00002502 case Primitive::kPrimLong:
2503 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002504 case Primitive::kPrimBoolean:
2505 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002506 case Primitive::kPrimByte:
2507 case Primitive::kPrimShort:
2508 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002509 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002510 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002511 DCHECK_EQ(out.AsRegisterPairLow<Register>(), EAX);
2512 DCHECK_EQ(out.AsRegisterPairHigh<Register>(), EDX);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002513 DCHECK_EQ(in.AsRegister<Register>(), EAX);
Roland Levillaindff1f282014-11-05 14:15:05 +00002514 __ cdq();
2515 break;
2516
2517 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00002518 // Processing a Dex `float-to-long' instruction.
Serban Constantinescuba45db02016-07-12 22:53:02 +01002519 codegen_->InvokeRuntime(kQuickF2l, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00002520 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
Roland Levillain624279f2014-12-04 11:54:28 +00002521 break;
2522
Roland Levillaindff1f282014-11-05 14:15:05 +00002523 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002524 // Processing a Dex `double-to-long' instruction.
Serban Constantinescuba45db02016-07-12 22:53:02 +01002525 codegen_->InvokeRuntime(kQuickD2l, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00002526 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
Roland Levillaindff1f282014-11-05 14:15:05 +00002527 break;
2528
2529 default:
2530 LOG(FATAL) << "Unexpected type conversion from " << input_type
2531 << " to " << result_type;
2532 }
2533 break;
2534
Roland Levillain981e4542014-11-14 11:47:14 +00002535 case Primitive::kPrimChar:
2536 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002537 case Primitive::kPrimLong:
2538 // Type conversion from long to short is a result of code transformations.
2539 if (in.IsRegisterPair()) {
2540 __ movzxw(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
2541 } else if (in.IsDoubleStackSlot()) {
2542 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
2543 } else {
2544 DCHECK(in.GetConstant()->IsLongConstant());
2545 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2546 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
2547 }
2548 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002549 case Primitive::kPrimBoolean:
2550 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002551 case Primitive::kPrimByte:
2552 case Primitive::kPrimShort:
2553 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002554 // Processing a Dex `Process a Dex `int-to-char'' instruction.
2555 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002556 __ movzxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain981e4542014-11-14 11:47:14 +00002557 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002558 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain981e4542014-11-14 11:47:14 +00002559 } else {
2560 DCHECK(in.GetConstant()->IsIntConstant());
2561 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002562 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
Roland Levillain981e4542014-11-14 11:47:14 +00002563 }
2564 break;
2565
2566 default:
2567 LOG(FATAL) << "Unexpected type conversion from " << input_type
2568 << " to " << result_type;
2569 }
2570 break;
2571
Roland Levillaindff1f282014-11-05 14:15:05 +00002572 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002573 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002574 case Primitive::kPrimBoolean:
2575 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002576 case Primitive::kPrimByte:
2577 case Primitive::kPrimShort:
2578 case Primitive::kPrimInt:
2579 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002580 // Processing a Dex `int-to-float' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002581 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002582 break;
2583
Roland Levillain6d0e4832014-11-27 18:31:21 +00002584 case Primitive::kPrimLong: {
2585 // Processing a Dex `long-to-float' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002586 size_t adjustment = 0;
Roland Levillain6d0e4832014-11-27 18:31:21 +00002587
Roland Levillain232ade02015-04-20 15:14:36 +01002588 // Create stack space for the call to
2589 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstps below.
2590 // TODO: enhance register allocator to ask for stack temporaries.
2591 if (!in.IsDoubleStackSlot() || !out.IsStackSlot()) {
2592 adjustment = Primitive::ComponentSize(Primitive::kPrimLong);
2593 __ subl(ESP, Immediate(adjustment));
2594 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002595
Roland Levillain232ade02015-04-20 15:14:36 +01002596 // Load the value to the FP stack, using temporaries if needed.
2597 PushOntoFPStack(in, 0, adjustment, false, true);
2598
2599 if (out.IsStackSlot()) {
2600 __ fstps(Address(ESP, out.GetStackIndex() + adjustment));
2601 } else {
2602 __ fstps(Address(ESP, 0));
2603 Location stack_temp = Location::StackSlot(0);
2604 codegen_->Move32(out, stack_temp);
2605 }
2606
2607 // Remove the temporary stack space we allocated.
2608 if (adjustment != 0) {
2609 __ addl(ESP, Immediate(adjustment));
2610 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002611 break;
2612 }
2613
Roland Levillaincff13742014-11-17 14:32:17 +00002614 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002615 // Processing a Dex `double-to-float' instruction.
2616 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002617 break;
2618
2619 default:
2620 LOG(FATAL) << "Unexpected type conversion from " << input_type
2621 << " to " << result_type;
2622 };
2623 break;
2624
Roland Levillaindff1f282014-11-05 14:15:05 +00002625 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002626 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002627 case Primitive::kPrimBoolean:
2628 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002629 case Primitive::kPrimByte:
2630 case Primitive::kPrimShort:
2631 case Primitive::kPrimInt:
2632 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002633 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002634 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002635 break;
2636
Roland Levillain647b9ed2014-11-27 12:06:00 +00002637 case Primitive::kPrimLong: {
2638 // Processing a Dex `long-to-double' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002639 size_t adjustment = 0;
Roland Levillain647b9ed2014-11-27 12:06:00 +00002640
Roland Levillain232ade02015-04-20 15:14:36 +01002641 // Create stack space for the call to
2642 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstpl below.
2643 // TODO: enhance register allocator to ask for stack temporaries.
2644 if (!in.IsDoubleStackSlot() || !out.IsDoubleStackSlot()) {
2645 adjustment = Primitive::ComponentSize(Primitive::kPrimLong);
2646 __ subl(ESP, Immediate(adjustment));
2647 }
2648
2649 // Load the value to the FP stack, using temporaries if needed.
2650 PushOntoFPStack(in, 0, adjustment, false, true);
2651
2652 if (out.IsDoubleStackSlot()) {
2653 __ fstpl(Address(ESP, out.GetStackIndex() + adjustment));
2654 } else {
2655 __ fstpl(Address(ESP, 0));
2656 Location stack_temp = Location::DoubleStackSlot(0);
2657 codegen_->Move64(out, stack_temp);
2658 }
2659
2660 // Remove the temporary stack space we allocated.
2661 if (adjustment != 0) {
2662 __ addl(ESP, Immediate(adjustment));
2663 }
Roland Levillain647b9ed2014-11-27 12:06:00 +00002664 break;
2665 }
2666
Roland Levillaincff13742014-11-17 14:32:17 +00002667 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002668 // Processing a Dex `float-to-double' instruction.
2669 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002670 break;
2671
2672 default:
2673 LOG(FATAL) << "Unexpected type conversion from " << input_type
2674 << " to " << result_type;
2675 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002676 break;
2677
2678 default:
2679 LOG(FATAL) << "Unexpected type conversion from " << input_type
2680 << " to " << result_type;
2681 }
2682}
2683
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002684void LocationsBuilderX86::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002685 LocationSummary* locations =
2686 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002687 switch (add->GetResultType()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002688 case Primitive::kPrimInt: {
2689 locations->SetInAt(0, Location::RequiresRegister());
2690 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
2691 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2692 break;
2693 }
2694
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002695 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002696 locations->SetInAt(0, Location::RequiresRegister());
2697 locations->SetInAt(1, Location::Any());
2698 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002699 break;
2700 }
2701
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002702 case Primitive::kPrimFloat:
2703 case Primitive::kPrimDouble: {
2704 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002705 if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
2706 DCHECK(add->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00002707 } else if (add->InputAt(1)->IsConstant()) {
2708 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002709 } else {
2710 locations->SetInAt(1, Location::Any());
2711 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002712 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002713 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002714 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002715
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002716 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002717 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
2718 break;
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002719 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002720}
2721
2722void InstructionCodeGeneratorX86::VisitAdd(HAdd* add) {
2723 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002724 Location first = locations->InAt(0);
2725 Location second = locations->InAt(1);
Mark Mendell09b84632015-02-13 17:48:38 -05002726 Location out = locations->Out();
2727
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002728 switch (add->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002729 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002730 if (second.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002731 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2732 __ addl(out.AsRegister<Register>(), second.AsRegister<Register>());
Mark Mendell33bf2452015-05-27 10:08:24 -04002733 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
2734 __ addl(out.AsRegister<Register>(), first.AsRegister<Register>());
Mark Mendell09b84632015-02-13 17:48:38 -05002735 } else {
2736 __ leal(out.AsRegister<Register>(), Address(
2737 first.AsRegister<Register>(), second.AsRegister<Register>(), TIMES_1, 0));
2738 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002739 } else if (second.IsConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002740 int32_t value = second.GetConstant()->AsIntConstant()->GetValue();
2741 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2742 __ addl(out.AsRegister<Register>(), Immediate(value));
2743 } else {
2744 __ leal(out.AsRegister<Register>(), Address(first.AsRegister<Register>(), value));
2745 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002746 } else {
Mark Mendell09b84632015-02-13 17:48:38 -05002747 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002748 __ addl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002749 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002750 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002751 }
2752
2753 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002754 if (second.IsRegisterPair()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002755 __ addl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
2756 __ adcl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002757 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002758 __ addl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
2759 __ adcl(first.AsRegisterPairHigh<Register>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002760 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002761 } else {
2762 DCHECK(second.IsConstant()) << second;
2763 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2764 __ addl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
2765 __ adcl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002766 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002767 break;
2768 }
2769
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002770 case Primitive::kPrimFloat: {
2771 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002772 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04002773 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
2774 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00002775 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04002776 __ addss(first.AsFpuRegister<XmmRegister>(),
2777 codegen_->LiteralFloatAddress(
2778 const_area->GetConstant()->AsFloatConstant()->GetValue(),
2779 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2780 } else {
2781 DCHECK(second.IsStackSlot());
2782 __ addss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002783 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002784 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002785 }
2786
2787 case Primitive::kPrimDouble: {
2788 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002789 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04002790 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
2791 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00002792 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04002793 __ addsd(first.AsFpuRegister<XmmRegister>(),
2794 codegen_->LiteralDoubleAddress(
2795 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
2796 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2797 } else {
2798 DCHECK(second.IsDoubleStackSlot());
2799 __ addsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002800 }
2801 break;
2802 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002803
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002804 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002805 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002806 }
2807}
2808
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002809void LocationsBuilderX86::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002810 LocationSummary* locations =
2811 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002812 switch (sub->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002813 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002814 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002815 locations->SetInAt(0, Location::RequiresRegister());
2816 locations->SetInAt(1, Location::Any());
2817 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002818 break;
2819 }
Calin Juravle11351682014-10-23 15:38:15 +01002820 case Primitive::kPrimFloat:
2821 case Primitive::kPrimDouble: {
2822 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002823 if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
2824 DCHECK(sub->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00002825 } else if (sub->InputAt(1)->IsConstant()) {
2826 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002827 } else {
2828 locations->SetInAt(1, Location::Any());
2829 }
Calin Juravle11351682014-10-23 15:38:15 +01002830 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002831 break;
Calin Juravle11351682014-10-23 15:38:15 +01002832 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002833
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002834 default:
Calin Juravle11351682014-10-23 15:38:15 +01002835 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002836 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002837}
2838
2839void InstructionCodeGeneratorX86::VisitSub(HSub* sub) {
2840 LocationSummary* locations = sub->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002841 Location first = locations->InAt(0);
2842 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01002843 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002844 switch (sub->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002845 case Primitive::kPrimInt: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002846 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002847 __ subl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002848 } else if (second.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002849 __ subl(first.AsRegister<Register>(),
2850 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002851 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002852 __ subl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002853 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002854 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002855 }
2856
2857 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002858 if (second.IsRegisterPair()) {
Calin Juravle11351682014-10-23 15:38:15 +01002859 __ subl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
2860 __ sbbl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002861 } else if (second.IsDoubleStackSlot()) {
Calin Juravle11351682014-10-23 15:38:15 +01002862 __ subl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002863 __ sbbl(first.AsRegisterPairHigh<Register>(),
2864 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002865 } else {
2866 DCHECK(second.IsConstant()) << second;
2867 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2868 __ subl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
2869 __ sbbl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002870 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002871 break;
2872 }
2873
Calin Juravle11351682014-10-23 15:38:15 +01002874 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04002875 if (second.IsFpuRegister()) {
2876 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2877 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
2878 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00002879 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04002880 __ subss(first.AsFpuRegister<XmmRegister>(),
2881 codegen_->LiteralFloatAddress(
2882 const_area->GetConstant()->AsFloatConstant()->GetValue(),
2883 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2884 } else {
2885 DCHECK(second.IsStackSlot());
2886 __ subss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
2887 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002888 break;
Calin Juravle11351682014-10-23 15:38:15 +01002889 }
2890
2891 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04002892 if (second.IsFpuRegister()) {
2893 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2894 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
2895 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00002896 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04002897 __ subsd(first.AsFpuRegister<XmmRegister>(),
2898 codegen_->LiteralDoubleAddress(
2899 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
2900 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2901 } else {
2902 DCHECK(second.IsDoubleStackSlot());
2903 __ subsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
2904 }
Calin Juravle11351682014-10-23 15:38:15 +01002905 break;
2906 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002907
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002908 default:
Calin Juravle11351682014-10-23 15:38:15 +01002909 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002910 }
2911}
2912
Calin Juravle34bacdf2014-10-07 20:23:36 +01002913void LocationsBuilderX86::VisitMul(HMul* mul) {
2914 LocationSummary* locations =
2915 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
2916 switch (mul->GetResultType()) {
2917 case Primitive::kPrimInt:
2918 locations->SetInAt(0, Location::RequiresRegister());
2919 locations->SetInAt(1, Location::Any());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002920 if (mul->InputAt(1)->IsIntConstant()) {
2921 // Can use 3 operand multiply.
2922 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2923 } else {
2924 locations->SetOut(Location::SameAsFirstInput());
2925 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002926 break;
2927 case Primitive::kPrimLong: {
2928 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002929 locations->SetInAt(1, Location::Any());
2930 locations->SetOut(Location::SameAsFirstInput());
2931 // Needed for imul on 32bits with 64bits output.
2932 locations->AddTemp(Location::RegisterLocation(EAX));
2933 locations->AddTemp(Location::RegisterLocation(EDX));
2934 break;
2935 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002936 case Primitive::kPrimFloat:
2937 case Primitive::kPrimDouble: {
2938 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002939 if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
2940 DCHECK(mul->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00002941 } else if (mul->InputAt(1)->IsConstant()) {
2942 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002943 } else {
2944 locations->SetInAt(1, Location::Any());
2945 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002946 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002947 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002948 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002949
2950 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002951 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002952 }
2953}
2954
2955void InstructionCodeGeneratorX86::VisitMul(HMul* mul) {
2956 LocationSummary* locations = mul->GetLocations();
2957 Location first = locations->InAt(0);
2958 Location second = locations->InAt(1);
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002959 Location out = locations->Out();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002960
2961 switch (mul->GetResultType()) {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002962 case Primitive::kPrimInt:
2963 // The constant may have ended up in a register, so test explicitly to avoid
2964 // problems where the output may not be the same as the first operand.
2965 if (mul->InputAt(1)->IsIntConstant()) {
2966 Immediate imm(mul->InputAt(1)->AsIntConstant()->GetValue());
2967 __ imull(out.AsRegister<Register>(), first.AsRegister<Register>(), imm);
2968 } else if (second.IsRegister()) {
2969 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002970 __ imull(first.AsRegister<Register>(), second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002971 } else {
2972 DCHECK(second.IsStackSlot());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002973 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002974 __ imull(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01002975 }
2976 break;
Calin Juravle34bacdf2014-10-07 20:23:36 +01002977
2978 case Primitive::kPrimLong: {
Calin Juravle34bacdf2014-10-07 20:23:36 +01002979 Register in1_hi = first.AsRegisterPairHigh<Register>();
2980 Register in1_lo = first.AsRegisterPairLow<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002981 Register eax = locations->GetTemp(0).AsRegister<Register>();
2982 Register edx = locations->GetTemp(1).AsRegister<Register>();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002983
2984 DCHECK_EQ(EAX, eax);
2985 DCHECK_EQ(EDX, edx);
2986
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002987 // input: in1 - 64 bits, in2 - 64 bits.
Calin Juravle34bacdf2014-10-07 20:23:36 +01002988 // output: in1
2989 // formula: in1.hi : in1.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
2990 // parts: in1.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
2991 // parts: in1.lo = (in1.lo * in2.lo)[31:0]
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002992 if (second.IsConstant()) {
2993 DCHECK(second.GetConstant()->IsLongConstant());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002994
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002995 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2996 int32_t low_value = Low32Bits(value);
2997 int32_t high_value = High32Bits(value);
2998 Immediate low(low_value);
2999 Immediate high(high_value);
3000
3001 __ movl(eax, high);
3002 // eax <- in1.lo * in2.hi
3003 __ imull(eax, in1_lo);
3004 // in1.hi <- in1.hi * in2.lo
3005 __ imull(in1_hi, low);
3006 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3007 __ addl(in1_hi, eax);
3008 // move in2_lo to eax to prepare for double precision
3009 __ movl(eax, low);
3010 // edx:eax <- in1.lo * in2.lo
3011 __ mull(in1_lo);
3012 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3013 __ addl(in1_hi, edx);
3014 // in1.lo <- (in1.lo * in2.lo)[31:0];
3015 __ movl(in1_lo, eax);
3016 } else if (second.IsRegisterPair()) {
3017 Register in2_hi = second.AsRegisterPairHigh<Register>();
3018 Register in2_lo = second.AsRegisterPairLow<Register>();
3019
3020 __ movl(eax, in2_hi);
3021 // eax <- in1.lo * in2.hi
3022 __ imull(eax, in1_lo);
3023 // in1.hi <- in1.hi * in2.lo
3024 __ imull(in1_hi, in2_lo);
3025 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3026 __ addl(in1_hi, eax);
3027 // move in1_lo to eax to prepare for double precision
3028 __ movl(eax, in1_lo);
3029 // edx:eax <- in1.lo * in2.lo
3030 __ mull(in2_lo);
3031 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3032 __ addl(in1_hi, edx);
3033 // in1.lo <- (in1.lo * in2.lo)[31:0];
3034 __ movl(in1_lo, eax);
3035 } else {
3036 DCHECK(second.IsDoubleStackSlot()) << second;
3037 Address in2_hi(ESP, second.GetHighStackIndex(kX86WordSize));
3038 Address in2_lo(ESP, second.GetStackIndex());
3039
3040 __ movl(eax, in2_hi);
3041 // eax <- in1.lo * in2.hi
3042 __ imull(eax, in1_lo);
3043 // in1.hi <- in1.hi * in2.lo
3044 __ imull(in1_hi, in2_lo);
3045 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3046 __ addl(in1_hi, eax);
3047 // move in1_lo to eax to prepare for double precision
3048 __ movl(eax, in1_lo);
3049 // edx:eax <- in1.lo * in2.lo
3050 __ mull(in2_lo);
3051 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3052 __ addl(in1_hi, edx);
3053 // in1.lo <- (in1.lo * in2.lo)[31:0];
3054 __ movl(in1_lo, eax);
3055 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003056
3057 break;
3058 }
3059
Calin Juravleb5bfa962014-10-21 18:02:24 +01003060 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003061 DCHECK(first.Equals(locations->Out()));
3062 if (second.IsFpuRegister()) {
3063 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3064 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3065 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003066 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003067 __ mulss(first.AsFpuRegister<XmmRegister>(),
3068 codegen_->LiteralFloatAddress(
3069 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3070 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3071 } else {
3072 DCHECK(second.IsStackSlot());
3073 __ mulss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3074 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003075 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003076 }
3077
3078 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003079 DCHECK(first.Equals(locations->Out()));
3080 if (second.IsFpuRegister()) {
3081 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3082 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3083 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003084 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003085 __ mulsd(first.AsFpuRegister<XmmRegister>(),
3086 codegen_->LiteralDoubleAddress(
3087 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3088 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3089 } else {
3090 DCHECK(second.IsDoubleStackSlot());
3091 __ mulsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3092 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003093 break;
3094 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003095
3096 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003097 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003098 }
3099}
3100
Roland Levillain232ade02015-04-20 15:14:36 +01003101void InstructionCodeGeneratorX86::PushOntoFPStack(Location source,
3102 uint32_t temp_offset,
3103 uint32_t stack_adjustment,
3104 bool is_fp,
3105 bool is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003106 if (source.IsStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01003107 DCHECK(!is_wide);
3108 if (is_fp) {
3109 __ flds(Address(ESP, source.GetStackIndex() + stack_adjustment));
3110 } else {
3111 __ filds(Address(ESP, source.GetStackIndex() + stack_adjustment));
3112 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003113 } else if (source.IsDoubleStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01003114 DCHECK(is_wide);
3115 if (is_fp) {
3116 __ fldl(Address(ESP, source.GetStackIndex() + stack_adjustment));
3117 } else {
3118 __ fildl(Address(ESP, source.GetStackIndex() + stack_adjustment));
3119 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003120 } else {
3121 // Write the value to the temporary location on the stack and load to FP stack.
Roland Levillain232ade02015-04-20 15:14:36 +01003122 if (!is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003123 Location stack_temp = Location::StackSlot(temp_offset);
3124 codegen_->Move32(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01003125 if (is_fp) {
3126 __ flds(Address(ESP, temp_offset));
3127 } else {
3128 __ filds(Address(ESP, temp_offset));
3129 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003130 } else {
3131 Location stack_temp = Location::DoubleStackSlot(temp_offset);
3132 codegen_->Move64(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01003133 if (is_fp) {
3134 __ fldl(Address(ESP, temp_offset));
3135 } else {
3136 __ fildl(Address(ESP, temp_offset));
3137 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003138 }
3139 }
3140}
3141
3142void InstructionCodeGeneratorX86::GenerateRemFP(HRem *rem) {
3143 Primitive::Type type = rem->GetResultType();
3144 bool is_float = type == Primitive::kPrimFloat;
3145 size_t elem_size = Primitive::ComponentSize(type);
3146 LocationSummary* locations = rem->GetLocations();
3147 Location first = locations->InAt(0);
3148 Location second = locations->InAt(1);
3149 Location out = locations->Out();
3150
3151 // Create stack space for 2 elements.
3152 // TODO: enhance register allocator to ask for stack temporaries.
3153 __ subl(ESP, Immediate(2 * elem_size));
3154
3155 // Load the values to the FP stack in reverse order, using temporaries if needed.
Roland Levillain232ade02015-04-20 15:14:36 +01003156 const bool is_wide = !is_float;
3157 PushOntoFPStack(second, elem_size, 2 * elem_size, /* is_fp */ true, is_wide);
3158 PushOntoFPStack(first, 0, 2 * elem_size, /* is_fp */ true, is_wide);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003159
3160 // Loop doing FPREM until we stabilize.
Mark Mendell0c9497d2015-08-21 09:30:05 -04003161 NearLabel retry;
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003162 __ Bind(&retry);
3163 __ fprem();
3164
3165 // Move FP status to AX.
3166 __ fstsw();
3167
3168 // And see if the argument reduction is complete. This is signaled by the
3169 // C2 FPU flag bit set to 0.
3170 __ andl(EAX, Immediate(kC2ConditionMask));
3171 __ j(kNotEqual, &retry);
3172
3173 // We have settled on the final value. Retrieve it into an XMM register.
3174 // Store FP top of stack to real stack.
3175 if (is_float) {
3176 __ fsts(Address(ESP, 0));
3177 } else {
3178 __ fstl(Address(ESP, 0));
3179 }
3180
3181 // Pop the 2 items from the FP stack.
3182 __ fucompp();
3183
3184 // Load the value from the stack into an XMM register.
3185 DCHECK(out.IsFpuRegister()) << out;
3186 if (is_float) {
3187 __ movss(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
3188 } else {
3189 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
3190 }
3191
3192 // And remove the temporary stack space we allocated.
3193 __ addl(ESP, Immediate(2 * elem_size));
3194}
3195
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003196
3197void InstructionCodeGeneratorX86::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3198 DCHECK(instruction->IsDiv() || instruction->IsRem());
3199
3200 LocationSummary* locations = instruction->GetLocations();
3201 DCHECK(locations->InAt(1).IsConstant());
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003202 DCHECK(locations->InAt(1).GetConstant()->IsIntConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003203
3204 Register out_register = locations->Out().AsRegister<Register>();
3205 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003206 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003207
3208 DCHECK(imm == 1 || imm == -1);
3209
3210 if (instruction->IsRem()) {
3211 __ xorl(out_register, out_register);
3212 } else {
3213 __ movl(out_register, input_register);
3214 if (imm == -1) {
3215 __ negl(out_register);
3216 }
3217 }
3218}
3219
3220
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003221void InstructionCodeGeneratorX86::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003222 LocationSummary* locations = instruction->GetLocations();
3223
3224 Register out_register = locations->Out().AsRegister<Register>();
3225 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003226 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003227 DCHECK(IsPowerOfTwo(AbsOrMin(imm)));
3228 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003229
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003230 Register num = locations->GetTemp(0).AsRegister<Register>();
3231
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003232 __ leal(num, Address(input_register, abs_imm - 1));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003233 __ testl(input_register, input_register);
3234 __ cmovl(kGreaterEqual, num, input_register);
3235 int shift = CTZ(imm);
3236 __ sarl(num, Immediate(shift));
3237
3238 if (imm < 0) {
3239 __ negl(num);
3240 }
3241
3242 __ movl(out_register, num);
3243}
3244
3245void InstructionCodeGeneratorX86::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3246 DCHECK(instruction->IsDiv() || instruction->IsRem());
3247
3248 LocationSummary* locations = instruction->GetLocations();
3249 int imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
3250
3251 Register eax = locations->InAt(0).AsRegister<Register>();
3252 Register out = locations->Out().AsRegister<Register>();
3253 Register num;
3254 Register edx;
3255
3256 if (instruction->IsDiv()) {
3257 edx = locations->GetTemp(0).AsRegister<Register>();
3258 num = locations->GetTemp(1).AsRegister<Register>();
3259 } else {
3260 edx = locations->Out().AsRegister<Register>();
3261 num = locations->GetTemp(0).AsRegister<Register>();
3262 }
3263
3264 DCHECK_EQ(EAX, eax);
3265 DCHECK_EQ(EDX, edx);
3266 if (instruction->IsDiv()) {
3267 DCHECK_EQ(EAX, out);
3268 } else {
3269 DCHECK_EQ(EDX, out);
3270 }
3271
3272 int64_t magic;
3273 int shift;
3274 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
3275
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003276 // Save the numerator.
3277 __ movl(num, eax);
3278
3279 // EAX = magic
3280 __ movl(eax, Immediate(magic));
3281
3282 // EDX:EAX = magic * numerator
3283 __ imull(num);
3284
3285 if (imm > 0 && magic < 0) {
3286 // EDX += num
3287 __ addl(edx, num);
3288 } else if (imm < 0 && magic > 0) {
3289 __ subl(edx, num);
3290 }
3291
3292 // Shift if needed.
3293 if (shift != 0) {
3294 __ sarl(edx, Immediate(shift));
3295 }
3296
3297 // EDX += 1 if EDX < 0
3298 __ movl(eax, edx);
3299 __ shrl(edx, Immediate(31));
3300 __ addl(edx, eax);
3301
3302 if (instruction->IsRem()) {
3303 __ movl(eax, num);
3304 __ imull(edx, Immediate(imm));
3305 __ subl(eax, edx);
3306 __ movl(edx, eax);
3307 } else {
3308 __ movl(eax, edx);
3309 }
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003310}
3311
Calin Juravlebacfec32014-11-14 15:54:36 +00003312void InstructionCodeGeneratorX86::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3313 DCHECK(instruction->IsDiv() || instruction->IsRem());
3314
3315 LocationSummary* locations = instruction->GetLocations();
3316 Location out = locations->Out();
3317 Location first = locations->InAt(0);
3318 Location second = locations->InAt(1);
3319 bool is_div = instruction->IsDiv();
3320
3321 switch (instruction->GetResultType()) {
3322 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003323 DCHECK_EQ(EAX, first.AsRegister<Register>());
3324 DCHECK_EQ(is_div ? EAX : EDX, out.AsRegister<Register>());
Calin Juravlebacfec32014-11-14 15:54:36 +00003325
Vladimir Marko13c86fd2015-11-11 12:37:46 +00003326 if (second.IsConstant()) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003327 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003328
3329 if (imm == 0) {
3330 // Do not generate anything for 0. DivZeroCheck would forbid any generated code.
3331 } else if (imm == 1 || imm == -1) {
3332 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003333 } else if (is_div && IsPowerOfTwo(AbsOrMin(imm))) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003334 DivByPowerOfTwo(instruction->AsDiv());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003335 } else {
3336 DCHECK(imm <= -2 || imm >= 2);
3337 GenerateDivRemWithAnyConstant(instruction);
3338 }
3339 } else {
David Srbecky9cd6d372016-02-09 15:24:47 +00003340 SlowPathCode* slow_path = new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86(
3341 instruction, out.AsRegister<Register>(), is_div);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003342 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00003343
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003344 Register second_reg = second.AsRegister<Register>();
3345 // 0x80000000/-1 triggers an arithmetic exception!
3346 // Dividing by -1 is actually negation and -0x800000000 = 0x80000000 so
3347 // it's safe to just use negl instead of more complex comparisons.
Calin Juravlebacfec32014-11-14 15:54:36 +00003348
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003349 __ cmpl(second_reg, Immediate(-1));
3350 __ j(kEqual, slow_path->GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +00003351
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003352 // edx:eax <- sign-extended of eax
3353 __ cdq();
3354 // eax = quotient, edx = remainder
3355 __ idivl(second_reg);
3356 __ Bind(slow_path->GetExitLabel());
3357 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003358 break;
3359 }
3360
3361 case Primitive::kPrimLong: {
3362 InvokeRuntimeCallingConvention calling_convention;
3363 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
3364 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
3365 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
3366 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
3367 DCHECK_EQ(EAX, out.AsRegisterPairLow<Register>());
3368 DCHECK_EQ(EDX, out.AsRegisterPairHigh<Register>());
3369
3370 if (is_div) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01003371 codegen_->InvokeRuntime(kQuickLdiv, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003372 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003373 } else {
Serban Constantinescuba45db02016-07-12 22:53:02 +01003374 codegen_->InvokeRuntime(kQuickLmod, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003375 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003376 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003377 break;
3378 }
3379
3380 default:
3381 LOG(FATAL) << "Unexpected type for GenerateDivRemIntegral " << instruction->GetResultType();
3382 }
3383}
3384
Calin Juravle7c4954d2014-10-28 16:57:40 +00003385void LocationsBuilderX86::VisitDiv(HDiv* div) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003386 LocationSummary::CallKind call_kind = (div->GetResultType() == Primitive::kPrimLong)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003387 ? LocationSummary::kCallOnMainOnly
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003388 : LocationSummary::kNoCall;
3389 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
3390
Calin Juravle7c4954d2014-10-28 16:57:40 +00003391 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00003392 case Primitive::kPrimInt: {
3393 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003394 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00003395 locations->SetOut(Location::SameAsFirstInput());
3396 // Intel uses edx:eax as the dividend.
3397 locations->AddTemp(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003398 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3399 // which enforces results to be in EAX and EDX, things are simpler if we use EAX also as
3400 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003401 if (div->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003402 locations->AddTemp(Location::RequiresRegister());
3403 }
Calin Juravled0d48522014-11-04 16:40:20 +00003404 break;
3405 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003406 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003407 InvokeRuntimeCallingConvention calling_convention;
3408 locations->SetInAt(0, Location::RegisterPairLocation(
3409 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3410 locations->SetInAt(1, Location::RegisterPairLocation(
3411 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3412 // Runtime helper puts the result in EAX, EDX.
3413 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Calin Juravle7c4954d2014-10-28 16:57:40 +00003414 break;
3415 }
3416 case Primitive::kPrimFloat:
3417 case Primitive::kPrimDouble: {
3418 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003419 if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3420 DCHECK(div->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003421 } else if (div->InputAt(1)->IsConstant()) {
3422 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003423 } else {
3424 locations->SetInAt(1, Location::Any());
3425 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003426 locations->SetOut(Location::SameAsFirstInput());
3427 break;
3428 }
3429
3430 default:
3431 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3432 }
3433}
3434
3435void InstructionCodeGeneratorX86::VisitDiv(HDiv* div) {
3436 LocationSummary* locations = div->GetLocations();
3437 Location first = locations->InAt(0);
3438 Location second = locations->InAt(1);
Calin Juravle7c4954d2014-10-28 16:57:40 +00003439
3440 switch (div->GetResultType()) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003441 case Primitive::kPrimInt:
Calin Juravle7c4954d2014-10-28 16:57:40 +00003442 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003443 GenerateDivRemIntegral(div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00003444 break;
3445 }
3446
3447 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003448 if (second.IsFpuRegister()) {
3449 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3450 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3451 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003452 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003453 __ divss(first.AsFpuRegister<XmmRegister>(),
3454 codegen_->LiteralFloatAddress(
3455 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3456 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3457 } else {
3458 DCHECK(second.IsStackSlot());
3459 __ divss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3460 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003461 break;
3462 }
3463
3464 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003465 if (second.IsFpuRegister()) {
3466 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3467 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3468 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003469 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003470 __ divsd(first.AsFpuRegister<XmmRegister>(),
3471 codegen_->LiteralDoubleAddress(
3472 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3473 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3474 } else {
3475 DCHECK(second.IsDoubleStackSlot());
3476 __ divsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3477 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003478 break;
3479 }
3480
3481 default:
3482 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3483 }
3484}
3485
Calin Juravlebacfec32014-11-14 15:54:36 +00003486void LocationsBuilderX86::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00003487 Primitive::Type type = rem->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003488
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003489 LocationSummary::CallKind call_kind = (rem->GetResultType() == Primitive::kPrimLong)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003490 ? LocationSummary::kCallOnMainOnly
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003491 : LocationSummary::kNoCall;
3492 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
Calin Juravlebacfec32014-11-14 15:54:36 +00003493
Calin Juravled2ec87d2014-12-08 14:24:46 +00003494 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003495 case Primitive::kPrimInt: {
3496 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003497 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00003498 locations->SetOut(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003499 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3500 // which enforces results to be in EAX and EDX, things are simpler if we use EDX also as
3501 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003502 if (rem->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003503 locations->AddTemp(Location::RequiresRegister());
3504 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003505 break;
3506 }
3507 case Primitive::kPrimLong: {
3508 InvokeRuntimeCallingConvention calling_convention;
3509 locations->SetInAt(0, Location::RegisterPairLocation(
3510 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3511 locations->SetInAt(1, Location::RegisterPairLocation(
3512 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3513 // Runtime helper puts the result in EAX, EDX.
3514 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
3515 break;
3516 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003517 case Primitive::kPrimDouble:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003518 case Primitive::kPrimFloat: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003519 locations->SetInAt(0, Location::Any());
3520 locations->SetInAt(1, Location::Any());
3521 locations->SetOut(Location::RequiresFpuRegister());
3522 locations->AddTemp(Location::RegisterLocation(EAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00003523 break;
3524 }
3525
3526 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003527 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003528 }
3529}
3530
3531void InstructionCodeGeneratorX86::VisitRem(HRem* rem) {
3532 Primitive::Type type = rem->GetResultType();
3533 switch (type) {
3534 case Primitive::kPrimInt:
3535 case Primitive::kPrimLong: {
3536 GenerateDivRemIntegral(rem);
3537 break;
3538 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003539 case Primitive::kPrimFloat:
Calin Juravlebacfec32014-11-14 15:54:36 +00003540 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003541 GenerateRemFP(rem);
Calin Juravlebacfec32014-11-14 15:54:36 +00003542 break;
3543 }
3544 default:
3545 LOG(FATAL) << "Unexpected rem type " << type;
3546 }
3547}
3548
Calin Juravled0d48522014-11-04 16:40:20 +00003549void LocationsBuilderX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003550 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003551 switch (instruction->GetType()) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003552 case Primitive::kPrimBoolean:
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003553 case Primitive::kPrimByte:
3554 case Primitive::kPrimChar:
3555 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003556 case Primitive::kPrimInt: {
3557 locations->SetInAt(0, Location::Any());
3558 break;
3559 }
3560 case Primitive::kPrimLong: {
3561 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
3562 if (!instruction->IsConstant()) {
3563 locations->AddTemp(Location::RequiresRegister());
3564 }
3565 break;
3566 }
3567 default:
3568 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
3569 }
Calin Juravled0d48522014-11-04 16:40:20 +00003570}
3571
3572void InstructionCodeGeneratorX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003573 SlowPathCode* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86(instruction);
Calin Juravled0d48522014-11-04 16:40:20 +00003574 codegen_->AddSlowPath(slow_path);
3575
3576 LocationSummary* locations = instruction->GetLocations();
3577 Location value = locations->InAt(0);
3578
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003579 switch (instruction->GetType()) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003580 case Primitive::kPrimBoolean:
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003581 case Primitive::kPrimByte:
3582 case Primitive::kPrimChar:
3583 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003584 case Primitive::kPrimInt: {
3585 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003586 __ testl(value.AsRegister<Register>(), value.AsRegister<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003587 __ j(kEqual, slow_path->GetEntryLabel());
3588 } else if (value.IsStackSlot()) {
3589 __ cmpl(Address(ESP, value.GetStackIndex()), Immediate(0));
3590 __ j(kEqual, slow_path->GetEntryLabel());
3591 } else {
3592 DCHECK(value.IsConstant()) << value;
3593 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01003594 __ jmp(slow_path->GetEntryLabel());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003595 }
3596 }
3597 break;
Calin Juravled0d48522014-11-04 16:40:20 +00003598 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003599 case Primitive::kPrimLong: {
3600 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003601 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003602 __ movl(temp, value.AsRegisterPairLow<Register>());
3603 __ orl(temp, value.AsRegisterPairHigh<Register>());
3604 __ j(kEqual, slow_path->GetEntryLabel());
3605 } else {
3606 DCHECK(value.IsConstant()) << value;
3607 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
3608 __ jmp(slow_path->GetEntryLabel());
3609 }
3610 }
3611 break;
3612 }
3613 default:
3614 LOG(FATAL) << "Unexpected type for HDivZeroCheck" << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00003615 }
Calin Juravled0d48522014-11-04 16:40:20 +00003616}
3617
Calin Juravle9aec02f2014-11-18 23:06:35 +00003618void LocationsBuilderX86::HandleShift(HBinaryOperation* op) {
3619 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3620
3621 LocationSummary* locations =
3622 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
3623
3624 switch (op->GetResultType()) {
Mark P Mendell73945692015-04-29 14:56:17 +00003625 case Primitive::kPrimInt:
Calin Juravle9aec02f2014-11-18 23:06:35 +00003626 case Primitive::kPrimLong: {
Mark P Mendell73945692015-04-29 14:56:17 +00003627 // Can't have Location::Any() and output SameAsFirstInput()
Calin Juravle9aec02f2014-11-18 23:06:35 +00003628 locations->SetInAt(0, Location::RequiresRegister());
Mark P Mendell73945692015-04-29 14:56:17 +00003629 // The shift count needs to be in CL or a constant.
3630 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, op->InputAt(1)));
Calin Juravle9aec02f2014-11-18 23:06:35 +00003631 locations->SetOut(Location::SameAsFirstInput());
3632 break;
3633 }
3634 default:
3635 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
3636 }
3637}
3638
3639void InstructionCodeGeneratorX86::HandleShift(HBinaryOperation* op) {
3640 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3641
3642 LocationSummary* locations = op->GetLocations();
3643 Location first = locations->InAt(0);
3644 Location second = locations->InAt(1);
3645 DCHECK(first.Equals(locations->Out()));
3646
3647 switch (op->GetResultType()) {
3648 case Primitive::kPrimInt: {
Mark P Mendell73945692015-04-29 14:56:17 +00003649 DCHECK(first.IsRegister());
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003650 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003651 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003652 Register second_reg = second.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003653 DCHECK_EQ(ECX, second_reg);
3654 if (op->IsShl()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003655 __ shll(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003656 } else if (op->IsShr()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003657 __ sarl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003658 } else {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003659 __ shrl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003660 }
3661 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003662 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance;
Mark P Mendell73945692015-04-29 14:56:17 +00003663 if (shift == 0) {
3664 return;
3665 }
3666 Immediate imm(shift);
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003667 if (op->IsShl()) {
3668 __ shll(first_reg, imm);
3669 } else if (op->IsShr()) {
3670 __ sarl(first_reg, imm);
3671 } else {
3672 __ shrl(first_reg, imm);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003673 }
3674 }
3675 break;
3676 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003677 case Primitive::kPrimLong: {
Mark P Mendell73945692015-04-29 14:56:17 +00003678 if (second.IsRegister()) {
3679 Register second_reg = second.AsRegister<Register>();
3680 DCHECK_EQ(ECX, second_reg);
3681 if (op->IsShl()) {
3682 GenerateShlLong(first, second_reg);
3683 } else if (op->IsShr()) {
3684 GenerateShrLong(first, second_reg);
3685 } else {
3686 GenerateUShrLong(first, second_reg);
3687 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003688 } else {
Mark P Mendell73945692015-04-29 14:56:17 +00003689 // Shift by a constant.
Roland Levillain5b5b9312016-03-22 14:57:31 +00003690 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance;
Mark P Mendell73945692015-04-29 14:56:17 +00003691 // Nothing to do if the shift is 0, as the input is already the output.
3692 if (shift != 0) {
3693 if (op->IsShl()) {
3694 GenerateShlLong(first, shift);
3695 } else if (op->IsShr()) {
3696 GenerateShrLong(first, shift);
3697 } else {
3698 GenerateUShrLong(first, shift);
3699 }
3700 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003701 }
3702 break;
3703 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003704 default:
3705 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
3706 }
3707}
3708
Mark P Mendell73945692015-04-29 14:56:17 +00003709void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, int shift) {
3710 Register low = loc.AsRegisterPairLow<Register>();
3711 Register high = loc.AsRegisterPairHigh<Register>();
Mark Mendellba56d062015-05-05 21:34:03 -04003712 if (shift == 1) {
3713 // This is just an addition.
3714 __ addl(low, low);
3715 __ adcl(high, high);
3716 } else if (shift == 32) {
Mark P Mendell73945692015-04-29 14:56:17 +00003717 // Shift by 32 is easy. High gets low, and low gets 0.
3718 codegen_->EmitParallelMoves(
3719 loc.ToLow(),
3720 loc.ToHigh(),
3721 Primitive::kPrimInt,
3722 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
3723 loc.ToLow(),
3724 Primitive::kPrimInt);
3725 } else if (shift > 32) {
3726 // Low part becomes 0. High part is low part << (shift-32).
3727 __ movl(high, low);
3728 __ shll(high, Immediate(shift - 32));
3729 __ xorl(low, low);
3730 } else {
3731 // Between 1 and 31.
3732 __ shld(high, low, Immediate(shift));
3733 __ shll(low, Immediate(shift));
3734 }
3735}
3736
Calin Juravle9aec02f2014-11-18 23:06:35 +00003737void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04003738 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00003739 __ shld(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>(), shifter);
3740 __ shll(loc.AsRegisterPairLow<Register>(), shifter);
3741 __ testl(shifter, Immediate(32));
3742 __ j(kEqual, &done);
3743 __ movl(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>());
3744 __ movl(loc.AsRegisterPairLow<Register>(), Immediate(0));
3745 __ Bind(&done);
3746}
3747
Mark P Mendell73945692015-04-29 14:56:17 +00003748void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, int shift) {
3749 Register low = loc.AsRegisterPairLow<Register>();
3750 Register high = loc.AsRegisterPairHigh<Register>();
3751 if (shift == 32) {
3752 // Need to copy the sign.
3753 DCHECK_NE(low, high);
3754 __ movl(low, high);
3755 __ sarl(high, Immediate(31));
3756 } else if (shift > 32) {
3757 DCHECK_NE(low, high);
3758 // High part becomes sign. Low part is shifted by shift - 32.
3759 __ movl(low, high);
3760 __ sarl(high, Immediate(31));
3761 __ sarl(low, Immediate(shift - 32));
3762 } else {
3763 // Between 1 and 31.
3764 __ shrd(low, high, Immediate(shift));
3765 __ sarl(high, Immediate(shift));
3766 }
3767}
3768
Calin Juravle9aec02f2014-11-18 23:06:35 +00003769void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04003770 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00003771 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
3772 __ sarl(loc.AsRegisterPairHigh<Register>(), shifter);
3773 __ testl(shifter, Immediate(32));
3774 __ j(kEqual, &done);
3775 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
3776 __ sarl(loc.AsRegisterPairHigh<Register>(), Immediate(31));
3777 __ Bind(&done);
3778}
3779
Mark P Mendell73945692015-04-29 14:56:17 +00003780void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, int shift) {
3781 Register low = loc.AsRegisterPairLow<Register>();
3782 Register high = loc.AsRegisterPairHigh<Register>();
3783 if (shift == 32) {
3784 // Shift by 32 is easy. Low gets high, and high gets 0.
3785 codegen_->EmitParallelMoves(
3786 loc.ToHigh(),
3787 loc.ToLow(),
3788 Primitive::kPrimInt,
3789 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
3790 loc.ToHigh(),
3791 Primitive::kPrimInt);
3792 } else if (shift > 32) {
3793 // Low part is high >> (shift - 32). High part becomes 0.
3794 __ movl(low, high);
3795 __ shrl(low, Immediate(shift - 32));
3796 __ xorl(high, high);
3797 } else {
3798 // Between 1 and 31.
3799 __ shrd(low, high, Immediate(shift));
3800 __ shrl(high, Immediate(shift));
3801 }
3802}
3803
Calin Juravle9aec02f2014-11-18 23:06:35 +00003804void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04003805 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00003806 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
3807 __ shrl(loc.AsRegisterPairHigh<Register>(), shifter);
3808 __ testl(shifter, Immediate(32));
3809 __ j(kEqual, &done);
3810 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
3811 __ movl(loc.AsRegisterPairHigh<Register>(), Immediate(0));
3812 __ Bind(&done);
3813}
3814
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003815void LocationsBuilderX86::VisitRor(HRor* ror) {
3816 LocationSummary* locations =
3817 new (GetGraph()->GetArena()) LocationSummary(ror, LocationSummary::kNoCall);
3818
3819 switch (ror->GetResultType()) {
3820 case Primitive::kPrimLong:
3821 // Add the temporary needed.
3822 locations->AddTemp(Location::RequiresRegister());
3823 FALLTHROUGH_INTENDED;
3824 case Primitive::kPrimInt:
3825 locations->SetInAt(0, Location::RequiresRegister());
3826 // The shift count needs to be in CL (unless it is a constant).
3827 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, ror->InputAt(1)));
3828 locations->SetOut(Location::SameAsFirstInput());
3829 break;
3830 default:
3831 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
3832 UNREACHABLE();
3833 }
3834}
3835
3836void InstructionCodeGeneratorX86::VisitRor(HRor* ror) {
3837 LocationSummary* locations = ror->GetLocations();
3838 Location first = locations->InAt(0);
3839 Location second = locations->InAt(1);
3840
3841 if (ror->GetResultType() == Primitive::kPrimInt) {
3842 Register first_reg = first.AsRegister<Register>();
3843 if (second.IsRegister()) {
3844 Register second_reg = second.AsRegister<Register>();
3845 __ rorl(first_reg, second_reg);
3846 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003847 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003848 __ rorl(first_reg, imm);
3849 }
3850 return;
3851 }
3852
3853 DCHECK_EQ(ror->GetResultType(), Primitive::kPrimLong);
3854 Register first_reg_lo = first.AsRegisterPairLow<Register>();
3855 Register first_reg_hi = first.AsRegisterPairHigh<Register>();
3856 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
3857 if (second.IsRegister()) {
3858 Register second_reg = second.AsRegister<Register>();
3859 DCHECK_EQ(second_reg, ECX);
3860 __ movl(temp_reg, first_reg_hi);
3861 __ shrd(first_reg_hi, first_reg_lo, second_reg);
3862 __ shrd(first_reg_lo, temp_reg, second_reg);
3863 __ movl(temp_reg, first_reg_hi);
3864 __ testl(second_reg, Immediate(32));
3865 __ cmovl(kNotEqual, first_reg_hi, first_reg_lo);
3866 __ cmovl(kNotEqual, first_reg_lo, temp_reg);
3867 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003868 int32_t shift_amt = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance;
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003869 if (shift_amt == 0) {
3870 // Already fine.
3871 return;
3872 }
3873 if (shift_amt == 32) {
3874 // Just swap.
3875 __ movl(temp_reg, first_reg_lo);
3876 __ movl(first_reg_lo, first_reg_hi);
3877 __ movl(first_reg_hi, temp_reg);
3878 return;
3879 }
3880
3881 Immediate imm(shift_amt);
3882 // Save the constents of the low value.
3883 __ movl(temp_reg, first_reg_lo);
3884
3885 // Shift right into low, feeding bits from high.
3886 __ shrd(first_reg_lo, first_reg_hi, imm);
3887
3888 // Shift right into high, feeding bits from the original low.
3889 __ shrd(first_reg_hi, temp_reg, imm);
3890
3891 // Swap if needed.
3892 if (shift_amt > 32) {
3893 __ movl(temp_reg, first_reg_lo);
3894 __ movl(first_reg_lo, first_reg_hi);
3895 __ movl(first_reg_hi, temp_reg);
3896 }
3897 }
3898}
3899
Calin Juravle9aec02f2014-11-18 23:06:35 +00003900void LocationsBuilderX86::VisitShl(HShl* shl) {
3901 HandleShift(shl);
3902}
3903
3904void InstructionCodeGeneratorX86::VisitShl(HShl* shl) {
3905 HandleShift(shl);
3906}
3907
3908void LocationsBuilderX86::VisitShr(HShr* shr) {
3909 HandleShift(shr);
3910}
3911
3912void InstructionCodeGeneratorX86::VisitShr(HShr* shr) {
3913 HandleShift(shr);
3914}
3915
3916void LocationsBuilderX86::VisitUShr(HUShr* ushr) {
3917 HandleShift(ushr);
3918}
3919
3920void InstructionCodeGeneratorX86::VisitUShr(HUShr* ushr) {
3921 HandleShift(ushr);
3922}
3923
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003924void LocationsBuilderX86::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003925 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003926 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003927 locations->SetOut(Location::RegisterLocation(EAX));
David Brazdil6de19382016-01-08 17:37:10 +00003928 if (instruction->IsStringAlloc()) {
3929 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
3930 } else {
3931 InvokeRuntimeCallingConvention calling_convention;
3932 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3933 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3934 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003935}
3936
3937void InstructionCodeGeneratorX86::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01003938 // Note: if heap poisoning is enabled, the entry point takes cares
3939 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00003940 if (instruction->IsStringAlloc()) {
3941 // String is allocated through StringFactory. Call NewEmptyString entry point.
3942 Register temp = instruction->GetLocations()->GetTemp(0).AsRegister<Register>();
Andreas Gampe542451c2016-07-26 09:02:02 -07003943 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86PointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00003944 __ fs()->movl(temp, Address::Absolute(QUICK_ENTRY_POINT(pNewEmptyString)));
3945 __ call(Address(temp, code_offset.Int32Value()));
3946 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3947 } else {
Serban Constantinescuba45db02016-07-12 22:53:02 +01003948 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
David Brazdil6de19382016-01-08 17:37:10 +00003949 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
3950 DCHECK(!codegen_->IsLeafMethod());
3951 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003952}
3953
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003954void LocationsBuilderX86::VisitNewArray(HNewArray* instruction) {
3955 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003956 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003957 locations->SetOut(Location::RegisterLocation(EAX));
3958 InvokeRuntimeCallingConvention calling_convention;
3959 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003960 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003961 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003962}
3963
3964void InstructionCodeGeneratorX86::VisitNewArray(HNewArray* instruction) {
3965 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003966 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
Roland Levillain4d027112015-07-01 15:41:14 +01003967 // Note: if heap poisoning is enabled, the entry point takes cares
3968 // of poisoning the reference.
Serban Constantinescuba45db02016-07-12 22:53:02 +01003969 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003970 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003971 DCHECK(!codegen_->IsLeafMethod());
3972}
3973
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003974void LocationsBuilderX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003975 LocationSummary* locations =
3976 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01003977 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
3978 if (location.IsStackSlot()) {
3979 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3980 } else if (location.IsDoubleStackSlot()) {
3981 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003982 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01003983 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003984}
3985
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003986void InstructionCodeGeneratorX86::VisitParameterValue(
3987 HParameterValue* instruction ATTRIBUTE_UNUSED) {
3988}
3989
3990void LocationsBuilderX86::VisitCurrentMethod(HCurrentMethod* instruction) {
3991 LocationSummary* locations =
3992 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3993 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
3994}
3995
3996void InstructionCodeGeneratorX86::VisitCurrentMethod(HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003997}
3998
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003999void LocationsBuilderX86::VisitClassTableGet(HClassTableGet* instruction) {
4000 LocationSummary* locations =
4001 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4002 locations->SetInAt(0, Location::RequiresRegister());
4003 locations->SetOut(Location::RequiresRegister());
4004}
4005
4006void InstructionCodeGeneratorX86::VisitClassTableGet(HClassTableGet* instruction) {
4007 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004008 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004009 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004010 instruction->GetIndex(), kX86PointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004011 __ movl(locations->Out().AsRegister<Register>(),
4012 Address(locations->InAt(0).AsRegister<Register>(), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004013 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004014 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00004015 instruction->GetIndex(), kX86PointerSize));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004016 __ movl(locations->Out().AsRegister<Register>(),
4017 Address(locations->InAt(0).AsRegister<Register>(),
4018 mirror::Class::ImtPtrOffset(kX86PointerSize).Uint32Value()));
4019 // temp = temp->GetImtEntryAt(method_offset);
4020 __ movl(locations->Out().AsRegister<Register>(),
4021 Address(locations->Out().AsRegister<Register>(), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004022 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004023}
4024
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004025void LocationsBuilderX86::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004026 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004027 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01004028 locations->SetInAt(0, Location::RequiresRegister());
4029 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004030}
4031
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004032void InstructionCodeGeneratorX86::VisitNot(HNot* not_) {
4033 LocationSummary* locations = not_->GetLocations();
Roland Levillain70566432014-10-24 16:20:17 +01004034 Location in = locations->InAt(0);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01004035 Location out = locations->Out();
Roland Levillain70566432014-10-24 16:20:17 +01004036 DCHECK(in.Equals(out));
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00004037 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004038 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00004039 __ notl(out.AsRegister<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004040 break;
4041
4042 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01004043 __ notl(out.AsRegisterPairLow<Register>());
4044 __ notl(out.AsRegisterPairHigh<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004045 break;
4046
4047 default:
4048 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
4049 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004050}
4051
David Brazdil66d126e2015-04-03 16:02:44 +01004052void LocationsBuilderX86::VisitBooleanNot(HBooleanNot* bool_not) {
4053 LocationSummary* locations =
4054 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
4055 locations->SetInAt(0, Location::RequiresRegister());
4056 locations->SetOut(Location::SameAsFirstInput());
4057}
4058
4059void InstructionCodeGeneratorX86::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01004060 LocationSummary* locations = bool_not->GetLocations();
4061 Location in = locations->InAt(0);
4062 Location out = locations->Out();
4063 DCHECK(in.Equals(out));
4064 __ xorl(out.AsRegister<Register>(), Immediate(1));
4065}
4066
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004067void LocationsBuilderX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004068 LocationSummary* locations =
4069 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00004070 switch (compare->InputAt(0)->GetType()) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00004071 case Primitive::kPrimBoolean:
4072 case Primitive::kPrimByte:
4073 case Primitive::kPrimShort:
4074 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08004075 case Primitive::kPrimInt:
Calin Juravleddb7df22014-11-25 20:56:51 +00004076 case Primitive::kPrimLong: {
4077 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravleddb7df22014-11-25 20:56:51 +00004078 locations->SetInAt(1, Location::Any());
4079 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4080 break;
4081 }
4082 case Primitive::kPrimFloat:
4083 case Primitive::kPrimDouble: {
4084 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004085 if (compare->InputAt(1)->IsX86LoadFromConstantTable()) {
4086 DCHECK(compare->InputAt(1)->IsEmittedAtUseSite());
4087 } else if (compare->InputAt(1)->IsConstant()) {
4088 locations->SetInAt(1, Location::RequiresFpuRegister());
4089 } else {
4090 locations->SetInAt(1, Location::Any());
4091 }
Calin Juravleddb7df22014-11-25 20:56:51 +00004092 locations->SetOut(Location::RequiresRegister());
4093 break;
4094 }
4095 default:
4096 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
4097 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004098}
4099
4100void InstructionCodeGeneratorX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004101 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004102 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00004103 Location left = locations->InAt(0);
4104 Location right = locations->InAt(1);
4105
Mark Mendell0c9497d2015-08-21 09:30:05 -04004106 NearLabel less, greater, done;
Aart Bika19616e2016-02-01 18:57:58 -08004107 Condition less_cond = kLess;
4108
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004109 switch (compare->InputAt(0)->GetType()) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00004110 case Primitive::kPrimBoolean:
4111 case Primitive::kPrimByte:
4112 case Primitive::kPrimShort:
4113 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08004114 case Primitive::kPrimInt: {
Roland Levillain0b671c02016-08-19 12:02:34 +01004115 codegen_->GenerateIntCompare(left, right);
Aart Bika19616e2016-02-01 18:57:58 -08004116 break;
4117 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004118 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004119 Register left_low = left.AsRegisterPairLow<Register>();
4120 Register left_high = left.AsRegisterPairHigh<Register>();
4121 int32_t val_low = 0;
4122 int32_t val_high = 0;
4123 bool right_is_const = false;
4124
4125 if (right.IsConstant()) {
4126 DCHECK(right.GetConstant()->IsLongConstant());
4127 right_is_const = true;
4128 int64_t val = right.GetConstant()->AsLongConstant()->GetValue();
4129 val_low = Low32Bits(val);
4130 val_high = High32Bits(val);
4131 }
4132
Calin Juravleddb7df22014-11-25 20:56:51 +00004133 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004134 __ cmpl(left_high, right.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004135 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004136 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004137 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004138 DCHECK(right_is_const) << right;
Aart Bika19616e2016-02-01 18:57:58 -08004139 codegen_->Compare32BitValue(left_high, val_high);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004140 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004141 __ j(kLess, &less); // Signed compare.
4142 __ j(kGreater, &greater); // Signed compare.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004143 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004144 __ cmpl(left_low, right.AsRegisterPairLow<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004145 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004146 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004147 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004148 DCHECK(right_is_const) << right;
Aart Bika19616e2016-02-01 18:57:58 -08004149 codegen_->Compare32BitValue(left_low, val_low);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004150 }
Aart Bika19616e2016-02-01 18:57:58 -08004151 less_cond = kBelow; // for CF (unsigned).
Calin Juravleddb7df22014-11-25 20:56:51 +00004152 break;
4153 }
4154 case Primitive::kPrimFloat: {
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004155 GenerateFPCompare(left, right, compare, false);
Calin Juravleddb7df22014-11-25 20:56:51 +00004156 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08004157 less_cond = kBelow; // for CF (floats).
Calin Juravleddb7df22014-11-25 20:56:51 +00004158 break;
4159 }
4160 case Primitive::kPrimDouble: {
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004161 GenerateFPCompare(left, right, compare, true);
Calin Juravleddb7df22014-11-25 20:56:51 +00004162 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08004163 less_cond = kBelow; // for CF (floats).
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004164 break;
4165 }
4166 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00004167 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004168 }
Aart Bika19616e2016-02-01 18:57:58 -08004169
Calin Juravleddb7df22014-11-25 20:56:51 +00004170 __ movl(out, Immediate(0));
4171 __ j(kEqual, &done);
Aart Bika19616e2016-02-01 18:57:58 -08004172 __ j(less_cond, &less);
Calin Juravleddb7df22014-11-25 20:56:51 +00004173
4174 __ Bind(&greater);
4175 __ movl(out, Immediate(1));
4176 __ jmp(&done);
4177
4178 __ Bind(&less);
4179 __ movl(out, Immediate(-1));
4180
4181 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004182}
4183
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004184void LocationsBuilderX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004185 LocationSummary* locations =
4186 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko372f10e2016-05-17 16:30:10 +01004187 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01004188 locations->SetInAt(i, Location::Any());
4189 }
4190 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004191}
4192
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004193void InstructionCodeGeneratorX86::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004194 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004195}
4196
Roland Levillain7c1559a2015-12-15 10:55:36 +00004197void CodeGeneratorX86::GenerateMemoryBarrier(MemBarrierKind kind) {
Calin Juravle52c48962014-12-16 17:02:57 +00004198 /*
4199 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
4200 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
4201 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
4202 */
4203 switch (kind) {
4204 case MemBarrierKind::kAnyAny: {
Mark P Mendell17077d82015-12-16 19:15:59 +00004205 MemoryFence();
Calin Juravle52c48962014-12-16 17:02:57 +00004206 break;
4207 }
4208 case MemBarrierKind::kAnyStore:
4209 case MemBarrierKind::kLoadAny:
4210 case MemBarrierKind::kStoreStore: {
4211 // nop
4212 break;
4213 }
Mark Mendell7aa04a12016-01-27 22:39:07 -05004214 case MemBarrierKind::kNTStoreStore:
4215 // Non-Temporal Store/Store needs an explicit fence.
4216 MemoryFence(/* non-temporal */ true);
4217 break;
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004218 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004219}
4220
Vladimir Markodc151b22015-10-15 18:02:30 +01004221HInvokeStaticOrDirect::DispatchInfo CodeGeneratorX86::GetSupportedInvokeStaticOrDirectDispatch(
4222 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004223 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00004224 HInvokeStaticOrDirect::DispatchInfo dispatch_info = desired_dispatch_info;
4225
4226 // We disable pc-relative load when there is an irreducible loop, as the optimization
4227 // is incompatible with it.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004228 // TODO: Create as many X86ComputeBaseMethodAddress instructions
4229 // as needed for methods with irreducible loops.
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00004230 if (GetGraph()->HasIrreducibleLoops() &&
4231 (dispatch_info.method_load_kind ==
4232 HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative)) {
4233 dispatch_info.method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod;
4234 }
4235 switch (dispatch_info.code_ptr_location) {
Vladimir Markodc151b22015-10-15 18:02:30 +01004236 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
4237 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
4238 // For direct code, we actually prefer to call via the code pointer from ArtMethod*.
4239 // (Though the direct CALL ptr16:32 is available for consideration).
4240 return HInvokeStaticOrDirect::DispatchInfo {
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00004241 dispatch_info.method_load_kind,
Vladimir Markodc151b22015-10-15 18:02:30 +01004242 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00004243 dispatch_info.method_load_data,
Vladimir Markodc151b22015-10-15 18:02:30 +01004244 0u
4245 };
4246 default:
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00004247 return dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01004248 }
4249}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004250
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004251Register CodeGeneratorX86::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
4252 Register temp) {
4253 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
Vladimir Markoc53c0792015-11-19 15:48:33 +00004254 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004255 if (!invoke->GetLocations()->Intrinsified()) {
4256 return location.AsRegister<Register>();
4257 }
4258 // For intrinsics we allow any location, so it may be on the stack.
4259 if (!location.IsRegister()) {
4260 __ movl(temp, Address(ESP, location.GetStackIndex()));
4261 return temp;
4262 }
4263 // For register locations, check if the register was saved. If so, get it from the stack.
4264 // Note: There is a chance that the register was saved but not overwritten, so we could
4265 // save one load. However, since this is just an intrinsic slow path we prefer this
4266 // simple and more robust approach rather that trying to determine if that's the case.
4267 SlowPathCode* slow_path = GetCurrentSlowPath();
Serguei Katkov288c7a82016-05-16 11:53:15 +06004268 if (slow_path != nullptr) {
4269 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
4270 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
4271 __ movl(temp, Address(ESP, stack_offset));
4272 return temp;
4273 }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004274 }
4275 return location.AsRegister<Register>();
4276}
4277
Serguei Katkov288c7a82016-05-16 11:53:15 +06004278Location CodeGeneratorX86::GenerateCalleeMethodStaticOrDirectCall(HInvokeStaticOrDirect* invoke,
4279 Location temp) {
Vladimir Marko58155012015-08-19 12:49:41 +00004280 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
4281 switch (invoke->GetMethodLoadKind()) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004282 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
Vladimir Marko58155012015-08-19 12:49:41 +00004283 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004284 uint32_t offset =
4285 GetThreadOffset<kX86PointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
4286 __ fs()->movl(temp.AsRegister<Register>(), Address::Absolute(offset));
Vladimir Marko58155012015-08-19 12:49:41 +00004287 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004288 }
Vladimir Marko58155012015-08-19 12:49:41 +00004289 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00004290 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004291 break;
4292 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
4293 __ movl(temp.AsRegister<Register>(), Immediate(invoke->GetMethodAddress()));
4294 break;
4295 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004296 __ movl(temp.AsRegister<Register>(), Immediate(/* placeholder */ 0));
Vladimir Marko58155012015-08-19 12:49:41 +00004297 method_patches_.emplace_back(invoke->GetTargetMethod());
4298 __ Bind(&method_patches_.back().label); // Bind the label at the end of the "movl" insn.
4299 break;
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004300 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
4301 Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
4302 temp.AsRegister<Register>());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004303 __ movl(temp.AsRegister<Register>(), Address(base_reg, kDummy32BitOffset));
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004304 // Bind a new fixup label at the end of the "movl" insn.
4305 uint32_t offset = invoke->GetDexCacheArrayOffset();
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004306 __ Bind(NewPcRelativeDexCacheArrayPatch(invoke->GetDexFile(), offset));
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004307 break;
4308 }
Vladimir Marko58155012015-08-19 12:49:41 +00004309 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004310 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004311 Register method_reg;
4312 Register reg = temp.AsRegister<Register>();
4313 if (current_method.IsRegister()) {
4314 method_reg = current_method.AsRegister<Register>();
4315 } else {
David Brazdil58282f42016-01-14 12:45:10 +00004316 DCHECK(invoke->GetLocations()->Intrinsified());
Vladimir Marko58155012015-08-19 12:49:41 +00004317 DCHECK(!current_method.IsValid());
4318 method_reg = reg;
4319 __ movl(reg, Address(ESP, kCurrentMethodStackOffset));
4320 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00004321 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +01004322 __ movl(reg, Address(method_reg,
4323 ArtMethod::DexCacheResolvedMethodsOffset(kX86PointerSize).Int32Value()));
Vladimir Marko40ecb122016-04-06 17:33:41 +01004324 // temp = temp[index_in_cache];
4325 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
4326 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Vladimir Marko58155012015-08-19 12:49:41 +00004327 __ movl(reg, Address(reg, CodeGenerator::GetCachePointerOffset(index_in_cache)));
4328 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +01004329 }
Vladimir Marko58155012015-08-19 12:49:41 +00004330 }
Serguei Katkov288c7a82016-05-16 11:53:15 +06004331 return callee_method;
4332}
4333
4334void CodeGeneratorX86::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
4335 Location callee_method = GenerateCalleeMethodStaticOrDirectCall(invoke, temp);
Vladimir Marko58155012015-08-19 12:49:41 +00004336
4337 switch (invoke->GetCodePtrLocation()) {
4338 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
4339 __ call(GetFrameEntryLabel());
4340 break;
4341 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative: {
4342 relative_call_patches_.emplace_back(invoke->GetTargetMethod());
4343 Label* label = &relative_call_patches_.back().label;
4344 __ call(label); // Bind to the patch label, override at link time.
4345 __ Bind(label); // Bind the label at the end of the "call" insn.
4346 break;
4347 }
4348 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
4349 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
Vladimir Markodc151b22015-10-15 18:02:30 +01004350 // Filtered out by GetSupportedInvokeStaticOrDirectDispatch().
4351 LOG(FATAL) << "Unsupported";
4352 UNREACHABLE();
Vladimir Marko58155012015-08-19 12:49:41 +00004353 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
4354 // (callee_method + offset_of_quick_compiled_code)()
4355 __ call(Address(callee_method.AsRegister<Register>(),
4356 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07004357 kX86PointerSize).Int32Value()));
Vladimir Marko58155012015-08-19 12:49:41 +00004358 break;
Mark Mendell09ed1a32015-03-25 08:30:06 -04004359 }
4360
4361 DCHECK(!IsLeafMethod());
Mark Mendell09ed1a32015-03-25 08:30:06 -04004362}
4363
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004364void CodeGeneratorX86::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_in) {
4365 Register temp = temp_in.AsRegister<Register>();
4366 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
4367 invoke->GetVTableIndex(), kX86PointerSize).Uint32Value();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004368
4369 // Use the calling convention instead of the location of the receiver, as
4370 // intrinsics may have put the receiver in a different register. In the intrinsics
4371 // slow path, the arguments have been moved to the right place, so here we are
4372 // guaranteed that the receiver is the first register of the calling convention.
4373 InvokeDexCallingConvention calling_convention;
4374 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004375 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004376 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004377 __ movl(temp, Address(receiver, class_offset));
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004378 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00004379 // Instead of simply (possibly) unpoisoning `temp` here, we should
4380 // emit a read barrier for the previous class reference load.
4381 // However this is not required in practice, as this is an
4382 // intermediate/temporary reference and because the current
4383 // concurrent copying collector keeps the from-space memory
4384 // intact/accessible until the end of the marking phase (the
4385 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004386 __ MaybeUnpoisonHeapReference(temp);
4387 // temp = temp->GetMethodAt(method_offset);
4388 __ movl(temp, Address(temp, method_offset));
4389 // call temp->GetEntryPoint();
4390 __ call(Address(
Andreas Gampe542451c2016-07-26 09:02:02 -07004391 temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86PointerSize).Int32Value()));
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004392}
4393
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004394void CodeGeneratorX86::RecordSimplePatch() {
4395 if (GetCompilerOptions().GetIncludePatchInformation()) {
4396 simple_patches_.emplace_back();
4397 __ Bind(&simple_patches_.back());
4398 }
4399}
4400
4401void CodeGeneratorX86::RecordStringPatch(HLoadString* load_string) {
4402 string_patches_.emplace_back(load_string->GetDexFile(), load_string->GetStringIndex());
4403 __ Bind(&string_patches_.back().label);
4404}
4405
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004406void CodeGeneratorX86::RecordTypePatch(HLoadClass* load_class) {
4407 type_patches_.emplace_back(load_class->GetDexFile(), load_class->GetTypeIndex());
4408 __ Bind(&type_patches_.back().label);
4409}
4410
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004411Label* CodeGeneratorX86::NewPcRelativeDexCacheArrayPatch(const DexFile& dex_file,
4412 uint32_t element_offset) {
4413 // Add the patch entry and bind its label at the end of the instruction.
4414 pc_relative_dex_cache_patches_.emplace_back(dex_file, element_offset);
4415 return &pc_relative_dex_cache_patches_.back().label;
4416}
4417
Vladimir Marko58155012015-08-19 12:49:41 +00004418void CodeGeneratorX86::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
4419 DCHECK(linker_patches->empty());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004420 size_t size =
4421 method_patches_.size() +
4422 relative_call_patches_.size() +
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004423 pc_relative_dex_cache_patches_.size() +
4424 simple_patches_.size() +
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004425 string_patches_.size() +
4426 type_patches_.size();
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004427 linker_patches->reserve(size);
4428 // The label points to the end of the "movl" insn but the literal offset for method
4429 // patch needs to point to the embedded constant which occupies the last 4 bytes.
4430 constexpr uint32_t kLabelPositionToLiteralOffsetAdjustment = 4u;
Vladimir Marko58155012015-08-19 12:49:41 +00004431 for (const MethodPatchInfo<Label>& info : method_patches_) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004432 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Marko58155012015-08-19 12:49:41 +00004433 linker_patches->push_back(LinkerPatch::MethodPatch(literal_offset,
4434 info.target_method.dex_file,
4435 info.target_method.dex_method_index));
4436 }
4437 for (const MethodPatchInfo<Label>& info : relative_call_patches_) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004438 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Marko58155012015-08-19 12:49:41 +00004439 linker_patches->push_back(LinkerPatch::RelativeCodePatch(literal_offset,
4440 info.target_method.dex_file,
4441 info.target_method.dex_method_index));
4442 }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004443 for (const PcRelativeDexCacheAccessInfo& info : pc_relative_dex_cache_patches_) {
4444 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
4445 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(literal_offset,
4446 &info.target_dex_file,
4447 GetMethodAddressOffset(),
4448 info.element_offset));
4449 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004450 for (const Label& label : simple_patches_) {
4451 uint32_t literal_offset = label.Position() - kLabelPositionToLiteralOffsetAdjustment;
4452 linker_patches->push_back(LinkerPatch::RecordPosition(literal_offset));
4453 }
4454 if (GetCompilerOptions().GetCompilePic()) {
4455 for (const StringPatchInfo<Label>& info : string_patches_) {
4456 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
4457 linker_patches->push_back(LinkerPatch::RelativeStringPatch(literal_offset,
4458 &info.dex_file,
4459 GetMethodAddressOffset(),
4460 info.string_index));
4461 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004462 for (const TypePatchInfo<Label>& info : type_patches_) {
4463 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
4464 linker_patches->push_back(LinkerPatch::RelativeTypePatch(literal_offset,
4465 &info.dex_file,
4466 GetMethodAddressOffset(),
4467 info.type_index));
4468 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004469 } else {
4470 for (const StringPatchInfo<Label>& info : string_patches_) {
4471 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
4472 linker_patches->push_back(LinkerPatch::StringPatch(literal_offset,
4473 &info.dex_file,
4474 info.string_index));
4475 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004476 for (const TypePatchInfo<Label>& info : type_patches_) {
4477 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
4478 linker_patches->push_back(LinkerPatch::TypePatch(literal_offset,
4479 &info.dex_file,
4480 info.type_index));
4481 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004482 }
Vladimir Marko58155012015-08-19 12:49:41 +00004483}
4484
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004485void CodeGeneratorX86::MarkGCCard(Register temp,
4486 Register card,
4487 Register object,
4488 Register value,
4489 bool value_can_be_null) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004490 NearLabel is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004491 if (value_can_be_null) {
4492 __ testl(value, value);
4493 __ j(kEqual, &is_null);
4494 }
Andreas Gampe542451c2016-07-26 09:02:02 -07004495 __ fs()->movl(card, Address::Absolute(Thread::CardTableOffset<kX86PointerSize>().Int32Value()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004496 __ movl(temp, object);
4497 __ shrl(temp, Immediate(gc::accounting::CardTable::kCardShift));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00004498 __ movb(Address(temp, card, TIMES_1, 0),
4499 X86ManagedRegister::FromCpuRegister(card).AsByteRegister());
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004500 if (value_can_be_null) {
4501 __ Bind(&is_null);
4502 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004503}
4504
Calin Juravle52c48962014-12-16 17:02:57 +00004505void LocationsBuilderX86::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
4506 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain0d5a2812015-11-13 10:07:31 +00004507
4508 bool object_field_get_with_read_barrier =
4509 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004510 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00004511 new (GetGraph()->GetArena()) LocationSummary(instruction,
4512 kEmitCompilerReadBarrier ?
4513 LocationSummary::kCallOnSlowPath :
4514 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01004515 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004516 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01004517 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004518 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004519
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004520 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4521 locations->SetOut(Location::RequiresFpuRegister());
4522 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004523 // The output overlaps in case of long: we don't want the low move
4524 // to overwrite the object's location. Likewise, in the case of
4525 // an object field get with read barriers enabled, we do not want
4526 // the move to overwrite the object's location, as we need it to emit
4527 // the read barrier.
4528 locations->SetOut(
4529 Location::RequiresRegister(),
4530 (object_field_get_with_read_barrier || instruction->GetType() == Primitive::kPrimLong) ?
4531 Location::kOutputOverlap :
4532 Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004533 }
Calin Juravle52c48962014-12-16 17:02:57 +00004534
4535 if (field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong)) {
4536 // Long values can be loaded atomically into an XMM using movsd.
Roland Levillain7c1559a2015-12-15 10:55:36 +00004537 // So we use an XMM register as a temp to achieve atomicity (first
4538 // load the temp into the XMM and then copy the XMM into the
4539 // output, 32 bits at a time).
Calin Juravle52c48962014-12-16 17:02:57 +00004540 locations->AddTemp(Location::RequiresFpuRegister());
4541 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004542}
4543
Calin Juravle52c48962014-12-16 17:02:57 +00004544void InstructionCodeGeneratorX86::HandleFieldGet(HInstruction* instruction,
4545 const FieldInfo& field_info) {
4546 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004547
Calin Juravle52c48962014-12-16 17:02:57 +00004548 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004549 Location base_loc = locations->InAt(0);
4550 Register base = base_loc.AsRegister<Register>();
Calin Juravle52c48962014-12-16 17:02:57 +00004551 Location out = locations->Out();
4552 bool is_volatile = field_info.IsVolatile();
4553 Primitive::Type field_type = field_info.GetFieldType();
4554 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4555
4556 switch (field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004557 case Primitive::kPrimBoolean: {
Calin Juravle52c48962014-12-16 17:02:57 +00004558 __ movzxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004559 break;
4560 }
4561
4562 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00004563 __ movsxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004564 break;
4565 }
4566
4567 case Primitive::kPrimShort: {
Calin Juravle52c48962014-12-16 17:02:57 +00004568 __ movsxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004569 break;
4570 }
4571
4572 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00004573 __ movzxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004574 break;
4575 }
4576
4577 case Primitive::kPrimInt:
Calin Juravle52c48962014-12-16 17:02:57 +00004578 __ movl(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004579 break;
Roland Levillain7c1559a2015-12-15 10:55:36 +00004580
4581 case Primitive::kPrimNot: {
4582 // /* HeapReference<Object> */ out = *(base + offset)
4583 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004584 // Note that a potential implicit null check is handled in this
4585 // CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier call.
4586 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00004587 instruction, out, base, offset, /* needs_null_check */ true);
Roland Levillain7c1559a2015-12-15 10:55:36 +00004588 if (is_volatile) {
4589 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4590 }
4591 } else {
4592 __ movl(out.AsRegister<Register>(), Address(base, offset));
4593 codegen_->MaybeRecordImplicitNullCheck(instruction);
4594 if (is_volatile) {
4595 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4596 }
4597 // If read barriers are enabled, emit read barriers other than
4598 // Baker's using a slow path (and also unpoison the loaded
4599 // reference, if heap poisoning is enabled).
4600 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
4601 }
4602 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004603 }
4604
4605 case Primitive::kPrimLong: {
Calin Juravle52c48962014-12-16 17:02:57 +00004606 if (is_volatile) {
4607 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4608 __ movsd(temp, Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004609 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004610 __ movd(out.AsRegisterPairLow<Register>(), temp);
4611 __ psrlq(temp, Immediate(32));
4612 __ movd(out.AsRegisterPairHigh<Register>(), temp);
4613 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004614 DCHECK_NE(base, out.AsRegisterPairLow<Register>());
Calin Juravle52c48962014-12-16 17:02:57 +00004615 __ movl(out.AsRegisterPairLow<Register>(), Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004616 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004617 __ movl(out.AsRegisterPairHigh<Register>(), Address(base, kX86WordSize + offset));
4618 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004619 break;
4620 }
4621
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004622 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00004623 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004624 break;
4625 }
4626
4627 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00004628 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004629 break;
4630 }
4631
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004632 case Primitive::kPrimVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00004633 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004634 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004635 }
Calin Juravle52c48962014-12-16 17:02:57 +00004636
Roland Levillain7c1559a2015-12-15 10:55:36 +00004637 if (field_type == Primitive::kPrimNot || field_type == Primitive::kPrimLong) {
4638 // Potential implicit null checks, in the case of reference or
4639 // long fields, are handled in the previous switch statement.
4640 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00004641 codegen_->MaybeRecordImplicitNullCheck(instruction);
4642 }
4643
Calin Juravle52c48962014-12-16 17:02:57 +00004644 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004645 if (field_type == Primitive::kPrimNot) {
4646 // Memory barriers, in the case of references, are also handled
4647 // in the previous switch statement.
4648 } else {
4649 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4650 }
Roland Levillain4d027112015-07-01 15:41:14 +01004651 }
Calin Juravle52c48962014-12-16 17:02:57 +00004652}
4653
4654void LocationsBuilderX86::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
4655 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4656
4657 LocationSummary* locations =
4658 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4659 locations->SetInAt(0, Location::RequiresRegister());
4660 bool is_volatile = field_info.IsVolatile();
4661 Primitive::Type field_type = field_info.GetFieldType();
4662 bool is_byte_type = (field_type == Primitive::kPrimBoolean)
4663 || (field_type == Primitive::kPrimByte);
4664
4665 // The register allocator does not support multiple
4666 // inputs that die at entry with one in a specific register.
4667 if (is_byte_type) {
4668 // Ensure the value is in a byte register.
4669 locations->SetInAt(1, Location::RegisterLocation(EAX));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004670 } else if (Primitive::IsFloatingPointType(field_type)) {
Mark Mendell81489372015-11-04 11:30:41 -05004671 if (is_volatile && field_type == Primitive::kPrimDouble) {
4672 // In order to satisfy the semantics of volatile, this must be a single instruction store.
4673 locations->SetInAt(1, Location::RequiresFpuRegister());
4674 } else {
4675 locations->SetInAt(1, Location::FpuRegisterOrConstant(instruction->InputAt(1)));
4676 }
4677 } else if (is_volatile && field_type == Primitive::kPrimLong) {
4678 // In order to satisfy the semantics of volatile, this must be a single instruction store.
Calin Juravle52c48962014-12-16 17:02:57 +00004679 locations->SetInAt(1, Location::RequiresRegister());
Mark Mendell81489372015-11-04 11:30:41 -05004680
Calin Juravle52c48962014-12-16 17:02:57 +00004681 // 64bits value can be atomically written to an address with movsd and an XMM register.
4682 // We need two XMM registers because there's no easier way to (bit) copy a register pair
4683 // into a single XMM register (we copy each pair part into the XMMs and then interleave them).
4684 // NB: We could make the register allocator understand fp_reg <-> core_reg moves but given the
4685 // isolated cases when we need this it isn't worth adding the extra complexity.
4686 locations->AddTemp(Location::RequiresFpuRegister());
4687 locations->AddTemp(Location::RequiresFpuRegister());
Mark Mendell81489372015-11-04 11:30:41 -05004688 } else {
4689 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4690
4691 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
4692 // Temporary registers for the write barrier.
4693 locations->AddTemp(Location::RequiresRegister()); // May be used for reference poisoning too.
4694 // Ensure the card is in a byte register.
4695 locations->AddTemp(Location::RegisterLocation(ECX));
4696 }
Calin Juravle52c48962014-12-16 17:02:57 +00004697 }
4698}
4699
4700void InstructionCodeGeneratorX86::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004701 const FieldInfo& field_info,
4702 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00004703 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4704
4705 LocationSummary* locations = instruction->GetLocations();
4706 Register base = locations->InAt(0).AsRegister<Register>();
4707 Location value = locations->InAt(1);
4708 bool is_volatile = field_info.IsVolatile();
4709 Primitive::Type field_type = field_info.GetFieldType();
4710 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01004711 bool needs_write_barrier =
4712 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00004713
4714 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004715 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
Calin Juravle52c48962014-12-16 17:02:57 +00004716 }
4717
Mark Mendell81489372015-11-04 11:30:41 -05004718 bool maybe_record_implicit_null_check_done = false;
4719
Calin Juravle52c48962014-12-16 17:02:57 +00004720 switch (field_type) {
4721 case Primitive::kPrimBoolean:
4722 case Primitive::kPrimByte: {
4723 __ movb(Address(base, offset), value.AsRegister<ByteRegister>());
4724 break;
4725 }
4726
4727 case Primitive::kPrimShort:
4728 case Primitive::kPrimChar: {
Mark Mendell81489372015-11-04 11:30:41 -05004729 if (value.IsConstant()) {
4730 int16_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4731 __ movw(Address(base, offset), Immediate(v));
4732 } else {
4733 __ movw(Address(base, offset), value.AsRegister<Register>());
4734 }
Calin Juravle52c48962014-12-16 17:02:57 +00004735 break;
4736 }
4737
4738 case Primitive::kPrimInt:
4739 case Primitive::kPrimNot: {
Roland Levillain4d027112015-07-01 15:41:14 +01004740 if (kPoisonHeapReferences && needs_write_barrier) {
4741 // Note that in the case where `value` is a null reference,
4742 // we do not enter this block, as the reference does not
4743 // need poisoning.
4744 DCHECK_EQ(field_type, Primitive::kPrimNot);
4745 Register temp = locations->GetTemp(0).AsRegister<Register>();
4746 __ movl(temp, value.AsRegister<Register>());
4747 __ PoisonHeapReference(temp);
4748 __ movl(Address(base, offset), temp);
Mark Mendell81489372015-11-04 11:30:41 -05004749 } else if (value.IsConstant()) {
4750 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4751 __ movl(Address(base, offset), Immediate(v));
Roland Levillain4d027112015-07-01 15:41:14 +01004752 } else {
Nicolas Geoffray03971632016-03-17 10:44:24 +00004753 DCHECK(value.IsRegister()) << value;
Roland Levillain4d027112015-07-01 15:41:14 +01004754 __ movl(Address(base, offset), value.AsRegister<Register>());
4755 }
Calin Juravle52c48962014-12-16 17:02:57 +00004756 break;
4757 }
4758
4759 case Primitive::kPrimLong: {
4760 if (is_volatile) {
4761 XmmRegister temp1 = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4762 XmmRegister temp2 = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
4763 __ movd(temp1, value.AsRegisterPairLow<Register>());
4764 __ movd(temp2, value.AsRegisterPairHigh<Register>());
4765 __ punpckldq(temp1, temp2);
4766 __ movsd(Address(base, offset), temp1);
Calin Juravle77520bc2015-01-12 18:45:46 +00004767 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell81489372015-11-04 11:30:41 -05004768 } else if (value.IsConstant()) {
4769 int64_t v = CodeGenerator::GetInt64ValueOf(value.GetConstant());
4770 __ movl(Address(base, offset), Immediate(Low32Bits(v)));
4771 codegen_->MaybeRecordImplicitNullCheck(instruction);
4772 __ movl(Address(base, kX86WordSize + offset), Immediate(High32Bits(v)));
Calin Juravle52c48962014-12-16 17:02:57 +00004773 } else {
4774 __ movl(Address(base, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00004775 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004776 __ movl(Address(base, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
4777 }
Mark Mendell81489372015-11-04 11:30:41 -05004778 maybe_record_implicit_null_check_done = true;
Calin Juravle52c48962014-12-16 17:02:57 +00004779 break;
4780 }
4781
4782 case Primitive::kPrimFloat: {
Mark Mendell81489372015-11-04 11:30:41 -05004783 if (value.IsConstant()) {
4784 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4785 __ movl(Address(base, offset), Immediate(v));
4786 } else {
4787 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4788 }
Calin Juravle52c48962014-12-16 17:02:57 +00004789 break;
4790 }
4791
4792 case Primitive::kPrimDouble: {
Mark Mendell81489372015-11-04 11:30:41 -05004793 if (value.IsConstant()) {
4794 int64_t v = CodeGenerator::GetInt64ValueOf(value.GetConstant());
4795 __ movl(Address(base, offset), Immediate(Low32Bits(v)));
4796 codegen_->MaybeRecordImplicitNullCheck(instruction);
4797 __ movl(Address(base, kX86WordSize + offset), Immediate(High32Bits(v)));
4798 maybe_record_implicit_null_check_done = true;
4799 } else {
4800 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4801 }
Calin Juravle52c48962014-12-16 17:02:57 +00004802 break;
4803 }
4804
4805 case Primitive::kPrimVoid:
4806 LOG(FATAL) << "Unreachable type " << field_type;
4807 UNREACHABLE();
4808 }
4809
Mark Mendell81489372015-11-04 11:30:41 -05004810 if (!maybe_record_implicit_null_check_done) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004811 codegen_->MaybeRecordImplicitNullCheck(instruction);
4812 }
4813
Roland Levillain4d027112015-07-01 15:41:14 +01004814 if (needs_write_barrier) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004815 Register temp = locations->GetTemp(0).AsRegister<Register>();
4816 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004817 codegen_->MarkGCCard(temp, card, base, value.AsRegister<Register>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00004818 }
4819
Calin Juravle52c48962014-12-16 17:02:57 +00004820 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004821 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
Calin Juravle52c48962014-12-16 17:02:57 +00004822 }
4823}
4824
4825void LocationsBuilderX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4826 HandleFieldGet(instruction, instruction->GetFieldInfo());
4827}
4828
4829void InstructionCodeGeneratorX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4830 HandleFieldGet(instruction, instruction->GetFieldInfo());
4831}
4832
4833void LocationsBuilderX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
4834 HandleFieldSet(instruction, instruction->GetFieldInfo());
4835}
4836
4837void InstructionCodeGeneratorX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004838 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00004839}
4840
4841void LocationsBuilderX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
4842 HandleFieldSet(instruction, instruction->GetFieldInfo());
4843}
4844
4845void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004846 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00004847}
4848
4849void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4850 HandleFieldGet(instruction, instruction->GetFieldInfo());
4851}
4852
4853void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4854 HandleFieldGet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004855}
4856
Calin Juravlee460d1d2015-09-29 04:52:17 +01004857void LocationsBuilderX86::VisitUnresolvedInstanceFieldGet(
4858 HUnresolvedInstanceFieldGet* instruction) {
4859 FieldAccessCallingConventionX86 calling_convention;
4860 codegen_->CreateUnresolvedFieldLocationSummary(
4861 instruction, instruction->GetFieldType(), calling_convention);
4862}
4863
4864void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldGet(
4865 HUnresolvedInstanceFieldGet* instruction) {
4866 FieldAccessCallingConventionX86 calling_convention;
4867 codegen_->GenerateUnresolvedFieldAccess(instruction,
4868 instruction->GetFieldType(),
4869 instruction->GetFieldIndex(),
4870 instruction->GetDexPc(),
4871 calling_convention);
4872}
4873
4874void LocationsBuilderX86::VisitUnresolvedInstanceFieldSet(
4875 HUnresolvedInstanceFieldSet* instruction) {
4876 FieldAccessCallingConventionX86 calling_convention;
4877 codegen_->CreateUnresolvedFieldLocationSummary(
4878 instruction, instruction->GetFieldType(), calling_convention);
4879}
4880
4881void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldSet(
4882 HUnresolvedInstanceFieldSet* instruction) {
4883 FieldAccessCallingConventionX86 calling_convention;
4884 codegen_->GenerateUnresolvedFieldAccess(instruction,
4885 instruction->GetFieldType(),
4886 instruction->GetFieldIndex(),
4887 instruction->GetDexPc(),
4888 calling_convention);
4889}
4890
4891void LocationsBuilderX86::VisitUnresolvedStaticFieldGet(
4892 HUnresolvedStaticFieldGet* instruction) {
4893 FieldAccessCallingConventionX86 calling_convention;
4894 codegen_->CreateUnresolvedFieldLocationSummary(
4895 instruction, instruction->GetFieldType(), calling_convention);
4896}
4897
4898void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldGet(
4899 HUnresolvedStaticFieldGet* instruction) {
4900 FieldAccessCallingConventionX86 calling_convention;
4901 codegen_->GenerateUnresolvedFieldAccess(instruction,
4902 instruction->GetFieldType(),
4903 instruction->GetFieldIndex(),
4904 instruction->GetDexPc(),
4905 calling_convention);
4906}
4907
4908void LocationsBuilderX86::VisitUnresolvedStaticFieldSet(
4909 HUnresolvedStaticFieldSet* instruction) {
4910 FieldAccessCallingConventionX86 calling_convention;
4911 codegen_->CreateUnresolvedFieldLocationSummary(
4912 instruction, instruction->GetFieldType(), calling_convention);
4913}
4914
4915void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldSet(
4916 HUnresolvedStaticFieldSet* instruction) {
4917 FieldAccessCallingConventionX86 calling_convention;
4918 codegen_->GenerateUnresolvedFieldAccess(instruction,
4919 instruction->GetFieldType(),
4920 instruction->GetFieldIndex(),
4921 instruction->GetDexPc(),
4922 calling_convention);
4923}
4924
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004925void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004926 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
4927 Location loc = codegen_->GetCompilerOptions().GetImplicitNullChecks()
4928 ? Location::RequiresRegister()
4929 : Location::Any();
4930 locations->SetInAt(0, loc);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004931}
4932
Calin Juravle2ae48182016-03-16 14:05:09 +00004933void CodeGeneratorX86::GenerateImplicitNullCheck(HNullCheck* instruction) {
4934 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004935 return;
4936 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004937 LocationSummary* locations = instruction->GetLocations();
4938 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00004939
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004940 __ testl(EAX, Address(obj.AsRegister<Register>(), 0));
Calin Juravle2ae48182016-03-16 14:05:09 +00004941 RecordPcInfo(instruction, instruction->GetDexPc());
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004942}
4943
Calin Juravle2ae48182016-03-16 14:05:09 +00004944void CodeGeneratorX86::GenerateExplicitNullCheck(HNullCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07004945 SlowPathCode* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00004946 AddSlowPath(slow_path);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004947
4948 LocationSummary* locations = instruction->GetLocations();
4949 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004950
4951 if (obj.IsRegister()) {
Mark Mendell42514f62015-03-31 11:34:22 -04004952 __ testl(obj.AsRegister<Register>(), obj.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004953 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004954 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004955 } else {
4956 DCHECK(obj.IsConstant()) << obj;
David Brazdil77a48ae2015-09-15 12:34:04 +00004957 DCHECK(obj.GetConstant()->IsNullConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004958 __ jmp(slow_path->GetEntryLabel());
4959 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004960 }
4961 __ j(kEqual, slow_path->GetEntryLabel());
4962}
4963
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004964void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00004965 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004966}
4967
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004968void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004969 bool object_array_get_with_read_barrier =
4970 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004971 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00004972 new (GetGraph()->GetArena()) LocationSummary(instruction,
4973 object_array_get_with_read_barrier ?
4974 LocationSummary::kCallOnSlowPath :
4975 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01004976 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004977 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01004978 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004979 locations->SetInAt(0, Location::RequiresRegister());
4980 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004981 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4982 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4983 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004984 // The output overlaps in case of long: we don't want the low move
4985 // to overwrite the array's location. Likewise, in the case of an
4986 // object array get with read barriers enabled, we do not want the
4987 // move to overwrite the array's location, as we need it to emit
4988 // the read barrier.
4989 locations->SetOut(
4990 Location::RequiresRegister(),
4991 (instruction->GetType() == Primitive::kPrimLong || object_array_get_with_read_barrier) ?
4992 Location::kOutputOverlap :
4993 Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004994 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004995}
4996
4997void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
4998 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004999 Location obj_loc = locations->InAt(0);
5000 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005001 Location index = locations->InAt(1);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005002 Location out_loc = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005003 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005004
Calin Juravle77520bc2015-01-12 18:45:46 +00005005 Primitive::Type type = instruction->GetType();
5006 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005007 case Primitive::kPrimBoolean: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005008 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005009 __ movzxb(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005010 break;
5011 }
5012
5013 case Primitive::kPrimByte: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005014 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005015 __ movsxb(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005016 break;
5017 }
5018
5019 case Primitive::kPrimShort: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005020 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005021 __ movsxw(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005022 break;
5023 }
5024
5025 case Primitive::kPrimChar: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005026 Register out = out_loc.AsRegister<Register>();
jessicahandojo4877b792016-09-08 19:49:13 -07005027 if (mirror::kUseStringCompression && instruction->IsStringCharAt()) {
5028 // Branch cases into compressed and uncompressed for each index's type.
5029 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
5030 NearLabel done, not_compressed;
5031 __ cmpl(Address(obj, count_offset), Immediate(0));
5032 codegen_->MaybeRecordImplicitNullCheck(instruction);
5033 __ j(kGreaterEqual, &not_compressed);
5034 __ movzxb(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_1, data_offset));
5035 __ jmp(&done);
5036 __ Bind(&not_compressed);
5037 __ movzxw(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_2, data_offset));
5038 __ Bind(&done);
5039 } else {
5040 // Common case for charAt of array of char or when string compression's
5041 // feature is turned off.
5042 __ movzxw(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_2, data_offset));
5043 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005044 break;
5045 }
5046
Roland Levillain7c1559a2015-12-15 10:55:36 +00005047 case Primitive::kPrimInt: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005048 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005049 __ movl(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005050 break;
5051 }
5052
Roland Levillain7c1559a2015-12-15 10:55:36 +00005053 case Primitive::kPrimNot: {
5054 static_assert(
5055 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
5056 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain7c1559a2015-12-15 10:55:36 +00005057 // /* HeapReference<Object> */ out =
5058 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
5059 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005060 // Note that a potential implicit null check is handled in this
5061 // CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier call.
5062 codegen_->GenerateArrayLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00005063 instruction, out_loc, obj, data_offset, index, /* needs_null_check */ true);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005064 } else {
5065 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005066 __ movl(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset));
5067 codegen_->MaybeRecordImplicitNullCheck(instruction);
5068 // If read barriers are enabled, emit read barriers other than
5069 // Baker's using a slow path (and also unpoison the loaded
5070 // reference, if heap poisoning is enabled).
Roland Levillain7c1559a2015-12-15 10:55:36 +00005071 if (index.IsConstant()) {
5072 uint32_t offset =
5073 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Roland Levillain7c1559a2015-12-15 10:55:36 +00005074 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
5075 } else {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005076 codegen_->MaybeGenerateReadBarrierSlow(
5077 instruction, out_loc, out_loc, obj_loc, data_offset, index);
5078 }
5079 }
5080 break;
5081 }
5082
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005083 case Primitive::kPrimLong: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005084 DCHECK_NE(obj, out_loc.AsRegisterPairLow<Register>());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005085 __ movl(out_loc.AsRegisterPairLow<Register>(),
5086 CodeGeneratorX86::ArrayAddress(obj, index, TIMES_8, data_offset));
5087 codegen_->MaybeRecordImplicitNullCheck(instruction);
5088 __ movl(out_loc.AsRegisterPairHigh<Register>(),
5089 CodeGeneratorX86::ArrayAddress(obj, index, TIMES_8, data_offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005090 break;
5091 }
5092
Mark Mendell7c8d0092015-01-26 11:21:33 -05005093 case Primitive::kPrimFloat: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005094 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005095 __ movss(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005096 break;
5097 }
5098
5099 case Primitive::kPrimDouble: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005100 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005101 __ movsd(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_8, data_offset));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005102 break;
5103 }
5104
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005105 case Primitive::kPrimVoid:
Calin Juravle77520bc2015-01-12 18:45:46 +00005106 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07005107 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005108 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005109
Roland Levillain7c1559a2015-12-15 10:55:36 +00005110 if (type == Primitive::kPrimNot || type == Primitive::kPrimLong) {
5111 // Potential implicit null checks, in the case of reference or
5112 // long arrays, are handled in the previous switch statement.
5113 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00005114 codegen_->MaybeRecordImplicitNullCheck(instruction);
5115 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005116}
5117
5118void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01005119 Primitive::Type value_type = instruction->GetComponentType();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005120
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005121 bool needs_write_barrier =
5122 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Roland Levillain0d5a2812015-11-13 10:07:31 +00005123 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005124
Nicolas Geoffray39468442014-09-02 15:17:15 +01005125 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
5126 instruction,
Vladimir Marko8d49fd72016-08-25 15:20:47 +01005127 may_need_runtime_call_for_type_check ?
Roland Levillain0d5a2812015-11-13 10:07:31 +00005128 LocationSummary::kCallOnSlowPath :
5129 LocationSummary::kNoCall);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005130
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005131 bool is_byte_type = (value_type == Primitive::kPrimBoolean)
5132 || (value_type == Primitive::kPrimByte);
5133 // We need the inputs to be different than the output in case of long operation.
5134 // In case of a byte operation, the register allocator does not support multiple
5135 // inputs that die at entry with one in a specific register.
5136 locations->SetInAt(0, Location::RequiresRegister());
5137 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
5138 if (is_byte_type) {
5139 // Ensure the value is in a byte register.
5140 locations->SetInAt(2, Location::ByteRegisterOrConstant(EAX, instruction->InputAt(2)));
5141 } else if (Primitive::IsFloatingPointType(value_type)) {
Mark Mendell81489372015-11-04 11:30:41 -05005142 locations->SetInAt(2, Location::FpuRegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005143 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005144 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
5145 }
5146 if (needs_write_barrier) {
5147 // Temporary registers for the write barrier.
5148 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
5149 // Ensure the card is in a byte register.
Roland Levillain4f6b0b52015-11-23 19:29:22 +00005150 locations->AddTemp(Location::RegisterLocation(ECX));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005151 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005152}
5153
5154void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
5155 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005156 Location array_loc = locations->InAt(0);
5157 Register array = array_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005158 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005159 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005160 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005161 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
5162 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5163 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005164 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005165 bool needs_write_barrier =
5166 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005167
5168 switch (value_type) {
5169 case Primitive::kPrimBoolean:
5170 case Primitive::kPrimByte: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005171 uint32_t offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005172 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_1, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005173 if (value.IsRegister()) {
5174 __ movb(address, value.AsRegister<ByteRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005175 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005176 __ movb(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005177 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005178 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005179 break;
5180 }
5181
5182 case Primitive::kPrimShort:
5183 case Primitive::kPrimChar: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005184 uint32_t offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005185 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_2, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005186 if (value.IsRegister()) {
5187 __ movw(address, value.AsRegister<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005188 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005189 __ movw(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005190 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005191 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005192 break;
5193 }
5194
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005195 case Primitive::kPrimNot: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005196 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005197 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_4, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005198
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005199 if (!value.IsRegister()) {
5200 // Just setting null.
5201 DCHECK(instruction->InputAt(2)->IsNullConstant());
5202 DCHECK(value.IsConstant()) << value;
5203 __ movl(address, Immediate(0));
Calin Juravle77520bc2015-01-12 18:45:46 +00005204 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005205 DCHECK(!needs_write_barrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005206 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005207 break;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005208 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005209
5210 DCHECK(needs_write_barrier);
5211 Register register_value = value.AsRegister<Register>();
Roland Levillain16d9f942016-08-25 17:27:56 +01005212 // We cannot use a NearLabel for `done`, as its range may be too
5213 // short when Baker read barriers are enabled.
5214 Label done;
5215 NearLabel not_null, do_put;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005216 SlowPathCode* slow_path = nullptr;
Roland Levillain16d9f942016-08-25 17:27:56 +01005217 Location temp_loc = locations->GetTemp(0);
5218 Register temp = temp_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005219 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005220 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathX86(instruction);
5221 codegen_->AddSlowPath(slow_path);
5222 if (instruction->GetValueCanBeNull()) {
5223 __ testl(register_value, register_value);
5224 __ j(kNotEqual, &not_null);
5225 __ movl(address, Immediate(0));
5226 codegen_->MaybeRecordImplicitNullCheck(instruction);
5227 __ jmp(&done);
5228 __ Bind(&not_null);
5229 }
5230
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005231 // Note that when Baker read barriers are enabled, the type
5232 // checks are performed without read barriers. This is fine,
5233 // even in the case where a class object is in the from-space
5234 // after the flip, as a comparison involving such a type would
5235 // not produce a false positive; it may of course produce a
5236 // false negative, in which case we would take the ArraySet
5237 // slow path.
Roland Levillain16d9f942016-08-25 17:27:56 +01005238
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005239 // /* HeapReference<Class> */ temp = array->klass_
5240 __ movl(temp, Address(array, class_offset));
5241 codegen_->MaybeRecordImplicitNullCheck(instruction);
5242 __ MaybeUnpoisonHeapReference(temp);
Roland Levillain16d9f942016-08-25 17:27:56 +01005243
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005244 // /* HeapReference<Class> */ temp = temp->component_type_
5245 __ movl(temp, Address(temp, component_offset));
5246 // If heap poisoning is enabled, no need to unpoison `temp`
5247 // nor the object reference in `register_value->klass`, as
5248 // we are comparing two poisoned references.
5249 __ cmpl(temp, Address(register_value, class_offset));
Roland Levillain16d9f942016-08-25 17:27:56 +01005250
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005251 if (instruction->StaticTypeOfArrayIsObjectArray()) {
5252 __ j(kEqual, &do_put);
5253 // If heap poisoning is enabled, the `temp` reference has
5254 // not been unpoisoned yet; unpoison it now.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005255 __ MaybeUnpoisonHeapReference(temp);
5256
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005257 // If heap poisoning is enabled, no need to unpoison the
5258 // heap reference loaded below, as it is only used for a
5259 // comparison with null.
5260 __ cmpl(Address(temp, super_offset), Immediate(0));
5261 __ j(kNotEqual, slow_path->GetEntryLabel());
5262 __ Bind(&do_put);
5263 } else {
5264 __ j(kNotEqual, slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005265 }
5266 }
5267
5268 if (kPoisonHeapReferences) {
5269 __ movl(temp, register_value);
5270 __ PoisonHeapReference(temp);
5271 __ movl(address, temp);
5272 } else {
5273 __ movl(address, register_value);
5274 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005275 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005276 codegen_->MaybeRecordImplicitNullCheck(instruction);
5277 }
5278
5279 Register card = locations->GetTemp(1).AsRegister<Register>();
5280 codegen_->MarkGCCard(
5281 temp, card, array, value.AsRegister<Register>(), instruction->GetValueCanBeNull());
5282 __ Bind(&done);
5283
5284 if (slow_path != nullptr) {
5285 __ Bind(slow_path->GetExitLabel());
5286 }
5287
5288 break;
5289 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005290
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005291 case Primitive::kPrimInt: {
5292 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005293 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_4, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005294 if (value.IsRegister()) {
5295 __ movl(address, value.AsRegister<Register>());
5296 } else {
5297 DCHECK(value.IsConstant()) << value;
5298 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
5299 __ movl(address, Immediate(v));
5300 }
5301 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005302 break;
5303 }
5304
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005305 case Primitive::kPrimLong: {
5306 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005307 if (value.IsRegisterPair()) {
5308 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset),
5309 value.AsRegisterPairLow<Register>());
5310 codegen_->MaybeRecordImplicitNullCheck(instruction);
5311 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset + kX86WordSize),
5312 value.AsRegisterPairHigh<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005313 } else {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005314 DCHECK(value.IsConstant());
5315 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
5316 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset),
5317 Immediate(Low32Bits(val)));
5318 codegen_->MaybeRecordImplicitNullCheck(instruction);
5319 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset + kX86WordSize),
5320 Immediate(High32Bits(val)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005321 }
5322 break;
5323 }
5324
Mark Mendell7c8d0092015-01-26 11:21:33 -05005325 case Primitive::kPrimFloat: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005326 uint32_t offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005327 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_4, offset);
Mark Mendell81489372015-11-04 11:30:41 -05005328 if (value.IsFpuRegister()) {
5329 __ movss(address, value.AsFpuRegister<XmmRegister>());
5330 } else {
5331 DCHECK(value.IsConstant());
5332 int32_t v = bit_cast<int32_t, float>(value.GetConstant()->AsFloatConstant()->GetValue());
5333 __ movl(address, Immediate(v));
5334 }
5335 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005336 break;
5337 }
5338
5339 case Primitive::kPrimDouble: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005340 uint32_t offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005341 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, offset);
Mark Mendell81489372015-11-04 11:30:41 -05005342 if (value.IsFpuRegister()) {
5343 __ movsd(address, value.AsFpuRegister<XmmRegister>());
5344 } else {
5345 DCHECK(value.IsConstant());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005346 Address address_hi =
5347 CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, offset + kX86WordSize);
Mark Mendell81489372015-11-04 11:30:41 -05005348 int64_t v = bit_cast<int64_t, double>(value.GetConstant()->AsDoubleConstant()->GetValue());
5349 __ movl(address, Immediate(Low32Bits(v)));
5350 codegen_->MaybeRecordImplicitNullCheck(instruction);
5351 __ movl(address_hi, Immediate(High32Bits(v)));
5352 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005353 break;
5354 }
5355
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005356 case Primitive::kPrimVoid:
5357 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07005358 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005359 }
5360}
5361
5362void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
5363 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005364 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellee8d9712016-07-12 11:13:15 -04005365 if (!instruction->IsEmittedAtUseSite()) {
5366 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5367 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005368}
5369
5370void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
Mark Mendellee8d9712016-07-12 11:13:15 -04005371 if (instruction->IsEmittedAtUseSite()) {
5372 return;
5373 }
5374
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005375 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01005376 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00005377 Register obj = locations->InAt(0).AsRegister<Register>();
5378 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005379 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005380 codegen_->MaybeRecordImplicitNullCheck(instruction);
jessicahandojo4877b792016-09-08 19:49:13 -07005381 // Mask out most significant bit in case the array is String's array of char.
5382 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
5383 __ andl(out, Immediate(INT32_MAX));
5384 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005385}
5386
5387void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005388 RegisterSet caller_saves = RegisterSet::Empty();
5389 InvokeRuntimeCallingConvention calling_convention;
5390 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5391 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
5392 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Mark Mendellf60c90b2015-03-04 15:12:59 -05005393 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendellee8d9712016-07-12 11:13:15 -04005394 HInstruction* length = instruction->InputAt(1);
5395 if (!length->IsEmittedAtUseSite()) {
5396 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
5397 }
jessicahandojo4877b792016-09-08 19:49:13 -07005398 // Need register to see array's length.
5399 if (mirror::kUseStringCompression && instruction->IsStringCharAt()) {
5400 locations->AddTemp(Location::RequiresRegister());
5401 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005402}
5403
5404void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
jessicahandojo4877b792016-09-08 19:49:13 -07005405 const bool is_string_compressed_char_at =
5406 mirror::kUseStringCompression && instruction->IsStringCharAt();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005407 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05005408 Location index_loc = locations->InAt(0);
5409 Location length_loc = locations->InAt(1);
Andreas Gampe85b62f22015-09-09 13:15:38 -07005410 SlowPathCode* slow_path =
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01005411 new (GetGraph()->GetArena()) BoundsCheckSlowPathX86(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005412
Mark Mendell99dbd682015-04-22 16:18:52 -04005413 if (length_loc.IsConstant()) {
5414 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
5415 if (index_loc.IsConstant()) {
5416 // BCE will remove the bounds check if we are guarenteed to pass.
5417 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5418 if (index < 0 || index >= length) {
5419 codegen_->AddSlowPath(slow_path);
5420 __ jmp(slow_path->GetEntryLabel());
5421 } else {
5422 // Some optimization after BCE may have generated this, and we should not
5423 // generate a bounds check if it is a valid range.
5424 }
5425 return;
5426 }
5427
5428 // We have to reverse the jump condition because the length is the constant.
5429 Register index_reg = index_loc.AsRegister<Register>();
5430 __ cmpl(index_reg, Immediate(length));
5431 codegen_->AddSlowPath(slow_path);
5432 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005433 } else {
Mark Mendellee8d9712016-07-12 11:13:15 -04005434 HInstruction* array_length = instruction->InputAt(1);
5435 if (array_length->IsEmittedAtUseSite()) {
5436 // Address the length field in the array.
5437 DCHECK(array_length->IsArrayLength());
5438 uint32_t len_offset = CodeGenerator::GetArrayLengthOffset(array_length->AsArrayLength());
5439 Location array_loc = array_length->GetLocations()->InAt(0);
5440 Address array_len(array_loc.AsRegister<Register>(), len_offset);
jessicahandojo4877b792016-09-08 19:49:13 -07005441 if (is_string_compressed_char_at) {
5442 Register length_reg = locations->GetTemp(0).AsRegister<Register>();
5443 __ movl(length_reg, array_len);
5444 codegen_->MaybeRecordImplicitNullCheck(array_length);
5445 __ andl(length_reg, Immediate(INT32_MAX));
5446 codegen_->GenerateIntCompare(length_reg, index_loc);
Mark Mendellee8d9712016-07-12 11:13:15 -04005447 } else {
jessicahandojo4877b792016-09-08 19:49:13 -07005448 // Checking bounds for general case:
5449 // Array of char or string's array with feature compression off.
5450 if (index_loc.IsConstant()) {
5451 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5452 __ cmpl(array_len, Immediate(value));
5453 } else {
5454 __ cmpl(array_len, index_loc.AsRegister<Register>());
5455 }
5456 codegen_->MaybeRecordImplicitNullCheck(array_length);
Mark Mendellee8d9712016-07-12 11:13:15 -04005457 }
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;
5994 case HLoadString::LoadKind::kDexCachePcRelative:
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)
Christina Wadsworth175d09b2016-08-31 16:26:01 -07006017 ? LocationSummary::kCallOnMainOnly
Nicolas Geoffray917d0162015-11-24 18:25:35 +00006018 : LocationSummary::kNoCall;
6019 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006020 HLoadString::LoadKind load_kind = load->GetLoadKind();
6021 if (load_kind == HLoadString::LoadKind::kDexCacheViaMethod ||
6022 load_kind == HLoadString::LoadKind::kBootImageLinkTimePcRelative ||
6023 load_kind == HLoadString::LoadKind::kDexCachePcRelative) {
6024 locations->SetInAt(0, Location::RequiresRegister());
6025 }
Christina Wadsworth175d09b2016-08-31 16:26:01 -07006026 if (load_kind == HLoadString::LoadKind::kDexCacheViaMethod) {
6027 locations->SetOut(Location::RegisterLocation(EAX));
6028 } else {
6029 locations->SetOut(Location::RequiresRegister());
6030 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006031}
6032
6033void InstructionCodeGeneratorX86::VisitLoadString(HLoadString* load) {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01006034 LocationSummary* locations = load->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006035 Location out_loc = locations->Out();
6036 Register out = out_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006037
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006038 switch (load->GetLoadKind()) {
6039 case HLoadString::LoadKind::kBootImageLinkTimeAddress: {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006040 __ movl(out, Immediate(/* placeholder */ 0));
6041 codegen_->RecordStringPatch(load);
6042 return; // No dex cache slow path.
6043 }
6044 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006045 Register method_address = locations->InAt(0).AsRegister<Register>();
6046 __ leal(out, Address(method_address, CodeGeneratorX86::kDummy32BitOffset));
6047 codegen_->RecordStringPatch(load);
6048 return; // No dex cache slow path.
6049 }
6050 case HLoadString::LoadKind::kBootImageAddress: {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006051 DCHECK_NE(load->GetAddress(), 0u);
6052 uint32_t address = dchecked_integral_cast<uint32_t>(load->GetAddress());
6053 __ movl(out, Immediate(address));
6054 codegen_->RecordSimplePatch();
6055 return; // No dex cache slow path.
6056 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006057 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07006058 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006059 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006060
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07006061 // TODO: Re-add the compiler code to do string dex cache lookup again.
Christina Wadsworth175d09b2016-08-31 16:26:01 -07006062 InvokeRuntimeCallingConvention calling_convention;
6063 __ movl(calling_convention.GetRegisterAt(0), Immediate(load->GetStringIndex()));
6064 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
6065 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006066}
6067
David Brazdilcb1c0552015-08-04 16:22:25 +01006068static Address GetExceptionTlsAddress() {
Andreas Gampe542451c2016-07-26 09:02:02 -07006069 return Address::Absolute(Thread::ExceptionOffset<kX86PointerSize>().Int32Value());
David Brazdilcb1c0552015-08-04 16:22:25 +01006070}
6071
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006072void LocationsBuilderX86::VisitLoadException(HLoadException* load) {
6073 LocationSummary* locations =
6074 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
6075 locations->SetOut(Location::RequiresRegister());
6076}
6077
6078void InstructionCodeGeneratorX86::VisitLoadException(HLoadException* load) {
David Brazdilcb1c0552015-08-04 16:22:25 +01006079 __ fs()->movl(load->GetLocations()->Out().AsRegister<Register>(), GetExceptionTlsAddress());
6080}
6081
6082void LocationsBuilderX86::VisitClearException(HClearException* clear) {
6083 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
6084}
6085
6086void InstructionCodeGeneratorX86::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
6087 __ fs()->movl(GetExceptionTlsAddress(), Immediate(0));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006088}
6089
6090void LocationsBuilderX86::VisitThrow(HThrow* instruction) {
6091 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006092 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006093 InvokeRuntimeCallingConvention calling_convention;
6094 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6095}
6096
6097void InstructionCodeGeneratorX86::VisitThrow(HThrow* instruction) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01006098 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006099 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006100}
6101
Roland Levillain7c1559a2015-12-15 10:55:36 +00006102static bool TypeCheckNeedsATemporary(TypeCheckKind type_check_kind) {
6103 return kEmitCompilerReadBarrier &&
Vladimir Marko953437b2016-08-24 08:30:46 +00006104 !kUseBakerReadBarrier &&
6105 (type_check_kind == TypeCheckKind::kAbstractClassCheck ||
Roland Levillain7c1559a2015-12-15 10:55:36 +00006106 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
6107 type_check_kind == TypeCheckKind::kArrayObjectCheck);
6108}
6109
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006110void LocationsBuilderX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006111 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain0d5a2812015-11-13 10:07:31 +00006112 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01006113 bool baker_read_barrier_slow_path = false;
Roland Levillain0d5a2812015-11-13 10:07:31 +00006114 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006115 case TypeCheckKind::kExactCheck:
6116 case TypeCheckKind::kAbstractClassCheck:
6117 case TypeCheckKind::kClassHierarchyCheck:
6118 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006119 call_kind =
6120 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Vladimir Marko70e97462016-08-09 11:04:26 +01006121 baker_read_barrier_slow_path = kUseBakerReadBarrier;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006122 break;
6123 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006124 case TypeCheckKind::kUnresolvedCheck:
6125 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006126 call_kind = LocationSummary::kCallOnSlowPath;
6127 break;
6128 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006129
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006130 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01006131 if (baker_read_barrier_slow_path) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006132 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01006133 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006134 locations->SetInAt(0, Location::RequiresRegister());
6135 locations->SetInAt(1, Location::Any());
6136 // Note that TypeCheckSlowPathX86 uses this "out" register too.
6137 locations->SetOut(Location::RequiresRegister());
6138 // When read barriers are enabled, we need a temporary register for
6139 // some cases.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006140 if (TypeCheckNeedsATemporary(type_check_kind)) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006141 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006142 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006143}
6144
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006145void InstructionCodeGeneratorX86::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006146 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006147 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006148 Location obj_loc = locations->InAt(0);
6149 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006150 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006151 Location out_loc = locations->Out();
6152 Register out = out_loc.AsRegister<Register>();
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006153 Location maybe_temp_loc = TypeCheckNeedsATemporary(type_check_kind) ?
Roland Levillain7c1559a2015-12-15 10:55:36 +00006154 locations->GetTemp(0) :
6155 Location::NoLocation();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006156 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006157 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6158 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6159 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Andreas Gampe85b62f22015-09-09 13:15:38 -07006160 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006161 NearLabel done, zero;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006162
6163 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006164 // Avoid null check if we know obj is not null.
6165 if (instruction->MustDoNullCheck()) {
6166 __ testl(obj, obj);
6167 __ j(kEqual, &zero);
6168 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006169
Roland Levillain0d5a2812015-11-13 10:07:31 +00006170 // /* HeapReference<Class> */ out = obj->klass_
Vladimir Marko953437b2016-08-24 08:30:46 +00006171 GenerateReferenceLoadTwoRegisters(instruction, out_loc, obj_loc, class_offset);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006172
Roland Levillain7c1559a2015-12-15 10:55:36 +00006173 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006174 case TypeCheckKind::kExactCheck: {
6175 if (cls.IsRegister()) {
6176 __ cmpl(out, cls.AsRegister<Register>());
6177 } else {
6178 DCHECK(cls.IsStackSlot()) << cls;
6179 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6180 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006181
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006182 // Classes must be equal for the instanceof to succeed.
6183 __ j(kNotEqual, &zero);
6184 __ movl(out, Immediate(1));
6185 __ jmp(&done);
6186 break;
6187 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006188
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006189 case TypeCheckKind::kAbstractClassCheck: {
6190 // If the class is abstract, we eagerly fetch the super class of the
6191 // object to avoid doing a comparison we know will fail.
6192 NearLabel loop;
6193 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006194 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006195 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006196 __ testl(out, out);
6197 // If `out` is null, we use it for the result, and jump to `done`.
6198 __ j(kEqual, &done);
6199 if (cls.IsRegister()) {
6200 __ cmpl(out, cls.AsRegister<Register>());
6201 } else {
6202 DCHECK(cls.IsStackSlot()) << cls;
6203 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6204 }
6205 __ j(kNotEqual, &loop);
6206 __ movl(out, Immediate(1));
6207 if (zero.IsLinked()) {
6208 __ jmp(&done);
6209 }
6210 break;
6211 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006212
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006213 case TypeCheckKind::kClassHierarchyCheck: {
6214 // Walk over the class hierarchy to find a match.
6215 NearLabel loop, success;
6216 __ Bind(&loop);
6217 if (cls.IsRegister()) {
6218 __ cmpl(out, cls.AsRegister<Register>());
6219 } else {
6220 DCHECK(cls.IsStackSlot()) << cls;
6221 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6222 }
6223 __ j(kEqual, &success);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006224 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006225 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006226 __ testl(out, out);
6227 __ j(kNotEqual, &loop);
6228 // If `out` is null, we use it for the result, and jump to `done`.
6229 __ jmp(&done);
6230 __ Bind(&success);
6231 __ movl(out, Immediate(1));
6232 if (zero.IsLinked()) {
6233 __ jmp(&done);
6234 }
6235 break;
6236 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006237
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006238 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006239 // Do an exact check.
6240 NearLabel exact_check;
6241 if (cls.IsRegister()) {
6242 __ cmpl(out, cls.AsRegister<Register>());
6243 } else {
6244 DCHECK(cls.IsStackSlot()) << cls;
6245 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6246 }
6247 __ j(kEqual, &exact_check);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006248 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006249 // /* HeapReference<Class> */ out = out->component_type_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006250 GenerateReferenceLoadOneRegister(instruction, out_loc, component_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006251 __ testl(out, out);
6252 // If `out` is null, we use it for the result, and jump to `done`.
6253 __ j(kEqual, &done);
6254 __ cmpw(Address(out, primitive_offset), Immediate(Primitive::kPrimNot));
6255 __ j(kNotEqual, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006256 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006257 __ movl(out, Immediate(1));
6258 __ jmp(&done);
6259 break;
6260 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006261
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006262 case TypeCheckKind::kArrayCheck: {
6263 if (cls.IsRegister()) {
6264 __ cmpl(out, cls.AsRegister<Register>());
6265 } else {
6266 DCHECK(cls.IsStackSlot()) << cls;
6267 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6268 }
6269 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain0d5a2812015-11-13 10:07:31 +00006270 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
6271 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006272 codegen_->AddSlowPath(slow_path);
6273 __ j(kNotEqual, slow_path->GetEntryLabel());
6274 __ movl(out, Immediate(1));
6275 if (zero.IsLinked()) {
6276 __ jmp(&done);
6277 }
6278 break;
6279 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006280
Calin Juravle98893e12015-10-02 21:05:03 +01006281 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006282 case TypeCheckKind::kInterfaceCheck: {
6283 // Note that we indeed only call on slow path, but we always go
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006284 // into the slow path for the unresolved and interface check
Roland Levillain0d5a2812015-11-13 10:07:31 +00006285 // cases.
6286 //
6287 // We cannot directly call the InstanceofNonTrivial runtime
6288 // entry point without resorting to a type checking slow path
6289 // here (i.e. by calling InvokeRuntime directly), as it would
6290 // require to assign fixed registers for the inputs of this
6291 // HInstanceOf instruction (following the runtime calling
6292 // convention), which might be cluttered by the potential first
6293 // read barrier emission at the beginning of this method.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006294 //
6295 // TODO: Introduce a new runtime entry point taking the object
6296 // to test (instead of its class) as argument, and let it deal
6297 // with the read barrier issues. This will let us refactor this
6298 // case of the `switch` code as it was previously (with a direct
6299 // call to the runtime not using a type checking slow path).
6300 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006301 DCHECK(locations->OnlyCallsOnSlowPath());
6302 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
6303 /* is_fatal */ false);
6304 codegen_->AddSlowPath(slow_path);
6305 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006306 if (zero.IsLinked()) {
6307 __ jmp(&done);
6308 }
6309 break;
6310 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006311 }
6312
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006313 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006314 __ Bind(&zero);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006315 __ xorl(out, out);
6316 }
6317
6318 if (done.IsLinked()) {
6319 __ Bind(&done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006320 }
6321
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006322 if (slow_path != nullptr) {
6323 __ Bind(slow_path->GetExitLabel());
6324 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006325}
6326
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006327void LocationsBuilderX86::VisitCheckCast(HCheckCast* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006328 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
6329 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006330 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
6331 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006332 case TypeCheckKind::kExactCheck:
6333 case TypeCheckKind::kAbstractClassCheck:
6334 case TypeCheckKind::kClassHierarchyCheck:
6335 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006336 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
6337 LocationSummary::kCallOnSlowPath :
6338 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006339 break;
6340 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006341 case TypeCheckKind::kUnresolvedCheck:
6342 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006343 call_kind = LocationSummary::kCallOnSlowPath;
6344 break;
6345 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006346 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
6347 locations->SetInAt(0, Location::RequiresRegister());
6348 locations->SetInAt(1, Location::Any());
6349 // Note that TypeCheckSlowPathX86 uses this "temp" register too.
6350 locations->AddTemp(Location::RequiresRegister());
6351 // When read barriers are enabled, we need an additional temporary
6352 // register for some cases.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006353 if (TypeCheckNeedsATemporary(type_check_kind)) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006354 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006355 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006356}
6357
6358void InstructionCodeGeneratorX86::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006359 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006360 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006361 Location obj_loc = locations->InAt(0);
6362 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006363 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006364 Location temp_loc = locations->GetTemp(0);
6365 Register temp = temp_loc.AsRegister<Register>();
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006366 Location maybe_temp2_loc = TypeCheckNeedsATemporary(type_check_kind) ?
Roland Levillain7c1559a2015-12-15 10:55:36 +00006367 locations->GetTemp(1) :
6368 Location::NoLocation();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006369 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
6370 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6371 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6372 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006373
Roland Levillain0d5a2812015-11-13 10:07:31 +00006374 bool is_type_check_slow_path_fatal =
6375 (type_check_kind == TypeCheckKind::kExactCheck ||
6376 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
6377 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
6378 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
6379 !instruction->CanThrowIntoCatchBlock();
6380 SlowPathCode* type_check_slow_path =
6381 new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
6382 is_type_check_slow_path_fatal);
6383 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006384
Roland Levillain0d5a2812015-11-13 10:07:31 +00006385 NearLabel done;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006386 // Avoid null check if we know obj is not null.
6387 if (instruction->MustDoNullCheck()) {
6388 __ testl(obj, obj);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006389 __ j(kEqual, &done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006390 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006391
Roland Levillain0d5a2812015-11-13 10:07:31 +00006392 // /* HeapReference<Class> */ temp = obj->klass_
Vladimir Marko953437b2016-08-24 08:30:46 +00006393 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006394
Roland Levillain0d5a2812015-11-13 10:07:31 +00006395 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006396 case TypeCheckKind::kExactCheck:
6397 case TypeCheckKind::kArrayCheck: {
6398 if (cls.IsRegister()) {
6399 __ cmpl(temp, cls.AsRegister<Register>());
6400 } else {
6401 DCHECK(cls.IsStackSlot()) << cls;
6402 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6403 }
6404 // Jump to slow path for throwing the exception or doing a
6405 // more involved array check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006406 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006407 break;
6408 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006409
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006410 case TypeCheckKind::kAbstractClassCheck: {
6411 // If the class is abstract, we eagerly fetch the super class of the
6412 // object to avoid doing a comparison we know will fail.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006413 NearLabel loop, compare_classes;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006414 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006415 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006416 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006417
6418 // If the class reference currently in `temp` is not null, jump
6419 // to the `compare_classes` label to compare it with the checked
6420 // class.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006421 __ testl(temp, temp);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006422 __ j(kNotEqual, &compare_classes);
6423 // Otherwise, jump to the slow path to throw the exception.
6424 //
6425 // But before, move back the object's class into `temp` before
6426 // going into the slow path, as it has been overwritten in the
6427 // meantime.
6428 // /* HeapReference<Class> */ temp = obj->klass_
Vladimir Marko953437b2016-08-24 08:30:46 +00006429 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006430 __ jmp(type_check_slow_path->GetEntryLabel());
6431
6432 __ Bind(&compare_classes);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006433 if (cls.IsRegister()) {
6434 __ cmpl(temp, cls.AsRegister<Register>());
6435 } else {
6436 DCHECK(cls.IsStackSlot()) << cls;
6437 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6438 }
6439 __ j(kNotEqual, &loop);
6440 break;
6441 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006442
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006443 case TypeCheckKind::kClassHierarchyCheck: {
6444 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006445 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006446 __ Bind(&loop);
6447 if (cls.IsRegister()) {
6448 __ cmpl(temp, cls.AsRegister<Register>());
6449 } else {
6450 DCHECK(cls.IsStackSlot()) << cls;
6451 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6452 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006453 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006454
Roland Levillain0d5a2812015-11-13 10:07:31 +00006455 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006456 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006457
6458 // If the class reference currently in `temp` is not null, jump
6459 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006460 __ testl(temp, temp);
6461 __ j(kNotEqual, &loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006462 // Otherwise, jump to the slow path to throw the exception.
6463 //
6464 // But before, move back the object's class into `temp` before
6465 // going into the slow path, as it has been overwritten in the
6466 // meantime.
6467 // /* HeapReference<Class> */ temp = obj->klass_
Vladimir Marko953437b2016-08-24 08:30:46 +00006468 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006469 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006470 break;
6471 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006472
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006473 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006474 // Do an exact check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006475 NearLabel check_non_primitive_component_type;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006476 if (cls.IsRegister()) {
6477 __ cmpl(temp, cls.AsRegister<Register>());
6478 } else {
6479 DCHECK(cls.IsStackSlot()) << cls;
6480 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6481 }
6482 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006483
6484 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006485 // /* HeapReference<Class> */ temp = temp->component_type_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006486 GenerateReferenceLoadOneRegister(instruction, temp_loc, component_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006487
6488 // If the component type is not null (i.e. the object is indeed
6489 // an array), jump to label `check_non_primitive_component_type`
6490 // to further check that this component type is not a primitive
6491 // type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006492 __ testl(temp, temp);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006493 __ j(kNotEqual, &check_non_primitive_component_type);
6494 // Otherwise, jump to the slow path to throw the exception.
6495 //
6496 // But before, move back the object's class into `temp` before
6497 // going into the slow path, as it has been overwritten in the
6498 // meantime.
6499 // /* HeapReference<Class> */ temp = obj->klass_
Vladimir Marko953437b2016-08-24 08:30:46 +00006500 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006501 __ jmp(type_check_slow_path->GetEntryLabel());
6502
6503 __ Bind(&check_non_primitive_component_type);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006504 __ cmpw(Address(temp, primitive_offset), Immediate(Primitive::kPrimNot));
Roland Levillain0d5a2812015-11-13 10:07:31 +00006505 __ j(kEqual, &done);
6506 // Same comment as above regarding `temp` and the slow path.
6507 // /* HeapReference<Class> */ temp = obj->klass_
Vladimir Marko953437b2016-08-24 08:30:46 +00006508 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006509 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006510 break;
6511 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006512
Calin Juravle98893e12015-10-02 21:05:03 +01006513 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006514 case TypeCheckKind::kInterfaceCheck:
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006515 // We always go into the type check slow path for the unresolved
6516 // and interface check cases.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006517 //
6518 // We cannot directly call the CheckCast runtime entry point
6519 // without resorting to a type checking slow path here (i.e. by
6520 // calling InvokeRuntime directly), as it would require to
6521 // assign fixed registers for the inputs of this HInstanceOf
6522 // instruction (following the runtime calling convention), which
6523 // might be cluttered by the potential first read barrier
6524 // emission at the beginning of this method.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006525 //
6526 // TODO: Introduce a new runtime entry point taking the object
6527 // to test (instead of its class) as argument, and let it deal
6528 // with the read barrier issues. This will let us refactor this
6529 // case of the `switch` code as it was previously (with a direct
6530 // call to the runtime not using a type checking slow path).
6531 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006532 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006533 break;
6534 }
6535 __ Bind(&done);
6536
Roland Levillain0d5a2812015-11-13 10:07:31 +00006537 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006538}
6539
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006540void LocationsBuilderX86::VisitMonitorOperation(HMonitorOperation* instruction) {
6541 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006542 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006543 InvokeRuntimeCallingConvention calling_convention;
6544 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6545}
6546
6547void InstructionCodeGeneratorX86::VisitMonitorOperation(HMonitorOperation* instruction) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01006548 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject
6549 : kQuickUnlockObject,
Alexandre Rames8158f282015-08-07 10:26:17 +01006550 instruction,
Serban Constantinescuba45db02016-07-12 22:53:02 +01006551 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006552 if (instruction->IsEnter()) {
6553 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
6554 } else {
6555 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
6556 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006557}
6558
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006559void LocationsBuilderX86::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
6560void LocationsBuilderX86::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
6561void LocationsBuilderX86::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
6562
6563void LocationsBuilderX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
6564 LocationSummary* locations =
6565 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6566 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
6567 || instruction->GetResultType() == Primitive::kPrimLong);
6568 locations->SetInAt(0, Location::RequiresRegister());
6569 locations->SetInAt(1, Location::Any());
6570 locations->SetOut(Location::SameAsFirstInput());
6571}
6572
6573void InstructionCodeGeneratorX86::VisitAnd(HAnd* instruction) {
6574 HandleBitwiseOperation(instruction);
6575}
6576
6577void InstructionCodeGeneratorX86::VisitOr(HOr* instruction) {
6578 HandleBitwiseOperation(instruction);
6579}
6580
6581void InstructionCodeGeneratorX86::VisitXor(HXor* instruction) {
6582 HandleBitwiseOperation(instruction);
6583}
6584
6585void InstructionCodeGeneratorX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
6586 LocationSummary* locations = instruction->GetLocations();
6587 Location first = locations->InAt(0);
6588 Location second = locations->InAt(1);
6589 DCHECK(first.Equals(locations->Out()));
6590
6591 if (instruction->GetResultType() == Primitive::kPrimInt) {
6592 if (second.IsRegister()) {
6593 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006594 __ andl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006595 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006596 __ orl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006597 } else {
6598 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006599 __ xorl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006600 }
6601 } else if (second.IsConstant()) {
6602 if (instruction->IsAnd()) {
Roland Levillain199f3362014-11-27 17:15:16 +00006603 __ andl(first.AsRegister<Register>(),
6604 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006605 } else if (instruction->IsOr()) {
Roland Levillain199f3362014-11-27 17:15:16 +00006606 __ orl(first.AsRegister<Register>(),
6607 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006608 } else {
6609 DCHECK(instruction->IsXor());
Roland Levillain199f3362014-11-27 17:15:16 +00006610 __ xorl(first.AsRegister<Register>(),
6611 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006612 }
6613 } else {
6614 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006615 __ andl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006616 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006617 __ orl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006618 } else {
6619 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006620 __ xorl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006621 }
6622 }
6623 } else {
6624 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
6625 if (second.IsRegisterPair()) {
6626 if (instruction->IsAnd()) {
6627 __ andl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
6628 __ andl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
6629 } else if (instruction->IsOr()) {
6630 __ orl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
6631 __ orl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
6632 } else {
6633 DCHECK(instruction->IsXor());
6634 __ xorl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
6635 __ xorl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
6636 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006637 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006638 if (instruction->IsAnd()) {
6639 __ andl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
6640 __ andl(first.AsRegisterPairHigh<Register>(),
6641 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
6642 } else if (instruction->IsOr()) {
6643 __ orl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
6644 __ orl(first.AsRegisterPairHigh<Register>(),
6645 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
6646 } else {
6647 DCHECK(instruction->IsXor());
6648 __ xorl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
6649 __ xorl(first.AsRegisterPairHigh<Register>(),
6650 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
6651 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006652 } else {
6653 DCHECK(second.IsConstant()) << second;
6654 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006655 int32_t low_value = Low32Bits(value);
6656 int32_t high_value = High32Bits(value);
6657 Immediate low(low_value);
6658 Immediate high(high_value);
6659 Register first_low = first.AsRegisterPairLow<Register>();
6660 Register first_high = first.AsRegisterPairHigh<Register>();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006661 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006662 if (low_value == 0) {
6663 __ xorl(first_low, first_low);
6664 } else if (low_value != -1) {
6665 __ andl(first_low, low);
6666 }
6667 if (high_value == 0) {
6668 __ xorl(first_high, first_high);
6669 } else if (high_value != -1) {
6670 __ andl(first_high, high);
6671 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006672 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006673 if (low_value != 0) {
6674 __ orl(first_low, low);
6675 }
6676 if (high_value != 0) {
6677 __ orl(first_high, high);
6678 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006679 } else {
6680 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006681 if (low_value != 0) {
6682 __ xorl(first_low, low);
6683 }
6684 if (high_value != 0) {
6685 __ xorl(first_high, high);
6686 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006687 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006688 }
6689 }
6690}
6691
Roland Levillain7c1559a2015-12-15 10:55:36 +00006692void InstructionCodeGeneratorX86::GenerateReferenceLoadOneRegister(HInstruction* instruction,
6693 Location out,
6694 uint32_t offset,
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006695 Location maybe_temp) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006696 Register out_reg = out.AsRegister<Register>();
6697 if (kEmitCompilerReadBarrier) {
6698 if (kUseBakerReadBarrier) {
6699 // Load with fast path based Baker's read barrier.
6700 // /* HeapReference<Object> */ out = *(out + offset)
6701 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00006702 instruction, out, out_reg, offset, /* needs_null_check */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006703 } else {
6704 // Load with slow path based read barrier.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006705 // Save the value of `out` into `maybe_temp` before overwriting it
Roland Levillain7c1559a2015-12-15 10:55:36 +00006706 // in the following move operation, as we will need it for the
6707 // read barrier below.
Vladimir Marko953437b2016-08-24 08:30:46 +00006708 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006709 __ movl(maybe_temp.AsRegister<Register>(), out_reg);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006710 // /* HeapReference<Object> */ out = *(out + offset)
6711 __ movl(out_reg, Address(out_reg, offset));
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006712 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006713 }
6714 } else {
6715 // Plain load with no read barrier.
6716 // /* HeapReference<Object> */ out = *(out + offset)
6717 __ movl(out_reg, Address(out_reg, offset));
6718 __ MaybeUnpoisonHeapReference(out_reg);
6719 }
6720}
6721
6722void InstructionCodeGeneratorX86::GenerateReferenceLoadTwoRegisters(HInstruction* instruction,
6723 Location out,
6724 Location obj,
Vladimir Marko953437b2016-08-24 08:30:46 +00006725 uint32_t offset) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006726 Register out_reg = out.AsRegister<Register>();
6727 Register obj_reg = obj.AsRegister<Register>();
6728 if (kEmitCompilerReadBarrier) {
6729 if (kUseBakerReadBarrier) {
6730 // Load with fast path based Baker's read barrier.
6731 // /* HeapReference<Object> */ out = *(obj + offset)
6732 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00006733 instruction, out, obj_reg, offset, /* needs_null_check */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006734 } else {
6735 // Load with slow path based read barrier.
6736 // /* HeapReference<Object> */ out = *(obj + offset)
6737 __ movl(out_reg, Address(obj_reg, offset));
6738 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
6739 }
6740 } else {
6741 // Plain load with no read barrier.
6742 // /* HeapReference<Object> */ out = *(obj + offset)
6743 __ movl(out_reg, Address(obj_reg, offset));
6744 __ MaybeUnpoisonHeapReference(out_reg);
6745 }
6746}
6747
6748void InstructionCodeGeneratorX86::GenerateGcRootFieldLoad(HInstruction* instruction,
6749 Location root,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006750 const Address& address,
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006751 Label* fixup_label,
6752 bool requires_read_barrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006753 Register root_reg = root.AsRegister<Register>();
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006754 if (requires_read_barrier) {
6755 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006756 if (kUseBakerReadBarrier) {
6757 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
6758 // Baker's read barrier are used:
6759 //
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006760 // root = *address;
Roland Levillain7c1559a2015-12-15 10:55:36 +00006761 // if (Thread::Current()->GetIsGcMarking()) {
6762 // root = ReadBarrier::Mark(root)
6763 // }
6764
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006765 // /* GcRoot<mirror::Object> */ root = *address
6766 __ movl(root_reg, address);
6767 if (fixup_label != nullptr) {
6768 __ Bind(fixup_label);
6769 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00006770 static_assert(
6771 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
6772 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
6773 "have different sizes.");
6774 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
6775 "art::mirror::CompressedReference<mirror::Object> and int32_t "
6776 "have different sizes.");
6777
Vladimir Marko953437b2016-08-24 08:30:46 +00006778 // Slow path marking the GC root `root`.
6779 SlowPathCode* slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathX86(
6780 instruction, root, /* unpoison */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006781 codegen_->AddSlowPath(slow_path);
6782
Andreas Gampe542451c2016-07-26 09:02:02 -07006783 __ fs()->cmpl(Address::Absolute(Thread::IsGcMarkingOffset<kX86PointerSize>().Int32Value()),
Roland Levillain7c1559a2015-12-15 10:55:36 +00006784 Immediate(0));
6785 __ j(kNotEqual, slow_path->GetEntryLabel());
6786 __ Bind(slow_path->GetExitLabel());
6787 } else {
6788 // GC root loaded through a slow path for read barriers other
6789 // than Baker's.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006790 // /* GcRoot<mirror::Object>* */ root = address
6791 __ leal(root_reg, address);
6792 if (fixup_label != nullptr) {
6793 __ Bind(fixup_label);
6794 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00006795 // /* mirror::Object* */ root = root->Read()
6796 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
6797 }
6798 } else {
6799 // Plain GC root load with no read barrier.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006800 // /* GcRoot<mirror::Object> */ root = *address
6801 __ movl(root_reg, address);
6802 if (fixup_label != nullptr) {
6803 __ Bind(fixup_label);
6804 }
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006805 // Note that GC roots are not affected by heap poisoning, thus we
6806 // do not have to unpoison `root_reg` here.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006807 }
6808}
6809
6810void CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
6811 Location ref,
6812 Register obj,
6813 uint32_t offset,
Roland Levillain7c1559a2015-12-15 10:55:36 +00006814 bool needs_null_check) {
6815 DCHECK(kEmitCompilerReadBarrier);
6816 DCHECK(kUseBakerReadBarrier);
6817
6818 // /* HeapReference<Object> */ ref = *(obj + offset)
6819 Address src(obj, offset);
Vladimir Marko953437b2016-08-24 08:30:46 +00006820 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, needs_null_check);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006821}
6822
6823void CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
6824 Location ref,
6825 Register obj,
6826 uint32_t data_offset,
6827 Location index,
Roland Levillain7c1559a2015-12-15 10:55:36 +00006828 bool needs_null_check) {
6829 DCHECK(kEmitCompilerReadBarrier);
6830 DCHECK(kUseBakerReadBarrier);
6831
Roland Levillain3d312422016-06-23 13:53:42 +01006832 static_assert(
6833 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
6834 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain7c1559a2015-12-15 10:55:36 +00006835 // /* HeapReference<Object> */ ref =
6836 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006837 Address src = CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset);
Vladimir Marko953437b2016-08-24 08:30:46 +00006838 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, needs_null_check);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006839}
6840
6841void CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
6842 Location ref,
6843 Register obj,
6844 const Address& src,
Roland Levillain7c1559a2015-12-15 10:55:36 +00006845 bool needs_null_check) {
6846 DCHECK(kEmitCompilerReadBarrier);
6847 DCHECK(kUseBakerReadBarrier);
6848
6849 // In slow path based read barriers, the read barrier call is
6850 // inserted after the original load. However, in fast path based
6851 // Baker's read barriers, we need to perform the load of
6852 // mirror::Object::monitor_ *before* the original reference load.
6853 // This load-load ordering is required by the read barrier.
6854 // The fast path/slow path (for Baker's algorithm) should look like:
6855 //
6856 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
6857 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
6858 // HeapReference<Object> ref = *src; // Original reference load.
6859 // bool is_gray = (rb_state == ReadBarrier::gray_ptr_);
6860 // if (is_gray) {
6861 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
6862 // }
6863 //
6864 // Note: the original implementation in ReadBarrier::Barrier is
6865 // slightly more complex as:
6866 // - it implements the load-load fence using a data dependency on
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006867 // the high-bits of rb_state, which are expected to be all zeroes
6868 // (we use CodeGeneratorX86::GenerateMemoryBarrier instead here,
6869 // which is a no-op thanks to the x86 memory model);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006870 // - it performs additional checks that we do not do here for
6871 // performance reasons.
6872
6873 Register ref_reg = ref.AsRegister<Register>();
Roland Levillain7c1559a2015-12-15 10:55:36 +00006874 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
6875
Vladimir Marko953437b2016-08-24 08:30:46 +00006876 // Given the numeric representation, it's enough to check the low bit of the rb_state.
6877 static_assert(ReadBarrier::white_ptr_ == 0, "Expecting white to have value 0");
6878 static_assert(ReadBarrier::gray_ptr_ == 1, "Expecting gray to have value 1");
6879 static_assert(ReadBarrier::black_ptr_ == 2, "Expecting black to have value 2");
6880 constexpr uint32_t gray_byte_position = LockWord::kReadBarrierStateShift / kBitsPerByte;
6881 constexpr uint32_t gray_bit_position = LockWord::kReadBarrierStateShift % kBitsPerByte;
6882 constexpr int32_t test_value = static_cast<int8_t>(1 << gray_bit_position);
6883
6884 // if (rb_state == ReadBarrier::gray_ptr_)
6885 // ref = ReadBarrier::Mark(ref);
6886 // At this point, just do the "if" and make sure that flags are preserved until the branch.
6887 __ testb(Address(obj, monitor_offset + gray_byte_position), Immediate(test_value));
Roland Levillain7c1559a2015-12-15 10:55:36 +00006888 if (needs_null_check) {
6889 MaybeRecordImplicitNullCheck(instruction);
6890 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00006891
6892 // Load fence to prevent load-load reordering.
6893 // Note that this is a no-op, thanks to the x86 memory model.
6894 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
6895
6896 // The actual reference load.
6897 // /* HeapReference<Object> */ ref = *src
Vladimir Marko953437b2016-08-24 08:30:46 +00006898 __ movl(ref_reg, src); // Flags are unaffected.
6899
6900 // Note: Reference unpoisoning modifies the flags, so we need to delay it after the branch.
6901 // Slow path marking the object `ref` when it is gray.
6902 SlowPathCode* slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathX86(
6903 instruction, ref, /* unpoison */ true);
6904 AddSlowPath(slow_path);
6905
6906 // We have done the "if" of the gray bit check above, now branch based on the flags.
6907 __ j(kNotZero, slow_path->GetEntryLabel());
Roland Levillain7c1559a2015-12-15 10:55:36 +00006908
6909 // Object* ref = ref_addr->AsMirrorPtr()
6910 __ MaybeUnpoisonHeapReference(ref_reg);
6911
Roland Levillain7c1559a2015-12-15 10:55:36 +00006912 __ Bind(slow_path->GetExitLabel());
6913}
6914
6915void CodeGeneratorX86::GenerateReadBarrierSlow(HInstruction* instruction,
6916 Location out,
6917 Location ref,
6918 Location obj,
6919 uint32_t offset,
6920 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006921 DCHECK(kEmitCompilerReadBarrier);
6922
Roland Levillain7c1559a2015-12-15 10:55:36 +00006923 // Insert a slow path based read barrier *after* the reference load.
6924 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00006925 // If heap poisoning is enabled, the unpoisoning of the loaded
6926 // reference will be carried out by the runtime within the slow
6927 // path.
6928 //
6929 // Note that `ref` currently does not get unpoisoned (when heap
6930 // poisoning is enabled), which is alright as the `ref` argument is
6931 // not used by the artReadBarrierSlow entry point.
6932 //
6933 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
6934 SlowPathCode* slow_path = new (GetGraph()->GetArena())
6935 ReadBarrierForHeapReferenceSlowPathX86(instruction, out, ref, obj, offset, index);
6936 AddSlowPath(slow_path);
6937
Roland Levillain0d5a2812015-11-13 10:07:31 +00006938 __ jmp(slow_path->GetEntryLabel());
6939 __ Bind(slow_path->GetExitLabel());
6940}
6941
Roland Levillain7c1559a2015-12-15 10:55:36 +00006942void CodeGeneratorX86::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
6943 Location out,
6944 Location ref,
6945 Location obj,
6946 uint32_t offset,
6947 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006948 if (kEmitCompilerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006949 // Baker's read barriers shall be handled by the fast path
6950 // (CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier).
6951 DCHECK(!kUseBakerReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006952 // If heap poisoning is enabled, unpoisoning will be taken care of
6953 // by the runtime within the slow path.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006954 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006955 } else if (kPoisonHeapReferences) {
6956 __ UnpoisonHeapReference(out.AsRegister<Register>());
6957 }
6958}
6959
Roland Levillain7c1559a2015-12-15 10:55:36 +00006960void CodeGeneratorX86::GenerateReadBarrierForRootSlow(HInstruction* instruction,
6961 Location out,
6962 Location root) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006963 DCHECK(kEmitCompilerReadBarrier);
6964
Roland Levillain7c1559a2015-12-15 10:55:36 +00006965 // Insert a slow path based read barrier *after* the GC root load.
6966 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00006967 // Note that GC roots are not affected by heap poisoning, so we do
6968 // not need to do anything special for this here.
6969 SlowPathCode* slow_path =
6970 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathX86(instruction, out, root);
6971 AddSlowPath(slow_path);
6972
Roland Levillain0d5a2812015-11-13 10:07:31 +00006973 __ jmp(slow_path->GetEntryLabel());
6974 __ Bind(slow_path->GetExitLabel());
6975}
6976
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006977void LocationsBuilderX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00006978 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00006979 LOG(FATAL) << "Unreachable";
6980}
6981
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006982void InstructionCodeGeneratorX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00006983 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00006984 LOG(FATAL) << "Unreachable";
6985}
6986
Mark Mendellfe57faa2015-09-18 09:26:15 -04006987// Simple implementation of packed switch - generate cascaded compare/jumps.
6988void LocationsBuilderX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6989 LocationSummary* locations =
6990 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
6991 locations->SetInAt(0, Location::RequiresRegister());
6992}
6993
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006994void InstructionCodeGeneratorX86::GenPackedSwitchWithCompares(Register value_reg,
6995 int32_t lower_bound,
6996 uint32_t num_entries,
6997 HBasicBlock* switch_block,
6998 HBasicBlock* default_block) {
6999 // Figure out the correct compare values and jump conditions.
7000 // Handle the first compare/branch as a special case because it might
7001 // jump to the default case.
7002 DCHECK_GT(num_entries, 2u);
7003 Condition first_condition;
7004 uint32_t index;
7005 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
7006 if (lower_bound != 0) {
7007 first_condition = kLess;
7008 __ cmpl(value_reg, Immediate(lower_bound));
7009 __ j(first_condition, codegen_->GetLabelOf(default_block));
7010 __ j(kEqual, codegen_->GetLabelOf(successors[0]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007011
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007012 index = 1;
7013 } else {
7014 // Handle all the compare/jumps below.
7015 first_condition = kBelow;
7016 index = 0;
7017 }
7018
7019 // Handle the rest of the compare/jumps.
7020 for (; index + 1 < num_entries; index += 2) {
7021 int32_t compare_to_value = lower_bound + index + 1;
7022 __ cmpl(value_reg, Immediate(compare_to_value));
7023 // Jump to successors[index] if value < case_value[index].
7024 __ j(first_condition, codegen_->GetLabelOf(successors[index]));
7025 // Jump to successors[index + 1] if value == case_value[index + 1].
7026 __ j(kEqual, codegen_->GetLabelOf(successors[index + 1]));
7027 }
7028
7029 if (index != num_entries) {
7030 // There are an odd number of entries. Handle the last one.
7031 DCHECK_EQ(index + 1, num_entries);
7032 __ cmpl(value_reg, Immediate(lower_bound + index));
7033 __ j(kEqual, codegen_->GetLabelOf(successors[index]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007034 }
7035
7036 // And the default for any other value.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007037 if (!codegen_->GoesToNextBlock(switch_block, default_block)) {
7038 __ jmp(codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007039 }
7040}
7041
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007042void InstructionCodeGeneratorX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7043 int32_t lower_bound = switch_instr->GetStartValue();
7044 uint32_t num_entries = switch_instr->GetNumEntries();
7045 LocationSummary* locations = switch_instr->GetLocations();
7046 Register value_reg = locations->InAt(0).AsRegister<Register>();
7047
7048 GenPackedSwitchWithCompares(value_reg,
7049 lower_bound,
7050 num_entries,
7051 switch_instr->GetBlock(),
7052 switch_instr->GetDefaultBlock());
7053}
7054
Mark Mendell805b3b52015-09-18 14:10:29 -04007055void LocationsBuilderX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
7056 LocationSummary* locations =
7057 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
7058 locations->SetInAt(0, Location::RequiresRegister());
7059
7060 // Constant area pointer.
7061 locations->SetInAt(1, Location::RequiresRegister());
7062
7063 // And the temporary we need.
7064 locations->AddTemp(Location::RequiresRegister());
7065}
7066
7067void InstructionCodeGeneratorX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
7068 int32_t lower_bound = switch_instr->GetStartValue();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007069 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendell805b3b52015-09-18 14:10:29 -04007070 LocationSummary* locations = switch_instr->GetLocations();
7071 Register value_reg = locations->InAt(0).AsRegister<Register>();
7072 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
7073
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007074 if (num_entries <= kPackedSwitchJumpTableThreshold) {
7075 GenPackedSwitchWithCompares(value_reg,
7076 lower_bound,
7077 num_entries,
7078 switch_instr->GetBlock(),
7079 default_block);
7080 return;
7081 }
7082
Mark Mendell805b3b52015-09-18 14:10:29 -04007083 // Optimizing has a jump area.
7084 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
7085 Register constant_area = locations->InAt(1).AsRegister<Register>();
7086
7087 // Remove the bias, if needed.
7088 if (lower_bound != 0) {
7089 __ leal(temp_reg, Address(value_reg, -lower_bound));
7090 value_reg = temp_reg;
7091 }
7092
7093 // Is the value in range?
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007094 DCHECK_GE(num_entries, 1u);
Mark Mendell805b3b52015-09-18 14:10:29 -04007095 __ cmpl(value_reg, Immediate(num_entries - 1));
7096 __ j(kAbove, codegen_->GetLabelOf(default_block));
7097
7098 // We are in the range of the table.
7099 // Load (target-constant_area) from the jump table, indexing by the value.
7100 __ movl(temp_reg, codegen_->LiteralCaseTable(switch_instr, constant_area, value_reg));
7101
7102 // Compute the actual target address by adding in constant_area.
7103 __ addl(temp_reg, constant_area);
7104
7105 // And jump.
7106 __ jmp(temp_reg);
7107}
7108
Mark Mendell0616ae02015-04-17 12:49:27 -04007109void LocationsBuilderX86::VisitX86ComputeBaseMethodAddress(
7110 HX86ComputeBaseMethodAddress* insn) {
7111 LocationSummary* locations =
7112 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
7113 locations->SetOut(Location::RequiresRegister());
7114}
7115
7116void InstructionCodeGeneratorX86::VisitX86ComputeBaseMethodAddress(
7117 HX86ComputeBaseMethodAddress* insn) {
7118 LocationSummary* locations = insn->GetLocations();
7119 Register reg = locations->Out().AsRegister<Register>();
7120
7121 // Generate call to next instruction.
7122 Label next_instruction;
7123 __ call(&next_instruction);
7124 __ Bind(&next_instruction);
7125
7126 // Remember this offset for later use with constant area.
7127 codegen_->SetMethodAddressOffset(GetAssembler()->CodeSize());
7128
7129 // Grab the return address off the stack.
7130 __ popl(reg);
7131}
7132
7133void LocationsBuilderX86::VisitX86LoadFromConstantTable(
7134 HX86LoadFromConstantTable* insn) {
7135 LocationSummary* locations =
7136 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
7137
7138 locations->SetInAt(0, Location::RequiresRegister());
7139 locations->SetInAt(1, Location::ConstantLocation(insn->GetConstant()));
7140
7141 // If we don't need to be materialized, we only need the inputs to be set.
David Brazdilb3e773e2016-01-26 11:28:37 +00007142 if (insn->IsEmittedAtUseSite()) {
Mark Mendell0616ae02015-04-17 12:49:27 -04007143 return;
7144 }
7145
7146 switch (insn->GetType()) {
7147 case Primitive::kPrimFloat:
7148 case Primitive::kPrimDouble:
7149 locations->SetOut(Location::RequiresFpuRegister());
7150 break;
7151
7152 case Primitive::kPrimInt:
7153 locations->SetOut(Location::RequiresRegister());
7154 break;
7155
7156 default:
7157 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
7158 }
7159}
7160
7161void InstructionCodeGeneratorX86::VisitX86LoadFromConstantTable(HX86LoadFromConstantTable* insn) {
David Brazdilb3e773e2016-01-26 11:28:37 +00007162 if (insn->IsEmittedAtUseSite()) {
Mark Mendell0616ae02015-04-17 12:49:27 -04007163 return;
7164 }
7165
7166 LocationSummary* locations = insn->GetLocations();
7167 Location out = locations->Out();
7168 Register const_area = locations->InAt(0).AsRegister<Register>();
7169 HConstant *value = insn->GetConstant();
7170
7171 switch (insn->GetType()) {
7172 case Primitive::kPrimFloat:
7173 __ movss(out.AsFpuRegister<XmmRegister>(),
7174 codegen_->LiteralFloatAddress(value->AsFloatConstant()->GetValue(), const_area));
7175 break;
7176
7177 case Primitive::kPrimDouble:
7178 __ movsd(out.AsFpuRegister<XmmRegister>(),
7179 codegen_->LiteralDoubleAddress(value->AsDoubleConstant()->GetValue(), const_area));
7180 break;
7181
7182 case Primitive::kPrimInt:
7183 __ movl(out.AsRegister<Register>(),
7184 codegen_->LiteralInt32Address(value->AsIntConstant()->GetValue(), const_area));
7185 break;
7186
7187 default:
7188 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
7189 }
7190}
7191
Mark Mendell0616ae02015-04-17 12:49:27 -04007192/**
7193 * Class to handle late fixup of offsets into constant area.
7194 */
Vladimir Marko5233f932015-09-29 19:01:15 +01007195class RIPFixup : public AssemblerFixup, public ArenaObject<kArenaAllocCodeGenerator> {
Mark Mendell0616ae02015-04-17 12:49:27 -04007196 public:
Mark Mendell805b3b52015-09-18 14:10:29 -04007197 RIPFixup(CodeGeneratorX86& codegen, size_t offset)
7198 : codegen_(&codegen), offset_into_constant_area_(offset) {}
7199
7200 protected:
7201 void SetOffset(size_t offset) { offset_into_constant_area_ = offset; }
7202
7203 CodeGeneratorX86* codegen_;
Mark Mendell0616ae02015-04-17 12:49:27 -04007204
7205 private:
7206 void Process(const MemoryRegion& region, int pos) OVERRIDE {
7207 // Patch the correct offset for the instruction. The place to patch is the
7208 // last 4 bytes of the instruction.
7209 // The value to patch is the distance from the offset in the constant area
7210 // from the address computed by the HX86ComputeBaseMethodAddress instruction.
Mark Mendell805b3b52015-09-18 14:10:29 -04007211 int32_t constant_offset = codegen_->ConstantAreaStart() + offset_into_constant_area_;
7212 int32_t relative_position = constant_offset - codegen_->GetMethodAddressOffset();;
Mark Mendell0616ae02015-04-17 12:49:27 -04007213
7214 // Patch in the right value.
7215 region.StoreUnaligned<int32_t>(pos - 4, relative_position);
7216 }
7217
Mark Mendell0616ae02015-04-17 12:49:27 -04007218 // Location in constant area that the fixup refers to.
Mark Mendell805b3b52015-09-18 14:10:29 -04007219 int32_t offset_into_constant_area_;
Mark Mendell0616ae02015-04-17 12:49:27 -04007220};
7221
Mark Mendell805b3b52015-09-18 14:10:29 -04007222/**
7223 * Class to handle late fixup of offsets to a jump table that will be created in the
7224 * constant area.
7225 */
7226class JumpTableRIPFixup : public RIPFixup {
7227 public:
7228 JumpTableRIPFixup(CodeGeneratorX86& codegen, HX86PackedSwitch* switch_instr)
7229 : RIPFixup(codegen, static_cast<size_t>(-1)), switch_instr_(switch_instr) {}
7230
7231 void CreateJumpTable() {
7232 X86Assembler* assembler = codegen_->GetAssembler();
7233
7234 // Ensure that the reference to the jump table has the correct offset.
7235 const int32_t offset_in_constant_table = assembler->ConstantAreaSize();
7236 SetOffset(offset_in_constant_table);
7237
7238 // The label values in the jump table are computed relative to the
7239 // instruction addressing the constant area.
7240 const int32_t relative_offset = codegen_->GetMethodAddressOffset();
7241
7242 // Populate the jump table with the correct values for the jump table.
7243 int32_t num_entries = switch_instr_->GetNumEntries();
7244 HBasicBlock* block = switch_instr_->GetBlock();
7245 const ArenaVector<HBasicBlock*>& successors = block->GetSuccessors();
7246 // The value that we want is the target offset - the position of the table.
7247 for (int32_t i = 0; i < num_entries; i++) {
7248 HBasicBlock* b = successors[i];
7249 Label* l = codegen_->GetLabelOf(b);
7250 DCHECK(l->IsBound());
7251 int32_t offset_to_block = l->Position() - relative_offset;
7252 assembler->AppendInt32(offset_to_block);
7253 }
7254 }
7255
7256 private:
7257 const HX86PackedSwitch* switch_instr_;
7258};
7259
7260void CodeGeneratorX86::Finalize(CodeAllocator* allocator) {
7261 // Generate the constant area if needed.
7262 X86Assembler* assembler = GetAssembler();
7263 if (!assembler->IsConstantAreaEmpty() || !fixups_to_jump_tables_.empty()) {
7264 // Align to 4 byte boundary to reduce cache misses, as the data is 4 and 8
7265 // byte values.
7266 assembler->Align(4, 0);
7267 constant_area_start_ = assembler->CodeSize();
7268
7269 // Populate any jump tables.
7270 for (auto jump_table : fixups_to_jump_tables_) {
7271 jump_table->CreateJumpTable();
7272 }
7273
7274 // And now add the constant area to the generated code.
7275 assembler->AddConstantArea();
7276 }
7277
7278 // And finish up.
7279 CodeGenerator::Finalize(allocator);
7280}
7281
Mark Mendell0616ae02015-04-17 12:49:27 -04007282Address CodeGeneratorX86::LiteralDoubleAddress(double v, Register reg) {
7283 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddDouble(v));
7284 return Address(reg, kDummy32BitOffset, fixup);
7285}
7286
7287Address CodeGeneratorX86::LiteralFloatAddress(float v, Register reg) {
7288 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddFloat(v));
7289 return Address(reg, kDummy32BitOffset, fixup);
7290}
7291
7292Address CodeGeneratorX86::LiteralInt32Address(int32_t v, Register reg) {
7293 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt32(v));
7294 return Address(reg, kDummy32BitOffset, fixup);
7295}
7296
7297Address CodeGeneratorX86::LiteralInt64Address(int64_t v, Register reg) {
7298 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt64(v));
7299 return Address(reg, kDummy32BitOffset, fixup);
7300}
7301
Aart Bika19616e2016-02-01 18:57:58 -08007302void CodeGeneratorX86::Load32BitValue(Register dest, int32_t value) {
7303 if (value == 0) {
7304 __ xorl(dest, dest);
7305 } else {
7306 __ movl(dest, Immediate(value));
7307 }
7308}
7309
7310void CodeGeneratorX86::Compare32BitValue(Register dest, int32_t value) {
7311 if (value == 0) {
7312 __ testl(dest, dest);
7313 } else {
7314 __ cmpl(dest, Immediate(value));
7315 }
7316}
7317
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007318void CodeGeneratorX86::GenerateIntCompare(Location lhs, Location rhs) {
7319 Register lhs_reg = lhs.AsRegister<Register>();
jessicahandojo4877b792016-09-08 19:49:13 -07007320 GenerateIntCompare(lhs_reg, rhs);
7321}
7322
7323void CodeGeneratorX86::GenerateIntCompare(Register lhs, Location rhs) {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007324 if (rhs.IsConstant()) {
7325 int32_t value = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
jessicahandojo4877b792016-09-08 19:49:13 -07007326 Compare32BitValue(lhs, value);
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007327 } else if (rhs.IsStackSlot()) {
jessicahandojo4877b792016-09-08 19:49:13 -07007328 __ cmpl(lhs, Address(ESP, rhs.GetStackIndex()));
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007329 } else {
jessicahandojo4877b792016-09-08 19:49:13 -07007330 __ cmpl(lhs, rhs.AsRegister<Register>());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007331 }
7332}
7333
7334Address CodeGeneratorX86::ArrayAddress(Register obj,
7335 Location index,
7336 ScaleFactor scale,
7337 uint32_t data_offset) {
7338 return index.IsConstant() ?
7339 Address(obj, (index.GetConstant()->AsIntConstant()->GetValue() << scale) + data_offset) :
7340 Address(obj, index.AsRegister<Register>(), scale, data_offset);
7341}
7342
Mark Mendell805b3b52015-09-18 14:10:29 -04007343Address CodeGeneratorX86::LiteralCaseTable(HX86PackedSwitch* switch_instr,
7344 Register reg,
7345 Register value) {
7346 // Create a fixup to be used to create and address the jump table.
7347 JumpTableRIPFixup* table_fixup =
7348 new (GetGraph()->GetArena()) JumpTableRIPFixup(*this, switch_instr);
7349
7350 // We have to populate the jump tables.
7351 fixups_to_jump_tables_.push_back(table_fixup);
7352
7353 // We want a scaled address, as we are extracting the correct offset from the table.
7354 return Address(reg, value, TIMES_4, kDummy32BitOffset, table_fixup);
7355}
7356
Andreas Gampe85b62f22015-09-09 13:15:38 -07007357// TODO: target as memory.
7358void CodeGeneratorX86::MoveFromReturnRegister(Location target, Primitive::Type type) {
7359 if (!target.IsValid()) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007360 DCHECK_EQ(type, Primitive::kPrimVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07007361 return;
7362 }
7363
7364 DCHECK_NE(type, Primitive::kPrimVoid);
7365
7366 Location return_loc = InvokeDexCallingConventionVisitorX86().GetReturnLocation(type);
7367 if (target.Equals(return_loc)) {
7368 return;
7369 }
7370
7371 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
7372 // with the else branch.
7373 if (type == Primitive::kPrimLong) {
7374 HParallelMove parallel_move(GetGraph()->GetArena());
7375 parallel_move.AddMove(return_loc.ToLow(), target.ToLow(), Primitive::kPrimInt, nullptr);
7376 parallel_move.AddMove(return_loc.ToHigh(), target.ToHigh(), Primitive::kPrimInt, nullptr);
7377 GetMoveResolver()->EmitNativeCode(&parallel_move);
7378 } else {
7379 // Let the parallel move resolver take care of all of this.
7380 HParallelMove parallel_move(GetGraph()->GetArena());
7381 parallel_move.AddMove(return_loc, target, type, nullptr);
7382 GetMoveResolver()->EmitNativeCode(&parallel_move);
7383 }
7384}
7385
Roland Levillain4d027112015-07-01 15:41:14 +01007386#undef __
7387
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00007388} // namespace x86
7389} // namespace art