blob: 425f31c3b292b239c0fee26a255d6606fb699d96 [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
Chih-Hung Hsiehfba39972016-05-11 11:26:48 -070050// NOLINT on __ macro to suppress wrong warning/fix from clang-tidy.
51#define __ down_cast<X86Assembler*>(codegen->GetAssembler())-> // NOLINT
Calin Juravle175dc732015-08-25 15:42:32 +010052#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kX86WordSize, 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 }
Alexandre Rames8158f282015-08-07 10:26:17 +010065 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pThrowNullPointer),
66 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());
David Brazdil77a48ae2015-09-15 12:34:04 +000087 if (instruction_->CanThrowIntoCatchBlock()) {
88 // Live registers will be restored in the catch block if caught.
89 SaveLiveRegisters(codegen, instruction_->GetLocations());
90 }
Alexandre Rames8158f282015-08-07 10:26:17 +010091 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pThrowDivZero),
92 instruction_,
93 instruction_->GetDexPc(),
94 this);
Roland Levillain888d0672015-11-23 18:53:50 +000095 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
Calin Juravled0d48522014-11-04 16:40:20 +000096 }
97
Alexandre Rames8158f282015-08-07 10:26:17 +010098 bool IsFatal() const OVERRIDE { return true; }
99
Alexandre Rames9931f312015-06-19 14:47:01 +0100100 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathX86"; }
101
Calin Juravled0d48522014-11-04 16:40:20 +0000102 private:
Calin Juravled0d48522014-11-04 16:40:20 +0000103 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathX86);
104};
105
Andreas Gampe85b62f22015-09-09 13:15:38 -0700106class DivRemMinusOneSlowPathX86 : public SlowPathCode {
Calin Juravled0d48522014-11-04 16:40:20 +0000107 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000108 DivRemMinusOneSlowPathX86(HInstruction* instruction, Register reg, bool is_div)
109 : SlowPathCode(instruction), reg_(reg), is_div_(is_div) {}
Calin Juravled0d48522014-11-04 16:40:20 +0000110
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000111 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +0000112 __ Bind(GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +0000113 if (is_div_) {
114 __ negl(reg_);
115 } else {
116 __ movl(reg_, Immediate(0));
117 }
Calin Juravled0d48522014-11-04 16:40:20 +0000118 __ jmp(GetExitLabel());
119 }
120
Alexandre Rames9931f312015-06-19 14:47:01 +0100121 const char* GetDescription() const OVERRIDE { return "DivRemMinusOneSlowPathX86"; }
122
Calin Juravled0d48522014-11-04 16:40:20 +0000123 private:
124 Register reg_;
Calin Juravlebacfec32014-11-14 15:54:36 +0000125 bool is_div_;
126 DISALLOW_COPY_AND_ASSIGN(DivRemMinusOneSlowPathX86);
Calin Juravled0d48522014-11-04 16:40:20 +0000127};
128
Andreas Gampe85b62f22015-09-09 13:15:38 -0700129class BoundsCheckSlowPathX86 : public SlowPathCode {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100130 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000131 explicit BoundsCheckSlowPathX86(HBoundsCheck* instruction) : SlowPathCode(instruction) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100132
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000133 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100134 LocationSummary* locations = instruction_->GetLocations();
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100135 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100136 __ Bind(GetEntryLabel());
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000137 // We're moving two locations to locations that could overlap, so we need a parallel
138 // move resolver.
David Brazdil77a48ae2015-09-15 12:34:04 +0000139 if (instruction_->CanThrowIntoCatchBlock()) {
140 // Live registers will be restored in the catch block if caught.
141 SaveLiveRegisters(codegen, instruction_->GetLocations());
142 }
Mark Mendellee8d9712016-07-12 11:13:15 -0400143
144 // Are we using an array length from memory?
145 HInstruction* array_length = instruction_->InputAt(1);
146 Location length_loc = locations->InAt(1);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100147 InvokeRuntimeCallingConvention calling_convention;
Mark Mendellee8d9712016-07-12 11:13:15 -0400148 if (array_length->IsArrayLength() && array_length->IsEmittedAtUseSite()) {
149 // Load the array length into our temporary.
150 uint32_t len_offset = CodeGenerator::GetArrayLengthOffset(array_length->AsArrayLength());
151 Location array_loc = array_length->GetLocations()->InAt(0);
152 Address array_len(array_loc.AsRegister<Register>(), len_offset);
153 length_loc = Location::RegisterLocation(calling_convention.GetRegisterAt(1));
154 // Check for conflicts with index.
155 if (length_loc.Equals(locations->InAt(0))) {
156 // We know we aren't using parameter 2.
157 length_loc = Location::RegisterLocation(calling_convention.GetRegisterAt(2));
158 }
159 __ movl(length_loc.AsRegister<Register>(), array_len);
160 }
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000161 x86_codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100162 locations->InAt(0),
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000163 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100164 Primitive::kPrimInt,
Mark Mendellee8d9712016-07-12 11:13:15 -0400165 length_loc,
Nicolas Geoffray90218252015-04-15 11:56:51 +0100166 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
167 Primitive::kPrimInt);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100168 uint32_t entry_point_offset = instruction_->AsBoundsCheck()->IsStringCharAt()
169 ? QUICK_ENTRY_POINT(pThrowStringBounds)
170 : QUICK_ENTRY_POINT(pThrowArrayBounds);
171 x86_codegen->InvokeRuntime(entry_point_offset,
Alexandre Rames8158f282015-08-07 10:26:17 +0100172 instruction_,
173 instruction_->GetDexPc(),
174 this);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100175 CheckEntrypointTypes<kQuickThrowStringBounds, void, int32_t, int32_t>();
Roland Levillain888d0672015-11-23 18:53:50 +0000176 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100177 }
178
Alexandre Rames8158f282015-08-07 10:26:17 +0100179 bool IsFatal() const OVERRIDE { return true; }
180
Alexandre Rames9931f312015-06-19 14:47:01 +0100181 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathX86"; }
182
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100183 private:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100184 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathX86);
185};
186
Andreas Gampe85b62f22015-09-09 13:15:38 -0700187class SuspendCheckSlowPathX86 : public SlowPathCode {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000188 public:
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000189 SuspendCheckSlowPathX86(HSuspendCheck* instruction, HBasicBlock* successor)
David Srbecky9cd6d372016-02-09 15:24:47 +0000190 : SlowPathCode(instruction), successor_(successor) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000191
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000192 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100193 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000194 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000195 SaveLiveRegisters(codegen, instruction_->GetLocations());
Alexandre Rames8158f282015-08-07 10:26:17 +0100196 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pTestSuspend),
197 instruction_,
198 instruction_->GetDexPc(),
199 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000200 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000201 RestoreLiveRegisters(codegen, instruction_->GetLocations());
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100202 if (successor_ == nullptr) {
203 __ jmp(GetReturnLabel());
204 } else {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100205 __ jmp(x86_codegen->GetLabelOf(successor_));
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100206 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000207 }
208
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100209 Label* GetReturnLabel() {
210 DCHECK(successor_ == nullptr);
211 return &return_label_;
212 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000213
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100214 HBasicBlock* GetSuccessor() const {
215 return successor_;
216 }
217
Alexandre Rames9931f312015-06-19 14:47:01 +0100218 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathX86"; }
219
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000220 private:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100221 HBasicBlock* const successor_;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000222 Label return_label_;
223
224 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathX86);
225};
226
Andreas Gampe85b62f22015-09-09 13:15:38 -0700227class LoadStringSlowPathX86 : public SlowPathCode {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000228 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000229 explicit LoadStringSlowPathX86(HLoadString* instruction): SlowPathCode(instruction) {}
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000230
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000231 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000232 LocationSummary* locations = instruction_->GetLocations();
233 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
234
235 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
236 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000237 SaveLiveRegisters(codegen, locations);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000238
239 InvokeRuntimeCallingConvention calling_convention;
David Srbecky9cd6d372016-02-09 15:24:47 +0000240 const uint32_t string_index = instruction_->AsLoadString()->GetStringIndex();
241 __ movl(calling_convention.GetRegisterAt(0), Immediate(string_index));
Alexandre Rames8158f282015-08-07 10:26:17 +0100242 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pResolveString),
243 instruction_,
244 instruction_->GetDexPc(),
245 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000246 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000247 x86_codegen->Move32(locations->Out(), Location::RegisterLocation(EAX));
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000248 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000249
250 __ jmp(GetExitLabel());
251 }
252
Alexandre Rames9931f312015-06-19 14:47:01 +0100253 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathX86"; }
254
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000255 private:
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000256 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathX86);
257};
258
Andreas Gampe85b62f22015-09-09 13:15:38 -0700259class LoadClassSlowPathX86 : public SlowPathCode {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000260 public:
261 LoadClassSlowPathX86(HLoadClass* cls,
262 HInstruction* at,
263 uint32_t dex_pc,
264 bool do_clinit)
David Srbecky9cd6d372016-02-09 15:24:47 +0000265 : SlowPathCode(at), cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000266 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
267 }
268
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000269 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000270 LocationSummary* locations = at_->GetLocations();
271 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
272 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000273 SaveLiveRegisters(codegen, locations);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000274
275 InvokeRuntimeCallingConvention calling_convention;
276 __ movl(calling_convention.GetRegisterAt(0), Immediate(cls_->GetTypeIndex()));
Alexandre Rames8158f282015-08-07 10:26:17 +0100277 x86_codegen->InvokeRuntime(do_clinit_ ? QUICK_ENTRY_POINT(pInitializeStaticStorage)
278 : QUICK_ENTRY_POINT(pInitializeType),
279 at_, dex_pc_, this);
Roland Levillain888d0672015-11-23 18:53:50 +0000280 if (do_clinit_) {
281 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
282 } else {
283 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
284 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000285
286 // Move the class to the desired location.
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000287 Location out = locations->Out();
288 if (out.IsValid()) {
289 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
290 x86_codegen->Move32(out, Location::RegisterLocation(EAX));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000291 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000292
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000293 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000294 __ jmp(GetExitLabel());
295 }
296
Alexandre Rames9931f312015-06-19 14:47:01 +0100297 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathX86"; }
298
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000299 private:
300 // The class this slow path will load.
301 HLoadClass* const cls_;
302
303 // The instruction where this slow path is happening.
304 // (Might be the load class or an initialization check).
305 HInstruction* const at_;
306
307 // The dex PC of `at_`.
308 const uint32_t dex_pc_;
309
310 // Whether to initialize the class.
311 const bool do_clinit_;
312
313 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathX86);
314};
315
Andreas Gampe85b62f22015-09-09 13:15:38 -0700316class TypeCheckSlowPathX86 : public SlowPathCode {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000317 public:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000318 TypeCheckSlowPathX86(HInstruction* instruction, bool is_fatal)
David Srbecky9cd6d372016-02-09 15:24:47 +0000319 : SlowPathCode(instruction), is_fatal_(is_fatal) {}
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000320
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000321 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000322 LocationSummary* locations = instruction_->GetLocations();
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100323 Location object_class = instruction_->IsCheckCast() ? locations->GetTemp(0)
324 : locations->Out();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000325 DCHECK(instruction_->IsCheckCast()
326 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000327
328 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
329 __ Bind(GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000330
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000331 if (!is_fatal_) {
332 SaveLiveRegisters(codegen, locations);
333 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000334
335 // We're moving two locations to locations that could overlap, so we need a parallel
336 // move resolver.
337 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000338 x86_codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100339 locations->InAt(1),
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000340 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100341 Primitive::kPrimNot,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100342 object_class,
Nicolas Geoffray90218252015-04-15 11:56:51 +0100343 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
344 Primitive::kPrimNot);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000345
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000346 if (instruction_->IsInstanceOf()) {
Alexandre Rames8158f282015-08-07 10:26:17 +0100347 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pInstanceofNonTrivial),
348 instruction_,
349 instruction_->GetDexPc(),
350 this);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000351 CheckEntrypointTypes<
Andreas Gampe67409972016-07-19 22:34:53 -0700352 kQuickInstanceofNonTrivial, size_t, const mirror::Class*, const mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000353 } else {
354 DCHECK(instruction_->IsCheckCast());
Alexandre Rames8158f282015-08-07 10:26:17 +0100355 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pCheckCast),
356 instruction_,
357 instruction_->GetDexPc(),
358 this);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000359 CheckEntrypointTypes<kQuickCheckCast, void, const mirror::Class*, const mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000360 }
361
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000362 if (!is_fatal_) {
363 if (instruction_->IsInstanceOf()) {
364 x86_codegen->Move32(locations->Out(), Location::RegisterLocation(EAX));
365 }
366 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray75374372015-09-17 17:12:19 +0000367
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000368 __ jmp(GetExitLabel());
369 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000370 }
371
Alexandre Rames9931f312015-06-19 14:47:01 +0100372 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathX86"; }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000373 bool IsFatal() const OVERRIDE { return is_fatal_; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100374
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000375 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000376 const bool is_fatal_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000377
378 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathX86);
379};
380
Andreas Gampe85b62f22015-09-09 13:15:38 -0700381class DeoptimizationSlowPathX86 : public SlowPathCode {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700382 public:
Aart Bik42249c32016-01-07 15:33:50 -0800383 explicit DeoptimizationSlowPathX86(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000384 : SlowPathCode(instruction) {}
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700385
386 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames8158f282015-08-07 10:26:17 +0100387 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700388 __ Bind(GetEntryLabel());
389 SaveLiveRegisters(codegen, instruction_->GetLocations());
Alexandre Rames8158f282015-08-07 10:26:17 +0100390 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pDeoptimize),
391 instruction_,
392 instruction_->GetDexPc(),
393 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000394 CheckEntrypointTypes<kQuickDeoptimize, void, void>();
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700395 }
396
Alexandre Rames9931f312015-06-19 14:47:01 +0100397 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathX86"; }
398
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700399 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700400 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathX86);
401};
402
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100403class ArraySetSlowPathX86 : public SlowPathCode {
404 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000405 explicit ArraySetSlowPathX86(HInstruction* instruction) : SlowPathCode(instruction) {}
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100406
407 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
408 LocationSummary* locations = instruction_->GetLocations();
409 __ Bind(GetEntryLabel());
410 SaveLiveRegisters(codegen, locations);
411
412 InvokeRuntimeCallingConvention calling_convention;
413 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
414 parallel_move.AddMove(
415 locations->InAt(0),
416 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
417 Primitive::kPrimNot,
418 nullptr);
419 parallel_move.AddMove(
420 locations->InAt(1),
421 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
422 Primitive::kPrimInt,
423 nullptr);
424 parallel_move.AddMove(
425 locations->InAt(2),
426 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
427 Primitive::kPrimNot,
428 nullptr);
429 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
430
431 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
432 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pAputObject),
433 instruction_,
434 instruction_->GetDexPc(),
435 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000436 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100437 RestoreLiveRegisters(codegen, locations);
438 __ jmp(GetExitLabel());
439 }
440
441 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathX86"; }
442
443 private:
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100444 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathX86);
445};
446
Roland Levillain7c1559a2015-12-15 10:55:36 +0000447// Slow path marking an object during a read barrier.
448class ReadBarrierMarkSlowPathX86 : public SlowPathCode {
449 public:
Roland Levillain02b75802016-07-13 11:54:35 +0100450 ReadBarrierMarkSlowPathX86(HInstruction* instruction, Location obj)
451 : SlowPathCode(instruction), obj_(obj) {
Roland Levillain7c1559a2015-12-15 10:55:36 +0000452 DCHECK(kEmitCompilerReadBarrier);
453 }
454
455 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathX86"; }
456
457 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
458 LocationSummary* locations = instruction_->GetLocations();
Roland Levillain02b75802016-07-13 11:54:35 +0100459 Register reg = obj_.AsRegister<Register>();
Roland Levillain7c1559a2015-12-15 10:55:36 +0000460 DCHECK(locations->CanCall());
Roland Levillain02b75802016-07-13 11:54:35 +0100461 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg));
Roland Levillain7c1559a2015-12-15 10:55:36 +0000462 DCHECK(instruction_->IsInstanceFieldGet() ||
463 instruction_->IsStaticFieldGet() ||
464 instruction_->IsArrayGet() ||
465 instruction_->IsLoadClass() ||
466 instruction_->IsLoadString() ||
467 instruction_->IsInstanceOf() ||
Roland Levillain3d312422016-06-23 13:53:42 +0100468 instruction_->IsCheckCast() ||
469 ((instruction_->IsInvokeStaticOrDirect() || instruction_->IsInvokeVirtual()) &&
470 instruction_->GetLocations()->Intrinsified()))
Roland Levillain7c1559a2015-12-15 10:55:36 +0000471 << "Unexpected instruction in read barrier marking slow path: "
472 << instruction_->DebugName();
473
474 __ Bind(GetEntryLabel());
Roland Levillain02b75802016-07-13 11:54:35 +0100475 // Save live registers before the runtime call, and in particular
476 // EAX (if it is live), as it is clobbered by functions
477 // art_quick_read_barrier_mark_regX.
Roland Levillain7c1559a2015-12-15 10:55:36 +0000478 SaveLiveRegisters(codegen, locations);
479
480 InvokeRuntimeCallingConvention calling_convention;
481 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Roland Levillain02b75802016-07-13 11:54:35 +0100482 DCHECK_NE(reg, ESP);
483 DCHECK(0 <= reg && reg < kNumberOfCpuRegisters) << reg;
484 // "Compact" slow path, saving two moves.
485 //
486 // Instead of using the standard runtime calling convention (input
487 // and output in EAX):
488 //
489 // EAX <- obj
490 // EAX <- ReadBarrierMark(EAX)
491 // obj <- EAX
492 //
493 // we just use rX (the register holding `obj`) as input and output
494 // of a dedicated entrypoint:
495 //
496 // rX <- ReadBarrierMarkRegX(rX)
497 //
498 int32_t entry_point_offset =
499 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kX86WordSize>(reg);
500 // TODO: Do not emit a stack map for this runtime call.
501 x86_codegen->InvokeRuntime(entry_point_offset,
Roland Levillain7c1559a2015-12-15 10:55:36 +0000502 instruction_,
503 instruction_->GetDexPc(),
504 this);
Roland Levillain7c1559a2015-12-15 10:55:36 +0000505
506 RestoreLiveRegisters(codegen, locations);
507 __ jmp(GetExitLabel());
508 }
509
510 private:
Roland Levillain7c1559a2015-12-15 10:55:36 +0000511 const Location obj_;
512
513 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathX86);
514};
515
Roland Levillain0d5a2812015-11-13 10:07:31 +0000516// Slow path generating a read barrier for a heap reference.
517class ReadBarrierForHeapReferenceSlowPathX86 : public SlowPathCode {
518 public:
519 ReadBarrierForHeapReferenceSlowPathX86(HInstruction* instruction,
520 Location out,
521 Location ref,
522 Location obj,
523 uint32_t offset,
524 Location index)
David Srbecky9cd6d372016-02-09 15:24:47 +0000525 : SlowPathCode(instruction),
Roland Levillain0d5a2812015-11-13 10:07:31 +0000526 out_(out),
527 ref_(ref),
528 obj_(obj),
529 offset_(offset),
530 index_(index) {
531 DCHECK(kEmitCompilerReadBarrier);
532 // If `obj` is equal to `out` or `ref`, it means the initial object
533 // has been overwritten by (or after) the heap object reference load
534 // to be instrumented, e.g.:
535 //
536 // __ movl(out, Address(out, offset));
Roland Levillain7c1559a2015-12-15 10:55:36 +0000537 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000538 //
539 // In that case, we have lost the information about the original
540 // object, and the emitted read barrier cannot work properly.
541 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
542 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
543 }
544
545 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
546 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
547 LocationSummary* locations = instruction_->GetLocations();
548 Register reg_out = out_.AsRegister<Register>();
549 DCHECK(locations->CanCall());
550 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
Roland Levillain3d312422016-06-23 13:53:42 +0100551 DCHECK(instruction_->IsInstanceFieldGet() ||
552 instruction_->IsStaticFieldGet() ||
553 instruction_->IsArrayGet() ||
554 instruction_->IsInstanceOf() ||
555 instruction_->IsCheckCast() ||
556 ((instruction_->IsInvokeStaticOrDirect() || instruction_->IsInvokeVirtual()) &&
Roland Levillain7c1559a2015-12-15 10:55:36 +0000557 instruction_->GetLocations()->Intrinsified()))
558 << "Unexpected instruction in read barrier for heap reference slow path: "
559 << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000560
561 __ Bind(GetEntryLabel());
562 SaveLiveRegisters(codegen, locations);
563
564 // We may have to change the index's value, but as `index_` is a
565 // constant member (like other "inputs" of this slow path),
566 // introduce a copy of it, `index`.
567 Location index = index_;
568 if (index_.IsValid()) {
Roland Levillain3d312422016-06-23 13:53:42 +0100569 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
Roland Levillain0d5a2812015-11-13 10:07:31 +0000570 if (instruction_->IsArrayGet()) {
571 // Compute the actual memory offset and store it in `index`.
572 Register index_reg = index_.AsRegister<Register>();
573 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg));
574 if (codegen->IsCoreCalleeSaveRegister(index_reg)) {
575 // We are about to change the value of `index_reg` (see the
576 // calls to art::x86::X86Assembler::shll and
577 // art::x86::X86Assembler::AddImmediate below), but it has
578 // not been saved by the previous call to
579 // art::SlowPathCode::SaveLiveRegisters, as it is a
580 // callee-save register --
581 // art::SlowPathCode::SaveLiveRegisters does not consider
582 // callee-save registers, as it has been designed with the
583 // assumption that callee-save registers are supposed to be
584 // handled by the called function. So, as a callee-save
585 // register, `index_reg` _would_ eventually be saved onto
586 // the stack, but it would be too late: we would have
587 // changed its value earlier. Therefore, we manually save
588 // it here into another freely available register,
589 // `free_reg`, chosen of course among the caller-save
590 // registers (as a callee-save `free_reg` register would
591 // exhibit the same problem).
592 //
593 // Note we could have requested a temporary register from
594 // the register allocator instead; but we prefer not to, as
595 // this is a slow path, and we know we can find a
596 // caller-save register that is available.
597 Register free_reg = FindAvailableCallerSaveRegister(codegen);
598 __ movl(free_reg, index_reg);
599 index_reg = free_reg;
600 index = Location::RegisterLocation(index_reg);
601 } else {
602 // The initial register stored in `index_` has already been
603 // saved in the call to art::SlowPathCode::SaveLiveRegisters
604 // (as it is not a callee-save register), so we can freely
605 // use it.
606 }
607 // Shifting the index value contained in `index_reg` by the scale
608 // factor (2) cannot overflow in practice, as the runtime is
609 // unable to allocate object arrays with a size larger than
610 // 2^26 - 1 (that is, 2^28 - 4 bytes).
611 __ shll(index_reg, Immediate(TIMES_4));
612 static_assert(
613 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
614 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
615 __ AddImmediate(index_reg, Immediate(offset_));
616 } else {
Roland Levillain3d312422016-06-23 13:53:42 +0100617 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
618 // intrinsics, `index_` is not shifted by a scale factor of 2
619 // (as in the case of ArrayGet), as it is actually an offset
620 // to an object field within an object.
621 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000622 DCHECK(instruction_->GetLocations()->Intrinsified());
623 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
624 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
625 << instruction_->AsInvoke()->GetIntrinsic();
626 DCHECK_EQ(offset_, 0U);
627 DCHECK(index_.IsRegisterPair());
628 // UnsafeGet's offset location is a register pair, the low
629 // part contains the correct offset.
630 index = index_.ToLow();
631 }
632 }
633
634 // We're moving two or three locations to locations that could
635 // overlap, so we need a parallel move resolver.
636 InvokeRuntimeCallingConvention calling_convention;
637 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
638 parallel_move.AddMove(ref_,
639 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
640 Primitive::kPrimNot,
641 nullptr);
642 parallel_move.AddMove(obj_,
643 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
644 Primitive::kPrimNot,
645 nullptr);
646 if (index.IsValid()) {
647 parallel_move.AddMove(index,
648 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
649 Primitive::kPrimInt,
650 nullptr);
651 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
652 } else {
653 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
654 __ movl(calling_convention.GetRegisterAt(2), Immediate(offset_));
655 }
656 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pReadBarrierSlow),
657 instruction_,
658 instruction_->GetDexPc(),
659 this);
660 CheckEntrypointTypes<
661 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
662 x86_codegen->Move32(out_, Location::RegisterLocation(EAX));
663
664 RestoreLiveRegisters(codegen, locations);
665 __ jmp(GetExitLabel());
666 }
667
668 const char* GetDescription() const OVERRIDE { return "ReadBarrierForHeapReferenceSlowPathX86"; }
669
670 private:
671 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
672 size_t ref = static_cast<int>(ref_.AsRegister<Register>());
673 size_t obj = static_cast<int>(obj_.AsRegister<Register>());
674 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
675 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
676 return static_cast<Register>(i);
677 }
678 }
679 // We shall never fail to find a free caller-save register, as
680 // there are more than two core caller-save registers on x86
681 // (meaning it is possible to find one which is different from
682 // `ref` and `obj`).
683 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
684 LOG(FATAL) << "Could not find a free caller-save register";
685 UNREACHABLE();
686 }
687
Roland Levillain0d5a2812015-11-13 10:07:31 +0000688 const Location out_;
689 const Location ref_;
690 const Location obj_;
691 const uint32_t offset_;
692 // An additional location containing an index to an array.
693 // Only used for HArrayGet and the UnsafeGetObject &
694 // UnsafeGetObjectVolatile intrinsics.
695 const Location index_;
696
697 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathX86);
698};
699
700// Slow path generating a read barrier for a GC root.
701class ReadBarrierForRootSlowPathX86 : public SlowPathCode {
702 public:
703 ReadBarrierForRootSlowPathX86(HInstruction* instruction, Location out, Location root)
David Srbecky9cd6d372016-02-09 15:24:47 +0000704 : SlowPathCode(instruction), out_(out), root_(root) {
Roland Levillain7c1559a2015-12-15 10:55:36 +0000705 DCHECK(kEmitCompilerReadBarrier);
706 }
Roland Levillain0d5a2812015-11-13 10:07:31 +0000707
708 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
709 LocationSummary* locations = instruction_->GetLocations();
710 Register reg_out = out_.AsRegister<Register>();
711 DCHECK(locations->CanCall());
712 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
Roland Levillain7c1559a2015-12-15 10:55:36 +0000713 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
714 << "Unexpected instruction in read barrier for GC root slow path: "
715 << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000716
717 __ Bind(GetEntryLabel());
718 SaveLiveRegisters(codegen, locations);
719
720 InvokeRuntimeCallingConvention calling_convention;
721 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
722 x86_codegen->Move32(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), root_);
723 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pReadBarrierForRootSlow),
724 instruction_,
725 instruction_->GetDexPc(),
726 this);
727 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
728 x86_codegen->Move32(out_, Location::RegisterLocation(EAX));
729
730 RestoreLiveRegisters(codegen, locations);
731 __ jmp(GetExitLabel());
732 }
733
734 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathX86"; }
735
736 private:
Roland Levillain0d5a2812015-11-13 10:07:31 +0000737 const Location out_;
738 const Location root_;
739
740 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathX86);
741};
742
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100743#undef __
Chih-Hung Hsiehfba39972016-05-11 11:26:48 -0700744// NOLINT on __ macro to suppress wrong warning/fix from clang-tidy.
745#define __ down_cast<X86Assembler*>(GetAssembler())-> /* NOLINT */
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100746
Aart Bike9f37602015-10-09 11:15:55 -0700747inline Condition X86Condition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -0700748 switch (cond) {
749 case kCondEQ: return kEqual;
750 case kCondNE: return kNotEqual;
751 case kCondLT: return kLess;
752 case kCondLE: return kLessEqual;
753 case kCondGT: return kGreater;
754 case kCondGE: return kGreaterEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700755 case kCondB: return kBelow;
756 case kCondBE: return kBelowEqual;
757 case kCondA: return kAbove;
758 case kCondAE: return kAboveEqual;
Dave Allison20dfc792014-06-16 20:44:29 -0700759 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100760 LOG(FATAL) << "Unreachable";
761 UNREACHABLE();
762}
763
Aart Bike9f37602015-10-09 11:15:55 -0700764// Maps signed condition to unsigned condition and FP condition to x86 name.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100765inline Condition X86UnsignedOrFPCondition(IfCondition cond) {
766 switch (cond) {
767 case kCondEQ: return kEqual;
768 case kCondNE: return kNotEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700769 // Signed to unsigned, and FP to x86 name.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100770 case kCondLT: return kBelow;
771 case kCondLE: return kBelowEqual;
772 case kCondGT: return kAbove;
773 case kCondGE: return kAboveEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700774 // Unsigned remain unchanged.
775 case kCondB: return kBelow;
776 case kCondBE: return kBelowEqual;
777 case kCondA: return kAbove;
778 case kCondAE: return kAboveEqual;
Roland Levillain4fa13f62015-07-06 18:11:54 +0100779 }
780 LOG(FATAL) << "Unreachable";
781 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -0700782}
783
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100784void CodeGeneratorX86::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100785 stream << Register(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100786}
787
788void CodeGeneratorX86::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100789 stream << XmmRegister(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100790}
791
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100792size_t CodeGeneratorX86::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
793 __ movl(Address(ESP, stack_index), static_cast<Register>(reg_id));
794 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100795}
796
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100797size_t CodeGeneratorX86::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
798 __ movl(static_cast<Register>(reg_id), Address(ESP, stack_index));
799 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100800}
801
Mark Mendell7c8d0092015-01-26 11:21:33 -0500802size_t CodeGeneratorX86::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
803 __ movsd(Address(ESP, stack_index), XmmRegister(reg_id));
804 return GetFloatingPointSpillSlotSize();
805}
806
807size_t CodeGeneratorX86::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
808 __ movsd(XmmRegister(reg_id), Address(ESP, stack_index));
809 return GetFloatingPointSpillSlotSize();
810}
811
Calin Juravle175dc732015-08-25 15:42:32 +0100812void CodeGeneratorX86::InvokeRuntime(QuickEntrypointEnum entrypoint,
813 HInstruction* instruction,
814 uint32_t dex_pc,
815 SlowPathCode* slow_path) {
816 InvokeRuntime(GetThreadOffset<kX86WordSize>(entrypoint).Int32Value(),
817 instruction,
818 dex_pc,
819 slow_path);
820}
821
822void CodeGeneratorX86::InvokeRuntime(int32_t entry_point_offset,
Alexandre Rames8158f282015-08-07 10:26:17 +0100823 HInstruction* instruction,
824 uint32_t dex_pc,
825 SlowPathCode* slow_path) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +0100826 ValidateInvokeRuntime(instruction, slow_path);
Calin Juravle175dc732015-08-25 15:42:32 +0100827 __ fs()->call(Address::Absolute(entry_point_offset));
Alexandre Rames8158f282015-08-07 10:26:17 +0100828 RecordPcInfo(instruction, dex_pc, slow_path);
Alexandre Rames8158f282015-08-07 10:26:17 +0100829}
830
Mark Mendellfb8d2792015-03-31 22:16:59 -0400831CodeGeneratorX86::CodeGeneratorX86(HGraph* graph,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000832 const X86InstructionSetFeatures& isa_features,
833 const CompilerOptions& compiler_options,
834 OptimizingCompilerStats* stats)
Mark Mendell5f874182015-03-04 15:42:45 -0500835 : CodeGenerator(graph,
836 kNumberOfCpuRegisters,
837 kNumberOfXmmRegisters,
838 kNumberOfRegisterPairs,
839 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
840 arraysize(kCoreCalleeSaves))
841 | (1 << kFakeReturnRegister),
Serban Constantinescuecc43662015-08-13 13:33:12 +0100842 0,
843 compiler_options,
844 stats),
Vladimir Marko225b6462015-09-28 12:17:40 +0100845 block_labels_(nullptr),
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100846 location_builder_(graph, this),
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100847 instruction_visitor_(graph, this),
Mark Mendellfb8d2792015-03-31 22:16:59 -0400848 move_resolver_(graph->GetArena(), this),
Vladimir Marko93205e32016-04-13 11:59:46 +0100849 assembler_(graph->GetArena()),
Vladimir Marko58155012015-08-19 12:49:41 +0000850 isa_features_(isa_features),
Vladimir Marko5233f932015-09-29 19:01:15 +0100851 method_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Mark Mendell805b3b52015-09-18 14:10:29 -0400852 relative_call_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000853 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000854 simple_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
855 string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100856 type_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko93205e32016-04-13 11:59:46 +0100857 constant_area_start_(-1),
858 fixups_to_jump_tables_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
859 method_address_offset_(-1) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000860 // Use a fake return address register to mimic Quick.
861 AddAllocatedRegister(Location::RegisterLocation(kFakeReturnRegister));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100862}
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100863
David Brazdil58282f42016-01-14 12:45:10 +0000864void CodeGeneratorX86::SetupBlockedRegisters() const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100865 // Don't allocate the dalvik style register pair passing.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100866 blocked_register_pairs_[ECX_EDX] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100867
868 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100869 blocked_core_registers_[ESP] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100870
Calin Juravle34bacdf2014-10-07 20:23:36 +0100871 UpdateBlockedPairRegisters();
872}
873
874void CodeGeneratorX86::UpdateBlockedPairRegisters() const {
875 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
876 X86ManagedRegister current =
877 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
878 if (blocked_core_registers_[current.AsRegisterPairLow()]
879 || blocked_core_registers_[current.AsRegisterPairHigh()]) {
880 blocked_register_pairs_[i] = true;
881 }
882 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100883}
884
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100885InstructionCodeGeneratorX86::InstructionCodeGeneratorX86(HGraph* graph, CodeGeneratorX86* codegen)
Aart Bik42249c32016-01-07 15:33:50 -0800886 : InstructionCodeGenerator(graph, codegen),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100887 assembler_(codegen->GetAssembler()),
888 codegen_(codegen) {}
889
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100890static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +0100891 return dwarf::Reg::X86Core(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100892}
893
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000894void CodeGeneratorX86::GenerateFrameEntry() {
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100895 __ cfi().SetCurrentCFAOffset(kX86WordSize); // return address
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000896 __ Bind(&frame_entry_label_);
Roland Levillain199f3362014-11-27 17:15:16 +0000897 bool skip_overflow_check =
898 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000899 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Calin Juravle93edf732015-01-20 20:14:07 +0000900
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000901 if (!skip_overflow_check) {
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100902 __ testl(EAX, Address(ESP, -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86))));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100903 RecordPcInfo(nullptr, 0);
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100904 }
905
Mark Mendell5f874182015-03-04 15:42:45 -0500906 if (HasEmptyFrame()) {
907 return;
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000908 }
Mark Mendell5f874182015-03-04 15:42:45 -0500909
910 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
911 Register reg = kCoreCalleeSaves[i];
912 if (allocated_registers_.ContainsCoreRegister(reg)) {
913 __ pushl(reg);
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100914 __ cfi().AdjustCFAOffset(kX86WordSize);
915 __ cfi().RelOffset(DWARFReg(reg), 0);
Mark Mendell5f874182015-03-04 15:42:45 -0500916 }
917 }
918
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100919 int adjust = GetFrameSize() - FrameEntrySpillSize();
920 __ subl(ESP, Immediate(adjust));
921 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100922 __ movl(Address(ESP, kCurrentMethodStackOffset), kMethodRegisterArgument);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000923}
924
925void CodeGeneratorX86::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +0100926 __ cfi().RememberState();
927 if (!HasEmptyFrame()) {
928 int adjust = GetFrameSize() - FrameEntrySpillSize();
929 __ addl(ESP, Immediate(adjust));
930 __ cfi().AdjustCFAOffset(-adjust);
Mark Mendell5f874182015-03-04 15:42:45 -0500931
David Srbeckyc34dc932015-04-12 09:27:43 +0100932 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
933 Register reg = kCoreCalleeSaves[i];
934 if (allocated_registers_.ContainsCoreRegister(reg)) {
935 __ popl(reg);
936 __ cfi().AdjustCFAOffset(-static_cast<int>(kX86WordSize));
937 __ cfi().Restore(DWARFReg(reg));
938 }
Mark Mendell5f874182015-03-04 15:42:45 -0500939 }
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000940 }
David Srbeckyc34dc932015-04-12 09:27:43 +0100941 __ ret();
942 __ cfi().RestoreState();
943 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000944}
945
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100946void CodeGeneratorX86::Bind(HBasicBlock* block) {
947 __ Bind(GetLabelOf(block));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000948}
949
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100950Location InvokeDexCallingConventionVisitorX86::GetReturnLocation(Primitive::Type type) const {
951 switch (type) {
952 case Primitive::kPrimBoolean:
953 case Primitive::kPrimByte:
954 case Primitive::kPrimChar:
955 case Primitive::kPrimShort:
956 case Primitive::kPrimInt:
957 case Primitive::kPrimNot:
958 return Location::RegisterLocation(EAX);
959
960 case Primitive::kPrimLong:
961 return Location::RegisterPairLocation(EAX, EDX);
962
963 case Primitive::kPrimVoid:
964 return Location::NoLocation();
965
966 case Primitive::kPrimDouble:
967 case Primitive::kPrimFloat:
968 return Location::FpuRegisterLocation(XMM0);
969 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +0100970
971 UNREACHABLE();
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100972}
973
974Location InvokeDexCallingConventionVisitorX86::GetMethodLocation() const {
975 return Location::RegisterLocation(kMethodRegisterArgument);
976}
977
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100978Location InvokeDexCallingConventionVisitorX86::GetNextLocation(Primitive::Type type) {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100979 switch (type) {
980 case Primitive::kPrimBoolean:
981 case Primitive::kPrimByte:
982 case Primitive::kPrimChar:
983 case Primitive::kPrimShort:
984 case Primitive::kPrimInt:
985 case Primitive::kPrimNot: {
986 uint32_t index = gp_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000987 stack_index_++;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100988 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100989 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100990 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000991 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100992 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100993 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100994
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000995 case Primitive::kPrimLong: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100996 uint32_t index = gp_index_;
997 gp_index_ += 2;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000998 stack_index_ += 2;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100999 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001000 X86ManagedRegister pair = X86ManagedRegister::FromRegisterPair(
1001 calling_convention.GetRegisterPairAt(index));
1002 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001003 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001004 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
1005 }
1006 }
1007
1008 case Primitive::kPrimFloat: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001009 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001010 stack_index_++;
1011 if (index < calling_convention.GetNumberOfFpuRegisters()) {
1012 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
1013 } else {
1014 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
1015 }
1016 }
1017
1018 case Primitive::kPrimDouble: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001019 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001020 stack_index_ += 2;
1021 if (index < calling_convention.GetNumberOfFpuRegisters()) {
1022 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
1023 } else {
1024 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001025 }
1026 }
1027
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001028 case Primitive::kPrimVoid:
1029 LOG(FATAL) << "Unexpected parameter type " << type;
1030 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001031 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00001032 return Location::NoLocation();
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001033}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001034
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001035void CodeGeneratorX86::Move32(Location destination, Location source) {
1036 if (source.Equals(destination)) {
1037 return;
1038 }
1039 if (destination.IsRegister()) {
1040 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001041 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001042 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001043 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001044 } else {
1045 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001046 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001047 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001048 } else if (destination.IsFpuRegister()) {
1049 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001050 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001051 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001052 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001053 } else {
1054 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001055 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001056 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001057 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001058 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001059 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001060 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001061 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001062 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Mark Mendell7c8d0092015-01-26 11:21:33 -05001063 } else if (source.IsConstant()) {
1064 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001065 int32_t value = GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05001066 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001067 } else {
1068 DCHECK(source.IsStackSlot());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001069 __ pushl(Address(ESP, source.GetStackIndex()));
1070 __ popl(Address(ESP, destination.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001071 }
1072 }
1073}
1074
1075void CodeGeneratorX86::Move64(Location destination, Location source) {
1076 if (source.Equals(destination)) {
1077 return;
1078 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001079 if (destination.IsRegisterPair()) {
1080 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001081 EmitParallelMoves(
1082 Location::RegisterLocation(source.AsRegisterPairHigh<Register>()),
1083 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001084 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001085 Location::RegisterLocation(source.AsRegisterPairLow<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001086 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()),
1087 Primitive::kPrimInt);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001088 } else if (source.IsFpuRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001089 XmmRegister src_reg = source.AsFpuRegister<XmmRegister>();
1090 __ movd(destination.AsRegisterPairLow<Register>(), src_reg);
1091 __ psrlq(src_reg, Immediate(32));
1092 __ movd(destination.AsRegisterPairHigh<Register>(), src_reg);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001093 } else {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001094 // No conflict possible, so just do the moves.
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001095 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001096 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
1097 __ movl(destination.AsRegisterPairHigh<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001098 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
1099 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001100 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05001101 if (source.IsFpuRegister()) {
1102 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
1103 } else if (source.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001104 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01001105 } else if (source.IsRegisterPair()) {
1106 size_t elem_size = Primitive::ComponentSize(Primitive::kPrimInt);
1107 // Create stack space for 2 elements.
1108 __ subl(ESP, Immediate(2 * elem_size));
1109 __ movl(Address(ESP, 0), source.AsRegisterPairLow<Register>());
1110 __ movl(Address(ESP, elem_size), source.AsRegisterPairHigh<Register>());
1111 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
1112 // And remove the temporary stack space we allocated.
1113 __ addl(ESP, Immediate(2 * elem_size));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001114 } else {
1115 LOG(FATAL) << "Unimplemented";
1116 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001117 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001118 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001119 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001120 // No conflict possible, so just do the moves.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001121 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegisterPairLow<Register>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001122 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001123 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001124 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001125 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001126 } else if (source.IsConstant()) {
1127 HConstant* constant = source.GetConstant();
1128 int64_t value;
1129 if (constant->IsLongConstant()) {
1130 value = constant->AsLongConstant()->GetValue();
1131 } else {
1132 DCHECK(constant->IsDoubleConstant());
Roland Levillainda4d79b2015-03-24 14:36:11 +00001133 value = bit_cast<int64_t, double>(constant->AsDoubleConstant()->GetValue());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001134 }
1135 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(Low32Bits(value)));
1136 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), Immediate(High32Bits(value)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001137 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001138 DCHECK(source.IsDoubleStackSlot()) << source;
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001139 EmitParallelMoves(
1140 Location::StackSlot(source.GetStackIndex()),
1141 Location::StackSlot(destination.GetStackIndex()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001142 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001143 Location::StackSlot(source.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001144 Location::StackSlot(destination.GetHighStackIndex(kX86WordSize)),
1145 Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001146 }
1147 }
1148}
1149
Calin Juravle175dc732015-08-25 15:42:32 +01001150void CodeGeneratorX86::MoveConstant(Location location, int32_t value) {
1151 DCHECK(location.IsRegister());
1152 __ movl(location.AsRegister<Register>(), Immediate(value));
1153}
1154
Calin Juravlee460d1d2015-09-29 04:52:17 +01001155void CodeGeneratorX86::MoveLocation(Location dst, Location src, Primitive::Type dst_type) {
David Brazdil74eb1b22015-12-14 11:44:01 +00001156 HParallelMove move(GetGraph()->GetArena());
1157 if (dst_type == Primitive::kPrimLong && !src.IsConstant() && !src.IsFpuRegister()) {
1158 move.AddMove(src.ToLow(), dst.ToLow(), Primitive::kPrimInt, nullptr);
1159 move.AddMove(src.ToHigh(), dst.ToHigh(), Primitive::kPrimInt, nullptr);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001160 } else {
David Brazdil74eb1b22015-12-14 11:44:01 +00001161 move.AddMove(src, dst, dst_type, nullptr);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001162 }
David Brazdil74eb1b22015-12-14 11:44:01 +00001163 GetMoveResolver()->EmitNativeCode(&move);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001164}
1165
1166void CodeGeneratorX86::AddLocationAsTemp(Location location, LocationSummary* locations) {
1167 if (location.IsRegister()) {
1168 locations->AddTemp(location);
1169 } else if (location.IsRegisterPair()) {
1170 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
1171 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
1172 } else {
1173 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1174 }
1175}
1176
David Brazdilfc6a86a2015-06-26 10:33:45 +00001177void InstructionCodeGeneratorX86::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001178 DCHECK(!successor->IsExitBlock());
1179
1180 HBasicBlock* block = got->GetBlock();
1181 HInstruction* previous = got->GetPrevious();
1182
1183 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +00001184 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001185 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1186 return;
1187 }
1188
1189 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1190 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1191 }
1192 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001193 __ jmp(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001194 }
1195}
1196
David Brazdilfc6a86a2015-06-26 10:33:45 +00001197void LocationsBuilderX86::VisitGoto(HGoto* got) {
1198 got->SetLocations(nullptr);
1199}
1200
1201void InstructionCodeGeneratorX86::VisitGoto(HGoto* got) {
1202 HandleGoto(got, got->GetSuccessor());
1203}
1204
1205void LocationsBuilderX86::VisitTryBoundary(HTryBoundary* try_boundary) {
1206 try_boundary->SetLocations(nullptr);
1207}
1208
1209void InstructionCodeGeneratorX86::VisitTryBoundary(HTryBoundary* try_boundary) {
1210 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
1211 if (!successor->IsExitBlock()) {
1212 HandleGoto(try_boundary, successor);
1213 }
1214}
1215
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001216void LocationsBuilderX86::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001217 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001218}
1219
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001220void InstructionCodeGeneratorX86::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001221}
1222
Mark Mendell152408f2015-12-31 12:28:50 -05001223template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001224void InstructionCodeGeneratorX86::GenerateFPJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001225 LabelType* true_label,
1226 LabelType* false_label) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001227 if (cond->IsFPConditionTrueIfNaN()) {
1228 __ j(kUnordered, true_label);
1229 } else if (cond->IsFPConditionFalseIfNaN()) {
1230 __ j(kUnordered, false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001231 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001232 __ j(X86UnsignedOrFPCondition(cond->GetCondition()), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001233}
1234
Mark Mendell152408f2015-12-31 12:28:50 -05001235template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001236void InstructionCodeGeneratorX86::GenerateLongComparesAndJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001237 LabelType* true_label,
1238 LabelType* false_label) {
Mark Mendellc4701932015-04-10 13:18:51 -04001239 LocationSummary* locations = cond->GetLocations();
1240 Location left = locations->InAt(0);
1241 Location right = locations->InAt(1);
1242 IfCondition if_cond = cond->GetCondition();
1243
Mark Mendellc4701932015-04-10 13:18:51 -04001244 Register left_high = left.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001245 Register left_low = left.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001246 IfCondition true_high_cond = if_cond;
1247 IfCondition false_high_cond = cond->GetOppositeCondition();
Aart Bike9f37602015-10-09 11:15:55 -07001248 Condition final_condition = X86UnsignedOrFPCondition(if_cond); // unsigned on lower part
Mark Mendellc4701932015-04-10 13:18:51 -04001249
1250 // Set the conditions for the test, remembering that == needs to be
1251 // decided using the low words.
1252 switch (if_cond) {
1253 case kCondEQ:
Mark Mendellc4701932015-04-10 13:18:51 -04001254 case kCondNE:
Roland Levillain4fa13f62015-07-06 18:11:54 +01001255 // Nothing to do.
Mark Mendellc4701932015-04-10 13:18:51 -04001256 break;
1257 case kCondLT:
1258 false_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -04001259 break;
1260 case kCondLE:
1261 true_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -04001262 break;
1263 case kCondGT:
1264 false_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -04001265 break;
1266 case kCondGE:
1267 true_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -04001268 break;
Aart Bike9f37602015-10-09 11:15:55 -07001269 case kCondB:
1270 false_high_cond = kCondA;
1271 break;
1272 case kCondBE:
1273 true_high_cond = kCondB;
1274 break;
1275 case kCondA:
1276 false_high_cond = kCondB;
1277 break;
1278 case kCondAE:
1279 true_high_cond = kCondA;
1280 break;
Mark Mendellc4701932015-04-10 13:18:51 -04001281 }
1282
1283 if (right.IsConstant()) {
1284 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
Mark Mendellc4701932015-04-10 13:18:51 -04001285 int32_t val_high = High32Bits(value);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001286 int32_t val_low = Low32Bits(value);
Mark Mendellc4701932015-04-10 13:18:51 -04001287
Aart Bika19616e2016-02-01 18:57:58 -08001288 codegen_->Compare32BitValue(left_high, val_high);
Mark Mendellc4701932015-04-10 13:18:51 -04001289 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001290 __ j(X86Condition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001291 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001292 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001293 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001294 __ j(X86Condition(true_high_cond), true_label);
1295 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001296 }
1297 // Must be equal high, so compare the lows.
Aart Bika19616e2016-02-01 18:57:58 -08001298 codegen_->Compare32BitValue(left_low, val_low);
Mark Mendell8659e842016-02-16 10:41:46 -05001299 } else if (right.IsRegisterPair()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001300 Register right_high = right.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001301 Register right_low = right.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001302
1303 __ cmpl(left_high, right_high);
1304 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001305 __ j(X86Condition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001306 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001307 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001308 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001309 __ j(X86Condition(true_high_cond), true_label);
1310 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001311 }
1312 // Must be equal high, so compare the lows.
1313 __ cmpl(left_low, right_low);
Mark Mendell8659e842016-02-16 10:41:46 -05001314 } else {
1315 DCHECK(right.IsDoubleStackSlot());
1316 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
1317 if (if_cond == kCondNE) {
1318 __ j(X86Condition(true_high_cond), true_label);
1319 } else if (if_cond == kCondEQ) {
1320 __ j(X86Condition(false_high_cond), false_label);
1321 } else {
1322 __ j(X86Condition(true_high_cond), true_label);
1323 __ j(X86Condition(false_high_cond), false_label);
1324 }
1325 // Must be equal high, so compare the lows.
1326 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Mark Mendellc4701932015-04-10 13:18:51 -04001327 }
1328 // The last comparison might be unsigned.
1329 __ j(final_condition, true_label);
1330}
1331
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001332void InstructionCodeGeneratorX86::GenerateFPCompare(Location lhs,
1333 Location rhs,
1334 HInstruction* insn,
1335 bool is_double) {
1336 HX86LoadFromConstantTable* const_area = insn->InputAt(1)->AsX86LoadFromConstantTable();
1337 if (is_double) {
1338 if (rhs.IsFpuRegister()) {
1339 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1340 } else if (const_area != nullptr) {
1341 DCHECK(const_area->IsEmittedAtUseSite());
1342 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(),
1343 codegen_->LiteralDoubleAddress(
1344 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
1345 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
1346 } else {
1347 DCHECK(rhs.IsDoubleStackSlot());
1348 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(), Address(ESP, rhs.GetStackIndex()));
1349 }
1350 } else {
1351 if (rhs.IsFpuRegister()) {
1352 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1353 } else if (const_area != nullptr) {
1354 DCHECK(const_area->IsEmittedAtUseSite());
1355 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(),
1356 codegen_->LiteralFloatAddress(
1357 const_area->GetConstant()->AsFloatConstant()->GetValue(),
1358 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
1359 } else {
1360 DCHECK(rhs.IsStackSlot());
1361 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(), Address(ESP, rhs.GetStackIndex()));
1362 }
1363 }
1364}
1365
Mark Mendell152408f2015-12-31 12:28:50 -05001366template<class LabelType>
David Brazdil0debae72015-11-12 18:37:00 +00001367void InstructionCodeGeneratorX86::GenerateCompareTestAndBranch(HCondition* condition,
Mark Mendell152408f2015-12-31 12:28:50 -05001368 LabelType* true_target_in,
1369 LabelType* false_target_in) {
David Brazdil0debae72015-11-12 18:37:00 +00001370 // Generated branching requires both targets to be explicit. If either of the
1371 // targets is nullptr (fallthrough) use and bind `fallthrough_target` instead.
Mark Mendell152408f2015-12-31 12:28:50 -05001372 LabelType fallthrough_target;
1373 LabelType* true_target = true_target_in == nullptr ? &fallthrough_target : true_target_in;
1374 LabelType* false_target = false_target_in == nullptr ? &fallthrough_target : false_target_in;
David Brazdil0debae72015-11-12 18:37:00 +00001375
Mark Mendellc4701932015-04-10 13:18:51 -04001376 LocationSummary* locations = condition->GetLocations();
1377 Location left = locations->InAt(0);
1378 Location right = locations->InAt(1);
1379
Mark Mendellc4701932015-04-10 13:18:51 -04001380 Primitive::Type type = condition->InputAt(0)->GetType();
1381 switch (type) {
1382 case Primitive::kPrimLong:
1383 GenerateLongComparesAndJumps(condition, true_target, false_target);
1384 break;
1385 case Primitive::kPrimFloat:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001386 GenerateFPCompare(left, right, condition, false);
Mark Mendellc4701932015-04-10 13:18:51 -04001387 GenerateFPJumps(condition, true_target, false_target);
1388 break;
1389 case Primitive::kPrimDouble:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001390 GenerateFPCompare(left, right, condition, true);
Mark Mendellc4701932015-04-10 13:18:51 -04001391 GenerateFPJumps(condition, true_target, false_target);
1392 break;
1393 default:
1394 LOG(FATAL) << "Unexpected compare type " << type;
1395 }
1396
David Brazdil0debae72015-11-12 18:37:00 +00001397 if (false_target != &fallthrough_target) {
Mark Mendellc4701932015-04-10 13:18:51 -04001398 __ jmp(false_target);
1399 }
David Brazdil0debae72015-11-12 18:37:00 +00001400
1401 if (fallthrough_target.IsLinked()) {
1402 __ Bind(&fallthrough_target);
1403 }
Mark Mendellc4701932015-04-10 13:18:51 -04001404}
1405
David Brazdil0debae72015-11-12 18:37:00 +00001406static bool AreEflagsSetFrom(HInstruction* cond, HInstruction* branch) {
1407 // Moves may affect the eflags register (move zero uses xorl), so the EFLAGS
1408 // are set only strictly before `branch`. We can't use the eflags on long/FP
1409 // conditions if they are materialized due to the complex branching.
1410 return cond->IsCondition() &&
1411 cond->GetNext() == branch &&
1412 cond->InputAt(0)->GetType() != Primitive::kPrimLong &&
1413 !Primitive::IsFloatingPointType(cond->InputAt(0)->GetType());
1414}
1415
Mark Mendell152408f2015-12-31 12:28:50 -05001416template<class LabelType>
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001417void InstructionCodeGeneratorX86::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00001418 size_t condition_input_index,
Mark Mendell152408f2015-12-31 12:28:50 -05001419 LabelType* true_target,
1420 LabelType* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00001421 HInstruction* cond = instruction->InputAt(condition_input_index);
1422
1423 if (true_target == nullptr && false_target == nullptr) {
1424 // Nothing to do. The code always falls through.
1425 return;
1426 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00001427 // Constant condition, statically compared against "true" (integer value 1).
1428 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00001429 if (true_target != nullptr) {
1430 __ jmp(true_target);
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001431 }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001432 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00001433 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00001434 if (false_target != nullptr) {
1435 __ jmp(false_target);
1436 }
1437 }
1438 return;
1439 }
1440
1441 // The following code generates these patterns:
1442 // (1) true_target == nullptr && false_target != nullptr
1443 // - opposite condition true => branch to false_target
1444 // (2) true_target != nullptr && false_target == nullptr
1445 // - condition true => branch to true_target
1446 // (3) true_target != nullptr && false_target != nullptr
1447 // - condition true => branch to true_target
1448 // - branch to false_target
1449 if (IsBooleanValueOrMaterializedCondition(cond)) {
1450 if (AreEflagsSetFrom(cond, instruction)) {
1451 if (true_target == nullptr) {
1452 __ j(X86Condition(cond->AsCondition()->GetOppositeCondition()), false_target);
1453 } else {
1454 __ j(X86Condition(cond->AsCondition()->GetCondition()), true_target);
1455 }
1456 } else {
1457 // Materialized condition, compare against 0.
1458 Location lhs = instruction->GetLocations()->InAt(condition_input_index);
1459 if (lhs.IsRegister()) {
1460 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
1461 } else {
1462 __ cmpl(Address(ESP, lhs.GetStackIndex()), Immediate(0));
1463 }
1464 if (true_target == nullptr) {
1465 __ j(kEqual, false_target);
1466 } else {
1467 __ j(kNotEqual, true_target);
1468 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001469 }
1470 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001471 // Condition has not been materialized, use its inputs as the comparison and
1472 // its condition as the branch condition.
Mark Mendellb8b97692015-05-22 16:58:19 -04001473 HCondition* condition = cond->AsCondition();
David Brazdil0debae72015-11-12 18:37:00 +00001474
1475 // If this is a long or FP comparison that has been folded into
1476 // the HCondition, generate the comparison directly.
1477 Primitive::Type type = condition->InputAt(0)->GetType();
1478 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
1479 GenerateCompareTestAndBranch(condition, true_target, false_target);
1480 return;
1481 }
1482
1483 Location lhs = condition->GetLocations()->InAt(0);
1484 Location rhs = condition->GetLocations()->InAt(1);
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001485 // LHS is guaranteed to be in a register (see LocationsBuilderX86::HandleCondition).
David Brazdil0debae72015-11-12 18:37:00 +00001486 if (rhs.IsRegister()) {
1487 __ cmpl(lhs.AsRegister<Register>(), rhs.AsRegister<Register>());
1488 } else if (rhs.IsConstant()) {
1489 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
Aart Bika19616e2016-02-01 18:57:58 -08001490 codegen_->Compare32BitValue(lhs.AsRegister<Register>(), constant);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001491 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001492 __ cmpl(lhs.AsRegister<Register>(), Address(ESP, rhs.GetStackIndex()));
1493 }
1494 if (true_target == nullptr) {
1495 __ j(X86Condition(condition->GetOppositeCondition()), false_target);
1496 } else {
Mark Mendellb8b97692015-05-22 16:58:19 -04001497 __ j(X86Condition(condition->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -07001498 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001499 }
David Brazdil0debae72015-11-12 18:37:00 +00001500
1501 // If neither branch falls through (case 3), the conditional branch to `true_target`
1502 // was already emitted (case 2) and we need to emit a jump to `false_target`.
1503 if (true_target != nullptr && false_target != nullptr) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001504 __ jmp(false_target);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001505 }
1506}
1507
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001508void LocationsBuilderX86::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001509 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
1510 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001511 locations->SetInAt(0, Location::Any());
1512 }
1513}
1514
1515void InstructionCodeGeneratorX86::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001516 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
1517 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
1518 Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
1519 nullptr : codegen_->GetLabelOf(true_successor);
1520 Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
1521 nullptr : codegen_->GetLabelOf(false_successor);
1522 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001523}
1524
1525void LocationsBuilderX86::VisitDeoptimize(HDeoptimize* deoptimize) {
1526 LocationSummary* locations = new (GetGraph()->GetArena())
1527 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
David Brazdil0debae72015-11-12 18:37:00 +00001528 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001529 locations->SetInAt(0, Location::Any());
1530 }
1531}
1532
1533void InstructionCodeGeneratorX86::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08001534 SlowPathCode* slow_path = deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathX86>(deoptimize);
David Brazdil74eb1b22015-12-14 11:44:01 +00001535 GenerateTestAndBranch<Label>(deoptimize,
1536 /* condition_input_index */ 0,
1537 slow_path->GetEntryLabel(),
1538 /* false_target */ nullptr);
1539}
1540
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001541static bool SelectCanUseCMOV(HSelect* select) {
1542 // There are no conditional move instructions for XMMs.
1543 if (Primitive::IsFloatingPointType(select->GetType())) {
1544 return false;
1545 }
1546
1547 // A FP condition doesn't generate the single CC that we need.
1548 // In 32 bit mode, a long condition doesn't generate a single CC either.
1549 HInstruction* condition = select->GetCondition();
1550 if (condition->IsCondition()) {
1551 Primitive::Type compare_type = condition->InputAt(0)->GetType();
1552 if (compare_type == Primitive::kPrimLong ||
1553 Primitive::IsFloatingPointType(compare_type)) {
1554 return false;
1555 }
1556 }
1557
1558 // We can generate a CMOV for this Select.
1559 return true;
1560}
1561
David Brazdil74eb1b22015-12-14 11:44:01 +00001562void LocationsBuilderX86::VisitSelect(HSelect* select) {
1563 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001564 if (Primitive::IsFloatingPointType(select->GetType())) {
David Brazdil74eb1b22015-12-14 11:44:01 +00001565 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001566 locations->SetInAt(1, Location::Any());
David Brazdil74eb1b22015-12-14 11:44:01 +00001567 } else {
1568 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001569 if (SelectCanUseCMOV(select)) {
1570 if (select->InputAt(1)->IsConstant()) {
1571 // Cmov can't handle a constant value.
1572 locations->SetInAt(1, Location::RequiresRegister());
1573 } else {
1574 locations->SetInAt(1, Location::Any());
1575 }
1576 } else {
1577 locations->SetInAt(1, Location::Any());
1578 }
David Brazdil74eb1b22015-12-14 11:44:01 +00001579 }
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001580 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
1581 locations->SetInAt(2, Location::RequiresRegister());
David Brazdil74eb1b22015-12-14 11:44:01 +00001582 }
1583 locations->SetOut(Location::SameAsFirstInput());
1584}
1585
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001586void InstructionCodeGeneratorX86::GenerateIntCompare(Location lhs, Location rhs) {
1587 Register lhs_reg = lhs.AsRegister<Register>();
1588 if (rhs.IsConstant()) {
1589 int32_t value = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
1590 codegen_->Compare32BitValue(lhs_reg, value);
1591 } else if (rhs.IsStackSlot()) {
1592 __ cmpl(lhs_reg, Address(ESP, rhs.GetStackIndex()));
1593 } else {
1594 __ cmpl(lhs_reg, rhs.AsRegister<Register>());
1595 }
1596}
1597
David Brazdil74eb1b22015-12-14 11:44:01 +00001598void InstructionCodeGeneratorX86::VisitSelect(HSelect* select) {
1599 LocationSummary* locations = select->GetLocations();
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001600 DCHECK(locations->InAt(0).Equals(locations->Out()));
1601 if (SelectCanUseCMOV(select)) {
1602 // If both the condition and the source types are integer, we can generate
1603 // a CMOV to implement Select.
1604
1605 HInstruction* select_condition = select->GetCondition();
1606 Condition cond = kNotEqual;
1607
1608 // Figure out how to test the 'condition'.
1609 if (select_condition->IsCondition()) {
1610 HCondition* condition = select_condition->AsCondition();
1611 if (!condition->IsEmittedAtUseSite()) {
1612 // This was a previously materialized condition.
1613 // Can we use the existing condition code?
1614 if (AreEflagsSetFrom(condition, select)) {
1615 // Materialization was the previous instruction. Condition codes are right.
1616 cond = X86Condition(condition->GetCondition());
1617 } else {
1618 // No, we have to recreate the condition code.
1619 Register cond_reg = locations->InAt(2).AsRegister<Register>();
1620 __ testl(cond_reg, cond_reg);
1621 }
1622 } else {
1623 // We can't handle FP or long here.
1624 DCHECK_NE(condition->InputAt(0)->GetType(), Primitive::kPrimLong);
1625 DCHECK(!Primitive::IsFloatingPointType(condition->InputAt(0)->GetType()));
1626 LocationSummary* cond_locations = condition->GetLocations();
1627 GenerateIntCompare(cond_locations->InAt(0), cond_locations->InAt(1));
1628 cond = X86Condition(condition->GetCondition());
1629 }
1630 } else {
1631 // Must be a boolean condition, which needs to be compared to 0.
1632 Register cond_reg = locations->InAt(2).AsRegister<Register>();
1633 __ testl(cond_reg, cond_reg);
1634 }
1635
1636 // If the condition is true, overwrite the output, which already contains false.
1637 Location false_loc = locations->InAt(0);
1638 Location true_loc = locations->InAt(1);
1639 if (select->GetType() == Primitive::kPrimLong) {
1640 // 64 bit conditional move.
1641 Register false_high = false_loc.AsRegisterPairHigh<Register>();
1642 Register false_low = false_loc.AsRegisterPairLow<Register>();
1643 if (true_loc.IsRegisterPair()) {
1644 __ cmovl(cond, false_high, true_loc.AsRegisterPairHigh<Register>());
1645 __ cmovl(cond, false_low, true_loc.AsRegisterPairLow<Register>());
1646 } else {
1647 __ cmovl(cond, false_high, Address(ESP, true_loc.GetHighStackIndex(kX86WordSize)));
1648 __ cmovl(cond, false_low, Address(ESP, true_loc.GetStackIndex()));
1649 }
1650 } else {
1651 // 32 bit conditional move.
1652 Register false_reg = false_loc.AsRegister<Register>();
1653 if (true_loc.IsRegister()) {
1654 __ cmovl(cond, false_reg, true_loc.AsRegister<Register>());
1655 } else {
1656 __ cmovl(cond, false_reg, Address(ESP, true_loc.GetStackIndex()));
1657 }
1658 }
1659 } else {
1660 NearLabel false_target;
1661 GenerateTestAndBranch<NearLabel>(
1662 select, /* condition_input_index */ 2, /* true_target */ nullptr, &false_target);
1663 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
1664 __ Bind(&false_target);
1665 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001666}
1667
David Srbecky0cf44932015-12-09 14:09:59 +00001668void LocationsBuilderX86::VisitNativeDebugInfo(HNativeDebugInfo* info) {
1669 new (GetGraph()->GetArena()) LocationSummary(info);
1670}
1671
David Srbeckyd28f4a02016-03-14 17:14:24 +00001672void InstructionCodeGeneratorX86::VisitNativeDebugInfo(HNativeDebugInfo*) {
1673 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00001674}
1675
1676void CodeGeneratorX86::GenerateNop() {
1677 __ nop();
David Srbecky0cf44932015-12-09 14:09:59 +00001678}
1679
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001680void LocationsBuilderX86::HandleCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001681 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +01001682 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Mark Mendellc4701932015-04-10 13:18:51 -04001683 // Handle the long/FP comparisons made in instruction simplification.
1684 switch (cond->InputAt(0)->GetType()) {
1685 case Primitive::kPrimLong: {
1686 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell8659e842016-02-16 10:41:46 -05001687 locations->SetInAt(1, Location::Any());
David Brazdilb3e773e2016-01-26 11:28:37 +00001688 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001689 locations->SetOut(Location::RequiresRegister());
1690 }
1691 break;
1692 }
1693 case Primitive::kPrimFloat:
1694 case Primitive::kPrimDouble: {
1695 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001696 if (cond->InputAt(1)->IsX86LoadFromConstantTable()) {
1697 DCHECK(cond->InputAt(1)->IsEmittedAtUseSite());
1698 } else if (cond->InputAt(1)->IsConstant()) {
1699 locations->SetInAt(1, Location::RequiresFpuRegister());
1700 } else {
1701 locations->SetInAt(1, Location::Any());
1702 }
David Brazdilb3e773e2016-01-26 11:28:37 +00001703 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001704 locations->SetOut(Location::RequiresRegister());
1705 }
1706 break;
1707 }
1708 default:
1709 locations->SetInAt(0, Location::RequiresRegister());
1710 locations->SetInAt(1, Location::Any());
David Brazdilb3e773e2016-01-26 11:28:37 +00001711 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001712 // We need a byte register.
1713 locations->SetOut(Location::RegisterLocation(ECX));
1714 }
1715 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001716 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001717}
1718
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001719void InstructionCodeGeneratorX86::HandleCondition(HCondition* cond) {
David Brazdilb3e773e2016-01-26 11:28:37 +00001720 if (cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001721 return;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001722 }
Mark Mendellc4701932015-04-10 13:18:51 -04001723
1724 LocationSummary* locations = cond->GetLocations();
1725 Location lhs = locations->InAt(0);
1726 Location rhs = locations->InAt(1);
1727 Register reg = locations->Out().AsRegister<Register>();
Mark Mendell152408f2015-12-31 12:28:50 -05001728 NearLabel true_label, false_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001729
1730 switch (cond->InputAt(0)->GetType()) {
1731 default: {
1732 // Integer case.
1733
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01001734 // Clear output register: setb only sets the low byte.
Mark Mendellc4701932015-04-10 13:18:51 -04001735 __ xorl(reg, reg);
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001736 GenerateIntCompare(lhs, rhs);
Aart Bike9f37602015-10-09 11:15:55 -07001737 __ setb(X86Condition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001738 return;
1739 }
1740 case Primitive::kPrimLong:
1741 GenerateLongComparesAndJumps(cond, &true_label, &false_label);
1742 break;
1743 case Primitive::kPrimFloat:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001744 GenerateFPCompare(lhs, rhs, cond, false);
Mark Mendellc4701932015-04-10 13:18:51 -04001745 GenerateFPJumps(cond, &true_label, &false_label);
1746 break;
1747 case Primitive::kPrimDouble:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001748 GenerateFPCompare(lhs, rhs, cond, true);
Mark Mendellc4701932015-04-10 13:18:51 -04001749 GenerateFPJumps(cond, &true_label, &false_label);
1750 break;
1751 }
1752
1753 // Convert the jumps into the result.
Mark Mendell0c9497d2015-08-21 09:30:05 -04001754 NearLabel done_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001755
Roland Levillain4fa13f62015-07-06 18:11:54 +01001756 // False case: result = 0.
Mark Mendellc4701932015-04-10 13:18:51 -04001757 __ Bind(&false_label);
1758 __ xorl(reg, reg);
1759 __ jmp(&done_label);
1760
Roland Levillain4fa13f62015-07-06 18:11:54 +01001761 // True case: result = 1.
Mark Mendellc4701932015-04-10 13:18:51 -04001762 __ Bind(&true_label);
1763 __ movl(reg, Immediate(1));
1764 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07001765}
1766
1767void LocationsBuilderX86::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001768 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001769}
1770
1771void InstructionCodeGeneratorX86::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001772 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001773}
1774
1775void LocationsBuilderX86::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001776 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001777}
1778
1779void InstructionCodeGeneratorX86::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001780 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001781}
1782
1783void LocationsBuilderX86::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001784 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001785}
1786
1787void InstructionCodeGeneratorX86::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001788 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001789}
1790
1791void LocationsBuilderX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001792 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001793}
1794
1795void InstructionCodeGeneratorX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001796 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001797}
1798
1799void LocationsBuilderX86::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001800 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001801}
1802
1803void InstructionCodeGeneratorX86::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001804 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001805}
1806
1807void LocationsBuilderX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001808 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001809}
1810
1811void InstructionCodeGeneratorX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001812 HandleCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001813}
1814
Aart Bike9f37602015-10-09 11:15:55 -07001815void LocationsBuilderX86::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001816 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001817}
1818
1819void InstructionCodeGeneratorX86::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001820 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001821}
1822
1823void LocationsBuilderX86::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001824 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001825}
1826
1827void InstructionCodeGeneratorX86::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001828 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001829}
1830
1831void LocationsBuilderX86::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001832 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001833}
1834
1835void InstructionCodeGeneratorX86::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001836 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001837}
1838
1839void LocationsBuilderX86::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001840 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001841}
1842
1843void InstructionCodeGeneratorX86::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001844 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001845}
1846
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001847void LocationsBuilderX86::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001848 LocationSummary* locations =
1849 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001850 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001851}
1852
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001853void InstructionCodeGeneratorX86::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001854 // Will be generated at use site.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001855}
1856
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001857void LocationsBuilderX86::VisitNullConstant(HNullConstant* constant) {
1858 LocationSummary* locations =
1859 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1860 locations->SetOut(Location::ConstantLocation(constant));
1861}
1862
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001863void InstructionCodeGeneratorX86::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001864 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001865}
1866
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001867void LocationsBuilderX86::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001868 LocationSummary* locations =
1869 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001870 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001871}
1872
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001873void InstructionCodeGeneratorX86::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001874 // Will be generated at use site.
1875}
1876
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001877void LocationsBuilderX86::VisitFloatConstant(HFloatConstant* constant) {
1878 LocationSummary* locations =
1879 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1880 locations->SetOut(Location::ConstantLocation(constant));
1881}
1882
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001883void InstructionCodeGeneratorX86::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001884 // Will be generated at use site.
1885}
1886
1887void LocationsBuilderX86::VisitDoubleConstant(HDoubleConstant* constant) {
1888 LocationSummary* locations =
1889 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1890 locations->SetOut(Location::ConstantLocation(constant));
1891}
1892
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001893void InstructionCodeGeneratorX86::VisitDoubleConstant(HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001894 // Will be generated at use site.
1895}
1896
Calin Juravle27df7582015-04-17 19:12:31 +01001897void LocationsBuilderX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1898 memory_barrier->SetLocations(nullptr);
1899}
1900
1901void InstructionCodeGeneratorX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00001902 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01001903}
1904
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001905void LocationsBuilderX86::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001906 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001907}
1908
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001909void InstructionCodeGeneratorX86::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001910 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001911}
1912
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001913void LocationsBuilderX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001914 LocationSummary* locations =
1915 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001916 switch (ret->InputAt(0)->GetType()) {
1917 case Primitive::kPrimBoolean:
1918 case Primitive::kPrimByte:
1919 case Primitive::kPrimChar:
1920 case Primitive::kPrimShort:
1921 case Primitive::kPrimInt:
1922 case Primitive::kPrimNot:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001923 locations->SetInAt(0, Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001924 break;
1925
1926 case Primitive::kPrimLong:
1927 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001928 0, Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001929 break;
1930
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001931 case Primitive::kPrimFloat:
1932 case Primitive::kPrimDouble:
1933 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001934 0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001935 break;
1936
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001937 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001938 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001939 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001940}
1941
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001942void InstructionCodeGeneratorX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001943 if (kIsDebugBuild) {
1944 switch (ret->InputAt(0)->GetType()) {
1945 case Primitive::kPrimBoolean:
1946 case Primitive::kPrimByte:
1947 case Primitive::kPrimChar:
1948 case Primitive::kPrimShort:
1949 case Primitive::kPrimInt:
1950 case Primitive::kPrimNot:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001951 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<Register>(), EAX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001952 break;
1953
1954 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001955 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairLow<Register>(), EAX);
1956 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairHigh<Register>(), EDX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001957 break;
1958
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001959 case Primitive::kPrimFloat:
1960 case Primitive::kPrimDouble:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001961 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>(), XMM0);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001962 break;
1963
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001964 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001965 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001966 }
1967 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001968 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001969}
1970
Calin Juravle175dc732015-08-25 15:42:32 +01001971void LocationsBuilderX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
1972 // The trampoline uses the same calling convention as dex calling conventions,
1973 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
1974 // the method_idx.
1975 HandleInvoke(invoke);
1976}
1977
1978void InstructionCodeGeneratorX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
1979 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
1980}
1981
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001982void LocationsBuilderX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00001983 // Explicit clinit checks triggered by static invokes must have been pruned by
1984 // art::PrepareForRegisterAllocation.
1985 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001986
Mark Mendellfb8d2792015-03-31 22:16:59 -04001987 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001988 if (intrinsic.TryDispatch(invoke)) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00001989 if (invoke->GetLocations()->CanCall() && invoke->HasPcRelativeDexCache()) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00001990 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00001991 }
Mark Mendell09ed1a32015-03-25 08:30:06 -04001992 return;
1993 }
1994
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001995 HandleInvoke(invoke);
Nicolas Geoffray94015b92015-06-04 18:21:04 +01001996
Vladimir Marko0f7dca42015-11-02 14:36:43 +00001997 // For PC-relative dex cache the invoke has an extra input, the PC-relative address base.
1998 if (invoke->HasPcRelativeDexCache()) {
Vladimir Markob4536b72015-11-24 13:45:23 +00001999 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00002000 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002001}
2002
Mark Mendell09ed1a32015-03-25 08:30:06 -04002003static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86* codegen) {
2004 if (invoke->GetLocations()->Intrinsified()) {
2005 IntrinsicCodeGeneratorX86 intrinsic(codegen);
2006 intrinsic.Dispatch(invoke);
2007 return true;
2008 }
2009 return false;
2010}
2011
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002012void InstructionCodeGeneratorX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002013 // Explicit clinit checks triggered by static invokes must have been pruned by
2014 // art::PrepareForRegisterAllocation.
2015 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002016
Mark Mendell09ed1a32015-03-25 08:30:06 -04002017 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2018 return;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002019 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002020
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002021 LocationSummary* locations = invoke->GetLocations();
Mark Mendell09ed1a32015-03-25 08:30:06 -04002022 codegen_->GenerateStaticOrDirectCall(
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002023 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Mingyao Yang8693fe12015-04-17 16:51:08 -07002024 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002025}
2026
2027void LocationsBuilderX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00002028 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
2029 if (intrinsic.TryDispatch(invoke)) {
2030 return;
2031 }
2032
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002033 HandleInvoke(invoke);
2034}
2035
2036void LocationsBuilderX86::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002037 InvokeDexCallingConventionVisitorX86 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002038 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002039}
2040
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002041void InstructionCodeGeneratorX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002042 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2043 return;
2044 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002045
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002046 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002047 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01002048 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002049}
2050
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002051void LocationsBuilderX86::VisitInvokeInterface(HInvokeInterface* invoke) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002052 // This call to HandleInvoke allocates a temporary (core) register
2053 // which is also used to transfer the hidden argument from FP to
2054 // core register.
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002055 HandleInvoke(invoke);
2056 // Add the hidden argument.
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002057 invoke->GetLocations()->AddTemp(Location::FpuRegisterLocation(XMM7));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002058}
2059
2060void InstructionCodeGeneratorX86::VisitInvokeInterface(HInvokeInterface* invoke) {
2061 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain0d5a2812015-11-13 10:07:31 +00002062 LocationSummary* locations = invoke->GetLocations();
2063 Register temp = locations->GetTemp(0).AsRegister<Register>();
2064 XmmRegister hidden_reg = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002065 Location receiver = locations->InAt(0);
2066 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2067
Roland Levillain0d5a2812015-11-13 10:07:31 +00002068 // Set the hidden argument. This is safe to do this here, as XMM7
2069 // won't be modified thereafter, before the `call` instruction.
2070 DCHECK_EQ(XMM7, hidden_reg);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002071 __ movl(temp, Immediate(invoke->GetDexMethodIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002072 __ movd(hidden_reg, temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002073
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002074 if (receiver.IsStackSlot()) {
2075 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002076 // /* HeapReference<Class> */ temp = temp->klass_
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002077 __ movl(temp, Address(temp, class_offset));
2078 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002079 // /* HeapReference<Class> */ temp = receiver->klass_
Roland Levillain271ab9c2014-11-27 15:23:57 +00002080 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002081 }
Roland Levillain4d027112015-07-01 15:41:14 +01002082 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00002083 // Instead of simply (possibly) unpoisoning `temp` here, we should
2084 // emit a read barrier for the previous class reference load.
2085 // However this is not required in practice, as this is an
2086 // intermediate/temporary reference and because the current
2087 // concurrent copying collector keeps the from-space memory
2088 // intact/accessible until the end of the marking phase (the
2089 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01002090 __ MaybeUnpoisonHeapReference(temp);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002091 // temp = temp->GetAddressOfIMT()
2092 __ movl(temp,
2093 Address(temp, mirror::Class::ImtPtrOffset(kX86PointerSize).Uint32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002094 // temp = temp->GetImtEntryAt(method_offset);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002095 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
2096 invoke->GetImtIndex() % ImTable::kSize, kX86PointerSize));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002097 __ movl(temp, Address(temp, method_offset));
2098 // call temp->GetEntryPoint();
Roland Levillain0d5a2812015-11-13 10:07:31 +00002099 __ call(Address(temp,
2100 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize).Int32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002101
2102 DCHECK(!codegen_->IsLeafMethod());
2103 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2104}
2105
Roland Levillain88cb1752014-10-20 16:36:47 +01002106void LocationsBuilderX86::VisitNeg(HNeg* neg) {
2107 LocationSummary* locations =
2108 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2109 switch (neg->GetResultType()) {
2110 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002111 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01002112 locations->SetInAt(0, Location::RequiresRegister());
2113 locations->SetOut(Location::SameAsFirstInput());
2114 break;
2115
Roland Levillain88cb1752014-10-20 16:36:47 +01002116 case Primitive::kPrimFloat:
Roland Levillain5368c212014-11-27 15:03:41 +00002117 locations->SetInAt(0, Location::RequiresFpuRegister());
2118 locations->SetOut(Location::SameAsFirstInput());
2119 locations->AddTemp(Location::RequiresRegister());
2120 locations->AddTemp(Location::RequiresFpuRegister());
2121 break;
2122
Roland Levillain88cb1752014-10-20 16:36:47 +01002123 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002124 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00002125 locations->SetOut(Location::SameAsFirstInput());
2126 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01002127 break;
2128
2129 default:
2130 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2131 }
2132}
2133
2134void InstructionCodeGeneratorX86::VisitNeg(HNeg* neg) {
2135 LocationSummary* locations = neg->GetLocations();
2136 Location out = locations->Out();
2137 Location in = locations->InAt(0);
2138 switch (neg->GetResultType()) {
2139 case Primitive::kPrimInt:
2140 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002141 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002142 __ negl(out.AsRegister<Register>());
Roland Levillain88cb1752014-10-20 16:36:47 +01002143 break;
2144
2145 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002146 DCHECK(in.IsRegisterPair());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002147 DCHECK(in.Equals(out));
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002148 __ negl(out.AsRegisterPairLow<Register>());
2149 // Negation is similar to subtraction from zero. The least
2150 // significant byte triggers a borrow when it is different from
2151 // zero; to take it into account, add 1 to the most significant
2152 // byte if the carry flag (CF) is set to 1 after the first NEGL
2153 // operation.
2154 __ adcl(out.AsRegisterPairHigh<Register>(), Immediate(0));
2155 __ negl(out.AsRegisterPairHigh<Register>());
2156 break;
2157
Roland Levillain5368c212014-11-27 15:03:41 +00002158 case Primitive::kPrimFloat: {
2159 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002160 Register constant = locations->GetTemp(0).AsRegister<Register>();
2161 XmmRegister mask = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002162 // Implement float negation with an exclusive or with value
2163 // 0x80000000 (mask for bit 31, representing the sign of a
2164 // single-precision floating-point number).
2165 __ movl(constant, Immediate(INT32_C(0x80000000)));
2166 __ movd(mask, constant);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002167 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00002168 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002169 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00002170
Roland Levillain5368c212014-11-27 15:03:41 +00002171 case Primitive::kPrimDouble: {
2172 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002173 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002174 // Implement double negation with an exclusive or with value
2175 // 0x8000000000000000 (mask for bit 63, representing the sign of
2176 // a double-precision floating-point number).
2177 __ LoadLongConstant(mask, INT64_C(0x8000000000000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002178 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01002179 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002180 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002181
2182 default:
2183 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2184 }
2185}
2186
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002187void LocationsBuilderX86::VisitX86FPNeg(HX86FPNeg* neg) {
2188 LocationSummary* locations =
2189 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2190 DCHECK(Primitive::IsFloatingPointType(neg->GetType()));
2191 locations->SetInAt(0, Location::RequiresFpuRegister());
2192 locations->SetInAt(1, Location::RequiresRegister());
2193 locations->SetOut(Location::SameAsFirstInput());
2194 locations->AddTemp(Location::RequiresFpuRegister());
2195}
2196
2197void InstructionCodeGeneratorX86::VisitX86FPNeg(HX86FPNeg* neg) {
2198 LocationSummary* locations = neg->GetLocations();
2199 Location out = locations->Out();
2200 DCHECK(locations->InAt(0).Equals(out));
2201
2202 Register constant_area = locations->InAt(1).AsRegister<Register>();
2203 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
2204 if (neg->GetType() == Primitive::kPrimFloat) {
2205 __ movss(mask, codegen_->LiteralInt32Address(INT32_C(0x80000000), constant_area));
2206 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
2207 } else {
2208 __ movsd(mask, codegen_->LiteralInt64Address(INT64_C(0x8000000000000000), constant_area));
2209 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
2210 }
2211}
2212
Roland Levillaindff1f282014-11-05 14:15:05 +00002213void LocationsBuilderX86::VisitTypeConversion(HTypeConversion* conversion) {
Roland Levillaindff1f282014-11-05 14:15:05 +00002214 Primitive::Type result_type = conversion->GetResultType();
2215 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002216 DCHECK_NE(result_type, input_type);
Roland Levillain624279f2014-12-04 11:54:28 +00002217
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002218 // The float-to-long and double-to-long type conversions rely on a
2219 // call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00002220 LocationSummary::CallKind call_kind =
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002221 ((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
2222 && result_type == Primitive::kPrimLong)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01002223 ? LocationSummary::kCallOnMainOnly
Roland Levillain624279f2014-12-04 11:54:28 +00002224 : LocationSummary::kNoCall;
2225 LocationSummary* locations =
2226 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
2227
David Brazdilb2bd1c52015-03-25 11:17:37 +00002228 // The Java language does not allow treating boolean as an integral type but
2229 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00002230
Roland Levillaindff1f282014-11-05 14:15:05 +00002231 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002232 case Primitive::kPrimByte:
2233 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002234 case Primitive::kPrimLong: {
2235 // Type conversion from long to byte is a result of code transformations.
2236 HInstruction* input = conversion->InputAt(0);
2237 Location input_location = input->IsConstant()
2238 ? Location::ConstantLocation(input->AsConstant())
2239 : Location::RegisterPairLocation(EAX, EDX);
2240 locations->SetInAt(0, input_location);
2241 // Make the output overlap to please the register allocator. This greatly simplifies
2242 // the validation of the linear scan implementation
2243 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2244 break;
2245 }
David Brazdil46e2a392015-03-16 17:31:52 +00002246 case Primitive::kPrimBoolean:
2247 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002248 case Primitive::kPrimShort:
2249 case Primitive::kPrimInt:
2250 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002251 // Processing a Dex `int-to-byte' instruction.
Mark Mendell5f874182015-03-04 15:42:45 -05002252 locations->SetInAt(0, Location::ByteRegisterOrConstant(ECX, conversion->InputAt(0)));
2253 // Make the output overlap to please the register allocator. This greatly simplifies
2254 // the validation of the linear scan implementation
2255 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Roland Levillain51d3fc42014-11-13 14:11:42 +00002256 break;
2257
2258 default:
2259 LOG(FATAL) << "Unexpected type conversion from " << input_type
2260 << " to " << result_type;
2261 }
2262 break;
2263
Roland Levillain01a8d712014-11-14 16:27:39 +00002264 case Primitive::kPrimShort:
2265 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002266 case Primitive::kPrimLong:
2267 // Type conversion from long to short 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 Levillain01a8d712014-11-14 16:27:39 +00002270 case Primitive::kPrimByte:
2271 case Primitive::kPrimInt:
2272 case Primitive::kPrimChar:
2273 // Processing a Dex `int-to-short' 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 Levillain946e1432014-11-11 17:35:19 +00002284 case Primitive::kPrimInt:
2285 switch (input_type) {
2286 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002287 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002288 locations->SetInAt(0, Location::Any());
2289 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2290 break;
2291
2292 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00002293 // Processing a Dex `float-to-int' instruction.
2294 locations->SetInAt(0, Location::RequiresFpuRegister());
2295 locations->SetOut(Location::RequiresRegister());
2296 locations->AddTemp(Location::RequiresFpuRegister());
2297 break;
2298
Roland Levillain946e1432014-11-11 17:35:19 +00002299 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002300 // Processing a Dex `double-to-int' instruction.
2301 locations->SetInAt(0, Location::RequiresFpuRegister());
2302 locations->SetOut(Location::RequiresRegister());
2303 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00002304 break;
2305
2306 default:
2307 LOG(FATAL) << "Unexpected type conversion from " << input_type
2308 << " to " << result_type;
2309 }
2310 break;
2311
Roland Levillaindff1f282014-11-05 14:15:05 +00002312 case Primitive::kPrimLong:
2313 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002314 case Primitive::kPrimBoolean:
2315 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002316 case Primitive::kPrimByte:
2317 case Primitive::kPrimShort:
2318 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002319 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002320 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002321 locations->SetInAt(0, Location::RegisterLocation(EAX));
2322 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
2323 break;
2324
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002325 case Primitive::kPrimFloat:
Vladimir Marko949c91f2015-01-27 10:48:44 +00002326 case Primitive::kPrimDouble: {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002327 // Processing a Dex `float-to-long' or 'double-to-long' instruction.
Vladimir Marko949c91f2015-01-27 10:48:44 +00002328 InvokeRuntimeCallingConvention calling_convention;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002329 XmmRegister parameter = calling_convention.GetFpuRegisterAt(0);
2330 locations->SetInAt(0, Location::FpuRegisterLocation(parameter));
2331
Vladimir Marko949c91f2015-01-27 10:48:44 +00002332 // The runtime helper puts the result in EAX, EDX.
2333 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Vladimir Marko949c91f2015-01-27 10:48:44 +00002334 }
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002335 break;
Roland Levillaindff1f282014-11-05 14:15:05 +00002336
2337 default:
2338 LOG(FATAL) << "Unexpected type conversion from " << input_type
2339 << " to " << result_type;
2340 }
2341 break;
2342
Roland Levillain981e4542014-11-14 11:47:14 +00002343 case Primitive::kPrimChar:
2344 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002345 case Primitive::kPrimLong:
2346 // Type conversion from long to char is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002347 case Primitive::kPrimBoolean:
2348 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002349 case Primitive::kPrimByte:
2350 case Primitive::kPrimShort:
2351 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002352 // Processing a Dex `int-to-char' instruction.
2353 locations->SetInAt(0, Location::Any());
2354 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2355 break;
2356
2357 default:
2358 LOG(FATAL) << "Unexpected type conversion from " << input_type
2359 << " to " << result_type;
2360 }
2361 break;
2362
Roland Levillaindff1f282014-11-05 14:15:05 +00002363 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002364 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002365 case Primitive::kPrimBoolean:
2366 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002367 case Primitive::kPrimByte:
2368 case Primitive::kPrimShort:
2369 case Primitive::kPrimInt:
2370 case Primitive::kPrimChar:
2371 // Processing a Dex `int-to-float' instruction.
2372 locations->SetInAt(0, Location::RequiresRegister());
2373 locations->SetOut(Location::RequiresFpuRegister());
2374 break;
2375
2376 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002377 // Processing a Dex `long-to-float' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002378 locations->SetInAt(0, Location::Any());
2379 locations->SetOut(Location::Any());
Roland Levillain6d0e4832014-11-27 18:31:21 +00002380 break;
2381
Roland Levillaincff13742014-11-17 14:32:17 +00002382 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002383 // Processing a Dex `double-to-float' instruction.
2384 locations->SetInAt(0, Location::RequiresFpuRegister());
2385 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002386 break;
2387
2388 default:
2389 LOG(FATAL) << "Unexpected type conversion from " << input_type
2390 << " to " << result_type;
2391 };
2392 break;
2393
Roland Levillaindff1f282014-11-05 14:15:05 +00002394 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002395 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002396 case Primitive::kPrimBoolean:
2397 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002398 case Primitive::kPrimByte:
2399 case Primitive::kPrimShort:
2400 case Primitive::kPrimInt:
2401 case Primitive::kPrimChar:
2402 // Processing a Dex `int-to-double' instruction.
2403 locations->SetInAt(0, Location::RequiresRegister());
2404 locations->SetOut(Location::RequiresFpuRegister());
2405 break;
2406
2407 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002408 // Processing a Dex `long-to-double' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002409 locations->SetInAt(0, Location::Any());
2410 locations->SetOut(Location::Any());
Roland Levillain647b9ed2014-11-27 12:06:00 +00002411 break;
2412
Roland Levillaincff13742014-11-17 14:32:17 +00002413 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002414 // Processing a Dex `float-to-double' instruction.
2415 locations->SetInAt(0, Location::RequiresFpuRegister());
2416 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002417 break;
2418
2419 default:
2420 LOG(FATAL) << "Unexpected type conversion from " << input_type
2421 << " to " << result_type;
2422 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002423 break;
2424
2425 default:
2426 LOG(FATAL) << "Unexpected type conversion from " << input_type
2427 << " to " << result_type;
2428 }
2429}
2430
2431void InstructionCodeGeneratorX86::VisitTypeConversion(HTypeConversion* conversion) {
2432 LocationSummary* locations = conversion->GetLocations();
2433 Location out = locations->Out();
2434 Location in = locations->InAt(0);
2435 Primitive::Type result_type = conversion->GetResultType();
2436 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002437 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00002438 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002439 case Primitive::kPrimByte:
2440 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002441 case Primitive::kPrimLong:
2442 // Type conversion from long to byte is a result of code transformations.
2443 if (in.IsRegisterPair()) {
2444 __ movsxb(out.AsRegister<Register>(), in.AsRegisterPairLow<ByteRegister>());
2445 } else {
2446 DCHECK(in.GetConstant()->IsLongConstant());
2447 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2448 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
2449 }
2450 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002451 case Primitive::kPrimBoolean:
2452 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002453 case Primitive::kPrimShort:
2454 case Primitive::kPrimInt:
2455 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002456 // Processing a Dex `int-to-byte' instruction.
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002457 if (in.IsRegister()) {
2458 __ movsxb(out.AsRegister<Register>(), in.AsRegister<ByteRegister>());
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002459 } else {
2460 DCHECK(in.GetConstant()->IsIntConstant());
2461 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
2462 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
2463 }
Roland Levillain51d3fc42014-11-13 14:11:42 +00002464 break;
2465
2466 default:
2467 LOG(FATAL) << "Unexpected type conversion from " << input_type
2468 << " to " << result_type;
2469 }
2470 break;
2471
Roland Levillain01a8d712014-11-14 16:27:39 +00002472 case Primitive::kPrimShort:
2473 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002474 case Primitive::kPrimLong:
2475 // Type conversion from long to short is a result of code transformations.
2476 if (in.IsRegisterPair()) {
2477 __ movsxw(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
2478 } else if (in.IsDoubleStackSlot()) {
2479 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
2480 } else {
2481 DCHECK(in.GetConstant()->IsLongConstant());
2482 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2483 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
2484 }
2485 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002486 case Primitive::kPrimBoolean:
2487 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002488 case Primitive::kPrimByte:
2489 case Primitive::kPrimInt:
2490 case Primitive::kPrimChar:
2491 // Processing a Dex `int-to-short' instruction.
2492 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002493 __ movsxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain01a8d712014-11-14 16:27:39 +00002494 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002495 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain01a8d712014-11-14 16:27:39 +00002496 } else {
2497 DCHECK(in.GetConstant()->IsIntConstant());
2498 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002499 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
Roland Levillain01a8d712014-11-14 16:27:39 +00002500 }
2501 break;
2502
2503 default:
2504 LOG(FATAL) << "Unexpected type conversion from " << input_type
2505 << " to " << result_type;
2506 }
2507 break;
2508
Roland Levillain946e1432014-11-11 17:35:19 +00002509 case Primitive::kPrimInt:
2510 switch (input_type) {
2511 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002512 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002513 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002514 __ movl(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00002515 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002516 __ movl(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain946e1432014-11-11 17:35:19 +00002517 } else {
2518 DCHECK(in.IsConstant());
2519 DCHECK(in.GetConstant()->IsLongConstant());
2520 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002521 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00002522 }
2523 break;
2524
Roland Levillain3f8f9362014-12-02 17:45:01 +00002525 case Primitive::kPrimFloat: {
2526 // Processing a Dex `float-to-int' instruction.
2527 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2528 Register output = out.AsRegister<Register>();
2529 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002530 NearLabel done, nan;
Roland Levillain3f8f9362014-12-02 17:45:01 +00002531
2532 __ movl(output, Immediate(kPrimIntMax));
2533 // temp = int-to-float(output)
2534 __ cvtsi2ss(temp, output);
2535 // if input >= temp goto done
2536 __ comiss(input, temp);
2537 __ j(kAboveEqual, &done);
2538 // if input == NaN goto nan
2539 __ j(kUnordered, &nan);
2540 // output = float-to-int-truncate(input)
2541 __ cvttss2si(output, input);
2542 __ jmp(&done);
2543 __ Bind(&nan);
2544 // output = 0
2545 __ xorl(output, output);
2546 __ Bind(&done);
2547 break;
2548 }
2549
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002550 case Primitive::kPrimDouble: {
2551 // Processing a Dex `double-to-int' instruction.
2552 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2553 Register output = out.AsRegister<Register>();
2554 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002555 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002556
2557 __ movl(output, Immediate(kPrimIntMax));
2558 // temp = int-to-double(output)
2559 __ cvtsi2sd(temp, output);
2560 // if input >= temp goto done
2561 __ comisd(input, temp);
2562 __ j(kAboveEqual, &done);
2563 // if input == NaN goto nan
2564 __ j(kUnordered, &nan);
2565 // output = double-to-int-truncate(input)
2566 __ cvttsd2si(output, input);
2567 __ jmp(&done);
2568 __ Bind(&nan);
2569 // output = 0
2570 __ xorl(output, output);
2571 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00002572 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002573 }
Roland Levillain946e1432014-11-11 17:35:19 +00002574
2575 default:
2576 LOG(FATAL) << "Unexpected type conversion from " << input_type
2577 << " to " << result_type;
2578 }
2579 break;
2580
Roland Levillaindff1f282014-11-05 14:15:05 +00002581 case Primitive::kPrimLong:
2582 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002583 case Primitive::kPrimBoolean:
2584 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002585 case Primitive::kPrimByte:
2586 case Primitive::kPrimShort:
2587 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002588 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002589 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002590 DCHECK_EQ(out.AsRegisterPairLow<Register>(), EAX);
2591 DCHECK_EQ(out.AsRegisterPairHigh<Register>(), EDX);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002592 DCHECK_EQ(in.AsRegister<Register>(), EAX);
Roland Levillaindff1f282014-11-05 14:15:05 +00002593 __ cdq();
2594 break;
2595
2596 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00002597 // Processing a Dex `float-to-long' instruction.
Alexandre Rames8158f282015-08-07 10:26:17 +01002598 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pF2l),
2599 conversion,
2600 conversion->GetDexPc(),
2601 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00002602 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
Roland Levillain624279f2014-12-04 11:54:28 +00002603 break;
2604
Roland Levillaindff1f282014-11-05 14:15:05 +00002605 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002606 // Processing a Dex `double-to-long' instruction.
Alexandre Rames8158f282015-08-07 10:26:17 +01002607 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pD2l),
2608 conversion,
2609 conversion->GetDexPc(),
2610 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00002611 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
Roland Levillaindff1f282014-11-05 14:15:05 +00002612 break;
2613
2614 default:
2615 LOG(FATAL) << "Unexpected type conversion from " << input_type
2616 << " to " << result_type;
2617 }
2618 break;
2619
Roland Levillain981e4542014-11-14 11:47:14 +00002620 case Primitive::kPrimChar:
2621 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002622 case Primitive::kPrimLong:
2623 // Type conversion from long to short is a result of code transformations.
2624 if (in.IsRegisterPair()) {
2625 __ movzxw(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
2626 } else if (in.IsDoubleStackSlot()) {
2627 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
2628 } else {
2629 DCHECK(in.GetConstant()->IsLongConstant());
2630 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2631 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
2632 }
2633 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002634 case Primitive::kPrimBoolean:
2635 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002636 case Primitive::kPrimByte:
2637 case Primitive::kPrimShort:
2638 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002639 // Processing a Dex `Process a Dex `int-to-char'' instruction.
2640 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002641 __ movzxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain981e4542014-11-14 11:47:14 +00002642 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002643 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain981e4542014-11-14 11:47:14 +00002644 } else {
2645 DCHECK(in.GetConstant()->IsIntConstant());
2646 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002647 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
Roland Levillain981e4542014-11-14 11:47:14 +00002648 }
2649 break;
2650
2651 default:
2652 LOG(FATAL) << "Unexpected type conversion from " << input_type
2653 << " to " << result_type;
2654 }
2655 break;
2656
Roland Levillaindff1f282014-11-05 14:15:05 +00002657 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002658 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002659 case Primitive::kPrimBoolean:
2660 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002661 case Primitive::kPrimByte:
2662 case Primitive::kPrimShort:
2663 case Primitive::kPrimInt:
2664 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002665 // Processing a Dex `int-to-float' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002666 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002667 break;
2668
Roland Levillain6d0e4832014-11-27 18:31:21 +00002669 case Primitive::kPrimLong: {
2670 // Processing a Dex `long-to-float' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002671 size_t adjustment = 0;
Roland Levillain6d0e4832014-11-27 18:31:21 +00002672
Roland Levillain232ade02015-04-20 15:14:36 +01002673 // Create stack space for the call to
2674 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstps below.
2675 // TODO: enhance register allocator to ask for stack temporaries.
2676 if (!in.IsDoubleStackSlot() || !out.IsStackSlot()) {
2677 adjustment = Primitive::ComponentSize(Primitive::kPrimLong);
2678 __ subl(ESP, Immediate(adjustment));
2679 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002680
Roland Levillain232ade02015-04-20 15:14:36 +01002681 // Load the value to the FP stack, using temporaries if needed.
2682 PushOntoFPStack(in, 0, adjustment, false, true);
2683
2684 if (out.IsStackSlot()) {
2685 __ fstps(Address(ESP, out.GetStackIndex() + adjustment));
2686 } else {
2687 __ fstps(Address(ESP, 0));
2688 Location stack_temp = Location::StackSlot(0);
2689 codegen_->Move32(out, stack_temp);
2690 }
2691
2692 // Remove the temporary stack space we allocated.
2693 if (adjustment != 0) {
2694 __ addl(ESP, Immediate(adjustment));
2695 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002696 break;
2697 }
2698
Roland Levillaincff13742014-11-17 14:32:17 +00002699 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002700 // Processing a Dex `double-to-float' instruction.
2701 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002702 break;
2703
2704 default:
2705 LOG(FATAL) << "Unexpected type conversion from " << input_type
2706 << " to " << result_type;
2707 };
2708 break;
2709
Roland Levillaindff1f282014-11-05 14:15:05 +00002710 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002711 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002712 case Primitive::kPrimBoolean:
2713 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002714 case Primitive::kPrimByte:
2715 case Primitive::kPrimShort:
2716 case Primitive::kPrimInt:
2717 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002718 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002719 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002720 break;
2721
Roland Levillain647b9ed2014-11-27 12:06:00 +00002722 case Primitive::kPrimLong: {
2723 // Processing a Dex `long-to-double' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002724 size_t adjustment = 0;
Roland Levillain647b9ed2014-11-27 12:06:00 +00002725
Roland Levillain232ade02015-04-20 15:14:36 +01002726 // Create stack space for the call to
2727 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstpl below.
2728 // TODO: enhance register allocator to ask for stack temporaries.
2729 if (!in.IsDoubleStackSlot() || !out.IsDoubleStackSlot()) {
2730 adjustment = Primitive::ComponentSize(Primitive::kPrimLong);
2731 __ subl(ESP, Immediate(adjustment));
2732 }
2733
2734 // Load the value to the FP stack, using temporaries if needed.
2735 PushOntoFPStack(in, 0, adjustment, false, true);
2736
2737 if (out.IsDoubleStackSlot()) {
2738 __ fstpl(Address(ESP, out.GetStackIndex() + adjustment));
2739 } else {
2740 __ fstpl(Address(ESP, 0));
2741 Location stack_temp = Location::DoubleStackSlot(0);
2742 codegen_->Move64(out, stack_temp);
2743 }
2744
2745 // Remove the temporary stack space we allocated.
2746 if (adjustment != 0) {
2747 __ addl(ESP, Immediate(adjustment));
2748 }
Roland Levillain647b9ed2014-11-27 12:06:00 +00002749 break;
2750 }
2751
Roland Levillaincff13742014-11-17 14:32:17 +00002752 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002753 // Processing a Dex `float-to-double' instruction.
2754 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002755 break;
2756
2757 default:
2758 LOG(FATAL) << "Unexpected type conversion from " << input_type
2759 << " to " << result_type;
2760 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002761 break;
2762
2763 default:
2764 LOG(FATAL) << "Unexpected type conversion from " << input_type
2765 << " to " << result_type;
2766 }
2767}
2768
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002769void LocationsBuilderX86::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002770 LocationSummary* locations =
2771 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002772 switch (add->GetResultType()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002773 case Primitive::kPrimInt: {
2774 locations->SetInAt(0, Location::RequiresRegister());
2775 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
2776 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2777 break;
2778 }
2779
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002780 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002781 locations->SetInAt(0, Location::RequiresRegister());
2782 locations->SetInAt(1, Location::Any());
2783 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002784 break;
2785 }
2786
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002787 case Primitive::kPrimFloat:
2788 case Primitive::kPrimDouble: {
2789 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002790 if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
2791 DCHECK(add->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00002792 } else if (add->InputAt(1)->IsConstant()) {
2793 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002794 } else {
2795 locations->SetInAt(1, Location::Any());
2796 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002797 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002798 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002799 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002800
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002801 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002802 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
2803 break;
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002804 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002805}
2806
2807void InstructionCodeGeneratorX86::VisitAdd(HAdd* add) {
2808 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002809 Location first = locations->InAt(0);
2810 Location second = locations->InAt(1);
Mark Mendell09b84632015-02-13 17:48:38 -05002811 Location out = locations->Out();
2812
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002813 switch (add->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002814 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002815 if (second.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002816 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2817 __ addl(out.AsRegister<Register>(), second.AsRegister<Register>());
Mark Mendell33bf2452015-05-27 10:08:24 -04002818 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
2819 __ addl(out.AsRegister<Register>(), first.AsRegister<Register>());
Mark Mendell09b84632015-02-13 17:48:38 -05002820 } else {
2821 __ leal(out.AsRegister<Register>(), Address(
2822 first.AsRegister<Register>(), second.AsRegister<Register>(), TIMES_1, 0));
2823 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002824 } else if (second.IsConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002825 int32_t value = second.GetConstant()->AsIntConstant()->GetValue();
2826 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2827 __ addl(out.AsRegister<Register>(), Immediate(value));
2828 } else {
2829 __ leal(out.AsRegister<Register>(), Address(first.AsRegister<Register>(), value));
2830 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002831 } else {
Mark Mendell09b84632015-02-13 17:48:38 -05002832 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002833 __ addl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002834 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002835 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002836 }
2837
2838 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002839 if (second.IsRegisterPair()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002840 __ addl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
2841 __ adcl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002842 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002843 __ addl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
2844 __ adcl(first.AsRegisterPairHigh<Register>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002845 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002846 } else {
2847 DCHECK(second.IsConstant()) << second;
2848 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2849 __ addl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
2850 __ adcl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002851 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002852 break;
2853 }
2854
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002855 case Primitive::kPrimFloat: {
2856 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002857 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04002858 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
2859 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00002860 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04002861 __ addss(first.AsFpuRegister<XmmRegister>(),
2862 codegen_->LiteralFloatAddress(
2863 const_area->GetConstant()->AsFloatConstant()->GetValue(),
2864 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2865 } else {
2866 DCHECK(second.IsStackSlot());
2867 __ addss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002868 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002869 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002870 }
2871
2872 case Primitive::kPrimDouble: {
2873 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002874 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04002875 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
2876 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00002877 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04002878 __ addsd(first.AsFpuRegister<XmmRegister>(),
2879 codegen_->LiteralDoubleAddress(
2880 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
2881 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2882 } else {
2883 DCHECK(second.IsDoubleStackSlot());
2884 __ addsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002885 }
2886 break;
2887 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002888
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002889 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002890 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002891 }
2892}
2893
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002894void LocationsBuilderX86::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002895 LocationSummary* locations =
2896 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002897 switch (sub->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002898 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002899 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002900 locations->SetInAt(0, Location::RequiresRegister());
2901 locations->SetInAt(1, Location::Any());
2902 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002903 break;
2904 }
Calin Juravle11351682014-10-23 15:38:15 +01002905 case Primitive::kPrimFloat:
2906 case Primitive::kPrimDouble: {
2907 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002908 if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
2909 DCHECK(sub->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00002910 } else if (sub->InputAt(1)->IsConstant()) {
2911 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002912 } else {
2913 locations->SetInAt(1, Location::Any());
2914 }
Calin Juravle11351682014-10-23 15:38:15 +01002915 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002916 break;
Calin Juravle11351682014-10-23 15:38:15 +01002917 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002918
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002919 default:
Calin Juravle11351682014-10-23 15:38:15 +01002920 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002921 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002922}
2923
2924void InstructionCodeGeneratorX86::VisitSub(HSub* sub) {
2925 LocationSummary* locations = sub->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002926 Location first = locations->InAt(0);
2927 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01002928 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002929 switch (sub->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002930 case Primitive::kPrimInt: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002931 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002932 __ subl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002933 } else if (second.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002934 __ subl(first.AsRegister<Register>(),
2935 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002936 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002937 __ subl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002938 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002939 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002940 }
2941
2942 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002943 if (second.IsRegisterPair()) {
Calin Juravle11351682014-10-23 15:38:15 +01002944 __ subl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
2945 __ sbbl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002946 } else if (second.IsDoubleStackSlot()) {
Calin Juravle11351682014-10-23 15:38:15 +01002947 __ subl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002948 __ sbbl(first.AsRegisterPairHigh<Register>(),
2949 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002950 } else {
2951 DCHECK(second.IsConstant()) << second;
2952 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2953 __ subl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
2954 __ sbbl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002955 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002956 break;
2957 }
2958
Calin Juravle11351682014-10-23 15:38:15 +01002959 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04002960 if (second.IsFpuRegister()) {
2961 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2962 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
2963 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00002964 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04002965 __ subss(first.AsFpuRegister<XmmRegister>(),
2966 codegen_->LiteralFloatAddress(
2967 const_area->GetConstant()->AsFloatConstant()->GetValue(),
2968 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2969 } else {
2970 DCHECK(second.IsStackSlot());
2971 __ subss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
2972 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002973 break;
Calin Juravle11351682014-10-23 15:38:15 +01002974 }
2975
2976 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04002977 if (second.IsFpuRegister()) {
2978 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2979 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
2980 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00002981 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04002982 __ subsd(first.AsFpuRegister<XmmRegister>(),
2983 codegen_->LiteralDoubleAddress(
2984 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
2985 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2986 } else {
2987 DCHECK(second.IsDoubleStackSlot());
2988 __ subsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
2989 }
Calin Juravle11351682014-10-23 15:38:15 +01002990 break;
2991 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002992
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002993 default:
Calin Juravle11351682014-10-23 15:38:15 +01002994 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002995 }
2996}
2997
Calin Juravle34bacdf2014-10-07 20:23:36 +01002998void LocationsBuilderX86::VisitMul(HMul* mul) {
2999 LocationSummary* locations =
3000 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
3001 switch (mul->GetResultType()) {
3002 case Primitive::kPrimInt:
3003 locations->SetInAt(0, Location::RequiresRegister());
3004 locations->SetInAt(1, Location::Any());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003005 if (mul->InputAt(1)->IsIntConstant()) {
3006 // Can use 3 operand multiply.
3007 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3008 } else {
3009 locations->SetOut(Location::SameAsFirstInput());
3010 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003011 break;
3012 case Primitive::kPrimLong: {
3013 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003014 locations->SetInAt(1, Location::Any());
3015 locations->SetOut(Location::SameAsFirstInput());
3016 // Needed for imul on 32bits with 64bits output.
3017 locations->AddTemp(Location::RegisterLocation(EAX));
3018 locations->AddTemp(Location::RegisterLocation(EDX));
3019 break;
3020 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003021 case Primitive::kPrimFloat:
3022 case Primitive::kPrimDouble: {
3023 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003024 if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3025 DCHECK(mul->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003026 } else if (mul->InputAt(1)->IsConstant()) {
3027 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003028 } else {
3029 locations->SetInAt(1, Location::Any());
3030 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003031 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003032 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003033 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003034
3035 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003036 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003037 }
3038}
3039
3040void InstructionCodeGeneratorX86::VisitMul(HMul* mul) {
3041 LocationSummary* locations = mul->GetLocations();
3042 Location first = locations->InAt(0);
3043 Location second = locations->InAt(1);
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003044 Location out = locations->Out();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003045
3046 switch (mul->GetResultType()) {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003047 case Primitive::kPrimInt:
3048 // The constant may have ended up in a register, so test explicitly to avoid
3049 // problems where the output may not be the same as the first operand.
3050 if (mul->InputAt(1)->IsIntConstant()) {
3051 Immediate imm(mul->InputAt(1)->AsIntConstant()->GetValue());
3052 __ imull(out.AsRegister<Register>(), first.AsRegister<Register>(), imm);
3053 } else if (second.IsRegister()) {
3054 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003055 __ imull(first.AsRegister<Register>(), second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003056 } else {
3057 DCHECK(second.IsStackSlot());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003058 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003059 __ imull(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01003060 }
3061 break;
Calin Juravle34bacdf2014-10-07 20:23:36 +01003062
3063 case Primitive::kPrimLong: {
Calin Juravle34bacdf2014-10-07 20:23:36 +01003064 Register in1_hi = first.AsRegisterPairHigh<Register>();
3065 Register in1_lo = first.AsRegisterPairLow<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003066 Register eax = locations->GetTemp(0).AsRegister<Register>();
3067 Register edx = locations->GetTemp(1).AsRegister<Register>();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003068
3069 DCHECK_EQ(EAX, eax);
3070 DCHECK_EQ(EDX, edx);
3071
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003072 // input: in1 - 64 bits, in2 - 64 bits.
Calin Juravle34bacdf2014-10-07 20:23:36 +01003073 // output: in1
3074 // formula: in1.hi : in1.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
3075 // parts: in1.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
3076 // parts: in1.lo = (in1.lo * in2.lo)[31:0]
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003077 if (second.IsConstant()) {
3078 DCHECK(second.GetConstant()->IsLongConstant());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003079
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003080 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3081 int32_t low_value = Low32Bits(value);
3082 int32_t high_value = High32Bits(value);
3083 Immediate low(low_value);
3084 Immediate high(high_value);
3085
3086 __ movl(eax, high);
3087 // eax <- in1.lo * in2.hi
3088 __ imull(eax, in1_lo);
3089 // in1.hi <- in1.hi * in2.lo
3090 __ imull(in1_hi, low);
3091 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3092 __ addl(in1_hi, eax);
3093 // move in2_lo to eax to prepare for double precision
3094 __ movl(eax, low);
3095 // edx:eax <- in1.lo * in2.lo
3096 __ mull(in1_lo);
3097 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3098 __ addl(in1_hi, edx);
3099 // in1.lo <- (in1.lo * in2.lo)[31:0];
3100 __ movl(in1_lo, eax);
3101 } else if (second.IsRegisterPair()) {
3102 Register in2_hi = second.AsRegisterPairHigh<Register>();
3103 Register in2_lo = second.AsRegisterPairLow<Register>();
3104
3105 __ movl(eax, in2_hi);
3106 // eax <- in1.lo * in2.hi
3107 __ imull(eax, in1_lo);
3108 // in1.hi <- in1.hi * in2.lo
3109 __ imull(in1_hi, in2_lo);
3110 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3111 __ addl(in1_hi, eax);
3112 // move in1_lo to eax to prepare for double precision
3113 __ movl(eax, in1_lo);
3114 // edx:eax <- in1.lo * in2.lo
3115 __ mull(in2_lo);
3116 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3117 __ addl(in1_hi, edx);
3118 // in1.lo <- (in1.lo * in2.lo)[31:0];
3119 __ movl(in1_lo, eax);
3120 } else {
3121 DCHECK(second.IsDoubleStackSlot()) << second;
3122 Address in2_hi(ESP, second.GetHighStackIndex(kX86WordSize));
3123 Address in2_lo(ESP, second.GetStackIndex());
3124
3125 __ movl(eax, in2_hi);
3126 // eax <- in1.lo * in2.hi
3127 __ imull(eax, in1_lo);
3128 // in1.hi <- in1.hi * in2.lo
3129 __ imull(in1_hi, in2_lo);
3130 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3131 __ addl(in1_hi, eax);
3132 // move in1_lo to eax to prepare for double precision
3133 __ movl(eax, in1_lo);
3134 // edx:eax <- in1.lo * in2.lo
3135 __ mull(in2_lo);
3136 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3137 __ addl(in1_hi, edx);
3138 // in1.lo <- (in1.lo * in2.lo)[31:0];
3139 __ movl(in1_lo, eax);
3140 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003141
3142 break;
3143 }
3144
Calin Juravleb5bfa962014-10-21 18:02:24 +01003145 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003146 DCHECK(first.Equals(locations->Out()));
3147 if (second.IsFpuRegister()) {
3148 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3149 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3150 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003151 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003152 __ mulss(first.AsFpuRegister<XmmRegister>(),
3153 codegen_->LiteralFloatAddress(
3154 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3155 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3156 } else {
3157 DCHECK(second.IsStackSlot());
3158 __ mulss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3159 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003160 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003161 }
3162
3163 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003164 DCHECK(first.Equals(locations->Out()));
3165 if (second.IsFpuRegister()) {
3166 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3167 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3168 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003169 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003170 __ mulsd(first.AsFpuRegister<XmmRegister>(),
3171 codegen_->LiteralDoubleAddress(
3172 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3173 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3174 } else {
3175 DCHECK(second.IsDoubleStackSlot());
3176 __ mulsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3177 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003178 break;
3179 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003180
3181 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003182 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003183 }
3184}
3185
Roland Levillain232ade02015-04-20 15:14:36 +01003186void InstructionCodeGeneratorX86::PushOntoFPStack(Location source,
3187 uint32_t temp_offset,
3188 uint32_t stack_adjustment,
3189 bool is_fp,
3190 bool is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003191 if (source.IsStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01003192 DCHECK(!is_wide);
3193 if (is_fp) {
3194 __ flds(Address(ESP, source.GetStackIndex() + stack_adjustment));
3195 } else {
3196 __ filds(Address(ESP, source.GetStackIndex() + stack_adjustment));
3197 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003198 } else if (source.IsDoubleStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01003199 DCHECK(is_wide);
3200 if (is_fp) {
3201 __ fldl(Address(ESP, source.GetStackIndex() + stack_adjustment));
3202 } else {
3203 __ fildl(Address(ESP, source.GetStackIndex() + stack_adjustment));
3204 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003205 } else {
3206 // Write the value to the temporary location on the stack and load to FP stack.
Roland Levillain232ade02015-04-20 15:14:36 +01003207 if (!is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003208 Location stack_temp = Location::StackSlot(temp_offset);
3209 codegen_->Move32(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01003210 if (is_fp) {
3211 __ flds(Address(ESP, temp_offset));
3212 } else {
3213 __ filds(Address(ESP, temp_offset));
3214 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003215 } else {
3216 Location stack_temp = Location::DoubleStackSlot(temp_offset);
3217 codegen_->Move64(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01003218 if (is_fp) {
3219 __ fldl(Address(ESP, temp_offset));
3220 } else {
3221 __ fildl(Address(ESP, temp_offset));
3222 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003223 }
3224 }
3225}
3226
3227void InstructionCodeGeneratorX86::GenerateRemFP(HRem *rem) {
3228 Primitive::Type type = rem->GetResultType();
3229 bool is_float = type == Primitive::kPrimFloat;
3230 size_t elem_size = Primitive::ComponentSize(type);
3231 LocationSummary* locations = rem->GetLocations();
3232 Location first = locations->InAt(0);
3233 Location second = locations->InAt(1);
3234 Location out = locations->Out();
3235
3236 // Create stack space for 2 elements.
3237 // TODO: enhance register allocator to ask for stack temporaries.
3238 __ subl(ESP, Immediate(2 * elem_size));
3239
3240 // Load the values to the FP stack in reverse order, using temporaries if needed.
Roland Levillain232ade02015-04-20 15:14:36 +01003241 const bool is_wide = !is_float;
3242 PushOntoFPStack(second, elem_size, 2 * elem_size, /* is_fp */ true, is_wide);
3243 PushOntoFPStack(first, 0, 2 * elem_size, /* is_fp */ true, is_wide);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003244
3245 // Loop doing FPREM until we stabilize.
Mark Mendell0c9497d2015-08-21 09:30:05 -04003246 NearLabel retry;
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003247 __ Bind(&retry);
3248 __ fprem();
3249
3250 // Move FP status to AX.
3251 __ fstsw();
3252
3253 // And see if the argument reduction is complete. This is signaled by the
3254 // C2 FPU flag bit set to 0.
3255 __ andl(EAX, Immediate(kC2ConditionMask));
3256 __ j(kNotEqual, &retry);
3257
3258 // We have settled on the final value. Retrieve it into an XMM register.
3259 // Store FP top of stack to real stack.
3260 if (is_float) {
3261 __ fsts(Address(ESP, 0));
3262 } else {
3263 __ fstl(Address(ESP, 0));
3264 }
3265
3266 // Pop the 2 items from the FP stack.
3267 __ fucompp();
3268
3269 // Load the value from the stack into an XMM register.
3270 DCHECK(out.IsFpuRegister()) << out;
3271 if (is_float) {
3272 __ movss(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
3273 } else {
3274 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
3275 }
3276
3277 // And remove the temporary stack space we allocated.
3278 __ addl(ESP, Immediate(2 * elem_size));
3279}
3280
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003281
3282void InstructionCodeGeneratorX86::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3283 DCHECK(instruction->IsDiv() || instruction->IsRem());
3284
3285 LocationSummary* locations = instruction->GetLocations();
3286 DCHECK(locations->InAt(1).IsConstant());
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003287 DCHECK(locations->InAt(1).GetConstant()->IsIntConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003288
3289 Register out_register = locations->Out().AsRegister<Register>();
3290 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003291 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003292
3293 DCHECK(imm == 1 || imm == -1);
3294
3295 if (instruction->IsRem()) {
3296 __ xorl(out_register, out_register);
3297 } else {
3298 __ movl(out_register, input_register);
3299 if (imm == -1) {
3300 __ negl(out_register);
3301 }
3302 }
3303}
3304
3305
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003306void InstructionCodeGeneratorX86::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003307 LocationSummary* locations = instruction->GetLocations();
3308
3309 Register out_register = locations->Out().AsRegister<Register>();
3310 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003311 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003312 DCHECK(IsPowerOfTwo(AbsOrMin(imm)));
3313 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003314
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003315 Register num = locations->GetTemp(0).AsRegister<Register>();
3316
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003317 __ leal(num, Address(input_register, abs_imm - 1));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003318 __ testl(input_register, input_register);
3319 __ cmovl(kGreaterEqual, num, input_register);
3320 int shift = CTZ(imm);
3321 __ sarl(num, Immediate(shift));
3322
3323 if (imm < 0) {
3324 __ negl(num);
3325 }
3326
3327 __ movl(out_register, num);
3328}
3329
3330void InstructionCodeGeneratorX86::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3331 DCHECK(instruction->IsDiv() || instruction->IsRem());
3332
3333 LocationSummary* locations = instruction->GetLocations();
3334 int imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
3335
3336 Register eax = locations->InAt(0).AsRegister<Register>();
3337 Register out = locations->Out().AsRegister<Register>();
3338 Register num;
3339 Register edx;
3340
3341 if (instruction->IsDiv()) {
3342 edx = locations->GetTemp(0).AsRegister<Register>();
3343 num = locations->GetTemp(1).AsRegister<Register>();
3344 } else {
3345 edx = locations->Out().AsRegister<Register>();
3346 num = locations->GetTemp(0).AsRegister<Register>();
3347 }
3348
3349 DCHECK_EQ(EAX, eax);
3350 DCHECK_EQ(EDX, edx);
3351 if (instruction->IsDiv()) {
3352 DCHECK_EQ(EAX, out);
3353 } else {
3354 DCHECK_EQ(EDX, out);
3355 }
3356
3357 int64_t magic;
3358 int shift;
3359 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
3360
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003361 // Save the numerator.
3362 __ movl(num, eax);
3363
3364 // EAX = magic
3365 __ movl(eax, Immediate(magic));
3366
3367 // EDX:EAX = magic * numerator
3368 __ imull(num);
3369
3370 if (imm > 0 && magic < 0) {
3371 // EDX += num
3372 __ addl(edx, num);
3373 } else if (imm < 0 && magic > 0) {
3374 __ subl(edx, num);
3375 }
3376
3377 // Shift if needed.
3378 if (shift != 0) {
3379 __ sarl(edx, Immediate(shift));
3380 }
3381
3382 // EDX += 1 if EDX < 0
3383 __ movl(eax, edx);
3384 __ shrl(edx, Immediate(31));
3385 __ addl(edx, eax);
3386
3387 if (instruction->IsRem()) {
3388 __ movl(eax, num);
3389 __ imull(edx, Immediate(imm));
3390 __ subl(eax, edx);
3391 __ movl(edx, eax);
3392 } else {
3393 __ movl(eax, edx);
3394 }
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003395}
3396
Calin Juravlebacfec32014-11-14 15:54:36 +00003397void InstructionCodeGeneratorX86::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3398 DCHECK(instruction->IsDiv() || instruction->IsRem());
3399
3400 LocationSummary* locations = instruction->GetLocations();
3401 Location out = locations->Out();
3402 Location first = locations->InAt(0);
3403 Location second = locations->InAt(1);
3404 bool is_div = instruction->IsDiv();
3405
3406 switch (instruction->GetResultType()) {
3407 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003408 DCHECK_EQ(EAX, first.AsRegister<Register>());
3409 DCHECK_EQ(is_div ? EAX : EDX, out.AsRegister<Register>());
Calin Juravlebacfec32014-11-14 15:54:36 +00003410
Vladimir Marko13c86fd2015-11-11 12:37:46 +00003411 if (second.IsConstant()) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003412 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003413
3414 if (imm == 0) {
3415 // Do not generate anything for 0. DivZeroCheck would forbid any generated code.
3416 } else if (imm == 1 || imm == -1) {
3417 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003418 } else if (is_div && IsPowerOfTwo(AbsOrMin(imm))) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003419 DivByPowerOfTwo(instruction->AsDiv());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003420 } else {
3421 DCHECK(imm <= -2 || imm >= 2);
3422 GenerateDivRemWithAnyConstant(instruction);
3423 }
3424 } else {
David Srbecky9cd6d372016-02-09 15:24:47 +00003425 SlowPathCode* slow_path = new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86(
3426 instruction, out.AsRegister<Register>(), is_div);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003427 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00003428
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003429 Register second_reg = second.AsRegister<Register>();
3430 // 0x80000000/-1 triggers an arithmetic exception!
3431 // Dividing by -1 is actually negation and -0x800000000 = 0x80000000 so
3432 // it's safe to just use negl instead of more complex comparisons.
Calin Juravlebacfec32014-11-14 15:54:36 +00003433
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003434 __ cmpl(second_reg, Immediate(-1));
3435 __ j(kEqual, slow_path->GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +00003436
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003437 // edx:eax <- sign-extended of eax
3438 __ cdq();
3439 // eax = quotient, edx = remainder
3440 __ idivl(second_reg);
3441 __ Bind(slow_path->GetExitLabel());
3442 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003443 break;
3444 }
3445
3446 case Primitive::kPrimLong: {
3447 InvokeRuntimeCallingConvention calling_convention;
3448 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
3449 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
3450 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
3451 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
3452 DCHECK_EQ(EAX, out.AsRegisterPairLow<Register>());
3453 DCHECK_EQ(EDX, out.AsRegisterPairHigh<Register>());
3454
3455 if (is_div) {
Alexandre Rames8158f282015-08-07 10:26:17 +01003456 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLdiv),
3457 instruction,
3458 instruction->GetDexPc(),
3459 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003460 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003461 } else {
Alexandre Rames8158f282015-08-07 10:26:17 +01003462 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLmod),
3463 instruction,
3464 instruction->GetDexPc(),
3465 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003466 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003467 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003468 break;
3469 }
3470
3471 default:
3472 LOG(FATAL) << "Unexpected type for GenerateDivRemIntegral " << instruction->GetResultType();
3473 }
3474}
3475
Calin Juravle7c4954d2014-10-28 16:57:40 +00003476void LocationsBuilderX86::VisitDiv(HDiv* div) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003477 LocationSummary::CallKind call_kind = (div->GetResultType() == Primitive::kPrimLong)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003478 ? LocationSummary::kCallOnMainOnly
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003479 : LocationSummary::kNoCall;
3480 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
3481
Calin Juravle7c4954d2014-10-28 16:57:40 +00003482 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00003483 case Primitive::kPrimInt: {
3484 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003485 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00003486 locations->SetOut(Location::SameAsFirstInput());
3487 // Intel uses edx:eax as the dividend.
3488 locations->AddTemp(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003489 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3490 // which enforces results to be in EAX and EDX, things are simpler if we use EAX also as
3491 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003492 if (div->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003493 locations->AddTemp(Location::RequiresRegister());
3494 }
Calin Juravled0d48522014-11-04 16:40:20 +00003495 break;
3496 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003497 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003498 InvokeRuntimeCallingConvention calling_convention;
3499 locations->SetInAt(0, Location::RegisterPairLocation(
3500 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3501 locations->SetInAt(1, Location::RegisterPairLocation(
3502 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3503 // Runtime helper puts the result in EAX, EDX.
3504 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Calin Juravle7c4954d2014-10-28 16:57:40 +00003505 break;
3506 }
3507 case Primitive::kPrimFloat:
3508 case Primitive::kPrimDouble: {
3509 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003510 if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3511 DCHECK(div->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003512 } else if (div->InputAt(1)->IsConstant()) {
3513 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003514 } else {
3515 locations->SetInAt(1, Location::Any());
3516 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003517 locations->SetOut(Location::SameAsFirstInput());
3518 break;
3519 }
3520
3521 default:
3522 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3523 }
3524}
3525
3526void InstructionCodeGeneratorX86::VisitDiv(HDiv* div) {
3527 LocationSummary* locations = div->GetLocations();
3528 Location first = locations->InAt(0);
3529 Location second = locations->InAt(1);
Calin Juravle7c4954d2014-10-28 16:57:40 +00003530
3531 switch (div->GetResultType()) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003532 case Primitive::kPrimInt:
Calin Juravle7c4954d2014-10-28 16:57:40 +00003533 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003534 GenerateDivRemIntegral(div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00003535 break;
3536 }
3537
3538 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003539 if (second.IsFpuRegister()) {
3540 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3541 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3542 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003543 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003544 __ divss(first.AsFpuRegister<XmmRegister>(),
3545 codegen_->LiteralFloatAddress(
3546 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3547 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3548 } else {
3549 DCHECK(second.IsStackSlot());
3550 __ divss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3551 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003552 break;
3553 }
3554
3555 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003556 if (second.IsFpuRegister()) {
3557 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3558 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3559 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003560 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003561 __ divsd(first.AsFpuRegister<XmmRegister>(),
3562 codegen_->LiteralDoubleAddress(
3563 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3564 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3565 } else {
3566 DCHECK(second.IsDoubleStackSlot());
3567 __ divsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3568 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003569 break;
3570 }
3571
3572 default:
3573 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3574 }
3575}
3576
Calin Juravlebacfec32014-11-14 15:54:36 +00003577void LocationsBuilderX86::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00003578 Primitive::Type type = rem->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003579
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003580 LocationSummary::CallKind call_kind = (rem->GetResultType() == Primitive::kPrimLong)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003581 ? LocationSummary::kCallOnMainOnly
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003582 : LocationSummary::kNoCall;
3583 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
Calin Juravlebacfec32014-11-14 15:54:36 +00003584
Calin Juravled2ec87d2014-12-08 14:24:46 +00003585 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003586 case Primitive::kPrimInt: {
3587 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003588 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00003589 locations->SetOut(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003590 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3591 // which enforces results to be in EAX and EDX, things are simpler if we use EDX also as
3592 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003593 if (rem->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003594 locations->AddTemp(Location::RequiresRegister());
3595 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003596 break;
3597 }
3598 case Primitive::kPrimLong: {
3599 InvokeRuntimeCallingConvention calling_convention;
3600 locations->SetInAt(0, Location::RegisterPairLocation(
3601 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3602 locations->SetInAt(1, Location::RegisterPairLocation(
3603 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3604 // Runtime helper puts the result in EAX, EDX.
3605 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
3606 break;
3607 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003608 case Primitive::kPrimDouble:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003609 case Primitive::kPrimFloat: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003610 locations->SetInAt(0, Location::Any());
3611 locations->SetInAt(1, Location::Any());
3612 locations->SetOut(Location::RequiresFpuRegister());
3613 locations->AddTemp(Location::RegisterLocation(EAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00003614 break;
3615 }
3616
3617 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003618 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003619 }
3620}
3621
3622void InstructionCodeGeneratorX86::VisitRem(HRem* rem) {
3623 Primitive::Type type = rem->GetResultType();
3624 switch (type) {
3625 case Primitive::kPrimInt:
3626 case Primitive::kPrimLong: {
3627 GenerateDivRemIntegral(rem);
3628 break;
3629 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003630 case Primitive::kPrimFloat:
Calin Juravlebacfec32014-11-14 15:54:36 +00003631 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003632 GenerateRemFP(rem);
Calin Juravlebacfec32014-11-14 15:54:36 +00003633 break;
3634 }
3635 default:
3636 LOG(FATAL) << "Unexpected rem type " << type;
3637 }
3638}
3639
Calin Juravled0d48522014-11-04 16:40:20 +00003640void LocationsBuilderX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00003641 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
3642 ? LocationSummary::kCallOnSlowPath
3643 : LocationSummary::kNoCall;
3644 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003645 switch (instruction->GetType()) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003646 case Primitive::kPrimBoolean:
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003647 case Primitive::kPrimByte:
3648 case Primitive::kPrimChar:
3649 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003650 case Primitive::kPrimInt: {
3651 locations->SetInAt(0, Location::Any());
3652 break;
3653 }
3654 case Primitive::kPrimLong: {
3655 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
3656 if (!instruction->IsConstant()) {
3657 locations->AddTemp(Location::RequiresRegister());
3658 }
3659 break;
3660 }
3661 default:
3662 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
3663 }
Calin Juravled0d48522014-11-04 16:40:20 +00003664 if (instruction->HasUses()) {
3665 locations->SetOut(Location::SameAsFirstInput());
3666 }
3667}
3668
3669void InstructionCodeGeneratorX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003670 SlowPathCode* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86(instruction);
Calin Juravled0d48522014-11-04 16:40:20 +00003671 codegen_->AddSlowPath(slow_path);
3672
3673 LocationSummary* locations = instruction->GetLocations();
3674 Location value = locations->InAt(0);
3675
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003676 switch (instruction->GetType()) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003677 case Primitive::kPrimBoolean:
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003678 case Primitive::kPrimByte:
3679 case Primitive::kPrimChar:
3680 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003681 case Primitive::kPrimInt: {
3682 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003683 __ testl(value.AsRegister<Register>(), value.AsRegister<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003684 __ j(kEqual, slow_path->GetEntryLabel());
3685 } else if (value.IsStackSlot()) {
3686 __ cmpl(Address(ESP, value.GetStackIndex()), Immediate(0));
3687 __ j(kEqual, slow_path->GetEntryLabel());
3688 } else {
3689 DCHECK(value.IsConstant()) << value;
3690 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
3691 __ jmp(slow_path->GetEntryLabel());
3692 }
3693 }
3694 break;
Calin Juravled0d48522014-11-04 16:40:20 +00003695 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003696 case Primitive::kPrimLong: {
3697 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003698 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003699 __ movl(temp, value.AsRegisterPairLow<Register>());
3700 __ orl(temp, value.AsRegisterPairHigh<Register>());
3701 __ j(kEqual, slow_path->GetEntryLabel());
3702 } else {
3703 DCHECK(value.IsConstant()) << value;
3704 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
3705 __ jmp(slow_path->GetEntryLabel());
3706 }
3707 }
3708 break;
3709 }
3710 default:
3711 LOG(FATAL) << "Unexpected type for HDivZeroCheck" << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00003712 }
Calin Juravled0d48522014-11-04 16:40:20 +00003713}
3714
Calin Juravle9aec02f2014-11-18 23:06:35 +00003715void LocationsBuilderX86::HandleShift(HBinaryOperation* op) {
3716 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3717
3718 LocationSummary* locations =
3719 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
3720
3721 switch (op->GetResultType()) {
Mark P Mendell73945692015-04-29 14:56:17 +00003722 case Primitive::kPrimInt:
Calin Juravle9aec02f2014-11-18 23:06:35 +00003723 case Primitive::kPrimLong: {
Mark P Mendell73945692015-04-29 14:56:17 +00003724 // Can't have Location::Any() and output SameAsFirstInput()
Calin Juravle9aec02f2014-11-18 23:06:35 +00003725 locations->SetInAt(0, Location::RequiresRegister());
Mark P Mendell73945692015-04-29 14:56:17 +00003726 // The shift count needs to be in CL or a constant.
3727 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, op->InputAt(1)));
Calin Juravle9aec02f2014-11-18 23:06:35 +00003728 locations->SetOut(Location::SameAsFirstInput());
3729 break;
3730 }
3731 default:
3732 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
3733 }
3734}
3735
3736void InstructionCodeGeneratorX86::HandleShift(HBinaryOperation* op) {
3737 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3738
3739 LocationSummary* locations = op->GetLocations();
3740 Location first = locations->InAt(0);
3741 Location second = locations->InAt(1);
3742 DCHECK(first.Equals(locations->Out()));
3743
3744 switch (op->GetResultType()) {
3745 case Primitive::kPrimInt: {
Mark P Mendell73945692015-04-29 14:56:17 +00003746 DCHECK(first.IsRegister());
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003747 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003748 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003749 Register second_reg = second.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003750 DCHECK_EQ(ECX, second_reg);
3751 if (op->IsShl()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003752 __ shll(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003753 } else if (op->IsShr()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003754 __ sarl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003755 } else {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003756 __ shrl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003757 }
3758 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003759 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance;
Mark P Mendell73945692015-04-29 14:56:17 +00003760 if (shift == 0) {
3761 return;
3762 }
3763 Immediate imm(shift);
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003764 if (op->IsShl()) {
3765 __ shll(first_reg, imm);
3766 } else if (op->IsShr()) {
3767 __ sarl(first_reg, imm);
3768 } else {
3769 __ shrl(first_reg, imm);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003770 }
3771 }
3772 break;
3773 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003774 case Primitive::kPrimLong: {
Mark P Mendell73945692015-04-29 14:56:17 +00003775 if (second.IsRegister()) {
3776 Register second_reg = second.AsRegister<Register>();
3777 DCHECK_EQ(ECX, second_reg);
3778 if (op->IsShl()) {
3779 GenerateShlLong(first, second_reg);
3780 } else if (op->IsShr()) {
3781 GenerateShrLong(first, second_reg);
3782 } else {
3783 GenerateUShrLong(first, second_reg);
3784 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003785 } else {
Mark P Mendell73945692015-04-29 14:56:17 +00003786 // Shift by a constant.
Roland Levillain5b5b9312016-03-22 14:57:31 +00003787 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance;
Mark P Mendell73945692015-04-29 14:56:17 +00003788 // Nothing to do if the shift is 0, as the input is already the output.
3789 if (shift != 0) {
3790 if (op->IsShl()) {
3791 GenerateShlLong(first, shift);
3792 } else if (op->IsShr()) {
3793 GenerateShrLong(first, shift);
3794 } else {
3795 GenerateUShrLong(first, shift);
3796 }
3797 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003798 }
3799 break;
3800 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003801 default:
3802 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
3803 }
3804}
3805
Mark P Mendell73945692015-04-29 14:56:17 +00003806void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, int shift) {
3807 Register low = loc.AsRegisterPairLow<Register>();
3808 Register high = loc.AsRegisterPairHigh<Register>();
Mark Mendellba56d062015-05-05 21:34:03 -04003809 if (shift == 1) {
3810 // This is just an addition.
3811 __ addl(low, low);
3812 __ adcl(high, high);
3813 } else if (shift == 32) {
Mark P Mendell73945692015-04-29 14:56:17 +00003814 // Shift by 32 is easy. High gets low, and low gets 0.
3815 codegen_->EmitParallelMoves(
3816 loc.ToLow(),
3817 loc.ToHigh(),
3818 Primitive::kPrimInt,
3819 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
3820 loc.ToLow(),
3821 Primitive::kPrimInt);
3822 } else if (shift > 32) {
3823 // Low part becomes 0. High part is low part << (shift-32).
3824 __ movl(high, low);
3825 __ shll(high, Immediate(shift - 32));
3826 __ xorl(low, low);
3827 } else {
3828 // Between 1 and 31.
3829 __ shld(high, low, Immediate(shift));
3830 __ shll(low, Immediate(shift));
3831 }
3832}
3833
Calin Juravle9aec02f2014-11-18 23:06:35 +00003834void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04003835 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00003836 __ shld(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>(), shifter);
3837 __ shll(loc.AsRegisterPairLow<Register>(), shifter);
3838 __ testl(shifter, Immediate(32));
3839 __ j(kEqual, &done);
3840 __ movl(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>());
3841 __ movl(loc.AsRegisterPairLow<Register>(), Immediate(0));
3842 __ Bind(&done);
3843}
3844
Mark P Mendell73945692015-04-29 14:56:17 +00003845void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, int shift) {
3846 Register low = loc.AsRegisterPairLow<Register>();
3847 Register high = loc.AsRegisterPairHigh<Register>();
3848 if (shift == 32) {
3849 // Need to copy the sign.
3850 DCHECK_NE(low, high);
3851 __ movl(low, high);
3852 __ sarl(high, Immediate(31));
3853 } else if (shift > 32) {
3854 DCHECK_NE(low, high);
3855 // High part becomes sign. Low part is shifted by shift - 32.
3856 __ movl(low, high);
3857 __ sarl(high, Immediate(31));
3858 __ sarl(low, Immediate(shift - 32));
3859 } else {
3860 // Between 1 and 31.
3861 __ shrd(low, high, Immediate(shift));
3862 __ sarl(high, Immediate(shift));
3863 }
3864}
3865
Calin Juravle9aec02f2014-11-18 23:06:35 +00003866void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04003867 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00003868 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
3869 __ sarl(loc.AsRegisterPairHigh<Register>(), shifter);
3870 __ testl(shifter, Immediate(32));
3871 __ j(kEqual, &done);
3872 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
3873 __ sarl(loc.AsRegisterPairHigh<Register>(), Immediate(31));
3874 __ Bind(&done);
3875}
3876
Mark P Mendell73945692015-04-29 14:56:17 +00003877void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, int shift) {
3878 Register low = loc.AsRegisterPairLow<Register>();
3879 Register high = loc.AsRegisterPairHigh<Register>();
3880 if (shift == 32) {
3881 // Shift by 32 is easy. Low gets high, and high gets 0.
3882 codegen_->EmitParallelMoves(
3883 loc.ToHigh(),
3884 loc.ToLow(),
3885 Primitive::kPrimInt,
3886 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
3887 loc.ToHigh(),
3888 Primitive::kPrimInt);
3889 } else if (shift > 32) {
3890 // Low part is high >> (shift - 32). High part becomes 0.
3891 __ movl(low, high);
3892 __ shrl(low, Immediate(shift - 32));
3893 __ xorl(high, high);
3894 } else {
3895 // Between 1 and 31.
3896 __ shrd(low, high, Immediate(shift));
3897 __ shrl(high, Immediate(shift));
3898 }
3899}
3900
Calin Juravle9aec02f2014-11-18 23:06:35 +00003901void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04003902 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00003903 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
3904 __ shrl(loc.AsRegisterPairHigh<Register>(), shifter);
3905 __ testl(shifter, Immediate(32));
3906 __ j(kEqual, &done);
3907 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
3908 __ movl(loc.AsRegisterPairHigh<Register>(), Immediate(0));
3909 __ Bind(&done);
3910}
3911
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003912void LocationsBuilderX86::VisitRor(HRor* ror) {
3913 LocationSummary* locations =
3914 new (GetGraph()->GetArena()) LocationSummary(ror, LocationSummary::kNoCall);
3915
3916 switch (ror->GetResultType()) {
3917 case Primitive::kPrimLong:
3918 // Add the temporary needed.
3919 locations->AddTemp(Location::RequiresRegister());
3920 FALLTHROUGH_INTENDED;
3921 case Primitive::kPrimInt:
3922 locations->SetInAt(0, Location::RequiresRegister());
3923 // The shift count needs to be in CL (unless it is a constant).
3924 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, ror->InputAt(1)));
3925 locations->SetOut(Location::SameAsFirstInput());
3926 break;
3927 default:
3928 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
3929 UNREACHABLE();
3930 }
3931}
3932
3933void InstructionCodeGeneratorX86::VisitRor(HRor* ror) {
3934 LocationSummary* locations = ror->GetLocations();
3935 Location first = locations->InAt(0);
3936 Location second = locations->InAt(1);
3937
3938 if (ror->GetResultType() == Primitive::kPrimInt) {
3939 Register first_reg = first.AsRegister<Register>();
3940 if (second.IsRegister()) {
3941 Register second_reg = second.AsRegister<Register>();
3942 __ rorl(first_reg, second_reg);
3943 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003944 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003945 __ rorl(first_reg, imm);
3946 }
3947 return;
3948 }
3949
3950 DCHECK_EQ(ror->GetResultType(), Primitive::kPrimLong);
3951 Register first_reg_lo = first.AsRegisterPairLow<Register>();
3952 Register first_reg_hi = first.AsRegisterPairHigh<Register>();
3953 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
3954 if (second.IsRegister()) {
3955 Register second_reg = second.AsRegister<Register>();
3956 DCHECK_EQ(second_reg, ECX);
3957 __ movl(temp_reg, first_reg_hi);
3958 __ shrd(first_reg_hi, first_reg_lo, second_reg);
3959 __ shrd(first_reg_lo, temp_reg, second_reg);
3960 __ movl(temp_reg, first_reg_hi);
3961 __ testl(second_reg, Immediate(32));
3962 __ cmovl(kNotEqual, first_reg_hi, first_reg_lo);
3963 __ cmovl(kNotEqual, first_reg_lo, temp_reg);
3964 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003965 int32_t shift_amt = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance;
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003966 if (shift_amt == 0) {
3967 // Already fine.
3968 return;
3969 }
3970 if (shift_amt == 32) {
3971 // Just swap.
3972 __ movl(temp_reg, first_reg_lo);
3973 __ movl(first_reg_lo, first_reg_hi);
3974 __ movl(first_reg_hi, temp_reg);
3975 return;
3976 }
3977
3978 Immediate imm(shift_amt);
3979 // Save the constents of the low value.
3980 __ movl(temp_reg, first_reg_lo);
3981
3982 // Shift right into low, feeding bits from high.
3983 __ shrd(first_reg_lo, first_reg_hi, imm);
3984
3985 // Shift right into high, feeding bits from the original low.
3986 __ shrd(first_reg_hi, temp_reg, imm);
3987
3988 // Swap if needed.
3989 if (shift_amt > 32) {
3990 __ movl(temp_reg, first_reg_lo);
3991 __ movl(first_reg_lo, first_reg_hi);
3992 __ movl(first_reg_hi, temp_reg);
3993 }
3994 }
3995}
3996
Calin Juravle9aec02f2014-11-18 23:06:35 +00003997void LocationsBuilderX86::VisitShl(HShl* shl) {
3998 HandleShift(shl);
3999}
4000
4001void InstructionCodeGeneratorX86::VisitShl(HShl* shl) {
4002 HandleShift(shl);
4003}
4004
4005void LocationsBuilderX86::VisitShr(HShr* shr) {
4006 HandleShift(shr);
4007}
4008
4009void InstructionCodeGeneratorX86::VisitShr(HShr* shr) {
4010 HandleShift(shr);
4011}
4012
4013void LocationsBuilderX86::VisitUShr(HUShr* ushr) {
4014 HandleShift(ushr);
4015}
4016
4017void InstructionCodeGeneratorX86::VisitUShr(HUShr* ushr) {
4018 HandleShift(ushr);
4019}
4020
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004021void LocationsBuilderX86::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004022 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004023 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004024 locations->SetOut(Location::RegisterLocation(EAX));
David Brazdil6de19382016-01-08 17:37:10 +00004025 if (instruction->IsStringAlloc()) {
4026 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
4027 } else {
4028 InvokeRuntimeCallingConvention calling_convention;
4029 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4030 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
4031 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004032}
4033
4034void InstructionCodeGeneratorX86::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01004035 // Note: if heap poisoning is enabled, the entry point takes cares
4036 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00004037 if (instruction->IsStringAlloc()) {
4038 // String is allocated through StringFactory. Call NewEmptyString entry point.
4039 Register temp = instruction->GetLocations()->GetTemp(0).AsRegister<Register>();
4040 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize);
4041 __ fs()->movl(temp, Address::Absolute(QUICK_ENTRY_POINT(pNewEmptyString)));
4042 __ call(Address(temp, code_offset.Int32Value()));
4043 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4044 } else {
4045 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
4046 instruction,
4047 instruction->GetDexPc(),
4048 nullptr);
4049 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
4050 DCHECK(!codegen_->IsLeafMethod());
4051 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004052}
4053
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004054void LocationsBuilderX86::VisitNewArray(HNewArray* instruction) {
4055 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004056 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004057 locations->SetOut(Location::RegisterLocation(EAX));
4058 InvokeRuntimeCallingConvention calling_convention;
4059 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08004060 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004061 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004062}
4063
4064void InstructionCodeGeneratorX86::VisitNewArray(HNewArray* instruction) {
4065 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004066 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
Roland Levillain4d027112015-07-01 15:41:14 +01004067 // Note: if heap poisoning is enabled, the entry point takes cares
4068 // of poisoning the reference.
Calin Juravle175dc732015-08-25 15:42:32 +01004069 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
4070 instruction,
4071 instruction->GetDexPc(),
4072 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00004073 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004074 DCHECK(!codegen_->IsLeafMethod());
4075}
4076
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004077void LocationsBuilderX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004078 LocationSummary* locations =
4079 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01004080 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
4081 if (location.IsStackSlot()) {
4082 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4083 } else if (location.IsDoubleStackSlot()) {
4084 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004085 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01004086 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004087}
4088
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004089void InstructionCodeGeneratorX86::VisitParameterValue(
4090 HParameterValue* instruction ATTRIBUTE_UNUSED) {
4091}
4092
4093void LocationsBuilderX86::VisitCurrentMethod(HCurrentMethod* instruction) {
4094 LocationSummary* locations =
4095 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4096 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
4097}
4098
4099void InstructionCodeGeneratorX86::VisitCurrentMethod(HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004100}
4101
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004102void LocationsBuilderX86::VisitClassTableGet(HClassTableGet* instruction) {
4103 LocationSummary* locations =
4104 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4105 locations->SetInAt(0, Location::RequiresRegister());
4106 locations->SetOut(Location::RequiresRegister());
4107}
4108
4109void InstructionCodeGeneratorX86::VisitClassTableGet(HClassTableGet* instruction) {
4110 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004111 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004112 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004113 instruction->GetIndex(), kX86PointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004114 __ movl(locations->Out().AsRegister<Register>(),
4115 Address(locations->InAt(0).AsRegister<Register>(), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004116 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004117 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00004118 instruction->GetIndex() % ImTable::kSize, kX86PointerSize));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004119 __ movl(locations->Out().AsRegister<Register>(),
4120 Address(locations->InAt(0).AsRegister<Register>(),
4121 mirror::Class::ImtPtrOffset(kX86PointerSize).Uint32Value()));
4122 // temp = temp->GetImtEntryAt(method_offset);
4123 __ movl(locations->Out().AsRegister<Register>(),
4124 Address(locations->Out().AsRegister<Register>(), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004125 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004126}
4127
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004128void LocationsBuilderX86::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004129 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004130 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01004131 locations->SetInAt(0, Location::RequiresRegister());
4132 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004133}
4134
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004135void InstructionCodeGeneratorX86::VisitNot(HNot* not_) {
4136 LocationSummary* locations = not_->GetLocations();
Roland Levillain70566432014-10-24 16:20:17 +01004137 Location in = locations->InAt(0);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01004138 Location out = locations->Out();
Roland Levillain70566432014-10-24 16:20:17 +01004139 DCHECK(in.Equals(out));
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00004140 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004141 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00004142 __ notl(out.AsRegister<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004143 break;
4144
4145 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01004146 __ notl(out.AsRegisterPairLow<Register>());
4147 __ notl(out.AsRegisterPairHigh<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004148 break;
4149
4150 default:
4151 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
4152 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004153}
4154
David Brazdil66d126e2015-04-03 16:02:44 +01004155void LocationsBuilderX86::VisitBooleanNot(HBooleanNot* bool_not) {
4156 LocationSummary* locations =
4157 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
4158 locations->SetInAt(0, Location::RequiresRegister());
4159 locations->SetOut(Location::SameAsFirstInput());
4160}
4161
4162void InstructionCodeGeneratorX86::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01004163 LocationSummary* locations = bool_not->GetLocations();
4164 Location in = locations->InAt(0);
4165 Location out = locations->Out();
4166 DCHECK(in.Equals(out));
4167 __ xorl(out.AsRegister<Register>(), Immediate(1));
4168}
4169
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004170void LocationsBuilderX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004171 LocationSummary* locations =
4172 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00004173 switch (compare->InputAt(0)->GetType()) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00004174 case Primitive::kPrimBoolean:
4175 case Primitive::kPrimByte:
4176 case Primitive::kPrimShort:
4177 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08004178 case Primitive::kPrimInt:
Calin Juravleddb7df22014-11-25 20:56:51 +00004179 case Primitive::kPrimLong: {
4180 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravleddb7df22014-11-25 20:56:51 +00004181 locations->SetInAt(1, Location::Any());
4182 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4183 break;
4184 }
4185 case Primitive::kPrimFloat:
4186 case Primitive::kPrimDouble: {
4187 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004188 if (compare->InputAt(1)->IsX86LoadFromConstantTable()) {
4189 DCHECK(compare->InputAt(1)->IsEmittedAtUseSite());
4190 } else if (compare->InputAt(1)->IsConstant()) {
4191 locations->SetInAt(1, Location::RequiresFpuRegister());
4192 } else {
4193 locations->SetInAt(1, Location::Any());
4194 }
Calin Juravleddb7df22014-11-25 20:56:51 +00004195 locations->SetOut(Location::RequiresRegister());
4196 break;
4197 }
4198 default:
4199 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
4200 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004201}
4202
4203void InstructionCodeGeneratorX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004204 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004205 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00004206 Location left = locations->InAt(0);
4207 Location right = locations->InAt(1);
4208
Mark Mendell0c9497d2015-08-21 09:30:05 -04004209 NearLabel less, greater, done;
Aart Bika19616e2016-02-01 18:57:58 -08004210 Condition less_cond = kLess;
4211
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004212 switch (compare->InputAt(0)->GetType()) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00004213 case Primitive::kPrimBoolean:
4214 case Primitive::kPrimByte:
4215 case Primitive::kPrimShort:
4216 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08004217 case Primitive::kPrimInt: {
Mark Mendell0c5b18e2016-02-06 13:58:35 -05004218 GenerateIntCompare(left, right);
Aart Bika19616e2016-02-01 18:57:58 -08004219 break;
4220 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004221 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004222 Register left_low = left.AsRegisterPairLow<Register>();
4223 Register left_high = left.AsRegisterPairHigh<Register>();
4224 int32_t val_low = 0;
4225 int32_t val_high = 0;
4226 bool right_is_const = false;
4227
4228 if (right.IsConstant()) {
4229 DCHECK(right.GetConstant()->IsLongConstant());
4230 right_is_const = true;
4231 int64_t val = right.GetConstant()->AsLongConstant()->GetValue();
4232 val_low = Low32Bits(val);
4233 val_high = High32Bits(val);
4234 }
4235
Calin Juravleddb7df22014-11-25 20:56:51 +00004236 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004237 __ cmpl(left_high, right.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004238 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004239 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004240 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004241 DCHECK(right_is_const) << right;
Aart Bika19616e2016-02-01 18:57:58 -08004242 codegen_->Compare32BitValue(left_high, val_high);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004243 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004244 __ j(kLess, &less); // Signed compare.
4245 __ j(kGreater, &greater); // Signed compare.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004246 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004247 __ cmpl(left_low, right.AsRegisterPairLow<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004248 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004249 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004250 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004251 DCHECK(right_is_const) << right;
Aart Bika19616e2016-02-01 18:57:58 -08004252 codegen_->Compare32BitValue(left_low, val_low);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004253 }
Aart Bika19616e2016-02-01 18:57:58 -08004254 less_cond = kBelow; // for CF (unsigned).
Calin Juravleddb7df22014-11-25 20:56:51 +00004255 break;
4256 }
4257 case Primitive::kPrimFloat: {
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004258 GenerateFPCompare(left, right, compare, false);
Calin Juravleddb7df22014-11-25 20:56:51 +00004259 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08004260 less_cond = kBelow; // for CF (floats).
Calin Juravleddb7df22014-11-25 20:56:51 +00004261 break;
4262 }
4263 case Primitive::kPrimDouble: {
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004264 GenerateFPCompare(left, right, compare, true);
Calin Juravleddb7df22014-11-25 20:56:51 +00004265 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08004266 less_cond = kBelow; // for CF (floats).
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004267 break;
4268 }
4269 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00004270 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004271 }
Aart Bika19616e2016-02-01 18:57:58 -08004272
Calin Juravleddb7df22014-11-25 20:56:51 +00004273 __ movl(out, Immediate(0));
4274 __ j(kEqual, &done);
Aart Bika19616e2016-02-01 18:57:58 -08004275 __ j(less_cond, &less);
Calin Juravleddb7df22014-11-25 20:56:51 +00004276
4277 __ Bind(&greater);
4278 __ movl(out, Immediate(1));
4279 __ jmp(&done);
4280
4281 __ Bind(&less);
4282 __ movl(out, Immediate(-1));
4283
4284 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004285}
4286
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004287void LocationsBuilderX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004288 LocationSummary* locations =
4289 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko372f10e2016-05-17 16:30:10 +01004290 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01004291 locations->SetInAt(i, Location::Any());
4292 }
4293 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004294}
4295
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004296void InstructionCodeGeneratorX86::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004297 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004298}
4299
Roland Levillain7c1559a2015-12-15 10:55:36 +00004300void CodeGeneratorX86::GenerateMemoryBarrier(MemBarrierKind kind) {
Calin Juravle52c48962014-12-16 17:02:57 +00004301 /*
4302 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
4303 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
4304 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
4305 */
4306 switch (kind) {
4307 case MemBarrierKind::kAnyAny: {
Mark P Mendell17077d82015-12-16 19:15:59 +00004308 MemoryFence();
Calin Juravle52c48962014-12-16 17:02:57 +00004309 break;
4310 }
4311 case MemBarrierKind::kAnyStore:
4312 case MemBarrierKind::kLoadAny:
4313 case MemBarrierKind::kStoreStore: {
4314 // nop
4315 break;
4316 }
Mark Mendell7aa04a12016-01-27 22:39:07 -05004317 case MemBarrierKind::kNTStoreStore:
4318 // Non-Temporal Store/Store needs an explicit fence.
4319 MemoryFence(/* non-temporal */ true);
4320 break;
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004321 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004322}
4323
Vladimir Markodc151b22015-10-15 18:02:30 +01004324HInvokeStaticOrDirect::DispatchInfo CodeGeneratorX86::GetSupportedInvokeStaticOrDirectDispatch(
4325 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
4326 MethodReference target_method ATTRIBUTE_UNUSED) {
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00004327 HInvokeStaticOrDirect::DispatchInfo dispatch_info = desired_dispatch_info;
4328
4329 // We disable pc-relative load when there is an irreducible loop, as the optimization
4330 // is incompatible with it.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004331 // TODO: Create as many X86ComputeBaseMethodAddress instructions
4332 // as needed for methods with irreducible loops.
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00004333 if (GetGraph()->HasIrreducibleLoops() &&
4334 (dispatch_info.method_load_kind ==
4335 HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative)) {
4336 dispatch_info.method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod;
4337 }
4338 switch (dispatch_info.code_ptr_location) {
Vladimir Markodc151b22015-10-15 18:02:30 +01004339 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
4340 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
4341 // For direct code, we actually prefer to call via the code pointer from ArtMethod*.
4342 // (Though the direct CALL ptr16:32 is available for consideration).
4343 return HInvokeStaticOrDirect::DispatchInfo {
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00004344 dispatch_info.method_load_kind,
Vladimir Markodc151b22015-10-15 18:02:30 +01004345 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00004346 dispatch_info.method_load_data,
Vladimir Markodc151b22015-10-15 18:02:30 +01004347 0u
4348 };
4349 default:
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00004350 return dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01004351 }
4352}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004353
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004354Register CodeGeneratorX86::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
4355 Register temp) {
4356 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
Vladimir Markoc53c0792015-11-19 15:48:33 +00004357 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004358 if (!invoke->GetLocations()->Intrinsified()) {
4359 return location.AsRegister<Register>();
4360 }
4361 // For intrinsics we allow any location, so it may be on the stack.
4362 if (!location.IsRegister()) {
4363 __ movl(temp, Address(ESP, location.GetStackIndex()));
4364 return temp;
4365 }
4366 // For register locations, check if the register was saved. If so, get it from the stack.
4367 // Note: There is a chance that the register was saved but not overwritten, so we could
4368 // save one load. However, since this is just an intrinsic slow path we prefer this
4369 // simple and more robust approach rather that trying to determine if that's the case.
4370 SlowPathCode* slow_path = GetCurrentSlowPath();
Serguei Katkov288c7a82016-05-16 11:53:15 +06004371 if (slow_path != nullptr) {
4372 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
4373 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
4374 __ movl(temp, Address(ESP, stack_offset));
4375 return temp;
4376 }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004377 }
4378 return location.AsRegister<Register>();
4379}
4380
Serguei Katkov288c7a82016-05-16 11:53:15 +06004381Location CodeGeneratorX86::GenerateCalleeMethodStaticOrDirectCall(HInvokeStaticOrDirect* invoke,
4382 Location temp) {
Vladimir Marko58155012015-08-19 12:49:41 +00004383 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
4384 switch (invoke->GetMethodLoadKind()) {
4385 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
4386 // temp = thread->string_init_entrypoint
4387 __ fs()->movl(temp.AsRegister<Register>(), Address::Absolute(invoke->GetStringInitOffset()));
4388 break;
4389 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00004390 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004391 break;
4392 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
4393 __ movl(temp.AsRegister<Register>(), Immediate(invoke->GetMethodAddress()));
4394 break;
4395 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004396 __ movl(temp.AsRegister<Register>(), Immediate(/* placeholder */ 0));
Vladimir Marko58155012015-08-19 12:49:41 +00004397 method_patches_.emplace_back(invoke->GetTargetMethod());
4398 __ Bind(&method_patches_.back().label); // Bind the label at the end of the "movl" insn.
4399 break;
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004400 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
4401 Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
4402 temp.AsRegister<Register>());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004403 __ movl(temp.AsRegister<Register>(), Address(base_reg, kDummy32BitOffset));
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004404 // Bind a new fixup label at the end of the "movl" insn.
4405 uint32_t offset = invoke->GetDexCacheArrayOffset();
4406 __ Bind(NewPcRelativeDexCacheArrayPatch(*invoke->GetTargetMethod().dex_file, offset));
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004407 break;
4408 }
Vladimir Marko58155012015-08-19 12:49:41 +00004409 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004410 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004411 Register method_reg;
4412 Register reg = temp.AsRegister<Register>();
4413 if (current_method.IsRegister()) {
4414 method_reg = current_method.AsRegister<Register>();
4415 } else {
David Brazdil58282f42016-01-14 12:45:10 +00004416 DCHECK(invoke->GetLocations()->Intrinsified());
Vladimir Marko58155012015-08-19 12:49:41 +00004417 DCHECK(!current_method.IsValid());
4418 method_reg = reg;
4419 __ movl(reg, Address(ESP, kCurrentMethodStackOffset));
4420 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00004421 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +01004422 __ movl(reg, Address(method_reg,
4423 ArtMethod::DexCacheResolvedMethodsOffset(kX86PointerSize).Int32Value()));
Vladimir Marko40ecb122016-04-06 17:33:41 +01004424 // temp = temp[index_in_cache];
4425 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
4426 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Vladimir Marko58155012015-08-19 12:49:41 +00004427 __ movl(reg, Address(reg, CodeGenerator::GetCachePointerOffset(index_in_cache)));
4428 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +01004429 }
Vladimir Marko58155012015-08-19 12:49:41 +00004430 }
Serguei Katkov288c7a82016-05-16 11:53:15 +06004431 return callee_method;
4432}
4433
4434void CodeGeneratorX86::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
4435 Location callee_method = GenerateCalleeMethodStaticOrDirectCall(invoke, temp);
Vladimir Marko58155012015-08-19 12:49:41 +00004436
4437 switch (invoke->GetCodePtrLocation()) {
4438 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
4439 __ call(GetFrameEntryLabel());
4440 break;
4441 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative: {
4442 relative_call_patches_.emplace_back(invoke->GetTargetMethod());
4443 Label* label = &relative_call_patches_.back().label;
4444 __ call(label); // Bind to the patch label, override at link time.
4445 __ Bind(label); // Bind the label at the end of the "call" insn.
4446 break;
4447 }
4448 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
4449 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
Vladimir Markodc151b22015-10-15 18:02:30 +01004450 // Filtered out by GetSupportedInvokeStaticOrDirectDispatch().
4451 LOG(FATAL) << "Unsupported";
4452 UNREACHABLE();
Vladimir Marko58155012015-08-19 12:49:41 +00004453 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
4454 // (callee_method + offset_of_quick_compiled_code)()
4455 __ call(Address(callee_method.AsRegister<Register>(),
4456 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
4457 kX86WordSize).Int32Value()));
4458 break;
Mark Mendell09ed1a32015-03-25 08:30:06 -04004459 }
4460
4461 DCHECK(!IsLeafMethod());
Mark Mendell09ed1a32015-03-25 08:30:06 -04004462}
4463
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004464void CodeGeneratorX86::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_in) {
4465 Register temp = temp_in.AsRegister<Register>();
4466 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
4467 invoke->GetVTableIndex(), kX86PointerSize).Uint32Value();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004468
4469 // Use the calling convention instead of the location of the receiver, as
4470 // intrinsics may have put the receiver in a different register. In the intrinsics
4471 // slow path, the arguments have been moved to the right place, so here we are
4472 // guaranteed that the receiver is the first register of the calling convention.
4473 InvokeDexCallingConvention calling_convention;
4474 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004475 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004476 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004477 __ movl(temp, Address(receiver, class_offset));
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004478 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00004479 // Instead of simply (possibly) unpoisoning `temp` here, we should
4480 // emit a read barrier for the previous class reference load.
4481 // However this is not required in practice, as this is an
4482 // intermediate/temporary reference and because the current
4483 // concurrent copying collector keeps the from-space memory
4484 // intact/accessible until the end of the marking phase (the
4485 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004486 __ MaybeUnpoisonHeapReference(temp);
4487 // temp = temp->GetMethodAt(method_offset);
4488 __ movl(temp, Address(temp, method_offset));
4489 // call temp->GetEntryPoint();
4490 __ call(Address(
4491 temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize).Int32Value()));
4492}
4493
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004494void CodeGeneratorX86::RecordSimplePatch() {
4495 if (GetCompilerOptions().GetIncludePatchInformation()) {
4496 simple_patches_.emplace_back();
4497 __ Bind(&simple_patches_.back());
4498 }
4499}
4500
4501void CodeGeneratorX86::RecordStringPatch(HLoadString* load_string) {
4502 string_patches_.emplace_back(load_string->GetDexFile(), load_string->GetStringIndex());
4503 __ Bind(&string_patches_.back().label);
4504}
4505
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004506void CodeGeneratorX86::RecordTypePatch(HLoadClass* load_class) {
4507 type_patches_.emplace_back(load_class->GetDexFile(), load_class->GetTypeIndex());
4508 __ Bind(&type_patches_.back().label);
4509}
4510
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004511Label* CodeGeneratorX86::NewPcRelativeDexCacheArrayPatch(const DexFile& dex_file,
4512 uint32_t element_offset) {
4513 // Add the patch entry and bind its label at the end of the instruction.
4514 pc_relative_dex_cache_patches_.emplace_back(dex_file, element_offset);
4515 return &pc_relative_dex_cache_patches_.back().label;
4516}
4517
Vladimir Marko58155012015-08-19 12:49:41 +00004518void CodeGeneratorX86::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
4519 DCHECK(linker_patches->empty());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004520 size_t size =
4521 method_patches_.size() +
4522 relative_call_patches_.size() +
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004523 pc_relative_dex_cache_patches_.size() +
4524 simple_patches_.size() +
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004525 string_patches_.size() +
4526 type_patches_.size();
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004527 linker_patches->reserve(size);
4528 // The label points to the end of the "movl" insn but the literal offset for method
4529 // patch needs to point to the embedded constant which occupies the last 4 bytes.
4530 constexpr uint32_t kLabelPositionToLiteralOffsetAdjustment = 4u;
Vladimir Marko58155012015-08-19 12:49:41 +00004531 for (const MethodPatchInfo<Label>& info : method_patches_) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004532 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Marko58155012015-08-19 12:49:41 +00004533 linker_patches->push_back(LinkerPatch::MethodPatch(literal_offset,
4534 info.target_method.dex_file,
4535 info.target_method.dex_method_index));
4536 }
4537 for (const MethodPatchInfo<Label>& info : relative_call_patches_) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004538 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Marko58155012015-08-19 12:49:41 +00004539 linker_patches->push_back(LinkerPatch::RelativeCodePatch(literal_offset,
4540 info.target_method.dex_file,
4541 info.target_method.dex_method_index));
4542 }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004543 for (const PcRelativeDexCacheAccessInfo& info : pc_relative_dex_cache_patches_) {
4544 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
4545 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(literal_offset,
4546 &info.target_dex_file,
4547 GetMethodAddressOffset(),
4548 info.element_offset));
4549 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004550 for (const Label& label : simple_patches_) {
4551 uint32_t literal_offset = label.Position() - kLabelPositionToLiteralOffsetAdjustment;
4552 linker_patches->push_back(LinkerPatch::RecordPosition(literal_offset));
4553 }
4554 if (GetCompilerOptions().GetCompilePic()) {
4555 for (const StringPatchInfo<Label>& info : string_patches_) {
4556 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
4557 linker_patches->push_back(LinkerPatch::RelativeStringPatch(literal_offset,
4558 &info.dex_file,
4559 GetMethodAddressOffset(),
4560 info.string_index));
4561 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004562 for (const TypePatchInfo<Label>& info : type_patches_) {
4563 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
4564 linker_patches->push_back(LinkerPatch::RelativeTypePatch(literal_offset,
4565 &info.dex_file,
4566 GetMethodAddressOffset(),
4567 info.type_index));
4568 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004569 } else {
4570 for (const StringPatchInfo<Label>& info : string_patches_) {
4571 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
4572 linker_patches->push_back(LinkerPatch::StringPatch(literal_offset,
4573 &info.dex_file,
4574 info.string_index));
4575 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004576 for (const TypePatchInfo<Label>& info : type_patches_) {
4577 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
4578 linker_patches->push_back(LinkerPatch::TypePatch(literal_offset,
4579 &info.dex_file,
4580 info.type_index));
4581 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004582 }
Vladimir Marko58155012015-08-19 12:49:41 +00004583}
4584
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004585void CodeGeneratorX86::MarkGCCard(Register temp,
4586 Register card,
4587 Register object,
4588 Register value,
4589 bool value_can_be_null) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004590 NearLabel is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004591 if (value_can_be_null) {
4592 __ testl(value, value);
4593 __ j(kEqual, &is_null);
4594 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004595 __ fs()->movl(card, Address::Absolute(Thread::CardTableOffset<kX86WordSize>().Int32Value()));
4596 __ movl(temp, object);
4597 __ shrl(temp, Immediate(gc::accounting::CardTable::kCardShift));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00004598 __ movb(Address(temp, card, TIMES_1, 0),
4599 X86ManagedRegister::FromCpuRegister(card).AsByteRegister());
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004600 if (value_can_be_null) {
4601 __ Bind(&is_null);
4602 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004603}
4604
Calin Juravle52c48962014-12-16 17:02:57 +00004605void LocationsBuilderX86::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
4606 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain0d5a2812015-11-13 10:07:31 +00004607
4608 bool object_field_get_with_read_barrier =
4609 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004610 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00004611 new (GetGraph()->GetArena()) LocationSummary(instruction,
4612 kEmitCompilerReadBarrier ?
4613 LocationSummary::kCallOnSlowPath :
4614 LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004615 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004616
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004617 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4618 locations->SetOut(Location::RequiresFpuRegister());
4619 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004620 // The output overlaps in case of long: we don't want the low move
4621 // to overwrite the object's location. Likewise, in the case of
4622 // an object field get with read barriers enabled, we do not want
4623 // the move to overwrite the object's location, as we need it to emit
4624 // the read barrier.
4625 locations->SetOut(
4626 Location::RequiresRegister(),
4627 (object_field_get_with_read_barrier || instruction->GetType() == Primitive::kPrimLong) ?
4628 Location::kOutputOverlap :
4629 Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004630 }
Calin Juravle52c48962014-12-16 17:02:57 +00004631
4632 if (field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong)) {
4633 // Long values can be loaded atomically into an XMM using movsd.
Roland Levillain7c1559a2015-12-15 10:55:36 +00004634 // So we use an XMM register as a temp to achieve atomicity (first
4635 // load the temp into the XMM and then copy the XMM into the
4636 // output, 32 bits at a time).
Calin Juravle52c48962014-12-16 17:02:57 +00004637 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain7c1559a2015-12-15 10:55:36 +00004638 } else if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
4639 // We need a temporary register for the read barrier marking slow
4640 // path in CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier.
4641 locations->AddTemp(Location::RequiresRegister());
Calin Juravle52c48962014-12-16 17:02:57 +00004642 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004643}
4644
Calin Juravle52c48962014-12-16 17:02:57 +00004645void InstructionCodeGeneratorX86::HandleFieldGet(HInstruction* instruction,
4646 const FieldInfo& field_info) {
4647 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004648
Calin Juravle52c48962014-12-16 17:02:57 +00004649 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004650 Location base_loc = locations->InAt(0);
4651 Register base = base_loc.AsRegister<Register>();
Calin Juravle52c48962014-12-16 17:02:57 +00004652 Location out = locations->Out();
4653 bool is_volatile = field_info.IsVolatile();
4654 Primitive::Type field_type = field_info.GetFieldType();
4655 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4656
4657 switch (field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004658 case Primitive::kPrimBoolean: {
Calin Juravle52c48962014-12-16 17:02:57 +00004659 __ movzxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004660 break;
4661 }
4662
4663 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00004664 __ movsxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004665 break;
4666 }
4667
4668 case Primitive::kPrimShort: {
Calin Juravle52c48962014-12-16 17:02:57 +00004669 __ movsxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004670 break;
4671 }
4672
4673 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00004674 __ movzxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004675 break;
4676 }
4677
4678 case Primitive::kPrimInt:
Calin Juravle52c48962014-12-16 17:02:57 +00004679 __ movl(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004680 break;
Roland Levillain7c1559a2015-12-15 10:55:36 +00004681
4682 case Primitive::kPrimNot: {
4683 // /* HeapReference<Object> */ out = *(base + offset)
4684 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
4685 Location temp_loc = locations->GetTemp(0);
4686 // Note that a potential implicit null check is handled in this
4687 // CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier call.
4688 codegen_->GenerateFieldLoadWithBakerReadBarrier(
4689 instruction, out, base, offset, temp_loc, /* needs_null_check */ true);
4690 if (is_volatile) {
4691 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4692 }
4693 } else {
4694 __ movl(out.AsRegister<Register>(), Address(base, offset));
4695 codegen_->MaybeRecordImplicitNullCheck(instruction);
4696 if (is_volatile) {
4697 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4698 }
4699 // If read barriers are enabled, emit read barriers other than
4700 // Baker's using a slow path (and also unpoison the loaded
4701 // reference, if heap poisoning is enabled).
4702 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
4703 }
4704 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004705 }
4706
4707 case Primitive::kPrimLong: {
Calin Juravle52c48962014-12-16 17:02:57 +00004708 if (is_volatile) {
4709 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4710 __ movsd(temp, Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004711 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004712 __ movd(out.AsRegisterPairLow<Register>(), temp);
4713 __ psrlq(temp, Immediate(32));
4714 __ movd(out.AsRegisterPairHigh<Register>(), temp);
4715 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004716 DCHECK_NE(base, out.AsRegisterPairLow<Register>());
Calin Juravle52c48962014-12-16 17:02:57 +00004717 __ movl(out.AsRegisterPairLow<Register>(), Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004718 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004719 __ movl(out.AsRegisterPairHigh<Register>(), Address(base, kX86WordSize + offset));
4720 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004721 break;
4722 }
4723
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004724 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00004725 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004726 break;
4727 }
4728
4729 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00004730 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004731 break;
4732 }
4733
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004734 case Primitive::kPrimVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00004735 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004736 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004737 }
Calin Juravle52c48962014-12-16 17:02:57 +00004738
Roland Levillain7c1559a2015-12-15 10:55:36 +00004739 if (field_type == Primitive::kPrimNot || field_type == Primitive::kPrimLong) {
4740 // Potential implicit null checks, in the case of reference or
4741 // long fields, are handled in the previous switch statement.
4742 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00004743 codegen_->MaybeRecordImplicitNullCheck(instruction);
4744 }
4745
Calin Juravle52c48962014-12-16 17:02:57 +00004746 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004747 if (field_type == Primitive::kPrimNot) {
4748 // Memory barriers, in the case of references, are also handled
4749 // in the previous switch statement.
4750 } else {
4751 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4752 }
Roland Levillain4d027112015-07-01 15:41:14 +01004753 }
Calin Juravle52c48962014-12-16 17:02:57 +00004754}
4755
4756void LocationsBuilderX86::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
4757 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4758
4759 LocationSummary* locations =
4760 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4761 locations->SetInAt(0, Location::RequiresRegister());
4762 bool is_volatile = field_info.IsVolatile();
4763 Primitive::Type field_type = field_info.GetFieldType();
4764 bool is_byte_type = (field_type == Primitive::kPrimBoolean)
4765 || (field_type == Primitive::kPrimByte);
4766
4767 // The register allocator does not support multiple
4768 // inputs that die at entry with one in a specific register.
4769 if (is_byte_type) {
4770 // Ensure the value is in a byte register.
4771 locations->SetInAt(1, Location::RegisterLocation(EAX));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004772 } else if (Primitive::IsFloatingPointType(field_type)) {
Mark Mendell81489372015-11-04 11:30:41 -05004773 if (is_volatile && field_type == Primitive::kPrimDouble) {
4774 // In order to satisfy the semantics of volatile, this must be a single instruction store.
4775 locations->SetInAt(1, Location::RequiresFpuRegister());
4776 } else {
4777 locations->SetInAt(1, Location::FpuRegisterOrConstant(instruction->InputAt(1)));
4778 }
4779 } else if (is_volatile && field_type == Primitive::kPrimLong) {
4780 // In order to satisfy the semantics of volatile, this must be a single instruction store.
Calin Juravle52c48962014-12-16 17:02:57 +00004781 locations->SetInAt(1, Location::RequiresRegister());
Mark Mendell81489372015-11-04 11:30:41 -05004782
Calin Juravle52c48962014-12-16 17:02:57 +00004783 // 64bits value can be atomically written to an address with movsd and an XMM register.
4784 // We need two XMM registers because there's no easier way to (bit) copy a register pair
4785 // into a single XMM register (we copy each pair part into the XMMs and then interleave them).
4786 // NB: We could make the register allocator understand fp_reg <-> core_reg moves but given the
4787 // isolated cases when we need this it isn't worth adding the extra complexity.
4788 locations->AddTemp(Location::RequiresFpuRegister());
4789 locations->AddTemp(Location::RequiresFpuRegister());
Mark Mendell81489372015-11-04 11:30:41 -05004790 } else {
4791 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4792
4793 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
4794 // Temporary registers for the write barrier.
4795 locations->AddTemp(Location::RequiresRegister()); // May be used for reference poisoning too.
4796 // Ensure the card is in a byte register.
4797 locations->AddTemp(Location::RegisterLocation(ECX));
4798 }
Calin Juravle52c48962014-12-16 17:02:57 +00004799 }
4800}
4801
4802void InstructionCodeGeneratorX86::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004803 const FieldInfo& field_info,
4804 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00004805 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4806
4807 LocationSummary* locations = instruction->GetLocations();
4808 Register base = locations->InAt(0).AsRegister<Register>();
4809 Location value = locations->InAt(1);
4810 bool is_volatile = field_info.IsVolatile();
4811 Primitive::Type field_type = field_info.GetFieldType();
4812 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01004813 bool needs_write_barrier =
4814 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00004815
4816 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004817 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
Calin Juravle52c48962014-12-16 17:02:57 +00004818 }
4819
Mark Mendell81489372015-11-04 11:30:41 -05004820 bool maybe_record_implicit_null_check_done = false;
4821
Calin Juravle52c48962014-12-16 17:02:57 +00004822 switch (field_type) {
4823 case Primitive::kPrimBoolean:
4824 case Primitive::kPrimByte: {
4825 __ movb(Address(base, offset), value.AsRegister<ByteRegister>());
4826 break;
4827 }
4828
4829 case Primitive::kPrimShort:
4830 case Primitive::kPrimChar: {
Mark Mendell81489372015-11-04 11:30:41 -05004831 if (value.IsConstant()) {
4832 int16_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4833 __ movw(Address(base, offset), Immediate(v));
4834 } else {
4835 __ movw(Address(base, offset), value.AsRegister<Register>());
4836 }
Calin Juravle52c48962014-12-16 17:02:57 +00004837 break;
4838 }
4839
4840 case Primitive::kPrimInt:
4841 case Primitive::kPrimNot: {
Roland Levillain4d027112015-07-01 15:41:14 +01004842 if (kPoisonHeapReferences && needs_write_barrier) {
4843 // Note that in the case where `value` is a null reference,
4844 // we do not enter this block, as the reference does not
4845 // need poisoning.
4846 DCHECK_EQ(field_type, Primitive::kPrimNot);
4847 Register temp = locations->GetTemp(0).AsRegister<Register>();
4848 __ movl(temp, value.AsRegister<Register>());
4849 __ PoisonHeapReference(temp);
4850 __ movl(Address(base, offset), temp);
Mark Mendell81489372015-11-04 11:30:41 -05004851 } else if (value.IsConstant()) {
4852 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4853 __ movl(Address(base, offset), Immediate(v));
Roland Levillain4d027112015-07-01 15:41:14 +01004854 } else {
Nicolas Geoffray03971632016-03-17 10:44:24 +00004855 DCHECK(value.IsRegister()) << value;
Roland Levillain4d027112015-07-01 15:41:14 +01004856 __ movl(Address(base, offset), value.AsRegister<Register>());
4857 }
Calin Juravle52c48962014-12-16 17:02:57 +00004858 break;
4859 }
4860
4861 case Primitive::kPrimLong: {
4862 if (is_volatile) {
4863 XmmRegister temp1 = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4864 XmmRegister temp2 = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
4865 __ movd(temp1, value.AsRegisterPairLow<Register>());
4866 __ movd(temp2, value.AsRegisterPairHigh<Register>());
4867 __ punpckldq(temp1, temp2);
4868 __ movsd(Address(base, offset), temp1);
Calin Juravle77520bc2015-01-12 18:45:46 +00004869 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell81489372015-11-04 11:30:41 -05004870 } else if (value.IsConstant()) {
4871 int64_t v = CodeGenerator::GetInt64ValueOf(value.GetConstant());
4872 __ movl(Address(base, offset), Immediate(Low32Bits(v)));
4873 codegen_->MaybeRecordImplicitNullCheck(instruction);
4874 __ movl(Address(base, kX86WordSize + offset), Immediate(High32Bits(v)));
Calin Juravle52c48962014-12-16 17:02:57 +00004875 } else {
4876 __ movl(Address(base, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00004877 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004878 __ movl(Address(base, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
4879 }
Mark Mendell81489372015-11-04 11:30:41 -05004880 maybe_record_implicit_null_check_done = true;
Calin Juravle52c48962014-12-16 17:02:57 +00004881 break;
4882 }
4883
4884 case Primitive::kPrimFloat: {
Mark Mendell81489372015-11-04 11:30:41 -05004885 if (value.IsConstant()) {
4886 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4887 __ movl(Address(base, offset), Immediate(v));
4888 } else {
4889 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4890 }
Calin Juravle52c48962014-12-16 17:02:57 +00004891 break;
4892 }
4893
4894 case Primitive::kPrimDouble: {
Mark Mendell81489372015-11-04 11:30:41 -05004895 if (value.IsConstant()) {
4896 int64_t v = CodeGenerator::GetInt64ValueOf(value.GetConstant());
4897 __ movl(Address(base, offset), Immediate(Low32Bits(v)));
4898 codegen_->MaybeRecordImplicitNullCheck(instruction);
4899 __ movl(Address(base, kX86WordSize + offset), Immediate(High32Bits(v)));
4900 maybe_record_implicit_null_check_done = true;
4901 } else {
4902 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4903 }
Calin Juravle52c48962014-12-16 17:02:57 +00004904 break;
4905 }
4906
4907 case Primitive::kPrimVoid:
4908 LOG(FATAL) << "Unreachable type " << field_type;
4909 UNREACHABLE();
4910 }
4911
Mark Mendell81489372015-11-04 11:30:41 -05004912 if (!maybe_record_implicit_null_check_done) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004913 codegen_->MaybeRecordImplicitNullCheck(instruction);
4914 }
4915
Roland Levillain4d027112015-07-01 15:41:14 +01004916 if (needs_write_barrier) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004917 Register temp = locations->GetTemp(0).AsRegister<Register>();
4918 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004919 codegen_->MarkGCCard(temp, card, base, value.AsRegister<Register>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00004920 }
4921
Calin Juravle52c48962014-12-16 17:02:57 +00004922 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004923 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
Calin Juravle52c48962014-12-16 17:02:57 +00004924 }
4925}
4926
4927void LocationsBuilderX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4928 HandleFieldGet(instruction, instruction->GetFieldInfo());
4929}
4930
4931void InstructionCodeGeneratorX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4932 HandleFieldGet(instruction, instruction->GetFieldInfo());
4933}
4934
4935void LocationsBuilderX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
4936 HandleFieldSet(instruction, instruction->GetFieldInfo());
4937}
4938
4939void InstructionCodeGeneratorX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004940 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00004941}
4942
4943void LocationsBuilderX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
4944 HandleFieldSet(instruction, instruction->GetFieldInfo());
4945}
4946
4947void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004948 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00004949}
4950
4951void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4952 HandleFieldGet(instruction, instruction->GetFieldInfo());
4953}
4954
4955void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4956 HandleFieldGet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004957}
4958
Calin Juravlee460d1d2015-09-29 04:52:17 +01004959void LocationsBuilderX86::VisitUnresolvedInstanceFieldGet(
4960 HUnresolvedInstanceFieldGet* instruction) {
4961 FieldAccessCallingConventionX86 calling_convention;
4962 codegen_->CreateUnresolvedFieldLocationSummary(
4963 instruction, instruction->GetFieldType(), calling_convention);
4964}
4965
4966void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldGet(
4967 HUnresolvedInstanceFieldGet* instruction) {
4968 FieldAccessCallingConventionX86 calling_convention;
4969 codegen_->GenerateUnresolvedFieldAccess(instruction,
4970 instruction->GetFieldType(),
4971 instruction->GetFieldIndex(),
4972 instruction->GetDexPc(),
4973 calling_convention);
4974}
4975
4976void LocationsBuilderX86::VisitUnresolvedInstanceFieldSet(
4977 HUnresolvedInstanceFieldSet* instruction) {
4978 FieldAccessCallingConventionX86 calling_convention;
4979 codegen_->CreateUnresolvedFieldLocationSummary(
4980 instruction, instruction->GetFieldType(), calling_convention);
4981}
4982
4983void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldSet(
4984 HUnresolvedInstanceFieldSet* instruction) {
4985 FieldAccessCallingConventionX86 calling_convention;
4986 codegen_->GenerateUnresolvedFieldAccess(instruction,
4987 instruction->GetFieldType(),
4988 instruction->GetFieldIndex(),
4989 instruction->GetDexPc(),
4990 calling_convention);
4991}
4992
4993void LocationsBuilderX86::VisitUnresolvedStaticFieldGet(
4994 HUnresolvedStaticFieldGet* instruction) {
4995 FieldAccessCallingConventionX86 calling_convention;
4996 codegen_->CreateUnresolvedFieldLocationSummary(
4997 instruction, instruction->GetFieldType(), calling_convention);
4998}
4999
5000void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldGet(
5001 HUnresolvedStaticFieldGet* instruction) {
5002 FieldAccessCallingConventionX86 calling_convention;
5003 codegen_->GenerateUnresolvedFieldAccess(instruction,
5004 instruction->GetFieldType(),
5005 instruction->GetFieldIndex(),
5006 instruction->GetDexPc(),
5007 calling_convention);
5008}
5009
5010void LocationsBuilderX86::VisitUnresolvedStaticFieldSet(
5011 HUnresolvedStaticFieldSet* instruction) {
5012 FieldAccessCallingConventionX86 calling_convention;
5013 codegen_->CreateUnresolvedFieldLocationSummary(
5014 instruction, instruction->GetFieldType(), calling_convention);
5015}
5016
5017void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldSet(
5018 HUnresolvedStaticFieldSet* instruction) {
5019 FieldAccessCallingConventionX86 calling_convention;
5020 codegen_->GenerateUnresolvedFieldAccess(instruction,
5021 instruction->GetFieldType(),
5022 instruction->GetFieldIndex(),
5023 instruction->GetDexPc(),
5024 calling_convention);
5025}
5026
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005027void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00005028 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
5029 ? LocationSummary::kCallOnSlowPath
5030 : LocationSummary::kNoCall;
5031 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
5032 Location loc = codegen_->IsImplicitNullCheckAllowed(instruction)
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005033 ? Location::RequiresRegister()
5034 : Location::Any();
5035 locations->SetInAt(0, loc);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005036 if (instruction->HasUses()) {
5037 locations->SetOut(Location::SameAsFirstInput());
5038 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005039}
5040
Calin Juravle2ae48182016-03-16 14:05:09 +00005041void CodeGeneratorX86::GenerateImplicitNullCheck(HNullCheck* instruction) {
5042 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005043 return;
5044 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005045 LocationSummary* locations = instruction->GetLocations();
5046 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00005047
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005048 __ testl(EAX, Address(obj.AsRegister<Register>(), 0));
Calin Juravle2ae48182016-03-16 14:05:09 +00005049 RecordPcInfo(instruction, instruction->GetDexPc());
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005050}
5051
Calin Juravle2ae48182016-03-16 14:05:09 +00005052void CodeGeneratorX86::GenerateExplicitNullCheck(HNullCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07005053 SlowPathCode* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00005054 AddSlowPath(slow_path);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005055
5056 LocationSummary* locations = instruction->GetLocations();
5057 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005058
5059 if (obj.IsRegister()) {
Mark Mendell42514f62015-03-31 11:34:22 -04005060 __ testl(obj.AsRegister<Register>(), obj.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005061 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005062 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005063 } else {
5064 DCHECK(obj.IsConstant()) << obj;
David Brazdil77a48ae2015-09-15 12:34:04 +00005065 DCHECK(obj.GetConstant()->IsNullConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005066 __ jmp(slow_path->GetEntryLabel());
5067 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005068 }
5069 __ j(kEqual, slow_path->GetEntryLabel());
5070}
5071
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005072void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00005073 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005074}
5075
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005076void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005077 bool object_array_get_with_read_barrier =
5078 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005079 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00005080 new (GetGraph()->GetArena()) LocationSummary(instruction,
5081 object_array_get_with_read_barrier ?
5082 LocationSummary::kCallOnSlowPath :
5083 LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005084 locations->SetInAt(0, Location::RequiresRegister());
5085 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005086 if (Primitive::IsFloatingPointType(instruction->GetType())) {
5087 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5088 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005089 // The output overlaps in case of long: we don't want the low move
5090 // to overwrite the array's location. Likewise, in the case of an
5091 // object array get with read barriers enabled, we do not want the
5092 // move to overwrite the array's location, as we need it to emit
5093 // the read barrier.
5094 locations->SetOut(
5095 Location::RequiresRegister(),
5096 (instruction->GetType() == Primitive::kPrimLong || object_array_get_with_read_barrier) ?
5097 Location::kOutputOverlap :
5098 Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005099 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00005100 // We need a temporary register for the read barrier marking slow
5101 // path in CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier.
5102 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
5103 locations->AddTemp(Location::RequiresRegister());
5104 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005105}
5106
5107void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
5108 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005109 Location obj_loc = locations->InAt(0);
5110 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005111 Location index = locations->InAt(1);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005112 Location out_loc = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005113 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005114
Calin Juravle77520bc2015-01-12 18:45:46 +00005115 Primitive::Type type = instruction->GetType();
5116 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005117 case Primitive::kPrimBoolean: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005118 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005119 if (index.IsConstant()) {
5120 __ movzxb(out, Address(obj,
5121 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
5122 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005123 __ movzxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005124 }
5125 break;
5126 }
5127
5128 case Primitive::kPrimByte: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005129 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005130 if (index.IsConstant()) {
5131 __ movsxb(out, Address(obj,
5132 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
5133 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005134 __ movsxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005135 }
5136 break;
5137 }
5138
5139 case Primitive::kPrimShort: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005140 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005141 if (index.IsConstant()) {
5142 __ movsxw(out, Address(obj,
5143 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
5144 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005145 __ movsxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005146 }
5147 break;
5148 }
5149
5150 case Primitive::kPrimChar: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005151 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005152 if (index.IsConstant()) {
5153 __ movzxw(out, Address(obj,
5154 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
5155 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005156 __ movzxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005157 }
5158 break;
5159 }
5160
Roland Levillain7c1559a2015-12-15 10:55:36 +00005161 case Primitive::kPrimInt: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005162 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005163 if (index.IsConstant()) {
5164 __ movl(out, Address(obj,
5165 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
5166 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005167 __ movl(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005168 }
5169 break;
5170 }
5171
Roland Levillain7c1559a2015-12-15 10:55:36 +00005172 case Primitive::kPrimNot: {
5173 static_assert(
5174 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
5175 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain7c1559a2015-12-15 10:55:36 +00005176 // /* HeapReference<Object> */ out =
5177 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
5178 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
5179 Location temp = locations->GetTemp(0);
5180 // Note that a potential implicit null check is handled in this
5181 // CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier call.
5182 codegen_->GenerateArrayLoadWithBakerReadBarrier(
5183 instruction, out_loc, obj, data_offset, index, temp, /* needs_null_check */ true);
5184 } else {
5185 Register out = out_loc.AsRegister<Register>();
5186 if (index.IsConstant()) {
5187 uint32_t offset =
5188 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
5189 __ movl(out, Address(obj, offset));
5190 codegen_->MaybeRecordImplicitNullCheck(instruction);
5191 // If read barriers are enabled, emit read barriers other than
5192 // Baker's using a slow path (and also unpoison the loaded
5193 // reference, if heap poisoning is enabled).
5194 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
5195 } else {
5196 __ movl(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
5197 codegen_->MaybeRecordImplicitNullCheck(instruction);
5198 // If read barriers are enabled, emit read barriers other than
5199 // Baker's using a slow path (and also unpoison the loaded
5200 // reference, if heap poisoning is enabled).
5201 codegen_->MaybeGenerateReadBarrierSlow(
5202 instruction, out_loc, out_loc, obj_loc, data_offset, index);
5203 }
5204 }
5205 break;
5206 }
5207
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005208 case Primitive::kPrimLong: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005209 DCHECK_NE(obj, out_loc.AsRegisterPairLow<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005210 if (index.IsConstant()) {
5211 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Roland Levillain7c1559a2015-12-15 10:55:36 +00005212 __ movl(out_loc.AsRegisterPairLow<Register>(), Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005213 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005214 __ movl(out_loc.AsRegisterPairHigh<Register>(), Address(obj, offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005215 } else {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005216 __ movl(out_loc.AsRegisterPairLow<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005217 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005218 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005219 __ movl(out_loc.AsRegisterPairHigh<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005220 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005221 }
5222 break;
5223 }
5224
Mark Mendell7c8d0092015-01-26 11:21:33 -05005225 case Primitive::kPrimFloat: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005226 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Mark Mendell7c8d0092015-01-26 11:21:33 -05005227 if (index.IsConstant()) {
5228 __ movss(out, Address(obj,
5229 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
5230 } else {
5231 __ movss(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
5232 }
5233 break;
5234 }
5235
5236 case Primitive::kPrimDouble: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005237 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Mark Mendell7c8d0092015-01-26 11:21:33 -05005238 if (index.IsConstant()) {
5239 __ movsd(out, Address(obj,
5240 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
5241 } else {
5242 __ movsd(out, Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
5243 }
5244 break;
5245 }
5246
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005247 case Primitive::kPrimVoid:
Calin Juravle77520bc2015-01-12 18:45:46 +00005248 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07005249 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005250 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005251
Roland Levillain7c1559a2015-12-15 10:55:36 +00005252 if (type == Primitive::kPrimNot || type == Primitive::kPrimLong) {
5253 // Potential implicit null checks, in the case of reference or
5254 // long arrays, are handled in the previous switch statement.
5255 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00005256 codegen_->MaybeRecordImplicitNullCheck(instruction);
5257 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005258}
5259
5260void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01005261 Primitive::Type value_type = instruction->GetComponentType();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005262
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005263 bool needs_write_barrier =
5264 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Roland Levillain0d5a2812015-11-13 10:07:31 +00005265 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
5266 bool object_array_set_with_read_barrier =
5267 kEmitCompilerReadBarrier && (value_type == Primitive::kPrimNot);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005268
Nicolas Geoffray39468442014-09-02 15:17:15 +01005269 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
5270 instruction,
Roland Levillain0d5a2812015-11-13 10:07:31 +00005271 (may_need_runtime_call_for_type_check || object_array_set_with_read_barrier) ?
5272 LocationSummary::kCallOnSlowPath :
5273 LocationSummary::kNoCall);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005274
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005275 bool is_byte_type = (value_type == Primitive::kPrimBoolean)
5276 || (value_type == Primitive::kPrimByte);
5277 // We need the inputs to be different than the output in case of long operation.
5278 // In case of a byte operation, the register allocator does not support multiple
5279 // inputs that die at entry with one in a specific register.
5280 locations->SetInAt(0, Location::RequiresRegister());
5281 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
5282 if (is_byte_type) {
5283 // Ensure the value is in a byte register.
5284 locations->SetInAt(2, Location::ByteRegisterOrConstant(EAX, instruction->InputAt(2)));
5285 } else if (Primitive::IsFloatingPointType(value_type)) {
Mark Mendell81489372015-11-04 11:30:41 -05005286 locations->SetInAt(2, Location::FpuRegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005287 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005288 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
5289 }
5290 if (needs_write_barrier) {
5291 // Temporary registers for the write barrier.
5292 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
5293 // Ensure the card is in a byte register.
Roland Levillain4f6b0b52015-11-23 19:29:22 +00005294 locations->AddTemp(Location::RegisterLocation(ECX));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005295 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005296}
5297
5298void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
5299 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005300 Location array_loc = locations->InAt(0);
5301 Register array = array_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005302 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005303 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005304 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005305 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
5306 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5307 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005308 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005309 bool needs_write_barrier =
5310 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005311
5312 switch (value_type) {
5313 case Primitive::kPrimBoolean:
5314 case Primitive::kPrimByte: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005315 uint32_t offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
5316 Address address = index.IsConstant()
5317 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + offset)
5318 : Address(array, index.AsRegister<Register>(), TIMES_1, offset);
5319 if (value.IsRegister()) {
5320 __ movb(address, value.AsRegister<ByteRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005321 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005322 __ movb(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005323 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005324 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005325 break;
5326 }
5327
5328 case Primitive::kPrimShort:
5329 case Primitive::kPrimChar: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005330 uint32_t offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
5331 Address address = index.IsConstant()
5332 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + offset)
5333 : Address(array, index.AsRegister<Register>(), TIMES_2, offset);
5334 if (value.IsRegister()) {
5335 __ movw(address, value.AsRegister<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005336 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005337 __ movw(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005338 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005339 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005340 break;
5341 }
5342
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005343 case Primitive::kPrimNot: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005344 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
5345 Address address = index.IsConstant()
5346 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
5347 : Address(array, index.AsRegister<Register>(), TIMES_4, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005348
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005349 if (!value.IsRegister()) {
5350 // Just setting null.
5351 DCHECK(instruction->InputAt(2)->IsNullConstant());
5352 DCHECK(value.IsConstant()) << value;
5353 __ movl(address, Immediate(0));
Calin Juravle77520bc2015-01-12 18:45:46 +00005354 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005355 DCHECK(!needs_write_barrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005356 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005357 break;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005358 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005359
5360 DCHECK(needs_write_barrier);
5361 Register register_value = value.AsRegister<Register>();
5362 NearLabel done, not_null, do_put;
5363 SlowPathCode* slow_path = nullptr;
5364 Register temp = locations->GetTemp(0).AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005365 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005366 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathX86(instruction);
5367 codegen_->AddSlowPath(slow_path);
5368 if (instruction->GetValueCanBeNull()) {
5369 __ testl(register_value, register_value);
5370 __ j(kNotEqual, &not_null);
5371 __ movl(address, Immediate(0));
5372 codegen_->MaybeRecordImplicitNullCheck(instruction);
5373 __ jmp(&done);
5374 __ Bind(&not_null);
5375 }
5376
Roland Levillain0d5a2812015-11-13 10:07:31 +00005377 if (kEmitCompilerReadBarrier) {
5378 // When read barriers are enabled, the type checking
5379 // instrumentation requires two read barriers:
5380 //
5381 // __ movl(temp2, temp);
5382 // // /* HeapReference<Class> */ temp = temp->component_type_
5383 // __ movl(temp, Address(temp, component_offset));
Roland Levillain7c1559a2015-12-15 10:55:36 +00005384 // codegen_->GenerateReadBarrierSlow(
Roland Levillain0d5a2812015-11-13 10:07:31 +00005385 // instruction, temp_loc, temp_loc, temp2_loc, component_offset);
5386 //
5387 // // /* HeapReference<Class> */ temp2 = register_value->klass_
5388 // __ movl(temp2, Address(register_value, class_offset));
Roland Levillain7c1559a2015-12-15 10:55:36 +00005389 // codegen_->GenerateReadBarrierSlow(
Roland Levillain0d5a2812015-11-13 10:07:31 +00005390 // instruction, temp2_loc, temp2_loc, value, class_offset, temp_loc);
5391 //
5392 // __ cmpl(temp, temp2);
5393 //
5394 // However, the second read barrier may trash `temp`, as it
5395 // is a temporary register, and as such would not be saved
5396 // along with live registers before calling the runtime (nor
5397 // restored afterwards). So in this case, we bail out and
5398 // delegate the work to the array set slow path.
5399 //
5400 // TODO: Extend the register allocator to support a new
5401 // "(locally) live temp" location so as to avoid always
5402 // going into the slow path when read barriers are enabled.
5403 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005404 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005405 // /* HeapReference<Class> */ temp = array->klass_
5406 __ movl(temp, Address(array, class_offset));
5407 codegen_->MaybeRecordImplicitNullCheck(instruction);
5408 __ MaybeUnpoisonHeapReference(temp);
5409
5410 // /* HeapReference<Class> */ temp = temp->component_type_
5411 __ movl(temp, Address(temp, component_offset));
5412 // If heap poisoning is enabled, no need to unpoison `temp`
5413 // nor the object reference in `register_value->klass`, as
5414 // we are comparing two poisoned references.
5415 __ cmpl(temp, Address(register_value, class_offset));
5416
5417 if (instruction->StaticTypeOfArrayIsObjectArray()) {
5418 __ j(kEqual, &do_put);
5419 // If heap poisoning is enabled, the `temp` reference has
5420 // not been unpoisoned yet; unpoison it now.
5421 __ MaybeUnpoisonHeapReference(temp);
5422
5423 // /* HeapReference<Class> */ temp = temp->super_class_
5424 __ movl(temp, Address(temp, super_offset));
5425 // If heap poisoning is enabled, no need to unpoison
5426 // `temp`, as we are comparing against null below.
5427 __ testl(temp, temp);
5428 __ j(kNotEqual, slow_path->GetEntryLabel());
5429 __ Bind(&do_put);
5430 } else {
5431 __ j(kNotEqual, slow_path->GetEntryLabel());
5432 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005433 }
5434 }
5435
5436 if (kPoisonHeapReferences) {
5437 __ movl(temp, register_value);
5438 __ PoisonHeapReference(temp);
5439 __ movl(address, temp);
5440 } else {
5441 __ movl(address, register_value);
5442 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005443 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005444 codegen_->MaybeRecordImplicitNullCheck(instruction);
5445 }
5446
5447 Register card = locations->GetTemp(1).AsRegister<Register>();
5448 codegen_->MarkGCCard(
5449 temp, card, array, value.AsRegister<Register>(), instruction->GetValueCanBeNull());
5450 __ Bind(&done);
5451
5452 if (slow_path != nullptr) {
5453 __ Bind(slow_path->GetExitLabel());
5454 }
5455
5456 break;
5457 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005458
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005459 case Primitive::kPrimInt: {
5460 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
5461 Address address = index.IsConstant()
5462 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
5463 : Address(array, index.AsRegister<Register>(), TIMES_4, offset);
5464 if (value.IsRegister()) {
5465 __ movl(address, value.AsRegister<Register>());
5466 } else {
5467 DCHECK(value.IsConstant()) << value;
5468 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
5469 __ movl(address, Immediate(v));
5470 }
5471 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005472 break;
5473 }
5474
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005475 case Primitive::kPrimLong: {
5476 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005477 if (index.IsConstant()) {
5478 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005479 if (value.IsRegisterPair()) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005480 __ movl(Address(array, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00005481 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005482 __ movl(Address(array, offset + kX86WordSize), value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005483 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005484 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005485 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005486 __ movl(Address(array, offset), Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00005487 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005488 __ movl(Address(array, offset + kX86WordSize), Immediate(High32Bits(val)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005489 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005490 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005491 if (value.IsRegisterPair()) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005492 __ movl(Address(array, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005493 value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00005494 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005495 __ movl(Address(array, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005496 value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005497 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005498 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005499 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005500 __ movl(Address(array, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005501 Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00005502 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005503 __ movl(Address(array, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005504 Immediate(High32Bits(val)));
5505 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005506 }
5507 break;
5508 }
5509
Mark Mendell7c8d0092015-01-26 11:21:33 -05005510 case Primitive::kPrimFloat: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005511 uint32_t offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
5512 Address address = index.IsConstant()
5513 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
5514 : Address(array, index.AsRegister<Register>(), TIMES_4, offset);
Mark Mendell81489372015-11-04 11:30:41 -05005515 if (value.IsFpuRegister()) {
5516 __ movss(address, value.AsFpuRegister<XmmRegister>());
5517 } else {
5518 DCHECK(value.IsConstant());
5519 int32_t v = bit_cast<int32_t, float>(value.GetConstant()->AsFloatConstant()->GetValue());
5520 __ movl(address, Immediate(v));
5521 }
5522 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005523 break;
5524 }
5525
5526 case Primitive::kPrimDouble: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005527 uint32_t offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
5528 Address address = index.IsConstant()
5529 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + offset)
5530 : Address(array, index.AsRegister<Register>(), TIMES_8, offset);
Mark Mendell81489372015-11-04 11:30:41 -05005531 if (value.IsFpuRegister()) {
5532 __ movsd(address, value.AsFpuRegister<XmmRegister>());
5533 } else {
5534 DCHECK(value.IsConstant());
5535 Address address_hi = index.IsConstant() ?
5536 Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) +
5537 offset + kX86WordSize) :
5538 Address(array, index.AsRegister<Register>(), TIMES_8, offset + kX86WordSize);
5539 int64_t v = bit_cast<int64_t, double>(value.GetConstant()->AsDoubleConstant()->GetValue());
5540 __ movl(address, Immediate(Low32Bits(v)));
5541 codegen_->MaybeRecordImplicitNullCheck(instruction);
5542 __ movl(address_hi, Immediate(High32Bits(v)));
5543 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005544 break;
5545 }
5546
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005547 case Primitive::kPrimVoid:
5548 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07005549 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005550 }
5551}
5552
5553void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
5554 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005555 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellee8d9712016-07-12 11:13:15 -04005556 if (!instruction->IsEmittedAtUseSite()) {
5557 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5558 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005559}
5560
5561void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
Mark Mendellee8d9712016-07-12 11:13:15 -04005562 if (instruction->IsEmittedAtUseSite()) {
5563 return;
5564 }
5565
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005566 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01005567 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00005568 Register obj = locations->InAt(0).AsRegister<Register>();
5569 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005570 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005571 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005572}
5573
5574void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00005575 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
5576 ? LocationSummary::kCallOnSlowPath
5577 : LocationSummary::kNoCall;
5578 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Mark Mendellf60c90b2015-03-04 15:12:59 -05005579 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendellee8d9712016-07-12 11:13:15 -04005580 HInstruction* length = instruction->InputAt(1);
5581 if (!length->IsEmittedAtUseSite()) {
5582 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
5583 }
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005584 if (instruction->HasUses()) {
5585 locations->SetOut(Location::SameAsFirstInput());
5586 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005587}
5588
5589void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
5590 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05005591 Location index_loc = locations->InAt(0);
5592 Location length_loc = locations->InAt(1);
Andreas Gampe85b62f22015-09-09 13:15:38 -07005593 SlowPathCode* slow_path =
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01005594 new (GetGraph()->GetArena()) BoundsCheckSlowPathX86(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005595
Mark Mendell99dbd682015-04-22 16:18:52 -04005596 if (length_loc.IsConstant()) {
5597 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
5598 if (index_loc.IsConstant()) {
5599 // BCE will remove the bounds check if we are guarenteed to pass.
5600 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5601 if (index < 0 || index >= length) {
5602 codegen_->AddSlowPath(slow_path);
5603 __ jmp(slow_path->GetEntryLabel());
5604 } else {
5605 // Some optimization after BCE may have generated this, and we should not
5606 // generate a bounds check if it is a valid range.
5607 }
5608 return;
5609 }
5610
5611 // We have to reverse the jump condition because the length is the constant.
5612 Register index_reg = index_loc.AsRegister<Register>();
5613 __ cmpl(index_reg, Immediate(length));
5614 codegen_->AddSlowPath(slow_path);
5615 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005616 } else {
Mark Mendellee8d9712016-07-12 11:13:15 -04005617 HInstruction* array_length = instruction->InputAt(1);
5618 if (array_length->IsEmittedAtUseSite()) {
5619 // Address the length field in the array.
5620 DCHECK(array_length->IsArrayLength());
5621 uint32_t len_offset = CodeGenerator::GetArrayLengthOffset(array_length->AsArrayLength());
5622 Location array_loc = array_length->GetLocations()->InAt(0);
5623 Address array_len(array_loc.AsRegister<Register>(), len_offset);
5624 if (index_loc.IsConstant()) {
5625 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5626 __ cmpl(array_len, Immediate(value));
5627 } else {
5628 __ cmpl(array_len, index_loc.AsRegister<Register>());
5629 }
5630 codegen_->MaybeRecordImplicitNullCheck(array_length);
Mark Mendell99dbd682015-04-22 16:18:52 -04005631 } else {
Mark Mendellee8d9712016-07-12 11:13:15 -04005632 Register length = length_loc.AsRegister<Register>();
5633 if (index_loc.IsConstant()) {
5634 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5635 __ cmpl(length, Immediate(value));
5636 } else {
5637 __ cmpl(length, index_loc.AsRegister<Register>());
5638 }
Mark Mendell99dbd682015-04-22 16:18:52 -04005639 }
5640 codegen_->AddSlowPath(slow_path);
5641 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005642 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005643}
5644
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005645void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005646 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005647}
5648
5649void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005650 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5651}
5652
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005653void LocationsBuilderX86::VisitSuspendCheck(HSuspendCheck* instruction) {
5654 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
5655}
5656
5657void InstructionCodeGeneratorX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005658 HBasicBlock* block = instruction->GetBlock();
5659 if (block->GetLoopInformation() != nullptr) {
5660 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5661 // The back edge will generate the suspend check.
5662 return;
5663 }
5664 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5665 // The goto will generate the suspend check.
5666 return;
5667 }
5668 GenerateSuspendCheck(instruction, nullptr);
5669}
5670
5671void InstructionCodeGeneratorX86::GenerateSuspendCheck(HSuspendCheck* instruction,
5672 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005673 SuspendCheckSlowPathX86* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005674 down_cast<SuspendCheckSlowPathX86*>(instruction->GetSlowPath());
5675 if (slow_path == nullptr) {
5676 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathX86(instruction, successor);
5677 instruction->SetSlowPath(slow_path);
5678 codegen_->AddSlowPath(slow_path);
5679 if (successor != nullptr) {
5680 DCHECK(successor->IsLoopHeader());
5681 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
5682 }
5683 } else {
5684 DCHECK_EQ(slow_path->GetSuccessor(), successor);
5685 }
5686
Roland Levillain7c1559a2015-12-15 10:55:36 +00005687 __ fs()->cmpw(Address::Absolute(Thread::ThreadFlagsOffset<kX86WordSize>().Int32Value()),
5688 Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005689 if (successor == nullptr) {
5690 __ j(kNotEqual, slow_path->GetEntryLabel());
5691 __ Bind(slow_path->GetReturnLabel());
5692 } else {
5693 __ j(kEqual, codegen_->GetLabelOf(successor));
5694 __ jmp(slow_path->GetEntryLabel());
5695 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005696}
5697
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005698X86Assembler* ParallelMoveResolverX86::GetAssembler() const {
5699 return codegen_->GetAssembler();
5700}
5701
Mark Mendell7c8d0092015-01-26 11:21:33 -05005702void ParallelMoveResolverX86::MoveMemoryToMemory32(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005703 ScratchRegisterScope ensure_scratch(
5704 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5705 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5706 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5707 __ movl(temp_reg, Address(ESP, src + stack_offset));
5708 __ movl(Address(ESP, dst + stack_offset), temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005709}
5710
5711void ParallelMoveResolverX86::MoveMemoryToMemory64(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005712 ScratchRegisterScope ensure_scratch(
5713 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5714 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5715 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5716 __ movl(temp_reg, Address(ESP, src + stack_offset));
5717 __ movl(Address(ESP, dst + stack_offset), temp_reg);
5718 __ movl(temp_reg, Address(ESP, src + stack_offset + kX86WordSize));
5719 __ movl(Address(ESP, dst + stack_offset + kX86WordSize), temp_reg);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005720}
5721
5722void ParallelMoveResolverX86::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005723 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005724 Location source = move->GetSource();
5725 Location destination = move->GetDestination();
5726
5727 if (source.IsRegister()) {
5728 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005729 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
David Brazdil74eb1b22015-12-14 11:44:01 +00005730 } else if (destination.IsFpuRegister()) {
5731 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005732 } else {
5733 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00005734 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005735 }
David Brazdil74eb1b22015-12-14 11:44:01 +00005736 } else if (source.IsRegisterPair()) {
5737 size_t elem_size = Primitive::ComponentSize(Primitive::kPrimInt);
5738 // Create stack space for 2 elements.
5739 __ subl(ESP, Immediate(2 * elem_size));
5740 __ movl(Address(ESP, 0), source.AsRegisterPairLow<Register>());
5741 __ movl(Address(ESP, elem_size), source.AsRegisterPairHigh<Register>());
5742 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
5743 // And remove the temporary stack space we allocated.
5744 __ addl(ESP, Immediate(2 * elem_size));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005745 } else if (source.IsFpuRegister()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00005746 if (destination.IsRegister()) {
5747 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
5748 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005749 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
David Brazdil74eb1b22015-12-14 11:44:01 +00005750 } else if (destination.IsRegisterPair()) {
5751 XmmRegister src_reg = source.AsFpuRegister<XmmRegister>();
5752 __ movd(destination.AsRegisterPairLow<Register>(), src_reg);
5753 __ psrlq(src_reg, Immediate(32));
5754 __ movd(destination.AsRegisterPairHigh<Register>(), src_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005755 } else if (destination.IsStackSlot()) {
5756 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
5757 } else {
5758 DCHECK(destination.IsDoubleStackSlot());
5759 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
5760 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005761 } else if (source.IsStackSlot()) {
5762 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005763 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005764 } else if (destination.IsFpuRegister()) {
5765 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005766 } else {
5767 DCHECK(destination.IsStackSlot());
Mark Mendell7c8d0092015-01-26 11:21:33 -05005768 MoveMemoryToMemory32(destination.GetStackIndex(), source.GetStackIndex());
5769 }
5770 } else if (source.IsDoubleStackSlot()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00005771 if (destination.IsRegisterPair()) {
5772 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
5773 __ movl(destination.AsRegisterPairHigh<Register>(),
5774 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
5775 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005776 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
5777 } else {
5778 DCHECK(destination.IsDoubleStackSlot()) << destination;
5779 MoveMemoryToMemory64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005780 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005781 } else if (source.IsConstant()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005782 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00005783 if (constant->IsIntConstant() || constant->IsNullConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05005784 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005785 if (destination.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05005786 if (value == 0) {
5787 __ xorl(destination.AsRegister<Register>(), destination.AsRegister<Register>());
5788 } else {
5789 __ movl(destination.AsRegister<Register>(), Immediate(value));
5790 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005791 } else {
5792 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell09b84632015-02-13 17:48:38 -05005793 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005794 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005795 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005796 float fp_value = constant->AsFloatConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00005797 int32_t value = bit_cast<int32_t, float>(fp_value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005798 Immediate imm(value);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005799 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005800 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
5801 if (value == 0) {
5802 // Easy handling of 0.0.
5803 __ xorps(dest, dest);
5804 } else {
5805 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005806 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5807 Register temp = static_cast<Register>(ensure_scratch.GetRegister());
5808 __ movl(temp, Immediate(value));
5809 __ movd(dest, temp);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005810 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005811 } else {
5812 DCHECK(destination.IsStackSlot()) << destination;
5813 __ movl(Address(ESP, destination.GetStackIndex()), imm);
5814 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005815 } else if (constant->IsLongConstant()) {
5816 int64_t value = constant->AsLongConstant()->GetValue();
5817 int32_t low_value = Low32Bits(value);
5818 int32_t high_value = High32Bits(value);
5819 Immediate low(low_value);
5820 Immediate high(high_value);
5821 if (destination.IsDoubleStackSlot()) {
5822 __ movl(Address(ESP, destination.GetStackIndex()), low);
5823 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
5824 } else {
5825 __ movl(destination.AsRegisterPairLow<Register>(), low);
5826 __ movl(destination.AsRegisterPairHigh<Register>(), high);
5827 }
5828 } else {
5829 DCHECK(constant->IsDoubleConstant());
5830 double dbl_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00005831 int64_t value = bit_cast<int64_t, double>(dbl_value);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005832 int32_t low_value = Low32Bits(value);
5833 int32_t high_value = High32Bits(value);
5834 Immediate low(low_value);
5835 Immediate high(high_value);
5836 if (destination.IsFpuRegister()) {
5837 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
5838 if (value == 0) {
5839 // Easy handling of 0.0.
5840 __ xorpd(dest, dest);
5841 } else {
5842 __ pushl(high);
5843 __ pushl(low);
5844 __ movsd(dest, Address(ESP, 0));
5845 __ addl(ESP, Immediate(8));
5846 }
5847 } else {
5848 DCHECK(destination.IsDoubleStackSlot()) << destination;
5849 __ movl(Address(ESP, destination.GetStackIndex()), low);
5850 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
5851 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005852 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005853 } else {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00005854 LOG(FATAL) << "Unimplemented move: " << destination << " <- " << source;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005855 }
5856}
5857
Mark Mendella5c19ce2015-04-01 12:51:05 -04005858void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005859 Register suggested_scratch = reg == EAX ? EBX : EAX;
5860 ScratchRegisterScope ensure_scratch(
5861 this, reg, suggested_scratch, codegen_->GetNumberOfCoreRegisters());
5862
5863 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5864 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset));
5865 __ movl(Address(ESP, mem + stack_offset), reg);
5866 __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005867}
5868
Mark Mendell7c8d0092015-01-26 11:21:33 -05005869void ParallelMoveResolverX86::Exchange32(XmmRegister reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005870 ScratchRegisterScope ensure_scratch(
5871 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5872
5873 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5874 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5875 __ movl(temp_reg, Address(ESP, mem + stack_offset));
5876 __ movss(Address(ESP, mem + stack_offset), reg);
5877 __ movd(reg, temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005878}
5879
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005880void ParallelMoveResolverX86::Exchange(int mem1, int mem2) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005881 ScratchRegisterScope ensure_scratch1(
5882 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005883
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005884 Register suggested_scratch = ensure_scratch1.GetRegister() == EAX ? EBX : EAX;
5885 ScratchRegisterScope ensure_scratch2(
5886 this, ensure_scratch1.GetRegister(), suggested_scratch, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005887
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005888 int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0;
5889 stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0;
5890 __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset));
5891 __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset));
5892 __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister()));
5893 __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005894}
5895
5896void ParallelMoveResolverX86::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005897 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005898 Location source = move->GetSource();
5899 Location destination = move->GetDestination();
5900
5901 if (source.IsRegister() && destination.IsRegister()) {
Mark Mendell90979812015-07-28 16:41:21 -04005902 // Use XOR swap algorithm to avoid serializing XCHG instruction or using a temporary.
5903 DCHECK_NE(destination.AsRegister<Register>(), source.AsRegister<Register>());
5904 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
5905 __ xorl(source.AsRegister<Register>(), destination.AsRegister<Register>());
5906 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005907 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005908 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005909 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005910 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005911 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
5912 Exchange(destination.GetStackIndex(), source.GetStackIndex());
Mark Mendell7c8d0092015-01-26 11:21:33 -05005913 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
5914 // Use XOR Swap algorithm to avoid a temporary.
5915 DCHECK_NE(source.reg(), destination.reg());
5916 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
5917 __ xorpd(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
5918 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
5919 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
5920 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
5921 } else if (destination.IsFpuRegister() && source.IsStackSlot()) {
5922 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005923 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
5924 // Take advantage of the 16 bytes in the XMM register.
5925 XmmRegister reg = source.AsFpuRegister<XmmRegister>();
5926 Address stack(ESP, destination.GetStackIndex());
5927 // Load the double into the high doubleword.
5928 __ movhpd(reg, stack);
5929
5930 // Store the low double into the destination.
5931 __ movsd(stack, reg);
5932
5933 // Move the high double to the low double.
5934 __ psrldq(reg, Immediate(8));
5935 } else if (destination.IsFpuRegister() && source.IsDoubleStackSlot()) {
5936 // Take advantage of the 16 bytes in the XMM register.
5937 XmmRegister reg = destination.AsFpuRegister<XmmRegister>();
5938 Address stack(ESP, source.GetStackIndex());
5939 // Load the double into the high doubleword.
5940 __ movhpd(reg, stack);
5941
5942 // Store the low double into the destination.
5943 __ movsd(stack, reg);
5944
5945 // Move the high double to the low double.
5946 __ psrldq(reg, Immediate(8));
5947 } else if (destination.IsDoubleStackSlot() && source.IsDoubleStackSlot()) {
5948 Exchange(destination.GetStackIndex(), source.GetStackIndex());
5949 Exchange(destination.GetHighStackIndex(kX86WordSize), source.GetHighStackIndex(kX86WordSize));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005950 } else {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005951 LOG(FATAL) << "Unimplemented: source: " << source << ", destination: " << destination;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005952 }
5953}
5954
5955void ParallelMoveResolverX86::SpillScratch(int reg) {
5956 __ pushl(static_cast<Register>(reg));
5957}
5958
5959void ParallelMoveResolverX86::RestoreScratch(int reg) {
5960 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005961}
5962
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005963HLoadClass::LoadKind CodeGeneratorX86::GetSupportedLoadClassKind(
5964 HLoadClass::LoadKind desired_class_load_kind) {
5965 if (kEmitCompilerReadBarrier) {
5966 switch (desired_class_load_kind) {
5967 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
5968 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
5969 case HLoadClass::LoadKind::kBootImageAddress:
5970 // TODO: Implement for read barrier.
5971 return HLoadClass::LoadKind::kDexCacheViaMethod;
5972 default:
5973 break;
5974 }
5975 }
5976 switch (desired_class_load_kind) {
5977 case HLoadClass::LoadKind::kReferrersClass:
5978 break;
5979 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
5980 DCHECK(!GetCompilerOptions().GetCompilePic());
5981 break;
5982 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
5983 DCHECK(GetCompilerOptions().GetCompilePic());
5984 FALLTHROUGH_INTENDED;
5985 case HLoadClass::LoadKind::kDexCachePcRelative:
5986 DCHECK(!Runtime::Current()->UseJitCompilation()); // Note: boot image is also non-JIT.
5987 // We disable pc-relative load when there is an irreducible loop, as the optimization
5988 // is incompatible with it.
5989 // TODO: Create as many X86ComputeBaseMethodAddress instructions as needed for methods
5990 // with irreducible loops.
5991 if (GetGraph()->HasIrreducibleLoops()) {
5992 return HLoadClass::LoadKind::kDexCacheViaMethod;
5993 }
5994 break;
5995 case HLoadClass::LoadKind::kBootImageAddress:
5996 break;
5997 case HLoadClass::LoadKind::kDexCacheAddress:
5998 DCHECK(Runtime::Current()->UseJitCompilation());
5999 break;
6000 case HLoadClass::LoadKind::kDexCacheViaMethod:
6001 break;
6002 }
6003 return desired_class_load_kind;
6004}
6005
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006006void LocationsBuilderX86::VisitLoadClass(HLoadClass* cls) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006007 if (cls->NeedsAccessCheck()) {
6008 InvokeRuntimeCallingConvention calling_convention;
6009 CodeGenerator::CreateLoadClassLocationSummary(
6010 cls,
6011 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
6012 Location::RegisterLocation(EAX),
6013 /* code_generator_supports_read_barrier */ true);
6014 return;
6015 }
6016
6017 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || kEmitCompilerReadBarrier)
6018 ? LocationSummary::kCallOnSlowPath
6019 : LocationSummary::kNoCall;
6020 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
6021 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
6022 if (load_kind == HLoadClass::LoadKind::kReferrersClass ||
6023 load_kind == HLoadClass::LoadKind::kDexCacheViaMethod ||
6024 load_kind == HLoadClass::LoadKind::kBootImageLinkTimePcRelative ||
6025 load_kind == HLoadClass::LoadKind::kDexCachePcRelative) {
6026 locations->SetInAt(0, Location::RequiresRegister());
6027 }
6028 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006029}
6030
6031void InstructionCodeGeneratorX86::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01006032 LocationSummary* locations = cls->GetLocations();
Calin Juravle98893e12015-10-02 21:05:03 +01006033 if (cls->NeedsAccessCheck()) {
6034 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex());
6035 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pInitializeTypeAndVerifyAccess),
6036 cls,
6037 cls->GetDexPc(),
6038 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00006039 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Calin Juravle580b6092015-10-06 17:35:58 +01006040 return;
6041 }
6042
Roland Levillain0d5a2812015-11-13 10:07:31 +00006043 Location out_loc = locations->Out();
6044 Register out = out_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006045
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006046 bool generate_null_check = false;
6047 switch (cls->GetLoadKind()) {
6048 case HLoadClass::LoadKind::kReferrersClass: {
6049 DCHECK(!cls->CanCallRuntime());
6050 DCHECK(!cls->MustGenerateClinitCheck());
6051 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
6052 Register current_method = locations->InAt(0).AsRegister<Register>();
6053 GenerateGcRootFieldLoad(
6054 cls, out_loc, Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
6055 break;
6056 }
6057 case HLoadClass::LoadKind::kBootImageLinkTimeAddress: {
6058 DCHECK(!kEmitCompilerReadBarrier);
6059 __ movl(out, Immediate(/* placeholder */ 0));
6060 codegen_->RecordTypePatch(cls);
6061 break;
6062 }
6063 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
6064 DCHECK(!kEmitCompilerReadBarrier);
6065 Register method_address = locations->InAt(0).AsRegister<Register>();
6066 __ leal(out, Address(method_address, CodeGeneratorX86::kDummy32BitOffset));
6067 codegen_->RecordTypePatch(cls);
6068 break;
6069 }
6070 case HLoadClass::LoadKind::kBootImageAddress: {
6071 DCHECK(!kEmitCompilerReadBarrier);
6072 DCHECK_NE(cls->GetAddress(), 0u);
6073 uint32_t address = dchecked_integral_cast<uint32_t>(cls->GetAddress());
6074 __ movl(out, Immediate(address));
6075 codegen_->RecordSimplePatch();
6076 break;
6077 }
6078 case HLoadClass::LoadKind::kDexCacheAddress: {
6079 DCHECK_NE(cls->GetAddress(), 0u);
6080 uint32_t address = dchecked_integral_cast<uint32_t>(cls->GetAddress());
6081 // /* GcRoot<mirror::Class> */ out = *address
6082 GenerateGcRootFieldLoad(cls, out_loc, Address::Absolute(address));
6083 generate_null_check = !cls->IsInDexCache();
6084 break;
6085 }
6086 case HLoadClass::LoadKind::kDexCachePcRelative: {
6087 Register base_reg = locations->InAt(0).AsRegister<Register>();
6088 uint32_t offset = cls->GetDexCacheElementOffset();
6089 Label* fixup_label = codegen_->NewPcRelativeDexCacheArrayPatch(cls->GetDexFile(), offset);
6090 // /* GcRoot<mirror::Class> */ out = *(base + offset) /* PC-relative */
6091 GenerateGcRootFieldLoad(
6092 cls, out_loc, Address(base_reg, CodeGeneratorX86::kDummy32BitOffset), fixup_label);
6093 generate_null_check = !cls->IsInDexCache();
6094 break;
6095 }
6096 case HLoadClass::LoadKind::kDexCacheViaMethod: {
6097 // /* GcRoot<mirror::Class>[] */ out =
6098 // current_method.ptr_sized_fields_->dex_cache_resolved_types_
6099 Register current_method = locations->InAt(0).AsRegister<Register>();
6100 __ movl(out, Address(current_method,
6101 ArtMethod::DexCacheResolvedTypesOffset(kX86PointerSize).Int32Value()));
6102 // /* GcRoot<mirror::Class> */ out = out[type_index]
6103 GenerateGcRootFieldLoad(
6104 cls, out_loc, Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
6105 generate_null_check = !cls->IsInDexCache();
6106 break;
6107 }
6108 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006109
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006110 if (generate_null_check || cls->MustGenerateClinitCheck()) {
6111 DCHECK(cls->CanCallRuntime());
6112 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
6113 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
6114 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006115
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006116 if (generate_null_check) {
6117 __ testl(out, out);
6118 __ j(kEqual, slow_path->GetEntryLabel());
6119 }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006120
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006121 if (cls->MustGenerateClinitCheck()) {
6122 GenerateClassInitializationCheck(slow_path, out);
6123 } else {
6124 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006125 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006126 }
6127}
6128
6129void LocationsBuilderX86::VisitClinitCheck(HClinitCheck* check) {
6130 LocationSummary* locations =
6131 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
6132 locations->SetInAt(0, Location::RequiresRegister());
6133 if (check->HasUses()) {
6134 locations->SetOut(Location::SameAsFirstInput());
6135 }
6136}
6137
6138void InstructionCodeGeneratorX86::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006139 // We assume the class to not be null.
Andreas Gampe85b62f22015-09-09 13:15:38 -07006140 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006141 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006142 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00006143 GenerateClassInitializationCheck(slow_path,
6144 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006145}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006146
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006147void InstructionCodeGeneratorX86::GenerateClassInitializationCheck(
Andreas Gampe85b62f22015-09-09 13:15:38 -07006148 SlowPathCode* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006149 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
6150 Immediate(mirror::Class::kStatusInitialized));
6151 __ j(kLess, slow_path->GetEntryLabel());
6152 __ Bind(slow_path->GetExitLabel());
6153 // No need for memory fence, thanks to the X86 memory model.
6154}
6155
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006156HLoadString::LoadKind CodeGeneratorX86::GetSupportedLoadStringKind(
6157 HLoadString::LoadKind desired_string_load_kind) {
6158 if (kEmitCompilerReadBarrier) {
6159 switch (desired_string_load_kind) {
6160 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
6161 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
6162 case HLoadString::LoadKind::kBootImageAddress:
6163 // TODO: Implement for read barrier.
6164 return HLoadString::LoadKind::kDexCacheViaMethod;
6165 default:
6166 break;
6167 }
6168 }
6169 switch (desired_string_load_kind) {
6170 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
6171 DCHECK(!GetCompilerOptions().GetCompilePic());
6172 break;
6173 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
6174 DCHECK(GetCompilerOptions().GetCompilePic());
6175 FALLTHROUGH_INTENDED;
6176 case HLoadString::LoadKind::kDexCachePcRelative:
Calin Juravleffc87072016-04-20 14:22:09 +01006177 DCHECK(!Runtime::Current()->UseJitCompilation()); // Note: boot image is also non-JIT.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006178 // We disable pc-relative load when there is an irreducible loop, as the optimization
6179 // is incompatible with it.
6180 // TODO: Create as many X86ComputeBaseMethodAddress instructions as needed for methods
6181 // with irreducible loops.
6182 if (GetGraph()->HasIrreducibleLoops()) {
6183 return HLoadString::LoadKind::kDexCacheViaMethod;
6184 }
6185 break;
6186 case HLoadString::LoadKind::kBootImageAddress:
6187 break;
6188 case HLoadString::LoadKind::kDexCacheAddress:
Calin Juravleffc87072016-04-20 14:22:09 +01006189 DCHECK(Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006190 break;
6191 case HLoadString::LoadKind::kDexCacheViaMethod:
6192 break;
6193 }
6194 return desired_string_load_kind;
6195}
6196
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006197void LocationsBuilderX86::VisitLoadString(HLoadString* load) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006198 LocationSummary::CallKind call_kind = (load->NeedsEnvironment() || kEmitCompilerReadBarrier)
Nicolas Geoffray917d0162015-11-24 18:25:35 +00006199 ? LocationSummary::kCallOnSlowPath
6200 : LocationSummary::kNoCall;
6201 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006202 HLoadString::LoadKind load_kind = load->GetLoadKind();
6203 if (load_kind == HLoadString::LoadKind::kDexCacheViaMethod ||
6204 load_kind == HLoadString::LoadKind::kBootImageLinkTimePcRelative ||
6205 load_kind == HLoadString::LoadKind::kDexCachePcRelative) {
6206 locations->SetInAt(0, Location::RequiresRegister());
6207 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006208 locations->SetOut(Location::RequiresRegister());
6209}
6210
6211void InstructionCodeGeneratorX86::VisitLoadString(HLoadString* load) {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01006212 LocationSummary* locations = load->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006213 Location out_loc = locations->Out();
6214 Register out = out_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006215
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006216 switch (load->GetLoadKind()) {
6217 case HLoadString::LoadKind::kBootImageLinkTimeAddress: {
6218 DCHECK(!kEmitCompilerReadBarrier);
6219 __ movl(out, Immediate(/* placeholder */ 0));
6220 codegen_->RecordStringPatch(load);
6221 return; // No dex cache slow path.
6222 }
6223 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
6224 DCHECK(!kEmitCompilerReadBarrier);
6225 Register method_address = locations->InAt(0).AsRegister<Register>();
6226 __ leal(out, Address(method_address, CodeGeneratorX86::kDummy32BitOffset));
6227 codegen_->RecordStringPatch(load);
6228 return; // No dex cache slow path.
6229 }
6230 case HLoadString::LoadKind::kBootImageAddress: {
6231 DCHECK(!kEmitCompilerReadBarrier);
6232 DCHECK_NE(load->GetAddress(), 0u);
6233 uint32_t address = dchecked_integral_cast<uint32_t>(load->GetAddress());
6234 __ movl(out, Immediate(address));
6235 codegen_->RecordSimplePatch();
6236 return; // No dex cache slow path.
6237 }
6238 case HLoadString::LoadKind::kDexCacheAddress: {
6239 DCHECK_NE(load->GetAddress(), 0u);
6240 uint32_t address = dchecked_integral_cast<uint32_t>(load->GetAddress());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006241 // /* GcRoot<mirror::String> */ out = *address
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006242 GenerateGcRootFieldLoad(load, out_loc, Address::Absolute(address));
6243 break;
6244 }
6245 case HLoadString::LoadKind::kDexCachePcRelative: {
6246 Register base_reg = locations->InAt(0).AsRegister<Register>();
6247 uint32_t offset = load->GetDexCacheElementOffset();
6248 Label* fixup_label = codegen_->NewPcRelativeDexCacheArrayPatch(load->GetDexFile(), offset);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006249 // /* GcRoot<mirror::String> */ out = *(base + offset) /* PC-relative */
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006250 GenerateGcRootFieldLoad(
6251 load, out_loc, Address(base_reg, CodeGeneratorX86::kDummy32BitOffset), fixup_label);
6252 break;
6253 }
6254 case HLoadString::LoadKind::kDexCacheViaMethod: {
6255 Register current_method = locations->InAt(0).AsRegister<Register>();
6256
6257 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
6258 GenerateGcRootFieldLoad(
6259 load, out_loc, Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
6260
6261 // /* GcRoot<mirror::String>[] */ out = out->dex_cache_strings_
6262 __ movl(out, Address(out, mirror::Class::DexCacheStringsOffset().Int32Value()));
6263 // /* GcRoot<mirror::String> */ out = out[string_index]
6264 GenerateGcRootFieldLoad(
6265 load, out_loc, Address(out, CodeGenerator::GetCacheOffset(load->GetStringIndex())));
6266 break;
6267 }
6268 default:
6269 LOG(FATAL) << "Unexpected load kind: " << load->GetLoadKind();
6270 UNREACHABLE();
6271 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006272
Nicolas Geoffray917d0162015-11-24 18:25:35 +00006273 if (!load->IsInDexCache()) {
6274 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86(load);
6275 codegen_->AddSlowPath(slow_path);
6276 __ testl(out, out);
6277 __ j(kEqual, slow_path->GetEntryLabel());
6278 __ Bind(slow_path->GetExitLabel());
6279 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006280}
6281
David Brazdilcb1c0552015-08-04 16:22:25 +01006282static Address GetExceptionTlsAddress() {
6283 return Address::Absolute(Thread::ExceptionOffset<kX86WordSize>().Int32Value());
6284}
6285
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006286void LocationsBuilderX86::VisitLoadException(HLoadException* load) {
6287 LocationSummary* locations =
6288 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
6289 locations->SetOut(Location::RequiresRegister());
6290}
6291
6292void InstructionCodeGeneratorX86::VisitLoadException(HLoadException* load) {
David Brazdilcb1c0552015-08-04 16:22:25 +01006293 __ fs()->movl(load->GetLocations()->Out().AsRegister<Register>(), GetExceptionTlsAddress());
6294}
6295
6296void LocationsBuilderX86::VisitClearException(HClearException* clear) {
6297 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
6298}
6299
6300void InstructionCodeGeneratorX86::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
6301 __ fs()->movl(GetExceptionTlsAddress(), Immediate(0));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006302}
6303
6304void LocationsBuilderX86::VisitThrow(HThrow* instruction) {
6305 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006306 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006307 InvokeRuntimeCallingConvention calling_convention;
6308 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6309}
6310
6311void InstructionCodeGeneratorX86::VisitThrow(HThrow* instruction) {
Alexandre Rames8158f282015-08-07 10:26:17 +01006312 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pDeliverException),
6313 instruction,
6314 instruction->GetDexPc(),
6315 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00006316 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006317}
6318
Roland Levillain7c1559a2015-12-15 10:55:36 +00006319static bool TypeCheckNeedsATemporary(TypeCheckKind type_check_kind) {
6320 return kEmitCompilerReadBarrier &&
6321 (kUseBakerReadBarrier ||
6322 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
6323 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
6324 type_check_kind == TypeCheckKind::kArrayObjectCheck);
6325}
6326
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006327void LocationsBuilderX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006328 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain0d5a2812015-11-13 10:07:31 +00006329 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
6330 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006331 case TypeCheckKind::kExactCheck:
6332 case TypeCheckKind::kAbstractClassCheck:
6333 case TypeCheckKind::kClassHierarchyCheck:
6334 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006335 call_kind =
6336 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006337 break;
6338 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006339 case TypeCheckKind::kUnresolvedCheck:
6340 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006341 call_kind = LocationSummary::kCallOnSlowPath;
6342 break;
6343 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006344
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006345 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006346 locations->SetInAt(0, Location::RequiresRegister());
6347 locations->SetInAt(1, Location::Any());
6348 // Note that TypeCheckSlowPathX86 uses this "out" register too.
6349 locations->SetOut(Location::RequiresRegister());
6350 // When read barriers are enabled, we need a temporary register for
6351 // some cases.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006352 if (TypeCheckNeedsATemporary(type_check_kind)) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006353 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006354 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006355}
6356
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006357void InstructionCodeGeneratorX86::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006358 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006359 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006360 Location obj_loc = locations->InAt(0);
6361 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006362 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006363 Location out_loc = locations->Out();
6364 Register out = out_loc.AsRegister<Register>();
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006365 Location maybe_temp_loc = TypeCheckNeedsATemporary(type_check_kind) ?
Roland Levillain7c1559a2015-12-15 10:55:36 +00006366 locations->GetTemp(0) :
6367 Location::NoLocation();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006368 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006369 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6370 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6371 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Andreas Gampe85b62f22015-09-09 13:15:38 -07006372 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006373 NearLabel done, zero;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006374
6375 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006376 // Avoid null check if we know obj is not null.
6377 if (instruction->MustDoNullCheck()) {
6378 __ testl(obj, obj);
6379 __ j(kEqual, &zero);
6380 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006381
Roland Levillain0d5a2812015-11-13 10:07:31 +00006382 // /* HeapReference<Class> */ out = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006383 GenerateReferenceLoadTwoRegisters(instruction, out_loc, obj_loc, class_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006384
Roland Levillain7c1559a2015-12-15 10:55:36 +00006385 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006386 case TypeCheckKind::kExactCheck: {
6387 if (cls.IsRegister()) {
6388 __ cmpl(out, cls.AsRegister<Register>());
6389 } else {
6390 DCHECK(cls.IsStackSlot()) << cls;
6391 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6392 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006393
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006394 // Classes must be equal for the instanceof to succeed.
6395 __ j(kNotEqual, &zero);
6396 __ movl(out, Immediate(1));
6397 __ jmp(&done);
6398 break;
6399 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006400
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006401 case TypeCheckKind::kAbstractClassCheck: {
6402 // If the class is abstract, we eagerly fetch the super class of the
6403 // object to avoid doing a comparison we know will fail.
6404 NearLabel loop;
6405 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006406 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006407 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006408 __ testl(out, out);
6409 // If `out` is null, we use it for the result, and jump to `done`.
6410 __ j(kEqual, &done);
6411 if (cls.IsRegister()) {
6412 __ cmpl(out, cls.AsRegister<Register>());
6413 } else {
6414 DCHECK(cls.IsStackSlot()) << cls;
6415 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6416 }
6417 __ j(kNotEqual, &loop);
6418 __ movl(out, Immediate(1));
6419 if (zero.IsLinked()) {
6420 __ jmp(&done);
6421 }
6422 break;
6423 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006424
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006425 case TypeCheckKind::kClassHierarchyCheck: {
6426 // Walk over the class hierarchy to find a match.
6427 NearLabel loop, success;
6428 __ Bind(&loop);
6429 if (cls.IsRegister()) {
6430 __ cmpl(out, cls.AsRegister<Register>());
6431 } else {
6432 DCHECK(cls.IsStackSlot()) << cls;
6433 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6434 }
6435 __ j(kEqual, &success);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006436 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006437 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006438 __ testl(out, out);
6439 __ j(kNotEqual, &loop);
6440 // If `out` is null, we use it for the result, and jump to `done`.
6441 __ jmp(&done);
6442 __ Bind(&success);
6443 __ movl(out, Immediate(1));
6444 if (zero.IsLinked()) {
6445 __ jmp(&done);
6446 }
6447 break;
6448 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006449
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006450 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006451 // Do an exact check.
6452 NearLabel exact_check;
6453 if (cls.IsRegister()) {
6454 __ cmpl(out, cls.AsRegister<Register>());
6455 } else {
6456 DCHECK(cls.IsStackSlot()) << cls;
6457 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6458 }
6459 __ j(kEqual, &exact_check);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006460 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006461 // /* HeapReference<Class> */ out = out->component_type_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006462 GenerateReferenceLoadOneRegister(instruction, out_loc, component_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006463 __ testl(out, out);
6464 // If `out` is null, we use it for the result, and jump to `done`.
6465 __ j(kEqual, &done);
6466 __ cmpw(Address(out, primitive_offset), Immediate(Primitive::kPrimNot));
6467 __ j(kNotEqual, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006468 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006469 __ movl(out, Immediate(1));
6470 __ jmp(&done);
6471 break;
6472 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006473
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006474 case TypeCheckKind::kArrayCheck: {
6475 if (cls.IsRegister()) {
6476 __ cmpl(out, cls.AsRegister<Register>());
6477 } else {
6478 DCHECK(cls.IsStackSlot()) << cls;
6479 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6480 }
6481 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain0d5a2812015-11-13 10:07:31 +00006482 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
6483 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006484 codegen_->AddSlowPath(slow_path);
6485 __ j(kNotEqual, slow_path->GetEntryLabel());
6486 __ movl(out, Immediate(1));
6487 if (zero.IsLinked()) {
6488 __ jmp(&done);
6489 }
6490 break;
6491 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006492
Calin Juravle98893e12015-10-02 21:05:03 +01006493 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006494 case TypeCheckKind::kInterfaceCheck: {
6495 // Note that we indeed only call on slow path, but we always go
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006496 // into the slow path for the unresolved and interface check
Roland Levillain0d5a2812015-11-13 10:07:31 +00006497 // cases.
6498 //
6499 // We cannot directly call the InstanceofNonTrivial runtime
6500 // entry point without resorting to a type checking slow path
6501 // here (i.e. by calling InvokeRuntime directly), as it would
6502 // require to assign fixed registers for the inputs of this
6503 // HInstanceOf instruction (following the runtime calling
6504 // convention), which might be cluttered by the potential first
6505 // read barrier emission at the beginning of this method.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006506 //
6507 // TODO: Introduce a new runtime entry point taking the object
6508 // to test (instead of its class) as argument, and let it deal
6509 // with the read barrier issues. This will let us refactor this
6510 // case of the `switch` code as it was previously (with a direct
6511 // call to the runtime not using a type checking slow path).
6512 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006513 DCHECK(locations->OnlyCallsOnSlowPath());
6514 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
6515 /* is_fatal */ false);
6516 codegen_->AddSlowPath(slow_path);
6517 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006518 if (zero.IsLinked()) {
6519 __ jmp(&done);
6520 }
6521 break;
6522 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006523 }
6524
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006525 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006526 __ Bind(&zero);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006527 __ xorl(out, out);
6528 }
6529
6530 if (done.IsLinked()) {
6531 __ Bind(&done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006532 }
6533
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006534 if (slow_path != nullptr) {
6535 __ Bind(slow_path->GetExitLabel());
6536 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006537}
6538
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006539void LocationsBuilderX86::VisitCheckCast(HCheckCast* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006540 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
6541 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006542 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
6543 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006544 case TypeCheckKind::kExactCheck:
6545 case TypeCheckKind::kAbstractClassCheck:
6546 case TypeCheckKind::kClassHierarchyCheck:
6547 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006548 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
6549 LocationSummary::kCallOnSlowPath :
6550 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006551 break;
6552 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006553 case TypeCheckKind::kUnresolvedCheck:
6554 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006555 call_kind = LocationSummary::kCallOnSlowPath;
6556 break;
6557 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006558 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
6559 locations->SetInAt(0, Location::RequiresRegister());
6560 locations->SetInAt(1, Location::Any());
6561 // Note that TypeCheckSlowPathX86 uses this "temp" register too.
6562 locations->AddTemp(Location::RequiresRegister());
6563 // When read barriers are enabled, we need an additional temporary
6564 // register for some cases.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006565 if (TypeCheckNeedsATemporary(type_check_kind)) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006566 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006567 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006568}
6569
6570void InstructionCodeGeneratorX86::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006571 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006572 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006573 Location obj_loc = locations->InAt(0);
6574 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006575 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006576 Location temp_loc = locations->GetTemp(0);
6577 Register temp = temp_loc.AsRegister<Register>();
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006578 Location maybe_temp2_loc = TypeCheckNeedsATemporary(type_check_kind) ?
Roland Levillain7c1559a2015-12-15 10:55:36 +00006579 locations->GetTemp(1) :
6580 Location::NoLocation();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006581 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
6582 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6583 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6584 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006585
Roland Levillain0d5a2812015-11-13 10:07:31 +00006586 bool is_type_check_slow_path_fatal =
6587 (type_check_kind == TypeCheckKind::kExactCheck ||
6588 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
6589 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
6590 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
6591 !instruction->CanThrowIntoCatchBlock();
6592 SlowPathCode* type_check_slow_path =
6593 new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
6594 is_type_check_slow_path_fatal);
6595 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006596
Roland Levillain0d5a2812015-11-13 10:07:31 +00006597 NearLabel done;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006598 // Avoid null check if we know obj is not null.
6599 if (instruction->MustDoNullCheck()) {
6600 __ testl(obj, obj);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006601 __ j(kEqual, &done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006602 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006603
Roland Levillain0d5a2812015-11-13 10:07:31 +00006604 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006605 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006606
Roland Levillain0d5a2812015-11-13 10:07:31 +00006607 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006608 case TypeCheckKind::kExactCheck:
6609 case TypeCheckKind::kArrayCheck: {
6610 if (cls.IsRegister()) {
6611 __ cmpl(temp, cls.AsRegister<Register>());
6612 } else {
6613 DCHECK(cls.IsStackSlot()) << cls;
6614 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6615 }
6616 // Jump to slow path for throwing the exception or doing a
6617 // more involved array check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006618 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006619 break;
6620 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006621
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006622 case TypeCheckKind::kAbstractClassCheck: {
6623 // If the class is abstract, we eagerly fetch the super class of the
6624 // object to avoid doing a comparison we know will fail.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006625 NearLabel loop, compare_classes;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006626 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006627 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006628 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006629
6630 // If the class reference currently in `temp` is not null, jump
6631 // to the `compare_classes` label to compare it with the checked
6632 // class.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006633 __ testl(temp, temp);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006634 __ j(kNotEqual, &compare_classes);
6635 // Otherwise, jump to the slow path to throw the exception.
6636 //
6637 // But before, move back the object's class into `temp` before
6638 // going into the slow path, as it has been overwritten in the
6639 // meantime.
6640 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006641 GenerateReferenceLoadTwoRegisters(
6642 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006643 __ jmp(type_check_slow_path->GetEntryLabel());
6644
6645 __ Bind(&compare_classes);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006646 if (cls.IsRegister()) {
6647 __ cmpl(temp, cls.AsRegister<Register>());
6648 } else {
6649 DCHECK(cls.IsStackSlot()) << cls;
6650 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6651 }
6652 __ j(kNotEqual, &loop);
6653 break;
6654 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006655
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006656 case TypeCheckKind::kClassHierarchyCheck: {
6657 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006658 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006659 __ Bind(&loop);
6660 if (cls.IsRegister()) {
6661 __ cmpl(temp, cls.AsRegister<Register>());
6662 } else {
6663 DCHECK(cls.IsStackSlot()) << cls;
6664 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6665 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006666 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006667
Roland Levillain0d5a2812015-11-13 10:07:31 +00006668 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006669 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006670
6671 // If the class reference currently in `temp` is not null, jump
6672 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006673 __ testl(temp, temp);
6674 __ j(kNotEqual, &loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006675 // Otherwise, jump to the slow path to throw the exception.
6676 //
6677 // But before, move back the object's class into `temp` before
6678 // going into the slow path, as it has been overwritten in the
6679 // meantime.
6680 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006681 GenerateReferenceLoadTwoRegisters(
6682 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006683 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006684 break;
6685 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006686
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006687 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006688 // Do an exact check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006689 NearLabel check_non_primitive_component_type;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006690 if (cls.IsRegister()) {
6691 __ cmpl(temp, cls.AsRegister<Register>());
6692 } else {
6693 DCHECK(cls.IsStackSlot()) << cls;
6694 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6695 }
6696 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006697
6698 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006699 // /* HeapReference<Class> */ temp = temp->component_type_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006700 GenerateReferenceLoadOneRegister(instruction, temp_loc, component_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006701
6702 // If the component type is not null (i.e. the object is indeed
6703 // an array), jump to label `check_non_primitive_component_type`
6704 // to further check that this component type is not a primitive
6705 // type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006706 __ testl(temp, temp);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006707 __ j(kNotEqual, &check_non_primitive_component_type);
6708 // Otherwise, jump to the slow path to throw the exception.
6709 //
6710 // But before, move back the object's class into `temp` before
6711 // going into the slow path, as it has been overwritten in the
6712 // meantime.
6713 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006714 GenerateReferenceLoadTwoRegisters(
6715 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006716 __ jmp(type_check_slow_path->GetEntryLabel());
6717
6718 __ Bind(&check_non_primitive_component_type);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006719 __ cmpw(Address(temp, primitive_offset), Immediate(Primitive::kPrimNot));
Roland Levillain0d5a2812015-11-13 10:07:31 +00006720 __ j(kEqual, &done);
6721 // Same comment as above regarding `temp` and the slow path.
6722 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006723 GenerateReferenceLoadTwoRegisters(
6724 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006725 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006726 break;
6727 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006728
Calin Juravle98893e12015-10-02 21:05:03 +01006729 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006730 case TypeCheckKind::kInterfaceCheck:
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006731 // We always go into the type check slow path for the unresolved
6732 // and interface check cases.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006733 //
6734 // We cannot directly call the CheckCast runtime entry point
6735 // without resorting to a type checking slow path here (i.e. by
6736 // calling InvokeRuntime directly), as it would require to
6737 // assign fixed registers for the inputs of this HInstanceOf
6738 // instruction (following the runtime calling convention), which
6739 // might be cluttered by the potential first read barrier
6740 // emission at the beginning of this method.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006741 //
6742 // TODO: Introduce a new runtime entry point taking the object
6743 // to test (instead of its class) as argument, and let it deal
6744 // with the read barrier issues. This will let us refactor this
6745 // case of the `switch` code as it was previously (with a direct
6746 // call to the runtime not using a type checking slow path).
6747 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006748 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006749 break;
6750 }
6751 __ Bind(&done);
6752
Roland Levillain0d5a2812015-11-13 10:07:31 +00006753 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006754}
6755
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006756void LocationsBuilderX86::VisitMonitorOperation(HMonitorOperation* instruction) {
6757 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006758 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006759 InvokeRuntimeCallingConvention calling_convention;
6760 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6761}
6762
6763void InstructionCodeGeneratorX86::VisitMonitorOperation(HMonitorOperation* instruction) {
Alexandre Rames8158f282015-08-07 10:26:17 +01006764 codegen_->InvokeRuntime(instruction->IsEnter() ? QUICK_ENTRY_POINT(pLockObject)
6765 : QUICK_ENTRY_POINT(pUnlockObject),
6766 instruction,
6767 instruction->GetDexPc(),
6768 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00006769 if (instruction->IsEnter()) {
6770 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
6771 } else {
6772 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
6773 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006774}
6775
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006776void LocationsBuilderX86::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
6777void LocationsBuilderX86::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
6778void LocationsBuilderX86::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
6779
6780void LocationsBuilderX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
6781 LocationSummary* locations =
6782 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6783 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
6784 || instruction->GetResultType() == Primitive::kPrimLong);
6785 locations->SetInAt(0, Location::RequiresRegister());
6786 locations->SetInAt(1, Location::Any());
6787 locations->SetOut(Location::SameAsFirstInput());
6788}
6789
6790void InstructionCodeGeneratorX86::VisitAnd(HAnd* instruction) {
6791 HandleBitwiseOperation(instruction);
6792}
6793
6794void InstructionCodeGeneratorX86::VisitOr(HOr* instruction) {
6795 HandleBitwiseOperation(instruction);
6796}
6797
6798void InstructionCodeGeneratorX86::VisitXor(HXor* instruction) {
6799 HandleBitwiseOperation(instruction);
6800}
6801
6802void InstructionCodeGeneratorX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
6803 LocationSummary* locations = instruction->GetLocations();
6804 Location first = locations->InAt(0);
6805 Location second = locations->InAt(1);
6806 DCHECK(first.Equals(locations->Out()));
6807
6808 if (instruction->GetResultType() == Primitive::kPrimInt) {
6809 if (second.IsRegister()) {
6810 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006811 __ andl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006812 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006813 __ orl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006814 } else {
6815 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006816 __ xorl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006817 }
6818 } else if (second.IsConstant()) {
6819 if (instruction->IsAnd()) {
Roland Levillain199f3362014-11-27 17:15:16 +00006820 __ andl(first.AsRegister<Register>(),
6821 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006822 } else if (instruction->IsOr()) {
Roland Levillain199f3362014-11-27 17:15:16 +00006823 __ orl(first.AsRegister<Register>(),
6824 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006825 } else {
6826 DCHECK(instruction->IsXor());
Roland Levillain199f3362014-11-27 17:15:16 +00006827 __ xorl(first.AsRegister<Register>(),
6828 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006829 }
6830 } else {
6831 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006832 __ andl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006833 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006834 __ orl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006835 } else {
6836 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006837 __ xorl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006838 }
6839 }
6840 } else {
6841 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
6842 if (second.IsRegisterPair()) {
6843 if (instruction->IsAnd()) {
6844 __ andl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
6845 __ andl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
6846 } else if (instruction->IsOr()) {
6847 __ orl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
6848 __ orl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
6849 } else {
6850 DCHECK(instruction->IsXor());
6851 __ xorl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
6852 __ xorl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
6853 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006854 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006855 if (instruction->IsAnd()) {
6856 __ andl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
6857 __ andl(first.AsRegisterPairHigh<Register>(),
6858 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
6859 } else if (instruction->IsOr()) {
6860 __ orl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
6861 __ orl(first.AsRegisterPairHigh<Register>(),
6862 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
6863 } else {
6864 DCHECK(instruction->IsXor());
6865 __ xorl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
6866 __ xorl(first.AsRegisterPairHigh<Register>(),
6867 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
6868 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006869 } else {
6870 DCHECK(second.IsConstant()) << second;
6871 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006872 int32_t low_value = Low32Bits(value);
6873 int32_t high_value = High32Bits(value);
6874 Immediate low(low_value);
6875 Immediate high(high_value);
6876 Register first_low = first.AsRegisterPairLow<Register>();
6877 Register first_high = first.AsRegisterPairHigh<Register>();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006878 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006879 if (low_value == 0) {
6880 __ xorl(first_low, first_low);
6881 } else if (low_value != -1) {
6882 __ andl(first_low, low);
6883 }
6884 if (high_value == 0) {
6885 __ xorl(first_high, first_high);
6886 } else if (high_value != -1) {
6887 __ andl(first_high, high);
6888 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006889 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006890 if (low_value != 0) {
6891 __ orl(first_low, low);
6892 }
6893 if (high_value != 0) {
6894 __ orl(first_high, high);
6895 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006896 } else {
6897 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006898 if (low_value != 0) {
6899 __ xorl(first_low, low);
6900 }
6901 if (high_value != 0) {
6902 __ xorl(first_high, high);
6903 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006904 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006905 }
6906 }
6907}
6908
Roland Levillain7c1559a2015-12-15 10:55:36 +00006909void InstructionCodeGeneratorX86::GenerateReferenceLoadOneRegister(HInstruction* instruction,
6910 Location out,
6911 uint32_t offset,
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006912 Location maybe_temp) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006913 Register out_reg = out.AsRegister<Register>();
6914 if (kEmitCompilerReadBarrier) {
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006915 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillain7c1559a2015-12-15 10:55:36 +00006916 if (kUseBakerReadBarrier) {
6917 // Load with fast path based Baker's read barrier.
6918 // /* HeapReference<Object> */ out = *(out + offset)
6919 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006920 instruction, out, out_reg, offset, maybe_temp, /* needs_null_check */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006921 } else {
6922 // Load with slow path based read barrier.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006923 // Save the value of `out` into `maybe_temp` before overwriting it
Roland Levillain7c1559a2015-12-15 10:55:36 +00006924 // in the following move operation, as we will need it for the
6925 // read barrier below.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006926 __ movl(maybe_temp.AsRegister<Register>(), out_reg);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006927 // /* HeapReference<Object> */ out = *(out + offset)
6928 __ movl(out_reg, Address(out_reg, offset));
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006929 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006930 }
6931 } else {
6932 // Plain load with no read barrier.
6933 // /* HeapReference<Object> */ out = *(out + offset)
6934 __ movl(out_reg, Address(out_reg, offset));
6935 __ MaybeUnpoisonHeapReference(out_reg);
6936 }
6937}
6938
6939void InstructionCodeGeneratorX86::GenerateReferenceLoadTwoRegisters(HInstruction* instruction,
6940 Location out,
6941 Location obj,
6942 uint32_t offset,
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006943 Location maybe_temp) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006944 Register out_reg = out.AsRegister<Register>();
6945 Register obj_reg = obj.AsRegister<Register>();
6946 if (kEmitCompilerReadBarrier) {
6947 if (kUseBakerReadBarrier) {
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006948 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillain7c1559a2015-12-15 10:55:36 +00006949 // Load with fast path based Baker's read barrier.
6950 // /* HeapReference<Object> */ out = *(obj + offset)
6951 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006952 instruction, out, obj_reg, offset, maybe_temp, /* needs_null_check */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006953 } else {
6954 // Load with slow path based read barrier.
6955 // /* HeapReference<Object> */ out = *(obj + offset)
6956 __ movl(out_reg, Address(obj_reg, offset));
6957 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
6958 }
6959 } else {
6960 // Plain load with no read barrier.
6961 // /* HeapReference<Object> */ out = *(obj + offset)
6962 __ movl(out_reg, Address(obj_reg, offset));
6963 __ MaybeUnpoisonHeapReference(out_reg);
6964 }
6965}
6966
6967void InstructionCodeGeneratorX86::GenerateGcRootFieldLoad(HInstruction* instruction,
6968 Location root,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006969 const Address& address,
6970 Label* fixup_label) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006971 Register root_reg = root.AsRegister<Register>();
6972 if (kEmitCompilerReadBarrier) {
6973 if (kUseBakerReadBarrier) {
6974 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
6975 // Baker's read barrier are used:
6976 //
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006977 // root = *address;
Roland Levillain7c1559a2015-12-15 10:55:36 +00006978 // if (Thread::Current()->GetIsGcMarking()) {
6979 // root = ReadBarrier::Mark(root)
6980 // }
6981
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006982 // /* GcRoot<mirror::Object> */ root = *address
6983 __ movl(root_reg, address);
6984 if (fixup_label != nullptr) {
6985 __ Bind(fixup_label);
6986 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00006987 static_assert(
6988 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
6989 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
6990 "have different sizes.");
6991 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
6992 "art::mirror::CompressedReference<mirror::Object> and int32_t "
6993 "have different sizes.");
6994
6995 // Slow path used to mark the GC root `root`.
6996 SlowPathCode* slow_path =
Roland Levillain02b75802016-07-13 11:54:35 +01006997 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathX86(instruction, root);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006998 codegen_->AddSlowPath(slow_path);
6999
7000 __ fs()->cmpl(Address::Absolute(Thread::IsGcMarkingOffset<kX86WordSize>().Int32Value()),
7001 Immediate(0));
7002 __ j(kNotEqual, slow_path->GetEntryLabel());
7003 __ Bind(slow_path->GetExitLabel());
7004 } else {
7005 // GC root loaded through a slow path for read barriers other
7006 // than Baker's.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007007 // /* GcRoot<mirror::Object>* */ root = address
7008 __ leal(root_reg, address);
7009 if (fixup_label != nullptr) {
7010 __ Bind(fixup_label);
7011 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00007012 // /* mirror::Object* */ root = root->Read()
7013 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
7014 }
7015 } else {
7016 // Plain GC root load with no read barrier.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007017 // /* GcRoot<mirror::Object> */ root = *address
7018 __ movl(root_reg, address);
7019 if (fixup_label != nullptr) {
7020 __ Bind(fixup_label);
7021 }
Roland Levillaine3f43ac2016-01-19 15:07:47 +00007022 // Note that GC roots are not affected by heap poisoning, thus we
7023 // do not have to unpoison `root_reg` here.
Roland Levillain7c1559a2015-12-15 10:55:36 +00007024 }
7025}
7026
7027void CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
7028 Location ref,
7029 Register obj,
7030 uint32_t offset,
7031 Location temp,
7032 bool needs_null_check) {
7033 DCHECK(kEmitCompilerReadBarrier);
7034 DCHECK(kUseBakerReadBarrier);
7035
7036 // /* HeapReference<Object> */ ref = *(obj + offset)
7037 Address src(obj, offset);
7038 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, temp, needs_null_check);
7039}
7040
7041void CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
7042 Location ref,
7043 Register obj,
7044 uint32_t data_offset,
7045 Location index,
7046 Location temp,
7047 bool needs_null_check) {
7048 DCHECK(kEmitCompilerReadBarrier);
7049 DCHECK(kUseBakerReadBarrier);
7050
Roland Levillain3d312422016-06-23 13:53:42 +01007051 static_assert(
7052 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
7053 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain7c1559a2015-12-15 10:55:36 +00007054 // /* HeapReference<Object> */ ref =
7055 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
7056 Address src = index.IsConstant() ?
7057 Address(obj, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset) :
7058 Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset);
7059 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, temp, needs_null_check);
7060}
7061
7062void CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
7063 Location ref,
7064 Register obj,
7065 const Address& src,
7066 Location temp,
7067 bool needs_null_check) {
7068 DCHECK(kEmitCompilerReadBarrier);
7069 DCHECK(kUseBakerReadBarrier);
7070
7071 // In slow path based read barriers, the read barrier call is
7072 // inserted after the original load. However, in fast path based
7073 // Baker's read barriers, we need to perform the load of
7074 // mirror::Object::monitor_ *before* the original reference load.
7075 // This load-load ordering is required by the read barrier.
7076 // The fast path/slow path (for Baker's algorithm) should look like:
7077 //
7078 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
7079 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
7080 // HeapReference<Object> ref = *src; // Original reference load.
7081 // bool is_gray = (rb_state == ReadBarrier::gray_ptr_);
7082 // if (is_gray) {
7083 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
7084 // }
7085 //
7086 // Note: the original implementation in ReadBarrier::Barrier is
7087 // slightly more complex as:
7088 // - it implements the load-load fence using a data dependency on
Roland Levillaine3f43ac2016-01-19 15:07:47 +00007089 // the high-bits of rb_state, which are expected to be all zeroes
7090 // (we use CodeGeneratorX86::GenerateMemoryBarrier instead here,
7091 // which is a no-op thanks to the x86 memory model);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007092 // - it performs additional checks that we do not do here for
7093 // performance reasons.
7094
7095 Register ref_reg = ref.AsRegister<Register>();
7096 Register temp_reg = temp.AsRegister<Register>();
7097 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
7098
7099 // /* int32_t */ monitor = obj->monitor_
7100 __ movl(temp_reg, Address(obj, monitor_offset));
7101 if (needs_null_check) {
7102 MaybeRecordImplicitNullCheck(instruction);
7103 }
7104 // /* LockWord */ lock_word = LockWord(monitor)
7105 static_assert(sizeof(LockWord) == sizeof(int32_t),
7106 "art::LockWord and int32_t have different sizes.");
7107 // /* uint32_t */ rb_state = lock_word.ReadBarrierState()
7108 __ shrl(temp_reg, Immediate(LockWord::kReadBarrierStateShift));
7109 __ andl(temp_reg, Immediate(LockWord::kReadBarrierStateMask));
7110 static_assert(
7111 LockWord::kReadBarrierStateMask == ReadBarrier::rb_ptr_mask_,
7112 "art::LockWord::kReadBarrierStateMask is not equal to art::ReadBarrier::rb_ptr_mask_.");
7113
7114 // Load fence to prevent load-load reordering.
7115 // Note that this is a no-op, thanks to the x86 memory model.
7116 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
7117
7118 // The actual reference load.
7119 // /* HeapReference<Object> */ ref = *src
7120 __ movl(ref_reg, src);
7121
7122 // Object* ref = ref_addr->AsMirrorPtr()
7123 __ MaybeUnpoisonHeapReference(ref_reg);
7124
7125 // Slow path used to mark the object `ref` when it is gray.
7126 SlowPathCode* slow_path =
Roland Levillain02b75802016-07-13 11:54:35 +01007127 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathX86(instruction, ref);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007128 AddSlowPath(slow_path);
7129
7130 // if (rb_state == ReadBarrier::gray_ptr_)
7131 // ref = ReadBarrier::Mark(ref);
7132 __ cmpl(temp_reg, Immediate(ReadBarrier::gray_ptr_));
7133 __ j(kEqual, slow_path->GetEntryLabel());
7134 __ Bind(slow_path->GetExitLabel());
7135}
7136
7137void CodeGeneratorX86::GenerateReadBarrierSlow(HInstruction* instruction,
7138 Location out,
7139 Location ref,
7140 Location obj,
7141 uint32_t offset,
7142 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007143 DCHECK(kEmitCompilerReadBarrier);
7144
Roland Levillain7c1559a2015-12-15 10:55:36 +00007145 // Insert a slow path based read barrier *after* the reference load.
7146 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00007147 // If heap poisoning is enabled, the unpoisoning of the loaded
7148 // reference will be carried out by the runtime within the slow
7149 // path.
7150 //
7151 // Note that `ref` currently does not get unpoisoned (when heap
7152 // poisoning is enabled), which is alright as the `ref` argument is
7153 // not used by the artReadBarrierSlow entry point.
7154 //
7155 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
7156 SlowPathCode* slow_path = new (GetGraph()->GetArena())
7157 ReadBarrierForHeapReferenceSlowPathX86(instruction, out, ref, obj, offset, index);
7158 AddSlowPath(slow_path);
7159
Roland Levillain0d5a2812015-11-13 10:07:31 +00007160 __ jmp(slow_path->GetEntryLabel());
7161 __ Bind(slow_path->GetExitLabel());
7162}
7163
Roland Levillain7c1559a2015-12-15 10:55:36 +00007164void CodeGeneratorX86::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
7165 Location out,
7166 Location ref,
7167 Location obj,
7168 uint32_t offset,
7169 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007170 if (kEmitCompilerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007171 // Baker's read barriers shall be handled by the fast path
7172 // (CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier).
7173 DCHECK(!kUseBakerReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007174 // If heap poisoning is enabled, unpoisoning will be taken care of
7175 // by the runtime within the slow path.
Roland Levillain7c1559a2015-12-15 10:55:36 +00007176 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007177 } else if (kPoisonHeapReferences) {
7178 __ UnpoisonHeapReference(out.AsRegister<Register>());
7179 }
7180}
7181
Roland Levillain7c1559a2015-12-15 10:55:36 +00007182void CodeGeneratorX86::GenerateReadBarrierForRootSlow(HInstruction* instruction,
7183 Location out,
7184 Location root) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007185 DCHECK(kEmitCompilerReadBarrier);
7186
Roland Levillain7c1559a2015-12-15 10:55:36 +00007187 // Insert a slow path based read barrier *after* the GC root load.
7188 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00007189 // Note that GC roots are not affected by heap poisoning, so we do
7190 // not need to do anything special for this here.
7191 SlowPathCode* slow_path =
7192 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathX86(instruction, out, root);
7193 AddSlowPath(slow_path);
7194
Roland Levillain0d5a2812015-11-13 10:07:31 +00007195 __ jmp(slow_path->GetEntryLabel());
7196 __ Bind(slow_path->GetExitLabel());
7197}
7198
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007199void LocationsBuilderX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00007200 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00007201 LOG(FATAL) << "Unreachable";
7202}
7203
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007204void InstructionCodeGeneratorX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00007205 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00007206 LOG(FATAL) << "Unreachable";
7207}
7208
Mark Mendellfe57faa2015-09-18 09:26:15 -04007209// Simple implementation of packed switch - generate cascaded compare/jumps.
7210void LocationsBuilderX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7211 LocationSummary* locations =
7212 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
7213 locations->SetInAt(0, Location::RequiresRegister());
7214}
7215
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007216void InstructionCodeGeneratorX86::GenPackedSwitchWithCompares(Register value_reg,
7217 int32_t lower_bound,
7218 uint32_t num_entries,
7219 HBasicBlock* switch_block,
7220 HBasicBlock* default_block) {
7221 // Figure out the correct compare values and jump conditions.
7222 // Handle the first compare/branch as a special case because it might
7223 // jump to the default case.
7224 DCHECK_GT(num_entries, 2u);
7225 Condition first_condition;
7226 uint32_t index;
7227 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
7228 if (lower_bound != 0) {
7229 first_condition = kLess;
7230 __ cmpl(value_reg, Immediate(lower_bound));
7231 __ j(first_condition, codegen_->GetLabelOf(default_block));
7232 __ j(kEqual, codegen_->GetLabelOf(successors[0]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007233
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007234 index = 1;
7235 } else {
7236 // Handle all the compare/jumps below.
7237 first_condition = kBelow;
7238 index = 0;
7239 }
7240
7241 // Handle the rest of the compare/jumps.
7242 for (; index + 1 < num_entries; index += 2) {
7243 int32_t compare_to_value = lower_bound + index + 1;
7244 __ cmpl(value_reg, Immediate(compare_to_value));
7245 // Jump to successors[index] if value < case_value[index].
7246 __ j(first_condition, codegen_->GetLabelOf(successors[index]));
7247 // Jump to successors[index + 1] if value == case_value[index + 1].
7248 __ j(kEqual, codegen_->GetLabelOf(successors[index + 1]));
7249 }
7250
7251 if (index != num_entries) {
7252 // There are an odd number of entries. Handle the last one.
7253 DCHECK_EQ(index + 1, num_entries);
7254 __ cmpl(value_reg, Immediate(lower_bound + index));
7255 __ j(kEqual, codegen_->GetLabelOf(successors[index]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007256 }
7257
7258 // And the default for any other value.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007259 if (!codegen_->GoesToNextBlock(switch_block, default_block)) {
7260 __ jmp(codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007261 }
7262}
7263
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007264void InstructionCodeGeneratorX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7265 int32_t lower_bound = switch_instr->GetStartValue();
7266 uint32_t num_entries = switch_instr->GetNumEntries();
7267 LocationSummary* locations = switch_instr->GetLocations();
7268 Register value_reg = locations->InAt(0).AsRegister<Register>();
7269
7270 GenPackedSwitchWithCompares(value_reg,
7271 lower_bound,
7272 num_entries,
7273 switch_instr->GetBlock(),
7274 switch_instr->GetDefaultBlock());
7275}
7276
Mark Mendell805b3b52015-09-18 14:10:29 -04007277void LocationsBuilderX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
7278 LocationSummary* locations =
7279 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
7280 locations->SetInAt(0, Location::RequiresRegister());
7281
7282 // Constant area pointer.
7283 locations->SetInAt(1, Location::RequiresRegister());
7284
7285 // And the temporary we need.
7286 locations->AddTemp(Location::RequiresRegister());
7287}
7288
7289void InstructionCodeGeneratorX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
7290 int32_t lower_bound = switch_instr->GetStartValue();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007291 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendell805b3b52015-09-18 14:10:29 -04007292 LocationSummary* locations = switch_instr->GetLocations();
7293 Register value_reg = locations->InAt(0).AsRegister<Register>();
7294 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
7295
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007296 if (num_entries <= kPackedSwitchJumpTableThreshold) {
7297 GenPackedSwitchWithCompares(value_reg,
7298 lower_bound,
7299 num_entries,
7300 switch_instr->GetBlock(),
7301 default_block);
7302 return;
7303 }
7304
Mark Mendell805b3b52015-09-18 14:10:29 -04007305 // Optimizing has a jump area.
7306 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
7307 Register constant_area = locations->InAt(1).AsRegister<Register>();
7308
7309 // Remove the bias, if needed.
7310 if (lower_bound != 0) {
7311 __ leal(temp_reg, Address(value_reg, -lower_bound));
7312 value_reg = temp_reg;
7313 }
7314
7315 // Is the value in range?
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007316 DCHECK_GE(num_entries, 1u);
Mark Mendell805b3b52015-09-18 14:10:29 -04007317 __ cmpl(value_reg, Immediate(num_entries - 1));
7318 __ j(kAbove, codegen_->GetLabelOf(default_block));
7319
7320 // We are in the range of the table.
7321 // Load (target-constant_area) from the jump table, indexing by the value.
7322 __ movl(temp_reg, codegen_->LiteralCaseTable(switch_instr, constant_area, value_reg));
7323
7324 // Compute the actual target address by adding in constant_area.
7325 __ addl(temp_reg, constant_area);
7326
7327 // And jump.
7328 __ jmp(temp_reg);
7329}
7330
Mark Mendell0616ae02015-04-17 12:49:27 -04007331void LocationsBuilderX86::VisitX86ComputeBaseMethodAddress(
7332 HX86ComputeBaseMethodAddress* insn) {
7333 LocationSummary* locations =
7334 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
7335 locations->SetOut(Location::RequiresRegister());
7336}
7337
7338void InstructionCodeGeneratorX86::VisitX86ComputeBaseMethodAddress(
7339 HX86ComputeBaseMethodAddress* insn) {
7340 LocationSummary* locations = insn->GetLocations();
7341 Register reg = locations->Out().AsRegister<Register>();
7342
7343 // Generate call to next instruction.
7344 Label next_instruction;
7345 __ call(&next_instruction);
7346 __ Bind(&next_instruction);
7347
7348 // Remember this offset for later use with constant area.
7349 codegen_->SetMethodAddressOffset(GetAssembler()->CodeSize());
7350
7351 // Grab the return address off the stack.
7352 __ popl(reg);
7353}
7354
7355void LocationsBuilderX86::VisitX86LoadFromConstantTable(
7356 HX86LoadFromConstantTable* insn) {
7357 LocationSummary* locations =
7358 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
7359
7360 locations->SetInAt(0, Location::RequiresRegister());
7361 locations->SetInAt(1, Location::ConstantLocation(insn->GetConstant()));
7362
7363 // If we don't need to be materialized, we only need the inputs to be set.
David Brazdilb3e773e2016-01-26 11:28:37 +00007364 if (insn->IsEmittedAtUseSite()) {
Mark Mendell0616ae02015-04-17 12:49:27 -04007365 return;
7366 }
7367
7368 switch (insn->GetType()) {
7369 case Primitive::kPrimFloat:
7370 case Primitive::kPrimDouble:
7371 locations->SetOut(Location::RequiresFpuRegister());
7372 break;
7373
7374 case Primitive::kPrimInt:
7375 locations->SetOut(Location::RequiresRegister());
7376 break;
7377
7378 default:
7379 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
7380 }
7381}
7382
7383void InstructionCodeGeneratorX86::VisitX86LoadFromConstantTable(HX86LoadFromConstantTable* insn) {
David Brazdilb3e773e2016-01-26 11:28:37 +00007384 if (insn->IsEmittedAtUseSite()) {
Mark Mendell0616ae02015-04-17 12:49:27 -04007385 return;
7386 }
7387
7388 LocationSummary* locations = insn->GetLocations();
7389 Location out = locations->Out();
7390 Register const_area = locations->InAt(0).AsRegister<Register>();
7391 HConstant *value = insn->GetConstant();
7392
7393 switch (insn->GetType()) {
7394 case Primitive::kPrimFloat:
7395 __ movss(out.AsFpuRegister<XmmRegister>(),
7396 codegen_->LiteralFloatAddress(value->AsFloatConstant()->GetValue(), const_area));
7397 break;
7398
7399 case Primitive::kPrimDouble:
7400 __ movsd(out.AsFpuRegister<XmmRegister>(),
7401 codegen_->LiteralDoubleAddress(value->AsDoubleConstant()->GetValue(), const_area));
7402 break;
7403
7404 case Primitive::kPrimInt:
7405 __ movl(out.AsRegister<Register>(),
7406 codegen_->LiteralInt32Address(value->AsIntConstant()->GetValue(), const_area));
7407 break;
7408
7409 default:
7410 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
7411 }
7412}
7413
Mark Mendell0616ae02015-04-17 12:49:27 -04007414/**
7415 * Class to handle late fixup of offsets into constant area.
7416 */
Vladimir Marko5233f932015-09-29 19:01:15 +01007417class RIPFixup : public AssemblerFixup, public ArenaObject<kArenaAllocCodeGenerator> {
Mark Mendell0616ae02015-04-17 12:49:27 -04007418 public:
Mark Mendell805b3b52015-09-18 14:10:29 -04007419 RIPFixup(CodeGeneratorX86& codegen, size_t offset)
7420 : codegen_(&codegen), offset_into_constant_area_(offset) {}
7421
7422 protected:
7423 void SetOffset(size_t offset) { offset_into_constant_area_ = offset; }
7424
7425 CodeGeneratorX86* codegen_;
Mark Mendell0616ae02015-04-17 12:49:27 -04007426
7427 private:
7428 void Process(const MemoryRegion& region, int pos) OVERRIDE {
7429 // Patch the correct offset for the instruction. The place to patch is the
7430 // last 4 bytes of the instruction.
7431 // The value to patch is the distance from the offset in the constant area
7432 // from the address computed by the HX86ComputeBaseMethodAddress instruction.
Mark Mendell805b3b52015-09-18 14:10:29 -04007433 int32_t constant_offset = codegen_->ConstantAreaStart() + offset_into_constant_area_;
7434 int32_t relative_position = constant_offset - codegen_->GetMethodAddressOffset();;
Mark Mendell0616ae02015-04-17 12:49:27 -04007435
7436 // Patch in the right value.
7437 region.StoreUnaligned<int32_t>(pos - 4, relative_position);
7438 }
7439
Mark Mendell0616ae02015-04-17 12:49:27 -04007440 // Location in constant area that the fixup refers to.
Mark Mendell805b3b52015-09-18 14:10:29 -04007441 int32_t offset_into_constant_area_;
Mark Mendell0616ae02015-04-17 12:49:27 -04007442};
7443
Mark Mendell805b3b52015-09-18 14:10:29 -04007444/**
7445 * Class to handle late fixup of offsets to a jump table that will be created in the
7446 * constant area.
7447 */
7448class JumpTableRIPFixup : public RIPFixup {
7449 public:
7450 JumpTableRIPFixup(CodeGeneratorX86& codegen, HX86PackedSwitch* switch_instr)
7451 : RIPFixup(codegen, static_cast<size_t>(-1)), switch_instr_(switch_instr) {}
7452
7453 void CreateJumpTable() {
7454 X86Assembler* assembler = codegen_->GetAssembler();
7455
7456 // Ensure that the reference to the jump table has the correct offset.
7457 const int32_t offset_in_constant_table = assembler->ConstantAreaSize();
7458 SetOffset(offset_in_constant_table);
7459
7460 // The label values in the jump table are computed relative to the
7461 // instruction addressing the constant area.
7462 const int32_t relative_offset = codegen_->GetMethodAddressOffset();
7463
7464 // Populate the jump table with the correct values for the jump table.
7465 int32_t num_entries = switch_instr_->GetNumEntries();
7466 HBasicBlock* block = switch_instr_->GetBlock();
7467 const ArenaVector<HBasicBlock*>& successors = block->GetSuccessors();
7468 // The value that we want is the target offset - the position of the table.
7469 for (int32_t i = 0; i < num_entries; i++) {
7470 HBasicBlock* b = successors[i];
7471 Label* l = codegen_->GetLabelOf(b);
7472 DCHECK(l->IsBound());
7473 int32_t offset_to_block = l->Position() - relative_offset;
7474 assembler->AppendInt32(offset_to_block);
7475 }
7476 }
7477
7478 private:
7479 const HX86PackedSwitch* switch_instr_;
7480};
7481
7482void CodeGeneratorX86::Finalize(CodeAllocator* allocator) {
7483 // Generate the constant area if needed.
7484 X86Assembler* assembler = GetAssembler();
7485 if (!assembler->IsConstantAreaEmpty() || !fixups_to_jump_tables_.empty()) {
7486 // Align to 4 byte boundary to reduce cache misses, as the data is 4 and 8
7487 // byte values.
7488 assembler->Align(4, 0);
7489 constant_area_start_ = assembler->CodeSize();
7490
7491 // Populate any jump tables.
7492 for (auto jump_table : fixups_to_jump_tables_) {
7493 jump_table->CreateJumpTable();
7494 }
7495
7496 // And now add the constant area to the generated code.
7497 assembler->AddConstantArea();
7498 }
7499
7500 // And finish up.
7501 CodeGenerator::Finalize(allocator);
7502}
7503
Mark Mendell0616ae02015-04-17 12:49:27 -04007504Address CodeGeneratorX86::LiteralDoubleAddress(double v, Register reg) {
7505 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddDouble(v));
7506 return Address(reg, kDummy32BitOffset, fixup);
7507}
7508
7509Address CodeGeneratorX86::LiteralFloatAddress(float v, Register reg) {
7510 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddFloat(v));
7511 return Address(reg, kDummy32BitOffset, fixup);
7512}
7513
7514Address CodeGeneratorX86::LiteralInt32Address(int32_t v, Register reg) {
7515 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt32(v));
7516 return Address(reg, kDummy32BitOffset, fixup);
7517}
7518
7519Address CodeGeneratorX86::LiteralInt64Address(int64_t v, Register reg) {
7520 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt64(v));
7521 return Address(reg, kDummy32BitOffset, fixup);
7522}
7523
Aart Bika19616e2016-02-01 18:57:58 -08007524void CodeGeneratorX86::Load32BitValue(Register dest, int32_t value) {
7525 if (value == 0) {
7526 __ xorl(dest, dest);
7527 } else {
7528 __ movl(dest, Immediate(value));
7529 }
7530}
7531
7532void CodeGeneratorX86::Compare32BitValue(Register dest, int32_t value) {
7533 if (value == 0) {
7534 __ testl(dest, dest);
7535 } else {
7536 __ cmpl(dest, Immediate(value));
7537 }
7538}
7539
Mark Mendell805b3b52015-09-18 14:10:29 -04007540Address CodeGeneratorX86::LiteralCaseTable(HX86PackedSwitch* switch_instr,
7541 Register reg,
7542 Register value) {
7543 // Create a fixup to be used to create and address the jump table.
7544 JumpTableRIPFixup* table_fixup =
7545 new (GetGraph()->GetArena()) JumpTableRIPFixup(*this, switch_instr);
7546
7547 // We have to populate the jump tables.
7548 fixups_to_jump_tables_.push_back(table_fixup);
7549
7550 // We want a scaled address, as we are extracting the correct offset from the table.
7551 return Address(reg, value, TIMES_4, kDummy32BitOffset, table_fixup);
7552}
7553
Andreas Gampe85b62f22015-09-09 13:15:38 -07007554// TODO: target as memory.
7555void CodeGeneratorX86::MoveFromReturnRegister(Location target, Primitive::Type type) {
7556 if (!target.IsValid()) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007557 DCHECK_EQ(type, Primitive::kPrimVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07007558 return;
7559 }
7560
7561 DCHECK_NE(type, Primitive::kPrimVoid);
7562
7563 Location return_loc = InvokeDexCallingConventionVisitorX86().GetReturnLocation(type);
7564 if (target.Equals(return_loc)) {
7565 return;
7566 }
7567
7568 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
7569 // with the else branch.
7570 if (type == Primitive::kPrimLong) {
7571 HParallelMove parallel_move(GetGraph()->GetArena());
7572 parallel_move.AddMove(return_loc.ToLow(), target.ToLow(), Primitive::kPrimInt, nullptr);
7573 parallel_move.AddMove(return_loc.ToHigh(), target.ToHigh(), Primitive::kPrimInt, nullptr);
7574 GetMoveResolver()->EmitNativeCode(&parallel_move);
7575 } else {
7576 // Let the parallel move resolver take care of all of this.
7577 HParallelMove parallel_move(GetGraph()->GetArena());
7578 parallel_move.AddMove(return_loc, target, type, nullptr);
7579 GetMoveResolver()->EmitNativeCode(&parallel_move);
7580 }
7581}
7582
Roland Levillain4d027112015-07-01 15:41:14 +01007583#undef __
7584
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00007585} // namespace x86
7586} // namespace art