blob: 7f39661e6b9cab93539ec5763a3b77aff68e397e [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<
352 kQuickInstanceofNonTrivial, uint32_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:
450 ReadBarrierMarkSlowPathX86(HInstruction* instruction, Location out, Location obj)
David Srbecky9cd6d372016-02-09 15:24:47 +0000451 : SlowPathCode(instruction), out_(out), 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();
459 Register reg_out = out_.AsRegister<Register>();
460 DCHECK(locations->CanCall());
461 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
462 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());
475 SaveLiveRegisters(codegen, locations);
476
477 InvokeRuntimeCallingConvention calling_convention;
478 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
479 x86_codegen->Move32(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), obj_);
480 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pReadBarrierMark),
481 instruction_,
482 instruction_->GetDexPc(),
483 this);
484 CheckEntrypointTypes<kQuickReadBarrierMark, mirror::Object*, mirror::Object*>();
485 x86_codegen->Move32(out_, Location::RegisterLocation(EAX));
486
487 RestoreLiveRegisters(codegen, locations);
488 __ jmp(GetExitLabel());
489 }
490
491 private:
Roland Levillain7c1559a2015-12-15 10:55:36 +0000492 const Location out_;
493 const Location obj_;
494
495 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathX86);
496};
497
Roland Levillain0d5a2812015-11-13 10:07:31 +0000498// Slow path generating a read barrier for a heap reference.
499class ReadBarrierForHeapReferenceSlowPathX86 : public SlowPathCode {
500 public:
501 ReadBarrierForHeapReferenceSlowPathX86(HInstruction* instruction,
502 Location out,
503 Location ref,
504 Location obj,
505 uint32_t offset,
506 Location index)
David Srbecky9cd6d372016-02-09 15:24:47 +0000507 : SlowPathCode(instruction),
Roland Levillain0d5a2812015-11-13 10:07:31 +0000508 out_(out),
509 ref_(ref),
510 obj_(obj),
511 offset_(offset),
512 index_(index) {
513 DCHECK(kEmitCompilerReadBarrier);
514 // If `obj` is equal to `out` or `ref`, it means the initial object
515 // has been overwritten by (or after) the heap object reference load
516 // to be instrumented, e.g.:
517 //
518 // __ movl(out, Address(out, offset));
Roland Levillain7c1559a2015-12-15 10:55:36 +0000519 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000520 //
521 // In that case, we have lost the information about the original
522 // object, and the emitted read barrier cannot work properly.
523 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
524 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
525 }
526
527 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
528 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
529 LocationSummary* locations = instruction_->GetLocations();
530 Register reg_out = out_.AsRegister<Register>();
531 DCHECK(locations->CanCall());
532 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
Roland Levillain3d312422016-06-23 13:53:42 +0100533 DCHECK(instruction_->IsInstanceFieldGet() ||
534 instruction_->IsStaticFieldGet() ||
535 instruction_->IsArrayGet() ||
536 instruction_->IsInstanceOf() ||
537 instruction_->IsCheckCast() ||
538 ((instruction_->IsInvokeStaticOrDirect() || instruction_->IsInvokeVirtual()) &&
Roland Levillain7c1559a2015-12-15 10:55:36 +0000539 instruction_->GetLocations()->Intrinsified()))
540 << "Unexpected instruction in read barrier for heap reference slow path: "
541 << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000542
543 __ Bind(GetEntryLabel());
544 SaveLiveRegisters(codegen, locations);
545
546 // We may have to change the index's value, but as `index_` is a
547 // constant member (like other "inputs" of this slow path),
548 // introduce a copy of it, `index`.
549 Location index = index_;
550 if (index_.IsValid()) {
Roland Levillain3d312422016-06-23 13:53:42 +0100551 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
Roland Levillain0d5a2812015-11-13 10:07:31 +0000552 if (instruction_->IsArrayGet()) {
553 // Compute the actual memory offset and store it in `index`.
554 Register index_reg = index_.AsRegister<Register>();
555 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg));
556 if (codegen->IsCoreCalleeSaveRegister(index_reg)) {
557 // We are about to change the value of `index_reg` (see the
558 // calls to art::x86::X86Assembler::shll and
559 // art::x86::X86Assembler::AddImmediate below), but it has
560 // not been saved by the previous call to
561 // art::SlowPathCode::SaveLiveRegisters, as it is a
562 // callee-save register --
563 // art::SlowPathCode::SaveLiveRegisters does not consider
564 // callee-save registers, as it has been designed with the
565 // assumption that callee-save registers are supposed to be
566 // handled by the called function. So, as a callee-save
567 // register, `index_reg` _would_ eventually be saved onto
568 // the stack, but it would be too late: we would have
569 // changed its value earlier. Therefore, we manually save
570 // it here into another freely available register,
571 // `free_reg`, chosen of course among the caller-save
572 // registers (as a callee-save `free_reg` register would
573 // exhibit the same problem).
574 //
575 // Note we could have requested a temporary register from
576 // the register allocator instead; but we prefer not to, as
577 // this is a slow path, and we know we can find a
578 // caller-save register that is available.
579 Register free_reg = FindAvailableCallerSaveRegister(codegen);
580 __ movl(free_reg, index_reg);
581 index_reg = free_reg;
582 index = Location::RegisterLocation(index_reg);
583 } else {
584 // The initial register stored in `index_` has already been
585 // saved in the call to art::SlowPathCode::SaveLiveRegisters
586 // (as it is not a callee-save register), so we can freely
587 // use it.
588 }
589 // Shifting the index value contained in `index_reg` by the scale
590 // factor (2) cannot overflow in practice, as the runtime is
591 // unable to allocate object arrays with a size larger than
592 // 2^26 - 1 (that is, 2^28 - 4 bytes).
593 __ shll(index_reg, Immediate(TIMES_4));
594 static_assert(
595 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
596 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
597 __ AddImmediate(index_reg, Immediate(offset_));
598 } else {
Roland Levillain3d312422016-06-23 13:53:42 +0100599 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
600 // intrinsics, `index_` is not shifted by a scale factor of 2
601 // (as in the case of ArrayGet), as it is actually an offset
602 // to an object field within an object.
603 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000604 DCHECK(instruction_->GetLocations()->Intrinsified());
605 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
606 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
607 << instruction_->AsInvoke()->GetIntrinsic();
608 DCHECK_EQ(offset_, 0U);
609 DCHECK(index_.IsRegisterPair());
610 // UnsafeGet's offset location is a register pair, the low
611 // part contains the correct offset.
612 index = index_.ToLow();
613 }
614 }
615
616 // We're moving two or three locations to locations that could
617 // overlap, so we need a parallel move resolver.
618 InvokeRuntimeCallingConvention calling_convention;
619 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
620 parallel_move.AddMove(ref_,
621 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
622 Primitive::kPrimNot,
623 nullptr);
624 parallel_move.AddMove(obj_,
625 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
626 Primitive::kPrimNot,
627 nullptr);
628 if (index.IsValid()) {
629 parallel_move.AddMove(index,
630 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
631 Primitive::kPrimInt,
632 nullptr);
633 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
634 } else {
635 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
636 __ movl(calling_convention.GetRegisterAt(2), Immediate(offset_));
637 }
638 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pReadBarrierSlow),
639 instruction_,
640 instruction_->GetDexPc(),
641 this);
642 CheckEntrypointTypes<
643 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
644 x86_codegen->Move32(out_, Location::RegisterLocation(EAX));
645
646 RestoreLiveRegisters(codegen, locations);
647 __ jmp(GetExitLabel());
648 }
649
650 const char* GetDescription() const OVERRIDE { return "ReadBarrierForHeapReferenceSlowPathX86"; }
651
652 private:
653 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
654 size_t ref = static_cast<int>(ref_.AsRegister<Register>());
655 size_t obj = static_cast<int>(obj_.AsRegister<Register>());
656 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
657 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
658 return static_cast<Register>(i);
659 }
660 }
661 // We shall never fail to find a free caller-save register, as
662 // there are more than two core caller-save registers on x86
663 // (meaning it is possible to find one which is different from
664 // `ref` and `obj`).
665 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
666 LOG(FATAL) << "Could not find a free caller-save register";
667 UNREACHABLE();
668 }
669
Roland Levillain0d5a2812015-11-13 10:07:31 +0000670 const Location out_;
671 const Location ref_;
672 const Location obj_;
673 const uint32_t offset_;
674 // An additional location containing an index to an array.
675 // Only used for HArrayGet and the UnsafeGetObject &
676 // UnsafeGetObjectVolatile intrinsics.
677 const Location index_;
678
679 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathX86);
680};
681
682// Slow path generating a read barrier for a GC root.
683class ReadBarrierForRootSlowPathX86 : public SlowPathCode {
684 public:
685 ReadBarrierForRootSlowPathX86(HInstruction* instruction, Location out, Location root)
David Srbecky9cd6d372016-02-09 15:24:47 +0000686 : SlowPathCode(instruction), out_(out), root_(root) {
Roland Levillain7c1559a2015-12-15 10:55:36 +0000687 DCHECK(kEmitCompilerReadBarrier);
688 }
Roland Levillain0d5a2812015-11-13 10:07:31 +0000689
690 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
691 LocationSummary* locations = instruction_->GetLocations();
692 Register reg_out = out_.AsRegister<Register>();
693 DCHECK(locations->CanCall());
694 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
Roland Levillain7c1559a2015-12-15 10:55:36 +0000695 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
696 << "Unexpected instruction in read barrier for GC root slow path: "
697 << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000698
699 __ Bind(GetEntryLabel());
700 SaveLiveRegisters(codegen, locations);
701
702 InvokeRuntimeCallingConvention calling_convention;
703 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
704 x86_codegen->Move32(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), root_);
705 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pReadBarrierForRootSlow),
706 instruction_,
707 instruction_->GetDexPc(),
708 this);
709 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
710 x86_codegen->Move32(out_, Location::RegisterLocation(EAX));
711
712 RestoreLiveRegisters(codegen, locations);
713 __ jmp(GetExitLabel());
714 }
715
716 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathX86"; }
717
718 private:
Roland Levillain0d5a2812015-11-13 10:07:31 +0000719 const Location out_;
720 const Location root_;
721
722 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathX86);
723};
724
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100725#undef __
Chih-Hung Hsiehfba39972016-05-11 11:26:48 -0700726// NOLINT on __ macro to suppress wrong warning/fix from clang-tidy.
727#define __ down_cast<X86Assembler*>(GetAssembler())-> /* NOLINT */
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100728
Aart Bike9f37602015-10-09 11:15:55 -0700729inline Condition X86Condition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -0700730 switch (cond) {
731 case kCondEQ: return kEqual;
732 case kCondNE: return kNotEqual;
733 case kCondLT: return kLess;
734 case kCondLE: return kLessEqual;
735 case kCondGT: return kGreater;
736 case kCondGE: return kGreaterEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700737 case kCondB: return kBelow;
738 case kCondBE: return kBelowEqual;
739 case kCondA: return kAbove;
740 case kCondAE: return kAboveEqual;
Dave Allison20dfc792014-06-16 20:44:29 -0700741 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100742 LOG(FATAL) << "Unreachable";
743 UNREACHABLE();
744}
745
Aart Bike9f37602015-10-09 11:15:55 -0700746// Maps signed condition to unsigned condition and FP condition to x86 name.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100747inline Condition X86UnsignedOrFPCondition(IfCondition cond) {
748 switch (cond) {
749 case kCondEQ: return kEqual;
750 case kCondNE: return kNotEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700751 // Signed to unsigned, and FP to x86 name.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100752 case kCondLT: return kBelow;
753 case kCondLE: return kBelowEqual;
754 case kCondGT: return kAbove;
755 case kCondGE: return kAboveEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700756 // Unsigned remain unchanged.
757 case kCondB: return kBelow;
758 case kCondBE: return kBelowEqual;
759 case kCondA: return kAbove;
760 case kCondAE: return kAboveEqual;
Roland Levillain4fa13f62015-07-06 18:11:54 +0100761 }
762 LOG(FATAL) << "Unreachable";
763 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -0700764}
765
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100766void CodeGeneratorX86::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100767 stream << Register(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100768}
769
770void CodeGeneratorX86::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100771 stream << XmmRegister(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100772}
773
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100774size_t CodeGeneratorX86::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
775 __ movl(Address(ESP, stack_index), static_cast<Register>(reg_id));
776 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100777}
778
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100779size_t CodeGeneratorX86::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
780 __ movl(static_cast<Register>(reg_id), Address(ESP, stack_index));
781 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100782}
783
Mark Mendell7c8d0092015-01-26 11:21:33 -0500784size_t CodeGeneratorX86::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
785 __ movsd(Address(ESP, stack_index), XmmRegister(reg_id));
786 return GetFloatingPointSpillSlotSize();
787}
788
789size_t CodeGeneratorX86::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
790 __ movsd(XmmRegister(reg_id), Address(ESP, stack_index));
791 return GetFloatingPointSpillSlotSize();
792}
793
Calin Juravle175dc732015-08-25 15:42:32 +0100794void CodeGeneratorX86::InvokeRuntime(QuickEntrypointEnum entrypoint,
795 HInstruction* instruction,
796 uint32_t dex_pc,
797 SlowPathCode* slow_path) {
798 InvokeRuntime(GetThreadOffset<kX86WordSize>(entrypoint).Int32Value(),
799 instruction,
800 dex_pc,
801 slow_path);
802}
803
804void CodeGeneratorX86::InvokeRuntime(int32_t entry_point_offset,
Alexandre Rames8158f282015-08-07 10:26:17 +0100805 HInstruction* instruction,
806 uint32_t dex_pc,
807 SlowPathCode* slow_path) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +0100808 ValidateInvokeRuntime(instruction, slow_path);
Calin Juravle175dc732015-08-25 15:42:32 +0100809 __ fs()->call(Address::Absolute(entry_point_offset));
Alexandre Rames8158f282015-08-07 10:26:17 +0100810 RecordPcInfo(instruction, dex_pc, slow_path);
Alexandre Rames8158f282015-08-07 10:26:17 +0100811}
812
Mark Mendellfb8d2792015-03-31 22:16:59 -0400813CodeGeneratorX86::CodeGeneratorX86(HGraph* graph,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000814 const X86InstructionSetFeatures& isa_features,
815 const CompilerOptions& compiler_options,
816 OptimizingCompilerStats* stats)
Mark Mendell5f874182015-03-04 15:42:45 -0500817 : CodeGenerator(graph,
818 kNumberOfCpuRegisters,
819 kNumberOfXmmRegisters,
820 kNumberOfRegisterPairs,
821 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
822 arraysize(kCoreCalleeSaves))
823 | (1 << kFakeReturnRegister),
Serban Constantinescuecc43662015-08-13 13:33:12 +0100824 0,
825 compiler_options,
826 stats),
Vladimir Marko225b6462015-09-28 12:17:40 +0100827 block_labels_(nullptr),
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100828 location_builder_(graph, this),
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100829 instruction_visitor_(graph, this),
Mark Mendellfb8d2792015-03-31 22:16:59 -0400830 move_resolver_(graph->GetArena(), this),
Vladimir Marko93205e32016-04-13 11:59:46 +0100831 assembler_(graph->GetArena()),
Vladimir Marko58155012015-08-19 12:49:41 +0000832 isa_features_(isa_features),
Vladimir Marko5233f932015-09-29 19:01:15 +0100833 method_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Mark Mendell805b3b52015-09-18 14:10:29 -0400834 relative_call_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000835 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000836 simple_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
837 string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100838 type_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko93205e32016-04-13 11:59:46 +0100839 constant_area_start_(-1),
840 fixups_to_jump_tables_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
841 method_address_offset_(-1) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000842 // Use a fake return address register to mimic Quick.
843 AddAllocatedRegister(Location::RegisterLocation(kFakeReturnRegister));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100844}
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100845
David Brazdil58282f42016-01-14 12:45:10 +0000846void CodeGeneratorX86::SetupBlockedRegisters() const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100847 // Don't allocate the dalvik style register pair passing.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100848 blocked_register_pairs_[ECX_EDX] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100849
850 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100851 blocked_core_registers_[ESP] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100852
Calin Juravle34bacdf2014-10-07 20:23:36 +0100853 UpdateBlockedPairRegisters();
854}
855
856void CodeGeneratorX86::UpdateBlockedPairRegisters() const {
857 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
858 X86ManagedRegister current =
859 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
860 if (blocked_core_registers_[current.AsRegisterPairLow()]
861 || blocked_core_registers_[current.AsRegisterPairHigh()]) {
862 blocked_register_pairs_[i] = true;
863 }
864 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100865}
866
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100867InstructionCodeGeneratorX86::InstructionCodeGeneratorX86(HGraph* graph, CodeGeneratorX86* codegen)
Aart Bik42249c32016-01-07 15:33:50 -0800868 : InstructionCodeGenerator(graph, codegen),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100869 assembler_(codegen->GetAssembler()),
870 codegen_(codegen) {}
871
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100872static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +0100873 return dwarf::Reg::X86Core(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100874}
875
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000876void CodeGeneratorX86::GenerateFrameEntry() {
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100877 __ cfi().SetCurrentCFAOffset(kX86WordSize); // return address
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000878 __ Bind(&frame_entry_label_);
Roland Levillain199f3362014-11-27 17:15:16 +0000879 bool skip_overflow_check =
880 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000881 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Calin Juravle93edf732015-01-20 20:14:07 +0000882
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000883 if (!skip_overflow_check) {
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100884 __ testl(EAX, Address(ESP, -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86))));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100885 RecordPcInfo(nullptr, 0);
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100886 }
887
Mark Mendell5f874182015-03-04 15:42:45 -0500888 if (HasEmptyFrame()) {
889 return;
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000890 }
Mark Mendell5f874182015-03-04 15:42:45 -0500891
892 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
893 Register reg = kCoreCalleeSaves[i];
894 if (allocated_registers_.ContainsCoreRegister(reg)) {
895 __ pushl(reg);
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100896 __ cfi().AdjustCFAOffset(kX86WordSize);
897 __ cfi().RelOffset(DWARFReg(reg), 0);
Mark Mendell5f874182015-03-04 15:42:45 -0500898 }
899 }
900
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100901 int adjust = GetFrameSize() - FrameEntrySpillSize();
902 __ subl(ESP, Immediate(adjust));
903 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100904 __ movl(Address(ESP, kCurrentMethodStackOffset), kMethodRegisterArgument);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000905}
906
907void CodeGeneratorX86::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +0100908 __ cfi().RememberState();
909 if (!HasEmptyFrame()) {
910 int adjust = GetFrameSize() - FrameEntrySpillSize();
911 __ addl(ESP, Immediate(adjust));
912 __ cfi().AdjustCFAOffset(-adjust);
Mark Mendell5f874182015-03-04 15:42:45 -0500913
David Srbeckyc34dc932015-04-12 09:27:43 +0100914 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
915 Register reg = kCoreCalleeSaves[i];
916 if (allocated_registers_.ContainsCoreRegister(reg)) {
917 __ popl(reg);
918 __ cfi().AdjustCFAOffset(-static_cast<int>(kX86WordSize));
919 __ cfi().Restore(DWARFReg(reg));
920 }
Mark Mendell5f874182015-03-04 15:42:45 -0500921 }
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000922 }
David Srbeckyc34dc932015-04-12 09:27:43 +0100923 __ ret();
924 __ cfi().RestoreState();
925 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000926}
927
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100928void CodeGeneratorX86::Bind(HBasicBlock* block) {
929 __ Bind(GetLabelOf(block));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000930}
931
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100932Location InvokeDexCallingConventionVisitorX86::GetReturnLocation(Primitive::Type type) const {
933 switch (type) {
934 case Primitive::kPrimBoolean:
935 case Primitive::kPrimByte:
936 case Primitive::kPrimChar:
937 case Primitive::kPrimShort:
938 case Primitive::kPrimInt:
939 case Primitive::kPrimNot:
940 return Location::RegisterLocation(EAX);
941
942 case Primitive::kPrimLong:
943 return Location::RegisterPairLocation(EAX, EDX);
944
945 case Primitive::kPrimVoid:
946 return Location::NoLocation();
947
948 case Primitive::kPrimDouble:
949 case Primitive::kPrimFloat:
950 return Location::FpuRegisterLocation(XMM0);
951 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +0100952
953 UNREACHABLE();
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100954}
955
956Location InvokeDexCallingConventionVisitorX86::GetMethodLocation() const {
957 return Location::RegisterLocation(kMethodRegisterArgument);
958}
959
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100960Location InvokeDexCallingConventionVisitorX86::GetNextLocation(Primitive::Type type) {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100961 switch (type) {
962 case Primitive::kPrimBoolean:
963 case Primitive::kPrimByte:
964 case Primitive::kPrimChar:
965 case Primitive::kPrimShort:
966 case Primitive::kPrimInt:
967 case Primitive::kPrimNot: {
968 uint32_t index = gp_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000969 stack_index_++;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100970 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100971 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100972 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000973 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100974 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100975 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100976
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000977 case Primitive::kPrimLong: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100978 uint32_t index = gp_index_;
979 gp_index_ += 2;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000980 stack_index_ += 2;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100981 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100982 X86ManagedRegister pair = X86ManagedRegister::FromRegisterPair(
983 calling_convention.GetRegisterPairAt(index));
984 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100985 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000986 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
987 }
988 }
989
990 case Primitive::kPrimFloat: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100991 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000992 stack_index_++;
993 if (index < calling_convention.GetNumberOfFpuRegisters()) {
994 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
995 } else {
996 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
997 }
998 }
999
1000 case Primitive::kPrimDouble: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001001 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001002 stack_index_ += 2;
1003 if (index < calling_convention.GetNumberOfFpuRegisters()) {
1004 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
1005 } else {
1006 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001007 }
1008 }
1009
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001010 case Primitive::kPrimVoid:
1011 LOG(FATAL) << "Unexpected parameter type " << type;
1012 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001013 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00001014 return Location::NoLocation();
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001015}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001016
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001017void CodeGeneratorX86::Move32(Location destination, Location source) {
1018 if (source.Equals(destination)) {
1019 return;
1020 }
1021 if (destination.IsRegister()) {
1022 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001023 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001024 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001025 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001026 } else {
1027 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001028 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001029 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001030 } else if (destination.IsFpuRegister()) {
1031 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001032 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001033 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001034 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001035 } else {
1036 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001037 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001038 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001039 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001040 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001041 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001042 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001043 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001044 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Mark Mendell7c8d0092015-01-26 11:21:33 -05001045 } else if (source.IsConstant()) {
1046 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001047 int32_t value = GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05001048 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001049 } else {
1050 DCHECK(source.IsStackSlot());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001051 __ pushl(Address(ESP, source.GetStackIndex()));
1052 __ popl(Address(ESP, destination.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001053 }
1054 }
1055}
1056
1057void CodeGeneratorX86::Move64(Location destination, Location source) {
1058 if (source.Equals(destination)) {
1059 return;
1060 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001061 if (destination.IsRegisterPair()) {
1062 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001063 EmitParallelMoves(
1064 Location::RegisterLocation(source.AsRegisterPairHigh<Register>()),
1065 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001066 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001067 Location::RegisterLocation(source.AsRegisterPairLow<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001068 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()),
1069 Primitive::kPrimInt);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001070 } else if (source.IsFpuRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001071 XmmRegister src_reg = source.AsFpuRegister<XmmRegister>();
1072 __ movd(destination.AsRegisterPairLow<Register>(), src_reg);
1073 __ psrlq(src_reg, Immediate(32));
1074 __ movd(destination.AsRegisterPairHigh<Register>(), src_reg);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001075 } else {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001076 // No conflict possible, so just do the moves.
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001077 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001078 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
1079 __ movl(destination.AsRegisterPairHigh<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001080 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
1081 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001082 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05001083 if (source.IsFpuRegister()) {
1084 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
1085 } else if (source.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001086 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01001087 } else if (source.IsRegisterPair()) {
1088 size_t elem_size = Primitive::ComponentSize(Primitive::kPrimInt);
1089 // Create stack space for 2 elements.
1090 __ subl(ESP, Immediate(2 * elem_size));
1091 __ movl(Address(ESP, 0), source.AsRegisterPairLow<Register>());
1092 __ movl(Address(ESP, elem_size), source.AsRegisterPairHigh<Register>());
1093 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
1094 // And remove the temporary stack space we allocated.
1095 __ addl(ESP, Immediate(2 * elem_size));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001096 } else {
1097 LOG(FATAL) << "Unimplemented";
1098 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001099 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001100 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001101 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001102 // No conflict possible, so just do the moves.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001103 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegisterPairLow<Register>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001104 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001105 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001106 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001107 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001108 } else if (source.IsConstant()) {
1109 HConstant* constant = source.GetConstant();
1110 int64_t value;
1111 if (constant->IsLongConstant()) {
1112 value = constant->AsLongConstant()->GetValue();
1113 } else {
1114 DCHECK(constant->IsDoubleConstant());
Roland Levillainda4d79b2015-03-24 14:36:11 +00001115 value = bit_cast<int64_t, double>(constant->AsDoubleConstant()->GetValue());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001116 }
1117 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(Low32Bits(value)));
1118 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), Immediate(High32Bits(value)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001119 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001120 DCHECK(source.IsDoubleStackSlot()) << source;
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001121 EmitParallelMoves(
1122 Location::StackSlot(source.GetStackIndex()),
1123 Location::StackSlot(destination.GetStackIndex()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001124 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001125 Location::StackSlot(source.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001126 Location::StackSlot(destination.GetHighStackIndex(kX86WordSize)),
1127 Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001128 }
1129 }
1130}
1131
Calin Juravle175dc732015-08-25 15:42:32 +01001132void CodeGeneratorX86::MoveConstant(Location location, int32_t value) {
1133 DCHECK(location.IsRegister());
1134 __ movl(location.AsRegister<Register>(), Immediate(value));
1135}
1136
Calin Juravlee460d1d2015-09-29 04:52:17 +01001137void CodeGeneratorX86::MoveLocation(Location dst, Location src, Primitive::Type dst_type) {
David Brazdil74eb1b22015-12-14 11:44:01 +00001138 HParallelMove move(GetGraph()->GetArena());
1139 if (dst_type == Primitive::kPrimLong && !src.IsConstant() && !src.IsFpuRegister()) {
1140 move.AddMove(src.ToLow(), dst.ToLow(), Primitive::kPrimInt, nullptr);
1141 move.AddMove(src.ToHigh(), dst.ToHigh(), Primitive::kPrimInt, nullptr);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001142 } else {
David Brazdil74eb1b22015-12-14 11:44:01 +00001143 move.AddMove(src, dst, dst_type, nullptr);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001144 }
David Brazdil74eb1b22015-12-14 11:44:01 +00001145 GetMoveResolver()->EmitNativeCode(&move);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001146}
1147
1148void CodeGeneratorX86::AddLocationAsTemp(Location location, LocationSummary* locations) {
1149 if (location.IsRegister()) {
1150 locations->AddTemp(location);
1151 } else if (location.IsRegisterPair()) {
1152 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
1153 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
1154 } else {
1155 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1156 }
1157}
1158
David Brazdilfc6a86a2015-06-26 10:33:45 +00001159void InstructionCodeGeneratorX86::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001160 DCHECK(!successor->IsExitBlock());
1161
1162 HBasicBlock* block = got->GetBlock();
1163 HInstruction* previous = got->GetPrevious();
1164
1165 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +00001166 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001167 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1168 return;
1169 }
1170
1171 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1172 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1173 }
1174 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001175 __ jmp(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001176 }
1177}
1178
David Brazdilfc6a86a2015-06-26 10:33:45 +00001179void LocationsBuilderX86::VisitGoto(HGoto* got) {
1180 got->SetLocations(nullptr);
1181}
1182
1183void InstructionCodeGeneratorX86::VisitGoto(HGoto* got) {
1184 HandleGoto(got, got->GetSuccessor());
1185}
1186
1187void LocationsBuilderX86::VisitTryBoundary(HTryBoundary* try_boundary) {
1188 try_boundary->SetLocations(nullptr);
1189}
1190
1191void InstructionCodeGeneratorX86::VisitTryBoundary(HTryBoundary* try_boundary) {
1192 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
1193 if (!successor->IsExitBlock()) {
1194 HandleGoto(try_boundary, successor);
1195 }
1196}
1197
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001198void LocationsBuilderX86::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001199 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001200}
1201
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001202void InstructionCodeGeneratorX86::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001203}
1204
Mark Mendell152408f2015-12-31 12:28:50 -05001205template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001206void InstructionCodeGeneratorX86::GenerateFPJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001207 LabelType* true_label,
1208 LabelType* false_label) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001209 if (cond->IsFPConditionTrueIfNaN()) {
1210 __ j(kUnordered, true_label);
1211 } else if (cond->IsFPConditionFalseIfNaN()) {
1212 __ j(kUnordered, false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001213 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001214 __ j(X86UnsignedOrFPCondition(cond->GetCondition()), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001215}
1216
Mark Mendell152408f2015-12-31 12:28:50 -05001217template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001218void InstructionCodeGeneratorX86::GenerateLongComparesAndJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001219 LabelType* true_label,
1220 LabelType* false_label) {
Mark Mendellc4701932015-04-10 13:18:51 -04001221 LocationSummary* locations = cond->GetLocations();
1222 Location left = locations->InAt(0);
1223 Location right = locations->InAt(1);
1224 IfCondition if_cond = cond->GetCondition();
1225
Mark Mendellc4701932015-04-10 13:18:51 -04001226 Register left_high = left.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001227 Register left_low = left.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001228 IfCondition true_high_cond = if_cond;
1229 IfCondition false_high_cond = cond->GetOppositeCondition();
Aart Bike9f37602015-10-09 11:15:55 -07001230 Condition final_condition = X86UnsignedOrFPCondition(if_cond); // unsigned on lower part
Mark Mendellc4701932015-04-10 13:18:51 -04001231
1232 // Set the conditions for the test, remembering that == needs to be
1233 // decided using the low words.
1234 switch (if_cond) {
1235 case kCondEQ:
Mark Mendellc4701932015-04-10 13:18:51 -04001236 case kCondNE:
Roland Levillain4fa13f62015-07-06 18:11:54 +01001237 // Nothing to do.
Mark Mendellc4701932015-04-10 13:18:51 -04001238 break;
1239 case kCondLT:
1240 false_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -04001241 break;
1242 case kCondLE:
1243 true_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -04001244 break;
1245 case kCondGT:
1246 false_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -04001247 break;
1248 case kCondGE:
1249 true_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -04001250 break;
Aart Bike9f37602015-10-09 11:15:55 -07001251 case kCondB:
1252 false_high_cond = kCondA;
1253 break;
1254 case kCondBE:
1255 true_high_cond = kCondB;
1256 break;
1257 case kCondA:
1258 false_high_cond = kCondB;
1259 break;
1260 case kCondAE:
1261 true_high_cond = kCondA;
1262 break;
Mark Mendellc4701932015-04-10 13:18:51 -04001263 }
1264
1265 if (right.IsConstant()) {
1266 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
Mark Mendellc4701932015-04-10 13:18:51 -04001267 int32_t val_high = High32Bits(value);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001268 int32_t val_low = Low32Bits(value);
Mark Mendellc4701932015-04-10 13:18:51 -04001269
Aart Bika19616e2016-02-01 18:57:58 -08001270 codegen_->Compare32BitValue(left_high, val_high);
Mark Mendellc4701932015-04-10 13:18:51 -04001271 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001272 __ j(X86Condition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001273 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001274 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001275 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001276 __ j(X86Condition(true_high_cond), true_label);
1277 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001278 }
1279 // Must be equal high, so compare the lows.
Aart Bika19616e2016-02-01 18:57:58 -08001280 codegen_->Compare32BitValue(left_low, val_low);
Mark Mendell8659e842016-02-16 10:41:46 -05001281 } else if (right.IsRegisterPair()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001282 Register right_high = right.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001283 Register right_low = right.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001284
1285 __ cmpl(left_high, right_high);
1286 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001287 __ j(X86Condition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001288 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001289 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001290 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001291 __ j(X86Condition(true_high_cond), true_label);
1292 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001293 }
1294 // Must be equal high, so compare the lows.
1295 __ cmpl(left_low, right_low);
Mark Mendell8659e842016-02-16 10:41:46 -05001296 } else {
1297 DCHECK(right.IsDoubleStackSlot());
1298 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
1299 if (if_cond == kCondNE) {
1300 __ j(X86Condition(true_high_cond), true_label);
1301 } else if (if_cond == kCondEQ) {
1302 __ j(X86Condition(false_high_cond), false_label);
1303 } else {
1304 __ j(X86Condition(true_high_cond), true_label);
1305 __ j(X86Condition(false_high_cond), false_label);
1306 }
1307 // Must be equal high, so compare the lows.
1308 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Mark Mendellc4701932015-04-10 13:18:51 -04001309 }
1310 // The last comparison might be unsigned.
1311 __ j(final_condition, true_label);
1312}
1313
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001314void InstructionCodeGeneratorX86::GenerateFPCompare(Location lhs,
1315 Location rhs,
1316 HInstruction* insn,
1317 bool is_double) {
1318 HX86LoadFromConstantTable* const_area = insn->InputAt(1)->AsX86LoadFromConstantTable();
1319 if (is_double) {
1320 if (rhs.IsFpuRegister()) {
1321 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1322 } else if (const_area != nullptr) {
1323 DCHECK(const_area->IsEmittedAtUseSite());
1324 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(),
1325 codegen_->LiteralDoubleAddress(
1326 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
1327 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
1328 } else {
1329 DCHECK(rhs.IsDoubleStackSlot());
1330 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(), Address(ESP, rhs.GetStackIndex()));
1331 }
1332 } else {
1333 if (rhs.IsFpuRegister()) {
1334 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1335 } else if (const_area != nullptr) {
1336 DCHECK(const_area->IsEmittedAtUseSite());
1337 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(),
1338 codegen_->LiteralFloatAddress(
1339 const_area->GetConstant()->AsFloatConstant()->GetValue(),
1340 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
1341 } else {
1342 DCHECK(rhs.IsStackSlot());
1343 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(), Address(ESP, rhs.GetStackIndex()));
1344 }
1345 }
1346}
1347
Mark Mendell152408f2015-12-31 12:28:50 -05001348template<class LabelType>
David Brazdil0debae72015-11-12 18:37:00 +00001349void InstructionCodeGeneratorX86::GenerateCompareTestAndBranch(HCondition* condition,
Mark Mendell152408f2015-12-31 12:28:50 -05001350 LabelType* true_target_in,
1351 LabelType* false_target_in) {
David Brazdil0debae72015-11-12 18:37:00 +00001352 // Generated branching requires both targets to be explicit. If either of the
1353 // targets is nullptr (fallthrough) use and bind `fallthrough_target` instead.
Mark Mendell152408f2015-12-31 12:28:50 -05001354 LabelType fallthrough_target;
1355 LabelType* true_target = true_target_in == nullptr ? &fallthrough_target : true_target_in;
1356 LabelType* false_target = false_target_in == nullptr ? &fallthrough_target : false_target_in;
David Brazdil0debae72015-11-12 18:37:00 +00001357
Mark Mendellc4701932015-04-10 13:18:51 -04001358 LocationSummary* locations = condition->GetLocations();
1359 Location left = locations->InAt(0);
1360 Location right = locations->InAt(1);
1361
Mark Mendellc4701932015-04-10 13:18:51 -04001362 Primitive::Type type = condition->InputAt(0)->GetType();
1363 switch (type) {
1364 case Primitive::kPrimLong:
1365 GenerateLongComparesAndJumps(condition, true_target, false_target);
1366 break;
1367 case Primitive::kPrimFloat:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001368 GenerateFPCompare(left, right, condition, false);
Mark Mendellc4701932015-04-10 13:18:51 -04001369 GenerateFPJumps(condition, true_target, false_target);
1370 break;
1371 case Primitive::kPrimDouble:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001372 GenerateFPCompare(left, right, condition, true);
Mark Mendellc4701932015-04-10 13:18:51 -04001373 GenerateFPJumps(condition, true_target, false_target);
1374 break;
1375 default:
1376 LOG(FATAL) << "Unexpected compare type " << type;
1377 }
1378
David Brazdil0debae72015-11-12 18:37:00 +00001379 if (false_target != &fallthrough_target) {
Mark Mendellc4701932015-04-10 13:18:51 -04001380 __ jmp(false_target);
1381 }
David Brazdil0debae72015-11-12 18:37:00 +00001382
1383 if (fallthrough_target.IsLinked()) {
1384 __ Bind(&fallthrough_target);
1385 }
Mark Mendellc4701932015-04-10 13:18:51 -04001386}
1387
David Brazdil0debae72015-11-12 18:37:00 +00001388static bool AreEflagsSetFrom(HInstruction* cond, HInstruction* branch) {
1389 // Moves may affect the eflags register (move zero uses xorl), so the EFLAGS
1390 // are set only strictly before `branch`. We can't use the eflags on long/FP
1391 // conditions if they are materialized due to the complex branching.
1392 return cond->IsCondition() &&
1393 cond->GetNext() == branch &&
1394 cond->InputAt(0)->GetType() != Primitive::kPrimLong &&
1395 !Primitive::IsFloatingPointType(cond->InputAt(0)->GetType());
1396}
1397
Mark Mendell152408f2015-12-31 12:28:50 -05001398template<class LabelType>
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001399void InstructionCodeGeneratorX86::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00001400 size_t condition_input_index,
Mark Mendell152408f2015-12-31 12:28:50 -05001401 LabelType* true_target,
1402 LabelType* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00001403 HInstruction* cond = instruction->InputAt(condition_input_index);
1404
1405 if (true_target == nullptr && false_target == nullptr) {
1406 // Nothing to do. The code always falls through.
1407 return;
1408 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00001409 // Constant condition, statically compared against "true" (integer value 1).
1410 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00001411 if (true_target != nullptr) {
1412 __ jmp(true_target);
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001413 }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001414 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00001415 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00001416 if (false_target != nullptr) {
1417 __ jmp(false_target);
1418 }
1419 }
1420 return;
1421 }
1422
1423 // The following code generates these patterns:
1424 // (1) true_target == nullptr && false_target != nullptr
1425 // - opposite condition true => branch to false_target
1426 // (2) true_target != nullptr && false_target == nullptr
1427 // - condition true => branch to true_target
1428 // (3) true_target != nullptr && false_target != nullptr
1429 // - condition true => branch to true_target
1430 // - branch to false_target
1431 if (IsBooleanValueOrMaterializedCondition(cond)) {
1432 if (AreEflagsSetFrom(cond, instruction)) {
1433 if (true_target == nullptr) {
1434 __ j(X86Condition(cond->AsCondition()->GetOppositeCondition()), false_target);
1435 } else {
1436 __ j(X86Condition(cond->AsCondition()->GetCondition()), true_target);
1437 }
1438 } else {
1439 // Materialized condition, compare against 0.
1440 Location lhs = instruction->GetLocations()->InAt(condition_input_index);
1441 if (lhs.IsRegister()) {
1442 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
1443 } else {
1444 __ cmpl(Address(ESP, lhs.GetStackIndex()), Immediate(0));
1445 }
1446 if (true_target == nullptr) {
1447 __ j(kEqual, false_target);
1448 } else {
1449 __ j(kNotEqual, true_target);
1450 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001451 }
1452 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001453 // Condition has not been materialized, use its inputs as the comparison and
1454 // its condition as the branch condition.
Mark Mendellb8b97692015-05-22 16:58:19 -04001455 HCondition* condition = cond->AsCondition();
David Brazdil0debae72015-11-12 18:37:00 +00001456
1457 // If this is a long or FP comparison that has been folded into
1458 // the HCondition, generate the comparison directly.
1459 Primitive::Type type = condition->InputAt(0)->GetType();
1460 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
1461 GenerateCompareTestAndBranch(condition, true_target, false_target);
1462 return;
1463 }
1464
1465 Location lhs = condition->GetLocations()->InAt(0);
1466 Location rhs = condition->GetLocations()->InAt(1);
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001467 // LHS is guaranteed to be in a register (see LocationsBuilderX86::HandleCondition).
David Brazdil0debae72015-11-12 18:37:00 +00001468 if (rhs.IsRegister()) {
1469 __ cmpl(lhs.AsRegister<Register>(), rhs.AsRegister<Register>());
1470 } else if (rhs.IsConstant()) {
1471 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
Aart Bika19616e2016-02-01 18:57:58 -08001472 codegen_->Compare32BitValue(lhs.AsRegister<Register>(), constant);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001473 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001474 __ cmpl(lhs.AsRegister<Register>(), Address(ESP, rhs.GetStackIndex()));
1475 }
1476 if (true_target == nullptr) {
1477 __ j(X86Condition(condition->GetOppositeCondition()), false_target);
1478 } else {
Mark Mendellb8b97692015-05-22 16:58:19 -04001479 __ j(X86Condition(condition->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -07001480 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001481 }
David Brazdil0debae72015-11-12 18:37:00 +00001482
1483 // If neither branch falls through (case 3), the conditional branch to `true_target`
1484 // was already emitted (case 2) and we need to emit a jump to `false_target`.
1485 if (true_target != nullptr && false_target != nullptr) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001486 __ jmp(false_target);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001487 }
1488}
1489
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001490void LocationsBuilderX86::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001491 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
1492 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001493 locations->SetInAt(0, Location::Any());
1494 }
1495}
1496
1497void InstructionCodeGeneratorX86::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001498 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
1499 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
1500 Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
1501 nullptr : codegen_->GetLabelOf(true_successor);
1502 Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
1503 nullptr : codegen_->GetLabelOf(false_successor);
1504 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001505}
1506
1507void LocationsBuilderX86::VisitDeoptimize(HDeoptimize* deoptimize) {
1508 LocationSummary* locations = new (GetGraph()->GetArena())
1509 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
David Brazdil0debae72015-11-12 18:37:00 +00001510 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001511 locations->SetInAt(0, Location::Any());
1512 }
1513}
1514
1515void InstructionCodeGeneratorX86::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08001516 SlowPathCode* slow_path = deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathX86>(deoptimize);
David Brazdil74eb1b22015-12-14 11:44:01 +00001517 GenerateTestAndBranch<Label>(deoptimize,
1518 /* condition_input_index */ 0,
1519 slow_path->GetEntryLabel(),
1520 /* false_target */ nullptr);
1521}
1522
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001523static bool SelectCanUseCMOV(HSelect* select) {
1524 // There are no conditional move instructions for XMMs.
1525 if (Primitive::IsFloatingPointType(select->GetType())) {
1526 return false;
1527 }
1528
1529 // A FP condition doesn't generate the single CC that we need.
1530 // In 32 bit mode, a long condition doesn't generate a single CC either.
1531 HInstruction* condition = select->GetCondition();
1532 if (condition->IsCondition()) {
1533 Primitive::Type compare_type = condition->InputAt(0)->GetType();
1534 if (compare_type == Primitive::kPrimLong ||
1535 Primitive::IsFloatingPointType(compare_type)) {
1536 return false;
1537 }
1538 }
1539
1540 // We can generate a CMOV for this Select.
1541 return true;
1542}
1543
David Brazdil74eb1b22015-12-14 11:44:01 +00001544void LocationsBuilderX86::VisitSelect(HSelect* select) {
1545 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001546 if (Primitive::IsFloatingPointType(select->GetType())) {
David Brazdil74eb1b22015-12-14 11:44:01 +00001547 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001548 locations->SetInAt(1, Location::Any());
David Brazdil74eb1b22015-12-14 11:44:01 +00001549 } else {
1550 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001551 if (SelectCanUseCMOV(select)) {
1552 if (select->InputAt(1)->IsConstant()) {
1553 // Cmov can't handle a constant value.
1554 locations->SetInAt(1, Location::RequiresRegister());
1555 } else {
1556 locations->SetInAt(1, Location::Any());
1557 }
1558 } else {
1559 locations->SetInAt(1, Location::Any());
1560 }
David Brazdil74eb1b22015-12-14 11:44:01 +00001561 }
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001562 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
1563 locations->SetInAt(2, Location::RequiresRegister());
David Brazdil74eb1b22015-12-14 11:44:01 +00001564 }
1565 locations->SetOut(Location::SameAsFirstInput());
1566}
1567
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001568void InstructionCodeGeneratorX86::GenerateIntCompare(Location lhs, Location rhs) {
1569 Register lhs_reg = lhs.AsRegister<Register>();
1570 if (rhs.IsConstant()) {
1571 int32_t value = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
1572 codegen_->Compare32BitValue(lhs_reg, value);
1573 } else if (rhs.IsStackSlot()) {
1574 __ cmpl(lhs_reg, Address(ESP, rhs.GetStackIndex()));
1575 } else {
1576 __ cmpl(lhs_reg, rhs.AsRegister<Register>());
1577 }
1578}
1579
David Brazdil74eb1b22015-12-14 11:44:01 +00001580void InstructionCodeGeneratorX86::VisitSelect(HSelect* select) {
1581 LocationSummary* locations = select->GetLocations();
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001582 DCHECK(locations->InAt(0).Equals(locations->Out()));
1583 if (SelectCanUseCMOV(select)) {
1584 // If both the condition and the source types are integer, we can generate
1585 // a CMOV to implement Select.
1586
1587 HInstruction* select_condition = select->GetCondition();
1588 Condition cond = kNotEqual;
1589
1590 // Figure out how to test the 'condition'.
1591 if (select_condition->IsCondition()) {
1592 HCondition* condition = select_condition->AsCondition();
1593 if (!condition->IsEmittedAtUseSite()) {
1594 // This was a previously materialized condition.
1595 // Can we use the existing condition code?
1596 if (AreEflagsSetFrom(condition, select)) {
1597 // Materialization was the previous instruction. Condition codes are right.
1598 cond = X86Condition(condition->GetCondition());
1599 } else {
1600 // No, we have to recreate the condition code.
1601 Register cond_reg = locations->InAt(2).AsRegister<Register>();
1602 __ testl(cond_reg, cond_reg);
1603 }
1604 } else {
1605 // We can't handle FP or long here.
1606 DCHECK_NE(condition->InputAt(0)->GetType(), Primitive::kPrimLong);
1607 DCHECK(!Primitive::IsFloatingPointType(condition->InputAt(0)->GetType()));
1608 LocationSummary* cond_locations = condition->GetLocations();
1609 GenerateIntCompare(cond_locations->InAt(0), cond_locations->InAt(1));
1610 cond = X86Condition(condition->GetCondition());
1611 }
1612 } else {
1613 // Must be a boolean condition, which needs to be compared to 0.
1614 Register cond_reg = locations->InAt(2).AsRegister<Register>();
1615 __ testl(cond_reg, cond_reg);
1616 }
1617
1618 // If the condition is true, overwrite the output, which already contains false.
1619 Location false_loc = locations->InAt(0);
1620 Location true_loc = locations->InAt(1);
1621 if (select->GetType() == Primitive::kPrimLong) {
1622 // 64 bit conditional move.
1623 Register false_high = false_loc.AsRegisterPairHigh<Register>();
1624 Register false_low = false_loc.AsRegisterPairLow<Register>();
1625 if (true_loc.IsRegisterPair()) {
1626 __ cmovl(cond, false_high, true_loc.AsRegisterPairHigh<Register>());
1627 __ cmovl(cond, false_low, true_loc.AsRegisterPairLow<Register>());
1628 } else {
1629 __ cmovl(cond, false_high, Address(ESP, true_loc.GetHighStackIndex(kX86WordSize)));
1630 __ cmovl(cond, false_low, Address(ESP, true_loc.GetStackIndex()));
1631 }
1632 } else {
1633 // 32 bit conditional move.
1634 Register false_reg = false_loc.AsRegister<Register>();
1635 if (true_loc.IsRegister()) {
1636 __ cmovl(cond, false_reg, true_loc.AsRegister<Register>());
1637 } else {
1638 __ cmovl(cond, false_reg, Address(ESP, true_loc.GetStackIndex()));
1639 }
1640 }
1641 } else {
1642 NearLabel false_target;
1643 GenerateTestAndBranch<NearLabel>(
1644 select, /* condition_input_index */ 2, /* true_target */ nullptr, &false_target);
1645 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
1646 __ Bind(&false_target);
1647 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001648}
1649
David Srbecky0cf44932015-12-09 14:09:59 +00001650void LocationsBuilderX86::VisitNativeDebugInfo(HNativeDebugInfo* info) {
1651 new (GetGraph()->GetArena()) LocationSummary(info);
1652}
1653
David Srbeckyd28f4a02016-03-14 17:14:24 +00001654void InstructionCodeGeneratorX86::VisitNativeDebugInfo(HNativeDebugInfo*) {
1655 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00001656}
1657
1658void CodeGeneratorX86::GenerateNop() {
1659 __ nop();
David Srbecky0cf44932015-12-09 14:09:59 +00001660}
1661
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001662void LocationsBuilderX86::HandleCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001663 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +01001664 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Mark Mendellc4701932015-04-10 13:18:51 -04001665 // Handle the long/FP comparisons made in instruction simplification.
1666 switch (cond->InputAt(0)->GetType()) {
1667 case Primitive::kPrimLong: {
1668 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell8659e842016-02-16 10:41:46 -05001669 locations->SetInAt(1, Location::Any());
David Brazdilb3e773e2016-01-26 11:28:37 +00001670 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001671 locations->SetOut(Location::RequiresRegister());
1672 }
1673 break;
1674 }
1675 case Primitive::kPrimFloat:
1676 case Primitive::kPrimDouble: {
1677 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001678 if (cond->InputAt(1)->IsX86LoadFromConstantTable()) {
1679 DCHECK(cond->InputAt(1)->IsEmittedAtUseSite());
1680 } else if (cond->InputAt(1)->IsConstant()) {
1681 locations->SetInAt(1, Location::RequiresFpuRegister());
1682 } else {
1683 locations->SetInAt(1, Location::Any());
1684 }
David Brazdilb3e773e2016-01-26 11:28:37 +00001685 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001686 locations->SetOut(Location::RequiresRegister());
1687 }
1688 break;
1689 }
1690 default:
1691 locations->SetInAt(0, Location::RequiresRegister());
1692 locations->SetInAt(1, Location::Any());
David Brazdilb3e773e2016-01-26 11:28:37 +00001693 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001694 // We need a byte register.
1695 locations->SetOut(Location::RegisterLocation(ECX));
1696 }
1697 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001698 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001699}
1700
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001701void InstructionCodeGeneratorX86::HandleCondition(HCondition* cond) {
David Brazdilb3e773e2016-01-26 11:28:37 +00001702 if (cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001703 return;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001704 }
Mark Mendellc4701932015-04-10 13:18:51 -04001705
1706 LocationSummary* locations = cond->GetLocations();
1707 Location lhs = locations->InAt(0);
1708 Location rhs = locations->InAt(1);
1709 Register reg = locations->Out().AsRegister<Register>();
Mark Mendell152408f2015-12-31 12:28:50 -05001710 NearLabel true_label, false_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001711
1712 switch (cond->InputAt(0)->GetType()) {
1713 default: {
1714 // Integer case.
1715
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01001716 // Clear output register: setb only sets the low byte.
Mark Mendellc4701932015-04-10 13:18:51 -04001717 __ xorl(reg, reg);
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001718 GenerateIntCompare(lhs, rhs);
Aart Bike9f37602015-10-09 11:15:55 -07001719 __ setb(X86Condition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001720 return;
1721 }
1722 case Primitive::kPrimLong:
1723 GenerateLongComparesAndJumps(cond, &true_label, &false_label);
1724 break;
1725 case Primitive::kPrimFloat:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001726 GenerateFPCompare(lhs, rhs, cond, false);
Mark Mendellc4701932015-04-10 13:18:51 -04001727 GenerateFPJumps(cond, &true_label, &false_label);
1728 break;
1729 case Primitive::kPrimDouble:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001730 GenerateFPCompare(lhs, rhs, cond, true);
Mark Mendellc4701932015-04-10 13:18:51 -04001731 GenerateFPJumps(cond, &true_label, &false_label);
1732 break;
1733 }
1734
1735 // Convert the jumps into the result.
Mark Mendell0c9497d2015-08-21 09:30:05 -04001736 NearLabel done_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001737
Roland Levillain4fa13f62015-07-06 18:11:54 +01001738 // False case: result = 0.
Mark Mendellc4701932015-04-10 13:18:51 -04001739 __ Bind(&false_label);
1740 __ xorl(reg, reg);
1741 __ jmp(&done_label);
1742
Roland Levillain4fa13f62015-07-06 18:11:54 +01001743 // True case: result = 1.
Mark Mendellc4701932015-04-10 13:18:51 -04001744 __ Bind(&true_label);
1745 __ movl(reg, Immediate(1));
1746 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07001747}
1748
1749void LocationsBuilderX86::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001750 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001751}
1752
1753void InstructionCodeGeneratorX86::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001754 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001755}
1756
1757void LocationsBuilderX86::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001758 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001759}
1760
1761void InstructionCodeGeneratorX86::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001762 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001763}
1764
1765void LocationsBuilderX86::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001766 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001767}
1768
1769void InstructionCodeGeneratorX86::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001770 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001771}
1772
1773void LocationsBuilderX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001774 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001775}
1776
1777void InstructionCodeGeneratorX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001778 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001779}
1780
1781void LocationsBuilderX86::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001782 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001783}
1784
1785void InstructionCodeGeneratorX86::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001786 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001787}
1788
1789void LocationsBuilderX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001790 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001791}
1792
1793void InstructionCodeGeneratorX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001794 HandleCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001795}
1796
Aart Bike9f37602015-10-09 11:15:55 -07001797void LocationsBuilderX86::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001798 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001799}
1800
1801void InstructionCodeGeneratorX86::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001802 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001803}
1804
1805void LocationsBuilderX86::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001806 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001807}
1808
1809void InstructionCodeGeneratorX86::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001810 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001811}
1812
1813void LocationsBuilderX86::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001814 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001815}
1816
1817void InstructionCodeGeneratorX86::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001818 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001819}
1820
1821void LocationsBuilderX86::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001822 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001823}
1824
1825void InstructionCodeGeneratorX86::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001826 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001827}
1828
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001829void LocationsBuilderX86::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001830 LocationSummary* locations =
1831 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001832 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001833}
1834
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001835void InstructionCodeGeneratorX86::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001836 // Will be generated at use site.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001837}
1838
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001839void LocationsBuilderX86::VisitNullConstant(HNullConstant* constant) {
1840 LocationSummary* locations =
1841 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1842 locations->SetOut(Location::ConstantLocation(constant));
1843}
1844
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001845void InstructionCodeGeneratorX86::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001846 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001847}
1848
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001849void LocationsBuilderX86::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001850 LocationSummary* locations =
1851 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001852 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001853}
1854
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001855void InstructionCodeGeneratorX86::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001856 // Will be generated at use site.
1857}
1858
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001859void LocationsBuilderX86::VisitFloatConstant(HFloatConstant* constant) {
1860 LocationSummary* locations =
1861 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1862 locations->SetOut(Location::ConstantLocation(constant));
1863}
1864
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001865void InstructionCodeGeneratorX86::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001866 // Will be generated at use site.
1867}
1868
1869void LocationsBuilderX86::VisitDoubleConstant(HDoubleConstant* constant) {
1870 LocationSummary* locations =
1871 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1872 locations->SetOut(Location::ConstantLocation(constant));
1873}
1874
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001875void InstructionCodeGeneratorX86::VisitDoubleConstant(HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001876 // Will be generated at use site.
1877}
1878
Calin Juravle27df7582015-04-17 19:12:31 +01001879void LocationsBuilderX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1880 memory_barrier->SetLocations(nullptr);
1881}
1882
1883void InstructionCodeGeneratorX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00001884 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01001885}
1886
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001887void LocationsBuilderX86::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001888 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001889}
1890
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001891void InstructionCodeGeneratorX86::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001892 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001893}
1894
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001895void LocationsBuilderX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001896 LocationSummary* locations =
1897 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001898 switch (ret->InputAt(0)->GetType()) {
1899 case Primitive::kPrimBoolean:
1900 case Primitive::kPrimByte:
1901 case Primitive::kPrimChar:
1902 case Primitive::kPrimShort:
1903 case Primitive::kPrimInt:
1904 case Primitive::kPrimNot:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001905 locations->SetInAt(0, Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001906 break;
1907
1908 case Primitive::kPrimLong:
1909 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001910 0, Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001911 break;
1912
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001913 case Primitive::kPrimFloat:
1914 case Primitive::kPrimDouble:
1915 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001916 0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001917 break;
1918
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001919 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001920 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001921 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001922}
1923
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001924void InstructionCodeGeneratorX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001925 if (kIsDebugBuild) {
1926 switch (ret->InputAt(0)->GetType()) {
1927 case Primitive::kPrimBoolean:
1928 case Primitive::kPrimByte:
1929 case Primitive::kPrimChar:
1930 case Primitive::kPrimShort:
1931 case Primitive::kPrimInt:
1932 case Primitive::kPrimNot:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001933 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<Register>(), EAX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001934 break;
1935
1936 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001937 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairLow<Register>(), EAX);
1938 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairHigh<Register>(), EDX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001939 break;
1940
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001941 case Primitive::kPrimFloat:
1942 case Primitive::kPrimDouble:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001943 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>(), XMM0);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001944 break;
1945
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001946 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001947 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001948 }
1949 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001950 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001951}
1952
Calin Juravle175dc732015-08-25 15:42:32 +01001953void LocationsBuilderX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
1954 // The trampoline uses the same calling convention as dex calling conventions,
1955 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
1956 // the method_idx.
1957 HandleInvoke(invoke);
1958}
1959
1960void InstructionCodeGeneratorX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
1961 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
1962}
1963
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001964void LocationsBuilderX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00001965 // Explicit clinit checks triggered by static invokes must have been pruned by
1966 // art::PrepareForRegisterAllocation.
1967 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001968
Mark Mendellfb8d2792015-03-31 22:16:59 -04001969 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001970 if (intrinsic.TryDispatch(invoke)) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00001971 if (invoke->GetLocations()->CanCall() && invoke->HasPcRelativeDexCache()) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00001972 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00001973 }
Mark Mendell09ed1a32015-03-25 08:30:06 -04001974 return;
1975 }
1976
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001977 HandleInvoke(invoke);
Nicolas Geoffray94015b92015-06-04 18:21:04 +01001978
Vladimir Marko0f7dca42015-11-02 14:36:43 +00001979 // For PC-relative dex cache the invoke has an extra input, the PC-relative address base.
1980 if (invoke->HasPcRelativeDexCache()) {
Vladimir Markob4536b72015-11-24 13:45:23 +00001981 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00001982 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001983}
1984
Mark Mendell09ed1a32015-03-25 08:30:06 -04001985static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86* codegen) {
1986 if (invoke->GetLocations()->Intrinsified()) {
1987 IntrinsicCodeGeneratorX86 intrinsic(codegen);
1988 intrinsic.Dispatch(invoke);
1989 return true;
1990 }
1991 return false;
1992}
1993
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001994void InstructionCodeGeneratorX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00001995 // Explicit clinit checks triggered by static invokes must have been pruned by
1996 // art::PrepareForRegisterAllocation.
1997 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001998
Mark Mendell09ed1a32015-03-25 08:30:06 -04001999 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2000 return;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002001 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002002
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002003 LocationSummary* locations = invoke->GetLocations();
Mark Mendell09ed1a32015-03-25 08:30:06 -04002004 codegen_->GenerateStaticOrDirectCall(
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002005 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Mingyao Yang8693fe12015-04-17 16:51:08 -07002006 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002007}
2008
2009void LocationsBuilderX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00002010 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
2011 if (intrinsic.TryDispatch(invoke)) {
2012 return;
2013 }
2014
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002015 HandleInvoke(invoke);
2016}
2017
2018void LocationsBuilderX86::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002019 InvokeDexCallingConventionVisitorX86 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002020 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002021}
2022
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002023void InstructionCodeGeneratorX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002024 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2025 return;
2026 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002027
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002028 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002029 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01002030 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002031}
2032
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002033void LocationsBuilderX86::VisitInvokeInterface(HInvokeInterface* invoke) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002034 // This call to HandleInvoke allocates a temporary (core) register
2035 // which is also used to transfer the hidden argument from FP to
2036 // core register.
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002037 HandleInvoke(invoke);
2038 // Add the hidden argument.
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002039 invoke->GetLocations()->AddTemp(Location::FpuRegisterLocation(XMM7));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002040}
2041
2042void InstructionCodeGeneratorX86::VisitInvokeInterface(HInvokeInterface* invoke) {
2043 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain0d5a2812015-11-13 10:07:31 +00002044 LocationSummary* locations = invoke->GetLocations();
2045 Register temp = locations->GetTemp(0).AsRegister<Register>();
2046 XmmRegister hidden_reg = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002047 Location receiver = locations->InAt(0);
2048 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2049
Roland Levillain0d5a2812015-11-13 10:07:31 +00002050 // Set the hidden argument. This is safe to do this here, as XMM7
2051 // won't be modified thereafter, before the `call` instruction.
2052 DCHECK_EQ(XMM7, hidden_reg);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002053 __ movl(temp, Immediate(invoke->GetDexMethodIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002054 __ movd(hidden_reg, temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002055
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002056 if (receiver.IsStackSlot()) {
2057 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002058 // /* HeapReference<Class> */ temp = temp->klass_
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002059 __ movl(temp, Address(temp, class_offset));
2060 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002061 // /* HeapReference<Class> */ temp = receiver->klass_
Roland Levillain271ab9c2014-11-27 15:23:57 +00002062 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002063 }
Roland Levillain4d027112015-07-01 15:41:14 +01002064 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00002065 // Instead of simply (possibly) unpoisoning `temp` here, we should
2066 // emit a read barrier for the previous class reference load.
2067 // However this is not required in practice, as this is an
2068 // intermediate/temporary reference and because the current
2069 // concurrent copying collector keeps the from-space memory
2070 // intact/accessible until the end of the marking phase (the
2071 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01002072 __ MaybeUnpoisonHeapReference(temp);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002073 // temp = temp->GetAddressOfIMT()
2074 __ movl(temp,
2075 Address(temp, mirror::Class::ImtPtrOffset(kX86PointerSize).Uint32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002076 // temp = temp->GetImtEntryAt(method_offset);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002077 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
2078 invoke->GetImtIndex() % ImTable::kSize, kX86PointerSize));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002079 __ movl(temp, Address(temp, method_offset));
2080 // call temp->GetEntryPoint();
Roland Levillain0d5a2812015-11-13 10:07:31 +00002081 __ call(Address(temp,
2082 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize).Int32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002083
2084 DCHECK(!codegen_->IsLeafMethod());
2085 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2086}
2087
Roland Levillain88cb1752014-10-20 16:36:47 +01002088void LocationsBuilderX86::VisitNeg(HNeg* neg) {
2089 LocationSummary* locations =
2090 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2091 switch (neg->GetResultType()) {
2092 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002093 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01002094 locations->SetInAt(0, Location::RequiresRegister());
2095 locations->SetOut(Location::SameAsFirstInput());
2096 break;
2097
Roland Levillain88cb1752014-10-20 16:36:47 +01002098 case Primitive::kPrimFloat:
Roland Levillain5368c212014-11-27 15:03:41 +00002099 locations->SetInAt(0, Location::RequiresFpuRegister());
2100 locations->SetOut(Location::SameAsFirstInput());
2101 locations->AddTemp(Location::RequiresRegister());
2102 locations->AddTemp(Location::RequiresFpuRegister());
2103 break;
2104
Roland Levillain88cb1752014-10-20 16:36:47 +01002105 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002106 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00002107 locations->SetOut(Location::SameAsFirstInput());
2108 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01002109 break;
2110
2111 default:
2112 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2113 }
2114}
2115
2116void InstructionCodeGeneratorX86::VisitNeg(HNeg* neg) {
2117 LocationSummary* locations = neg->GetLocations();
2118 Location out = locations->Out();
2119 Location in = locations->InAt(0);
2120 switch (neg->GetResultType()) {
2121 case Primitive::kPrimInt:
2122 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002123 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002124 __ negl(out.AsRegister<Register>());
Roland Levillain88cb1752014-10-20 16:36:47 +01002125 break;
2126
2127 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002128 DCHECK(in.IsRegisterPair());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002129 DCHECK(in.Equals(out));
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002130 __ negl(out.AsRegisterPairLow<Register>());
2131 // Negation is similar to subtraction from zero. The least
2132 // significant byte triggers a borrow when it is different from
2133 // zero; to take it into account, add 1 to the most significant
2134 // byte if the carry flag (CF) is set to 1 after the first NEGL
2135 // operation.
2136 __ adcl(out.AsRegisterPairHigh<Register>(), Immediate(0));
2137 __ negl(out.AsRegisterPairHigh<Register>());
2138 break;
2139
Roland Levillain5368c212014-11-27 15:03:41 +00002140 case Primitive::kPrimFloat: {
2141 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002142 Register constant = locations->GetTemp(0).AsRegister<Register>();
2143 XmmRegister mask = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002144 // Implement float negation with an exclusive or with value
2145 // 0x80000000 (mask for bit 31, representing the sign of a
2146 // single-precision floating-point number).
2147 __ movl(constant, Immediate(INT32_C(0x80000000)));
2148 __ movd(mask, constant);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002149 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00002150 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002151 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00002152
Roland Levillain5368c212014-11-27 15:03:41 +00002153 case Primitive::kPrimDouble: {
2154 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002155 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002156 // Implement double negation with an exclusive or with value
2157 // 0x8000000000000000 (mask for bit 63, representing the sign of
2158 // a double-precision floating-point number).
2159 __ LoadLongConstant(mask, INT64_C(0x8000000000000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002160 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01002161 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002162 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002163
2164 default:
2165 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2166 }
2167}
2168
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002169void LocationsBuilderX86::VisitX86FPNeg(HX86FPNeg* neg) {
2170 LocationSummary* locations =
2171 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2172 DCHECK(Primitive::IsFloatingPointType(neg->GetType()));
2173 locations->SetInAt(0, Location::RequiresFpuRegister());
2174 locations->SetInAt(1, Location::RequiresRegister());
2175 locations->SetOut(Location::SameAsFirstInput());
2176 locations->AddTemp(Location::RequiresFpuRegister());
2177}
2178
2179void InstructionCodeGeneratorX86::VisitX86FPNeg(HX86FPNeg* neg) {
2180 LocationSummary* locations = neg->GetLocations();
2181 Location out = locations->Out();
2182 DCHECK(locations->InAt(0).Equals(out));
2183
2184 Register constant_area = locations->InAt(1).AsRegister<Register>();
2185 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
2186 if (neg->GetType() == Primitive::kPrimFloat) {
2187 __ movss(mask, codegen_->LiteralInt32Address(INT32_C(0x80000000), constant_area));
2188 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
2189 } else {
2190 __ movsd(mask, codegen_->LiteralInt64Address(INT64_C(0x8000000000000000), constant_area));
2191 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
2192 }
2193}
2194
Roland Levillaindff1f282014-11-05 14:15:05 +00002195void LocationsBuilderX86::VisitTypeConversion(HTypeConversion* conversion) {
Roland Levillaindff1f282014-11-05 14:15:05 +00002196 Primitive::Type result_type = conversion->GetResultType();
2197 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002198 DCHECK_NE(result_type, input_type);
Roland Levillain624279f2014-12-04 11:54:28 +00002199
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002200 // The float-to-long and double-to-long type conversions rely on a
2201 // call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00002202 LocationSummary::CallKind call_kind =
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002203 ((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
2204 && result_type == Primitive::kPrimLong)
Roland Levillain624279f2014-12-04 11:54:28 +00002205 ? LocationSummary::kCall
2206 : LocationSummary::kNoCall;
2207 LocationSummary* locations =
2208 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
2209
David Brazdilb2bd1c52015-03-25 11:17:37 +00002210 // The Java language does not allow treating boolean as an integral type but
2211 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00002212
Roland Levillaindff1f282014-11-05 14:15:05 +00002213 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002214 case Primitive::kPrimByte:
2215 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002216 case Primitive::kPrimLong: {
2217 // Type conversion from long to byte is a result of code transformations.
2218 HInstruction* input = conversion->InputAt(0);
2219 Location input_location = input->IsConstant()
2220 ? Location::ConstantLocation(input->AsConstant())
2221 : Location::RegisterPairLocation(EAX, EDX);
2222 locations->SetInAt(0, input_location);
2223 // Make the output overlap to please the register allocator. This greatly simplifies
2224 // the validation of the linear scan implementation
2225 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2226 break;
2227 }
David Brazdil46e2a392015-03-16 17:31:52 +00002228 case Primitive::kPrimBoolean:
2229 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002230 case Primitive::kPrimShort:
2231 case Primitive::kPrimInt:
2232 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002233 // Processing a Dex `int-to-byte' instruction.
Mark Mendell5f874182015-03-04 15:42:45 -05002234 locations->SetInAt(0, Location::ByteRegisterOrConstant(ECX, conversion->InputAt(0)));
2235 // Make the output overlap to please the register allocator. This greatly simplifies
2236 // the validation of the linear scan implementation
2237 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Roland Levillain51d3fc42014-11-13 14:11:42 +00002238 break;
2239
2240 default:
2241 LOG(FATAL) << "Unexpected type conversion from " << input_type
2242 << " to " << result_type;
2243 }
2244 break;
2245
Roland Levillain01a8d712014-11-14 16:27:39 +00002246 case Primitive::kPrimShort:
2247 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002248 case Primitive::kPrimLong:
2249 // Type conversion from long to short is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002250 case Primitive::kPrimBoolean:
2251 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002252 case Primitive::kPrimByte:
2253 case Primitive::kPrimInt:
2254 case Primitive::kPrimChar:
2255 // Processing a Dex `int-to-short' instruction.
2256 locations->SetInAt(0, Location::Any());
2257 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2258 break;
2259
2260 default:
2261 LOG(FATAL) << "Unexpected type conversion from " << input_type
2262 << " to " << result_type;
2263 }
2264 break;
2265
Roland Levillain946e1432014-11-11 17:35:19 +00002266 case Primitive::kPrimInt:
2267 switch (input_type) {
2268 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002269 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002270 locations->SetInAt(0, Location::Any());
2271 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2272 break;
2273
2274 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00002275 // Processing a Dex `float-to-int' instruction.
2276 locations->SetInAt(0, Location::RequiresFpuRegister());
2277 locations->SetOut(Location::RequiresRegister());
2278 locations->AddTemp(Location::RequiresFpuRegister());
2279 break;
2280
Roland Levillain946e1432014-11-11 17:35:19 +00002281 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002282 // Processing a Dex `double-to-int' instruction.
2283 locations->SetInAt(0, Location::RequiresFpuRegister());
2284 locations->SetOut(Location::RequiresRegister());
2285 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00002286 break;
2287
2288 default:
2289 LOG(FATAL) << "Unexpected type conversion from " << input_type
2290 << " to " << result_type;
2291 }
2292 break;
2293
Roland Levillaindff1f282014-11-05 14:15:05 +00002294 case Primitive::kPrimLong:
2295 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002296 case Primitive::kPrimBoolean:
2297 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002298 case Primitive::kPrimByte:
2299 case Primitive::kPrimShort:
2300 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002301 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002302 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002303 locations->SetInAt(0, Location::RegisterLocation(EAX));
2304 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
2305 break;
2306
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002307 case Primitive::kPrimFloat:
Vladimir Marko949c91f2015-01-27 10:48:44 +00002308 case Primitive::kPrimDouble: {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002309 // Processing a Dex `float-to-long' or 'double-to-long' instruction.
Vladimir Marko949c91f2015-01-27 10:48:44 +00002310 InvokeRuntimeCallingConvention calling_convention;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002311 XmmRegister parameter = calling_convention.GetFpuRegisterAt(0);
2312 locations->SetInAt(0, Location::FpuRegisterLocation(parameter));
2313
Vladimir Marko949c91f2015-01-27 10:48:44 +00002314 // The runtime helper puts the result in EAX, EDX.
2315 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Vladimir Marko949c91f2015-01-27 10:48:44 +00002316 }
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002317 break;
Roland Levillaindff1f282014-11-05 14:15:05 +00002318
2319 default:
2320 LOG(FATAL) << "Unexpected type conversion from " << input_type
2321 << " to " << result_type;
2322 }
2323 break;
2324
Roland Levillain981e4542014-11-14 11:47:14 +00002325 case Primitive::kPrimChar:
2326 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002327 case Primitive::kPrimLong:
2328 // Type conversion from long to char is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002329 case Primitive::kPrimBoolean:
2330 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002331 case Primitive::kPrimByte:
2332 case Primitive::kPrimShort:
2333 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002334 // Processing a Dex `int-to-char' instruction.
2335 locations->SetInAt(0, Location::Any());
2336 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2337 break;
2338
2339 default:
2340 LOG(FATAL) << "Unexpected type conversion from " << input_type
2341 << " to " << result_type;
2342 }
2343 break;
2344
Roland Levillaindff1f282014-11-05 14:15:05 +00002345 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002346 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002347 case Primitive::kPrimBoolean:
2348 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002349 case Primitive::kPrimByte:
2350 case Primitive::kPrimShort:
2351 case Primitive::kPrimInt:
2352 case Primitive::kPrimChar:
2353 // Processing a Dex `int-to-float' instruction.
2354 locations->SetInAt(0, Location::RequiresRegister());
2355 locations->SetOut(Location::RequiresFpuRegister());
2356 break;
2357
2358 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002359 // Processing a Dex `long-to-float' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002360 locations->SetInAt(0, Location::Any());
2361 locations->SetOut(Location::Any());
Roland Levillain6d0e4832014-11-27 18:31:21 +00002362 break;
2363
Roland Levillaincff13742014-11-17 14:32:17 +00002364 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002365 // Processing a Dex `double-to-float' instruction.
2366 locations->SetInAt(0, Location::RequiresFpuRegister());
2367 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002368 break;
2369
2370 default:
2371 LOG(FATAL) << "Unexpected type conversion from " << input_type
2372 << " to " << result_type;
2373 };
2374 break;
2375
Roland Levillaindff1f282014-11-05 14:15:05 +00002376 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002377 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002378 case Primitive::kPrimBoolean:
2379 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002380 case Primitive::kPrimByte:
2381 case Primitive::kPrimShort:
2382 case Primitive::kPrimInt:
2383 case Primitive::kPrimChar:
2384 // Processing a Dex `int-to-double' instruction.
2385 locations->SetInAt(0, Location::RequiresRegister());
2386 locations->SetOut(Location::RequiresFpuRegister());
2387 break;
2388
2389 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002390 // Processing a Dex `long-to-double' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002391 locations->SetInAt(0, Location::Any());
2392 locations->SetOut(Location::Any());
Roland Levillain647b9ed2014-11-27 12:06:00 +00002393 break;
2394
Roland Levillaincff13742014-11-17 14:32:17 +00002395 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002396 // Processing a Dex `float-to-double' instruction.
2397 locations->SetInAt(0, Location::RequiresFpuRegister());
2398 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002399 break;
2400
2401 default:
2402 LOG(FATAL) << "Unexpected type conversion from " << input_type
2403 << " to " << result_type;
2404 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002405 break;
2406
2407 default:
2408 LOG(FATAL) << "Unexpected type conversion from " << input_type
2409 << " to " << result_type;
2410 }
2411}
2412
2413void InstructionCodeGeneratorX86::VisitTypeConversion(HTypeConversion* conversion) {
2414 LocationSummary* locations = conversion->GetLocations();
2415 Location out = locations->Out();
2416 Location in = locations->InAt(0);
2417 Primitive::Type result_type = conversion->GetResultType();
2418 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002419 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00002420 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002421 case Primitive::kPrimByte:
2422 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002423 case Primitive::kPrimLong:
2424 // Type conversion from long to byte is a result of code transformations.
2425 if (in.IsRegisterPair()) {
2426 __ movsxb(out.AsRegister<Register>(), in.AsRegisterPairLow<ByteRegister>());
2427 } else {
2428 DCHECK(in.GetConstant()->IsLongConstant());
2429 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2430 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
2431 }
2432 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002433 case Primitive::kPrimBoolean:
2434 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002435 case Primitive::kPrimShort:
2436 case Primitive::kPrimInt:
2437 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002438 // Processing a Dex `int-to-byte' instruction.
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002439 if (in.IsRegister()) {
2440 __ movsxb(out.AsRegister<Register>(), in.AsRegister<ByteRegister>());
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002441 } else {
2442 DCHECK(in.GetConstant()->IsIntConstant());
2443 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
2444 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
2445 }
Roland Levillain51d3fc42014-11-13 14:11:42 +00002446 break;
2447
2448 default:
2449 LOG(FATAL) << "Unexpected type conversion from " << input_type
2450 << " to " << result_type;
2451 }
2452 break;
2453
Roland Levillain01a8d712014-11-14 16:27:39 +00002454 case Primitive::kPrimShort:
2455 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002456 case Primitive::kPrimLong:
2457 // Type conversion from long to short is a result of code transformations.
2458 if (in.IsRegisterPair()) {
2459 __ movsxw(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
2460 } else if (in.IsDoubleStackSlot()) {
2461 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
2462 } else {
2463 DCHECK(in.GetConstant()->IsLongConstant());
2464 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2465 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
2466 }
2467 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002468 case Primitive::kPrimBoolean:
2469 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002470 case Primitive::kPrimByte:
2471 case Primitive::kPrimInt:
2472 case Primitive::kPrimChar:
2473 // Processing a Dex `int-to-short' instruction.
2474 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002475 __ movsxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain01a8d712014-11-14 16:27:39 +00002476 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002477 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain01a8d712014-11-14 16:27:39 +00002478 } else {
2479 DCHECK(in.GetConstant()->IsIntConstant());
2480 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002481 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
Roland Levillain01a8d712014-11-14 16:27:39 +00002482 }
2483 break;
2484
2485 default:
2486 LOG(FATAL) << "Unexpected type conversion from " << input_type
2487 << " to " << result_type;
2488 }
2489 break;
2490
Roland Levillain946e1432014-11-11 17:35:19 +00002491 case Primitive::kPrimInt:
2492 switch (input_type) {
2493 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002494 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002495 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002496 __ movl(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00002497 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002498 __ movl(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain946e1432014-11-11 17:35:19 +00002499 } else {
2500 DCHECK(in.IsConstant());
2501 DCHECK(in.GetConstant()->IsLongConstant());
2502 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002503 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00002504 }
2505 break;
2506
Roland Levillain3f8f9362014-12-02 17:45:01 +00002507 case Primitive::kPrimFloat: {
2508 // Processing a Dex `float-to-int' instruction.
2509 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2510 Register output = out.AsRegister<Register>();
2511 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002512 NearLabel done, nan;
Roland Levillain3f8f9362014-12-02 17:45:01 +00002513
2514 __ movl(output, Immediate(kPrimIntMax));
2515 // temp = int-to-float(output)
2516 __ cvtsi2ss(temp, output);
2517 // if input >= temp goto done
2518 __ comiss(input, temp);
2519 __ j(kAboveEqual, &done);
2520 // if input == NaN goto nan
2521 __ j(kUnordered, &nan);
2522 // output = float-to-int-truncate(input)
2523 __ cvttss2si(output, input);
2524 __ jmp(&done);
2525 __ Bind(&nan);
2526 // output = 0
2527 __ xorl(output, output);
2528 __ Bind(&done);
2529 break;
2530 }
2531
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002532 case Primitive::kPrimDouble: {
2533 // Processing a Dex `double-to-int' instruction.
2534 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2535 Register output = out.AsRegister<Register>();
2536 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002537 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002538
2539 __ movl(output, Immediate(kPrimIntMax));
2540 // temp = int-to-double(output)
2541 __ cvtsi2sd(temp, output);
2542 // if input >= temp goto done
2543 __ comisd(input, temp);
2544 __ j(kAboveEqual, &done);
2545 // if input == NaN goto nan
2546 __ j(kUnordered, &nan);
2547 // output = double-to-int-truncate(input)
2548 __ cvttsd2si(output, input);
2549 __ jmp(&done);
2550 __ Bind(&nan);
2551 // output = 0
2552 __ xorl(output, output);
2553 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00002554 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002555 }
Roland Levillain946e1432014-11-11 17:35:19 +00002556
2557 default:
2558 LOG(FATAL) << "Unexpected type conversion from " << input_type
2559 << " to " << result_type;
2560 }
2561 break;
2562
Roland Levillaindff1f282014-11-05 14:15:05 +00002563 case Primitive::kPrimLong:
2564 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002565 case Primitive::kPrimBoolean:
2566 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002567 case Primitive::kPrimByte:
2568 case Primitive::kPrimShort:
2569 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002570 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002571 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002572 DCHECK_EQ(out.AsRegisterPairLow<Register>(), EAX);
2573 DCHECK_EQ(out.AsRegisterPairHigh<Register>(), EDX);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002574 DCHECK_EQ(in.AsRegister<Register>(), EAX);
Roland Levillaindff1f282014-11-05 14:15:05 +00002575 __ cdq();
2576 break;
2577
2578 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00002579 // Processing a Dex `float-to-long' instruction.
Alexandre Rames8158f282015-08-07 10:26:17 +01002580 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pF2l),
2581 conversion,
2582 conversion->GetDexPc(),
2583 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00002584 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
Roland Levillain624279f2014-12-04 11:54:28 +00002585 break;
2586
Roland Levillaindff1f282014-11-05 14:15:05 +00002587 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002588 // Processing a Dex `double-to-long' instruction.
Alexandre Rames8158f282015-08-07 10:26:17 +01002589 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pD2l),
2590 conversion,
2591 conversion->GetDexPc(),
2592 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00002593 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
Roland Levillaindff1f282014-11-05 14:15:05 +00002594 break;
2595
2596 default:
2597 LOG(FATAL) << "Unexpected type conversion from " << input_type
2598 << " to " << result_type;
2599 }
2600 break;
2601
Roland Levillain981e4542014-11-14 11:47:14 +00002602 case Primitive::kPrimChar:
2603 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002604 case Primitive::kPrimLong:
2605 // Type conversion from long to short is a result of code transformations.
2606 if (in.IsRegisterPair()) {
2607 __ movzxw(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
2608 } else if (in.IsDoubleStackSlot()) {
2609 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
2610 } else {
2611 DCHECK(in.GetConstant()->IsLongConstant());
2612 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2613 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
2614 }
2615 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002616 case Primitive::kPrimBoolean:
2617 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002618 case Primitive::kPrimByte:
2619 case Primitive::kPrimShort:
2620 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002621 // Processing a Dex `Process a Dex `int-to-char'' instruction.
2622 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002623 __ movzxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain981e4542014-11-14 11:47:14 +00002624 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002625 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain981e4542014-11-14 11:47:14 +00002626 } else {
2627 DCHECK(in.GetConstant()->IsIntConstant());
2628 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002629 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
Roland Levillain981e4542014-11-14 11:47:14 +00002630 }
2631 break;
2632
2633 default:
2634 LOG(FATAL) << "Unexpected type conversion from " << input_type
2635 << " to " << result_type;
2636 }
2637 break;
2638
Roland Levillaindff1f282014-11-05 14:15:05 +00002639 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002640 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002641 case Primitive::kPrimBoolean:
2642 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002643 case Primitive::kPrimByte:
2644 case Primitive::kPrimShort:
2645 case Primitive::kPrimInt:
2646 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002647 // Processing a Dex `int-to-float' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002648 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002649 break;
2650
Roland Levillain6d0e4832014-11-27 18:31:21 +00002651 case Primitive::kPrimLong: {
2652 // Processing a Dex `long-to-float' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002653 size_t adjustment = 0;
Roland Levillain6d0e4832014-11-27 18:31:21 +00002654
Roland Levillain232ade02015-04-20 15:14:36 +01002655 // Create stack space for the call to
2656 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstps below.
2657 // TODO: enhance register allocator to ask for stack temporaries.
2658 if (!in.IsDoubleStackSlot() || !out.IsStackSlot()) {
2659 adjustment = Primitive::ComponentSize(Primitive::kPrimLong);
2660 __ subl(ESP, Immediate(adjustment));
2661 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002662
Roland Levillain232ade02015-04-20 15:14:36 +01002663 // Load the value to the FP stack, using temporaries if needed.
2664 PushOntoFPStack(in, 0, adjustment, false, true);
2665
2666 if (out.IsStackSlot()) {
2667 __ fstps(Address(ESP, out.GetStackIndex() + adjustment));
2668 } else {
2669 __ fstps(Address(ESP, 0));
2670 Location stack_temp = Location::StackSlot(0);
2671 codegen_->Move32(out, stack_temp);
2672 }
2673
2674 // Remove the temporary stack space we allocated.
2675 if (adjustment != 0) {
2676 __ addl(ESP, Immediate(adjustment));
2677 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002678 break;
2679 }
2680
Roland Levillaincff13742014-11-17 14:32:17 +00002681 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002682 // Processing a Dex `double-to-float' instruction.
2683 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002684 break;
2685
2686 default:
2687 LOG(FATAL) << "Unexpected type conversion from " << input_type
2688 << " to " << result_type;
2689 };
2690 break;
2691
Roland Levillaindff1f282014-11-05 14:15:05 +00002692 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002693 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002694 case Primitive::kPrimBoolean:
2695 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002696 case Primitive::kPrimByte:
2697 case Primitive::kPrimShort:
2698 case Primitive::kPrimInt:
2699 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002700 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002701 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002702 break;
2703
Roland Levillain647b9ed2014-11-27 12:06:00 +00002704 case Primitive::kPrimLong: {
2705 // Processing a Dex `long-to-double' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002706 size_t adjustment = 0;
Roland Levillain647b9ed2014-11-27 12:06:00 +00002707
Roland Levillain232ade02015-04-20 15:14:36 +01002708 // Create stack space for the call to
2709 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstpl below.
2710 // TODO: enhance register allocator to ask for stack temporaries.
2711 if (!in.IsDoubleStackSlot() || !out.IsDoubleStackSlot()) {
2712 adjustment = Primitive::ComponentSize(Primitive::kPrimLong);
2713 __ subl(ESP, Immediate(adjustment));
2714 }
2715
2716 // Load the value to the FP stack, using temporaries if needed.
2717 PushOntoFPStack(in, 0, adjustment, false, true);
2718
2719 if (out.IsDoubleStackSlot()) {
2720 __ fstpl(Address(ESP, out.GetStackIndex() + adjustment));
2721 } else {
2722 __ fstpl(Address(ESP, 0));
2723 Location stack_temp = Location::DoubleStackSlot(0);
2724 codegen_->Move64(out, stack_temp);
2725 }
2726
2727 // Remove the temporary stack space we allocated.
2728 if (adjustment != 0) {
2729 __ addl(ESP, Immediate(adjustment));
2730 }
Roland Levillain647b9ed2014-11-27 12:06:00 +00002731 break;
2732 }
2733
Roland Levillaincff13742014-11-17 14:32:17 +00002734 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002735 // Processing a Dex `float-to-double' instruction.
2736 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002737 break;
2738
2739 default:
2740 LOG(FATAL) << "Unexpected type conversion from " << input_type
2741 << " to " << result_type;
2742 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002743 break;
2744
2745 default:
2746 LOG(FATAL) << "Unexpected type conversion from " << input_type
2747 << " to " << result_type;
2748 }
2749}
2750
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002751void LocationsBuilderX86::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002752 LocationSummary* locations =
2753 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002754 switch (add->GetResultType()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002755 case Primitive::kPrimInt: {
2756 locations->SetInAt(0, Location::RequiresRegister());
2757 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
2758 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2759 break;
2760 }
2761
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002762 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002763 locations->SetInAt(0, Location::RequiresRegister());
2764 locations->SetInAt(1, Location::Any());
2765 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002766 break;
2767 }
2768
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002769 case Primitive::kPrimFloat:
2770 case Primitive::kPrimDouble: {
2771 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002772 if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
2773 DCHECK(add->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00002774 } else if (add->InputAt(1)->IsConstant()) {
2775 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002776 } else {
2777 locations->SetInAt(1, Location::Any());
2778 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002779 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002780 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002781 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002782
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002783 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002784 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
2785 break;
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002786 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002787}
2788
2789void InstructionCodeGeneratorX86::VisitAdd(HAdd* add) {
2790 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002791 Location first = locations->InAt(0);
2792 Location second = locations->InAt(1);
Mark Mendell09b84632015-02-13 17:48:38 -05002793 Location out = locations->Out();
2794
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002795 switch (add->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002796 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002797 if (second.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002798 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2799 __ addl(out.AsRegister<Register>(), second.AsRegister<Register>());
Mark Mendell33bf2452015-05-27 10:08:24 -04002800 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
2801 __ addl(out.AsRegister<Register>(), first.AsRegister<Register>());
Mark Mendell09b84632015-02-13 17:48:38 -05002802 } else {
2803 __ leal(out.AsRegister<Register>(), Address(
2804 first.AsRegister<Register>(), second.AsRegister<Register>(), TIMES_1, 0));
2805 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002806 } else if (second.IsConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002807 int32_t value = second.GetConstant()->AsIntConstant()->GetValue();
2808 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2809 __ addl(out.AsRegister<Register>(), Immediate(value));
2810 } else {
2811 __ leal(out.AsRegister<Register>(), Address(first.AsRegister<Register>(), value));
2812 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002813 } else {
Mark Mendell09b84632015-02-13 17:48:38 -05002814 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002815 __ addl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002816 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002817 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002818 }
2819
2820 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002821 if (second.IsRegisterPair()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002822 __ addl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
2823 __ adcl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002824 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002825 __ addl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
2826 __ adcl(first.AsRegisterPairHigh<Register>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002827 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002828 } else {
2829 DCHECK(second.IsConstant()) << second;
2830 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2831 __ addl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
2832 __ adcl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002833 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002834 break;
2835 }
2836
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002837 case Primitive::kPrimFloat: {
2838 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002839 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04002840 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
2841 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00002842 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04002843 __ addss(first.AsFpuRegister<XmmRegister>(),
2844 codegen_->LiteralFloatAddress(
2845 const_area->GetConstant()->AsFloatConstant()->GetValue(),
2846 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2847 } else {
2848 DCHECK(second.IsStackSlot());
2849 __ addss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002850 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002851 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002852 }
2853
2854 case Primitive::kPrimDouble: {
2855 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002856 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04002857 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
2858 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00002859 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04002860 __ addsd(first.AsFpuRegister<XmmRegister>(),
2861 codegen_->LiteralDoubleAddress(
2862 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
2863 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2864 } else {
2865 DCHECK(second.IsDoubleStackSlot());
2866 __ addsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002867 }
2868 break;
2869 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002870
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002871 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002872 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002873 }
2874}
2875
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002876void LocationsBuilderX86::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002877 LocationSummary* locations =
2878 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002879 switch (sub->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002880 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002881 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002882 locations->SetInAt(0, Location::RequiresRegister());
2883 locations->SetInAt(1, Location::Any());
2884 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002885 break;
2886 }
Calin Juravle11351682014-10-23 15:38:15 +01002887 case Primitive::kPrimFloat:
2888 case Primitive::kPrimDouble: {
2889 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002890 if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
2891 DCHECK(sub->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00002892 } else if (sub->InputAt(1)->IsConstant()) {
2893 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002894 } else {
2895 locations->SetInAt(1, Location::Any());
2896 }
Calin Juravle11351682014-10-23 15:38:15 +01002897 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002898 break;
Calin Juravle11351682014-10-23 15:38:15 +01002899 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002900
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002901 default:
Calin Juravle11351682014-10-23 15:38:15 +01002902 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002903 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002904}
2905
2906void InstructionCodeGeneratorX86::VisitSub(HSub* sub) {
2907 LocationSummary* locations = sub->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002908 Location first = locations->InAt(0);
2909 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01002910 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002911 switch (sub->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002912 case Primitive::kPrimInt: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002913 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002914 __ subl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002915 } else if (second.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002916 __ subl(first.AsRegister<Register>(),
2917 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002918 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002919 __ subl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002920 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002921 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002922 }
2923
2924 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002925 if (second.IsRegisterPair()) {
Calin Juravle11351682014-10-23 15:38:15 +01002926 __ subl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
2927 __ sbbl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002928 } else if (second.IsDoubleStackSlot()) {
Calin Juravle11351682014-10-23 15:38:15 +01002929 __ subl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002930 __ sbbl(first.AsRegisterPairHigh<Register>(),
2931 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002932 } else {
2933 DCHECK(second.IsConstant()) << second;
2934 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2935 __ subl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
2936 __ sbbl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002937 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002938 break;
2939 }
2940
Calin Juravle11351682014-10-23 15:38:15 +01002941 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04002942 if (second.IsFpuRegister()) {
2943 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2944 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
2945 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00002946 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04002947 __ subss(first.AsFpuRegister<XmmRegister>(),
2948 codegen_->LiteralFloatAddress(
2949 const_area->GetConstant()->AsFloatConstant()->GetValue(),
2950 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2951 } else {
2952 DCHECK(second.IsStackSlot());
2953 __ subss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
2954 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002955 break;
Calin Juravle11351682014-10-23 15:38:15 +01002956 }
2957
2958 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04002959 if (second.IsFpuRegister()) {
2960 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2961 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
2962 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00002963 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04002964 __ subsd(first.AsFpuRegister<XmmRegister>(),
2965 codegen_->LiteralDoubleAddress(
2966 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
2967 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2968 } else {
2969 DCHECK(second.IsDoubleStackSlot());
2970 __ subsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
2971 }
Calin Juravle11351682014-10-23 15:38:15 +01002972 break;
2973 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002974
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002975 default:
Calin Juravle11351682014-10-23 15:38:15 +01002976 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002977 }
2978}
2979
Calin Juravle34bacdf2014-10-07 20:23:36 +01002980void LocationsBuilderX86::VisitMul(HMul* mul) {
2981 LocationSummary* locations =
2982 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
2983 switch (mul->GetResultType()) {
2984 case Primitive::kPrimInt:
2985 locations->SetInAt(0, Location::RequiresRegister());
2986 locations->SetInAt(1, Location::Any());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002987 if (mul->InputAt(1)->IsIntConstant()) {
2988 // Can use 3 operand multiply.
2989 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2990 } else {
2991 locations->SetOut(Location::SameAsFirstInput());
2992 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002993 break;
2994 case Primitive::kPrimLong: {
2995 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002996 locations->SetInAt(1, Location::Any());
2997 locations->SetOut(Location::SameAsFirstInput());
2998 // Needed for imul on 32bits with 64bits output.
2999 locations->AddTemp(Location::RegisterLocation(EAX));
3000 locations->AddTemp(Location::RegisterLocation(EDX));
3001 break;
3002 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003003 case Primitive::kPrimFloat:
3004 case Primitive::kPrimDouble: {
3005 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003006 if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3007 DCHECK(mul->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003008 } else if (mul->InputAt(1)->IsConstant()) {
3009 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003010 } else {
3011 locations->SetInAt(1, Location::Any());
3012 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003013 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003014 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003015 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003016
3017 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003018 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003019 }
3020}
3021
3022void InstructionCodeGeneratorX86::VisitMul(HMul* mul) {
3023 LocationSummary* locations = mul->GetLocations();
3024 Location first = locations->InAt(0);
3025 Location second = locations->InAt(1);
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003026 Location out = locations->Out();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003027
3028 switch (mul->GetResultType()) {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003029 case Primitive::kPrimInt:
3030 // The constant may have ended up in a register, so test explicitly to avoid
3031 // problems where the output may not be the same as the first operand.
3032 if (mul->InputAt(1)->IsIntConstant()) {
3033 Immediate imm(mul->InputAt(1)->AsIntConstant()->GetValue());
3034 __ imull(out.AsRegister<Register>(), first.AsRegister<Register>(), imm);
3035 } else if (second.IsRegister()) {
3036 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003037 __ imull(first.AsRegister<Register>(), second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003038 } else {
3039 DCHECK(second.IsStackSlot());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003040 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003041 __ imull(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01003042 }
3043 break;
Calin Juravle34bacdf2014-10-07 20:23:36 +01003044
3045 case Primitive::kPrimLong: {
Calin Juravle34bacdf2014-10-07 20:23:36 +01003046 Register in1_hi = first.AsRegisterPairHigh<Register>();
3047 Register in1_lo = first.AsRegisterPairLow<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003048 Register eax = locations->GetTemp(0).AsRegister<Register>();
3049 Register edx = locations->GetTemp(1).AsRegister<Register>();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003050
3051 DCHECK_EQ(EAX, eax);
3052 DCHECK_EQ(EDX, edx);
3053
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003054 // input: in1 - 64 bits, in2 - 64 bits.
Calin Juravle34bacdf2014-10-07 20:23:36 +01003055 // output: in1
3056 // formula: in1.hi : in1.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
3057 // parts: in1.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
3058 // parts: in1.lo = (in1.lo * in2.lo)[31:0]
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003059 if (second.IsConstant()) {
3060 DCHECK(second.GetConstant()->IsLongConstant());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003061
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003062 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3063 int32_t low_value = Low32Bits(value);
3064 int32_t high_value = High32Bits(value);
3065 Immediate low(low_value);
3066 Immediate high(high_value);
3067
3068 __ movl(eax, high);
3069 // eax <- in1.lo * in2.hi
3070 __ imull(eax, in1_lo);
3071 // in1.hi <- in1.hi * in2.lo
3072 __ imull(in1_hi, low);
3073 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3074 __ addl(in1_hi, eax);
3075 // move in2_lo to eax to prepare for double precision
3076 __ movl(eax, low);
3077 // edx:eax <- in1.lo * in2.lo
3078 __ mull(in1_lo);
3079 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3080 __ addl(in1_hi, edx);
3081 // in1.lo <- (in1.lo * in2.lo)[31:0];
3082 __ movl(in1_lo, eax);
3083 } else if (second.IsRegisterPair()) {
3084 Register in2_hi = second.AsRegisterPairHigh<Register>();
3085 Register in2_lo = second.AsRegisterPairLow<Register>();
3086
3087 __ movl(eax, in2_hi);
3088 // eax <- in1.lo * in2.hi
3089 __ imull(eax, in1_lo);
3090 // in1.hi <- in1.hi * in2.lo
3091 __ imull(in1_hi, in2_lo);
3092 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3093 __ addl(in1_hi, eax);
3094 // move in1_lo to eax to prepare for double precision
3095 __ movl(eax, in1_lo);
3096 // edx:eax <- in1.lo * in2.lo
3097 __ mull(in2_lo);
3098 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3099 __ addl(in1_hi, edx);
3100 // in1.lo <- (in1.lo * in2.lo)[31:0];
3101 __ movl(in1_lo, eax);
3102 } else {
3103 DCHECK(second.IsDoubleStackSlot()) << second;
3104 Address in2_hi(ESP, second.GetHighStackIndex(kX86WordSize));
3105 Address in2_lo(ESP, second.GetStackIndex());
3106
3107 __ movl(eax, in2_hi);
3108 // eax <- in1.lo * in2.hi
3109 __ imull(eax, in1_lo);
3110 // in1.hi <- in1.hi * in2.lo
3111 __ imull(in1_hi, in2_lo);
3112 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3113 __ addl(in1_hi, eax);
3114 // move in1_lo to eax to prepare for double precision
3115 __ movl(eax, in1_lo);
3116 // edx:eax <- in1.lo * in2.lo
3117 __ mull(in2_lo);
3118 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3119 __ addl(in1_hi, edx);
3120 // in1.lo <- (in1.lo * in2.lo)[31:0];
3121 __ movl(in1_lo, eax);
3122 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003123
3124 break;
3125 }
3126
Calin Juravleb5bfa962014-10-21 18:02:24 +01003127 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003128 DCHECK(first.Equals(locations->Out()));
3129 if (second.IsFpuRegister()) {
3130 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3131 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3132 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003133 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003134 __ mulss(first.AsFpuRegister<XmmRegister>(),
3135 codegen_->LiteralFloatAddress(
3136 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3137 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3138 } else {
3139 DCHECK(second.IsStackSlot());
3140 __ mulss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3141 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003142 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003143 }
3144
3145 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003146 DCHECK(first.Equals(locations->Out()));
3147 if (second.IsFpuRegister()) {
3148 __ mulsd(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 __ mulsd(first.AsFpuRegister<XmmRegister>(),
3153 codegen_->LiteralDoubleAddress(
3154 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3155 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3156 } else {
3157 DCHECK(second.IsDoubleStackSlot());
3158 __ mulsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3159 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003160 break;
3161 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003162
3163 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003164 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003165 }
3166}
3167
Roland Levillain232ade02015-04-20 15:14:36 +01003168void InstructionCodeGeneratorX86::PushOntoFPStack(Location source,
3169 uint32_t temp_offset,
3170 uint32_t stack_adjustment,
3171 bool is_fp,
3172 bool is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003173 if (source.IsStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01003174 DCHECK(!is_wide);
3175 if (is_fp) {
3176 __ flds(Address(ESP, source.GetStackIndex() + stack_adjustment));
3177 } else {
3178 __ filds(Address(ESP, source.GetStackIndex() + stack_adjustment));
3179 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003180 } else if (source.IsDoubleStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01003181 DCHECK(is_wide);
3182 if (is_fp) {
3183 __ fldl(Address(ESP, source.GetStackIndex() + stack_adjustment));
3184 } else {
3185 __ fildl(Address(ESP, source.GetStackIndex() + stack_adjustment));
3186 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003187 } else {
3188 // Write the value to the temporary location on the stack and load to FP stack.
Roland Levillain232ade02015-04-20 15:14:36 +01003189 if (!is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003190 Location stack_temp = Location::StackSlot(temp_offset);
3191 codegen_->Move32(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01003192 if (is_fp) {
3193 __ flds(Address(ESP, temp_offset));
3194 } else {
3195 __ filds(Address(ESP, temp_offset));
3196 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003197 } else {
3198 Location stack_temp = Location::DoubleStackSlot(temp_offset);
3199 codegen_->Move64(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01003200 if (is_fp) {
3201 __ fldl(Address(ESP, temp_offset));
3202 } else {
3203 __ fildl(Address(ESP, temp_offset));
3204 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003205 }
3206 }
3207}
3208
3209void InstructionCodeGeneratorX86::GenerateRemFP(HRem *rem) {
3210 Primitive::Type type = rem->GetResultType();
3211 bool is_float = type == Primitive::kPrimFloat;
3212 size_t elem_size = Primitive::ComponentSize(type);
3213 LocationSummary* locations = rem->GetLocations();
3214 Location first = locations->InAt(0);
3215 Location second = locations->InAt(1);
3216 Location out = locations->Out();
3217
3218 // Create stack space for 2 elements.
3219 // TODO: enhance register allocator to ask for stack temporaries.
3220 __ subl(ESP, Immediate(2 * elem_size));
3221
3222 // Load the values to the FP stack in reverse order, using temporaries if needed.
Roland Levillain232ade02015-04-20 15:14:36 +01003223 const bool is_wide = !is_float;
3224 PushOntoFPStack(second, elem_size, 2 * elem_size, /* is_fp */ true, is_wide);
3225 PushOntoFPStack(first, 0, 2 * elem_size, /* is_fp */ true, is_wide);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003226
3227 // Loop doing FPREM until we stabilize.
Mark Mendell0c9497d2015-08-21 09:30:05 -04003228 NearLabel retry;
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003229 __ Bind(&retry);
3230 __ fprem();
3231
3232 // Move FP status to AX.
3233 __ fstsw();
3234
3235 // And see if the argument reduction is complete. This is signaled by the
3236 // C2 FPU flag bit set to 0.
3237 __ andl(EAX, Immediate(kC2ConditionMask));
3238 __ j(kNotEqual, &retry);
3239
3240 // We have settled on the final value. Retrieve it into an XMM register.
3241 // Store FP top of stack to real stack.
3242 if (is_float) {
3243 __ fsts(Address(ESP, 0));
3244 } else {
3245 __ fstl(Address(ESP, 0));
3246 }
3247
3248 // Pop the 2 items from the FP stack.
3249 __ fucompp();
3250
3251 // Load the value from the stack into an XMM register.
3252 DCHECK(out.IsFpuRegister()) << out;
3253 if (is_float) {
3254 __ movss(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
3255 } else {
3256 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
3257 }
3258
3259 // And remove the temporary stack space we allocated.
3260 __ addl(ESP, Immediate(2 * elem_size));
3261}
3262
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003263
3264void InstructionCodeGeneratorX86::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3265 DCHECK(instruction->IsDiv() || instruction->IsRem());
3266
3267 LocationSummary* locations = instruction->GetLocations();
3268 DCHECK(locations->InAt(1).IsConstant());
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003269 DCHECK(locations->InAt(1).GetConstant()->IsIntConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003270
3271 Register out_register = locations->Out().AsRegister<Register>();
3272 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003273 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003274
3275 DCHECK(imm == 1 || imm == -1);
3276
3277 if (instruction->IsRem()) {
3278 __ xorl(out_register, out_register);
3279 } else {
3280 __ movl(out_register, input_register);
3281 if (imm == -1) {
3282 __ negl(out_register);
3283 }
3284 }
3285}
3286
3287
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003288void InstructionCodeGeneratorX86::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003289 LocationSummary* locations = instruction->GetLocations();
3290
3291 Register out_register = locations->Out().AsRegister<Register>();
3292 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003293 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003294 DCHECK(IsPowerOfTwo(AbsOrMin(imm)));
3295 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003296
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003297 Register num = locations->GetTemp(0).AsRegister<Register>();
3298
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003299 __ leal(num, Address(input_register, abs_imm - 1));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003300 __ testl(input_register, input_register);
3301 __ cmovl(kGreaterEqual, num, input_register);
3302 int shift = CTZ(imm);
3303 __ sarl(num, Immediate(shift));
3304
3305 if (imm < 0) {
3306 __ negl(num);
3307 }
3308
3309 __ movl(out_register, num);
3310}
3311
3312void InstructionCodeGeneratorX86::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3313 DCHECK(instruction->IsDiv() || instruction->IsRem());
3314
3315 LocationSummary* locations = instruction->GetLocations();
3316 int imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
3317
3318 Register eax = locations->InAt(0).AsRegister<Register>();
3319 Register out = locations->Out().AsRegister<Register>();
3320 Register num;
3321 Register edx;
3322
3323 if (instruction->IsDiv()) {
3324 edx = locations->GetTemp(0).AsRegister<Register>();
3325 num = locations->GetTemp(1).AsRegister<Register>();
3326 } else {
3327 edx = locations->Out().AsRegister<Register>();
3328 num = locations->GetTemp(0).AsRegister<Register>();
3329 }
3330
3331 DCHECK_EQ(EAX, eax);
3332 DCHECK_EQ(EDX, edx);
3333 if (instruction->IsDiv()) {
3334 DCHECK_EQ(EAX, out);
3335 } else {
3336 DCHECK_EQ(EDX, out);
3337 }
3338
3339 int64_t magic;
3340 int shift;
3341 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
3342
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003343 // Save the numerator.
3344 __ movl(num, eax);
3345
3346 // EAX = magic
3347 __ movl(eax, Immediate(magic));
3348
3349 // EDX:EAX = magic * numerator
3350 __ imull(num);
3351
3352 if (imm > 0 && magic < 0) {
3353 // EDX += num
3354 __ addl(edx, num);
3355 } else if (imm < 0 && magic > 0) {
3356 __ subl(edx, num);
3357 }
3358
3359 // Shift if needed.
3360 if (shift != 0) {
3361 __ sarl(edx, Immediate(shift));
3362 }
3363
3364 // EDX += 1 if EDX < 0
3365 __ movl(eax, edx);
3366 __ shrl(edx, Immediate(31));
3367 __ addl(edx, eax);
3368
3369 if (instruction->IsRem()) {
3370 __ movl(eax, num);
3371 __ imull(edx, Immediate(imm));
3372 __ subl(eax, edx);
3373 __ movl(edx, eax);
3374 } else {
3375 __ movl(eax, edx);
3376 }
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003377}
3378
Calin Juravlebacfec32014-11-14 15:54:36 +00003379void InstructionCodeGeneratorX86::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3380 DCHECK(instruction->IsDiv() || instruction->IsRem());
3381
3382 LocationSummary* locations = instruction->GetLocations();
3383 Location out = locations->Out();
3384 Location first = locations->InAt(0);
3385 Location second = locations->InAt(1);
3386 bool is_div = instruction->IsDiv();
3387
3388 switch (instruction->GetResultType()) {
3389 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003390 DCHECK_EQ(EAX, first.AsRegister<Register>());
3391 DCHECK_EQ(is_div ? EAX : EDX, out.AsRegister<Register>());
Calin Juravlebacfec32014-11-14 15:54:36 +00003392
Vladimir Marko13c86fd2015-11-11 12:37:46 +00003393 if (second.IsConstant()) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003394 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003395
3396 if (imm == 0) {
3397 // Do not generate anything for 0. DivZeroCheck would forbid any generated code.
3398 } else if (imm == 1 || imm == -1) {
3399 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003400 } else if (is_div && IsPowerOfTwo(AbsOrMin(imm))) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003401 DivByPowerOfTwo(instruction->AsDiv());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003402 } else {
3403 DCHECK(imm <= -2 || imm >= 2);
3404 GenerateDivRemWithAnyConstant(instruction);
3405 }
3406 } else {
David Srbecky9cd6d372016-02-09 15:24:47 +00003407 SlowPathCode* slow_path = new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86(
3408 instruction, out.AsRegister<Register>(), is_div);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003409 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00003410
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003411 Register second_reg = second.AsRegister<Register>();
3412 // 0x80000000/-1 triggers an arithmetic exception!
3413 // Dividing by -1 is actually negation and -0x800000000 = 0x80000000 so
3414 // it's safe to just use negl instead of more complex comparisons.
Calin Juravlebacfec32014-11-14 15:54:36 +00003415
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003416 __ cmpl(second_reg, Immediate(-1));
3417 __ j(kEqual, slow_path->GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +00003418
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003419 // edx:eax <- sign-extended of eax
3420 __ cdq();
3421 // eax = quotient, edx = remainder
3422 __ idivl(second_reg);
3423 __ Bind(slow_path->GetExitLabel());
3424 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003425 break;
3426 }
3427
3428 case Primitive::kPrimLong: {
3429 InvokeRuntimeCallingConvention calling_convention;
3430 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
3431 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
3432 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
3433 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
3434 DCHECK_EQ(EAX, out.AsRegisterPairLow<Register>());
3435 DCHECK_EQ(EDX, out.AsRegisterPairHigh<Register>());
3436
3437 if (is_div) {
Alexandre Rames8158f282015-08-07 10:26:17 +01003438 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLdiv),
3439 instruction,
3440 instruction->GetDexPc(),
3441 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003442 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003443 } else {
Alexandre Rames8158f282015-08-07 10:26:17 +01003444 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLmod),
3445 instruction,
3446 instruction->GetDexPc(),
3447 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003448 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003449 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003450 break;
3451 }
3452
3453 default:
3454 LOG(FATAL) << "Unexpected type for GenerateDivRemIntegral " << instruction->GetResultType();
3455 }
3456}
3457
Calin Juravle7c4954d2014-10-28 16:57:40 +00003458void LocationsBuilderX86::VisitDiv(HDiv* div) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003459 LocationSummary::CallKind call_kind = (div->GetResultType() == Primitive::kPrimLong)
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003460 ? LocationSummary::kCall
3461 : LocationSummary::kNoCall;
3462 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
3463
Calin Juravle7c4954d2014-10-28 16:57:40 +00003464 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00003465 case Primitive::kPrimInt: {
3466 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003467 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00003468 locations->SetOut(Location::SameAsFirstInput());
3469 // Intel uses edx:eax as the dividend.
3470 locations->AddTemp(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003471 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3472 // which enforces results to be in EAX and EDX, things are simpler if we use EAX also as
3473 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003474 if (div->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003475 locations->AddTemp(Location::RequiresRegister());
3476 }
Calin Juravled0d48522014-11-04 16:40:20 +00003477 break;
3478 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003479 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003480 InvokeRuntimeCallingConvention calling_convention;
3481 locations->SetInAt(0, Location::RegisterPairLocation(
3482 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3483 locations->SetInAt(1, Location::RegisterPairLocation(
3484 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3485 // Runtime helper puts the result in EAX, EDX.
3486 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Calin Juravle7c4954d2014-10-28 16:57:40 +00003487 break;
3488 }
3489 case Primitive::kPrimFloat:
3490 case Primitive::kPrimDouble: {
3491 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003492 if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3493 DCHECK(div->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003494 } else if (div->InputAt(1)->IsConstant()) {
3495 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003496 } else {
3497 locations->SetInAt(1, Location::Any());
3498 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003499 locations->SetOut(Location::SameAsFirstInput());
3500 break;
3501 }
3502
3503 default:
3504 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3505 }
3506}
3507
3508void InstructionCodeGeneratorX86::VisitDiv(HDiv* div) {
3509 LocationSummary* locations = div->GetLocations();
3510 Location first = locations->InAt(0);
3511 Location second = locations->InAt(1);
Calin Juravle7c4954d2014-10-28 16:57:40 +00003512
3513 switch (div->GetResultType()) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003514 case Primitive::kPrimInt:
Calin Juravle7c4954d2014-10-28 16:57:40 +00003515 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003516 GenerateDivRemIntegral(div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00003517 break;
3518 }
3519
3520 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003521 if (second.IsFpuRegister()) {
3522 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3523 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3524 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003525 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003526 __ divss(first.AsFpuRegister<XmmRegister>(),
3527 codegen_->LiteralFloatAddress(
3528 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3529 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3530 } else {
3531 DCHECK(second.IsStackSlot());
3532 __ divss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3533 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003534 break;
3535 }
3536
3537 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003538 if (second.IsFpuRegister()) {
3539 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3540 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3541 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003542 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003543 __ divsd(first.AsFpuRegister<XmmRegister>(),
3544 codegen_->LiteralDoubleAddress(
3545 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3546 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3547 } else {
3548 DCHECK(second.IsDoubleStackSlot());
3549 __ divsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3550 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003551 break;
3552 }
3553
3554 default:
3555 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3556 }
3557}
3558
Calin Juravlebacfec32014-11-14 15:54:36 +00003559void LocationsBuilderX86::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00003560 Primitive::Type type = rem->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003561
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003562 LocationSummary::CallKind call_kind = (rem->GetResultType() == Primitive::kPrimLong)
3563 ? LocationSummary::kCall
3564 : LocationSummary::kNoCall;
3565 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
Calin Juravlebacfec32014-11-14 15:54:36 +00003566
Calin Juravled2ec87d2014-12-08 14:24:46 +00003567 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003568 case Primitive::kPrimInt: {
3569 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003570 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00003571 locations->SetOut(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003572 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3573 // which enforces results to be in EAX and EDX, things are simpler if we use EDX also as
3574 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003575 if (rem->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003576 locations->AddTemp(Location::RequiresRegister());
3577 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003578 break;
3579 }
3580 case Primitive::kPrimLong: {
3581 InvokeRuntimeCallingConvention calling_convention;
3582 locations->SetInAt(0, Location::RegisterPairLocation(
3583 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3584 locations->SetInAt(1, Location::RegisterPairLocation(
3585 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3586 // Runtime helper puts the result in EAX, EDX.
3587 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
3588 break;
3589 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003590 case Primitive::kPrimDouble:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003591 case Primitive::kPrimFloat: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003592 locations->SetInAt(0, Location::Any());
3593 locations->SetInAt(1, Location::Any());
3594 locations->SetOut(Location::RequiresFpuRegister());
3595 locations->AddTemp(Location::RegisterLocation(EAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00003596 break;
3597 }
3598
3599 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003600 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003601 }
3602}
3603
3604void InstructionCodeGeneratorX86::VisitRem(HRem* rem) {
3605 Primitive::Type type = rem->GetResultType();
3606 switch (type) {
3607 case Primitive::kPrimInt:
3608 case Primitive::kPrimLong: {
3609 GenerateDivRemIntegral(rem);
3610 break;
3611 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003612 case Primitive::kPrimFloat:
Calin Juravlebacfec32014-11-14 15:54:36 +00003613 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003614 GenerateRemFP(rem);
Calin Juravlebacfec32014-11-14 15:54:36 +00003615 break;
3616 }
3617 default:
3618 LOG(FATAL) << "Unexpected rem type " << type;
3619 }
3620}
3621
Calin Juravled0d48522014-11-04 16:40:20 +00003622void LocationsBuilderX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00003623 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
3624 ? LocationSummary::kCallOnSlowPath
3625 : LocationSummary::kNoCall;
3626 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003627 switch (instruction->GetType()) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003628 case Primitive::kPrimBoolean:
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003629 case Primitive::kPrimByte:
3630 case Primitive::kPrimChar:
3631 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003632 case Primitive::kPrimInt: {
3633 locations->SetInAt(0, Location::Any());
3634 break;
3635 }
3636 case Primitive::kPrimLong: {
3637 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
3638 if (!instruction->IsConstant()) {
3639 locations->AddTemp(Location::RequiresRegister());
3640 }
3641 break;
3642 }
3643 default:
3644 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
3645 }
Calin Juravled0d48522014-11-04 16:40:20 +00003646 if (instruction->HasUses()) {
3647 locations->SetOut(Location::SameAsFirstInput());
3648 }
3649}
3650
3651void InstructionCodeGeneratorX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003652 SlowPathCode* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86(instruction);
Calin Juravled0d48522014-11-04 16:40:20 +00003653 codegen_->AddSlowPath(slow_path);
3654
3655 LocationSummary* locations = instruction->GetLocations();
3656 Location value = locations->InAt(0);
3657
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003658 switch (instruction->GetType()) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003659 case Primitive::kPrimBoolean:
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003660 case Primitive::kPrimByte:
3661 case Primitive::kPrimChar:
3662 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003663 case Primitive::kPrimInt: {
3664 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003665 __ testl(value.AsRegister<Register>(), value.AsRegister<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003666 __ j(kEqual, slow_path->GetEntryLabel());
3667 } else if (value.IsStackSlot()) {
3668 __ cmpl(Address(ESP, value.GetStackIndex()), Immediate(0));
3669 __ j(kEqual, slow_path->GetEntryLabel());
3670 } else {
3671 DCHECK(value.IsConstant()) << value;
3672 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
3673 __ jmp(slow_path->GetEntryLabel());
3674 }
3675 }
3676 break;
Calin Juravled0d48522014-11-04 16:40:20 +00003677 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003678 case Primitive::kPrimLong: {
3679 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003680 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003681 __ movl(temp, value.AsRegisterPairLow<Register>());
3682 __ orl(temp, value.AsRegisterPairHigh<Register>());
3683 __ j(kEqual, slow_path->GetEntryLabel());
3684 } else {
3685 DCHECK(value.IsConstant()) << value;
3686 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
3687 __ jmp(slow_path->GetEntryLabel());
3688 }
3689 }
3690 break;
3691 }
3692 default:
3693 LOG(FATAL) << "Unexpected type for HDivZeroCheck" << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00003694 }
Calin Juravled0d48522014-11-04 16:40:20 +00003695}
3696
Calin Juravle9aec02f2014-11-18 23:06:35 +00003697void LocationsBuilderX86::HandleShift(HBinaryOperation* op) {
3698 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3699
3700 LocationSummary* locations =
3701 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
3702
3703 switch (op->GetResultType()) {
Mark P Mendell73945692015-04-29 14:56:17 +00003704 case Primitive::kPrimInt:
Calin Juravle9aec02f2014-11-18 23:06:35 +00003705 case Primitive::kPrimLong: {
Mark P Mendell73945692015-04-29 14:56:17 +00003706 // Can't have Location::Any() and output SameAsFirstInput()
Calin Juravle9aec02f2014-11-18 23:06:35 +00003707 locations->SetInAt(0, Location::RequiresRegister());
Mark P Mendell73945692015-04-29 14:56:17 +00003708 // The shift count needs to be in CL or a constant.
3709 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, op->InputAt(1)));
Calin Juravle9aec02f2014-11-18 23:06:35 +00003710 locations->SetOut(Location::SameAsFirstInput());
3711 break;
3712 }
3713 default:
3714 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
3715 }
3716}
3717
3718void InstructionCodeGeneratorX86::HandleShift(HBinaryOperation* op) {
3719 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3720
3721 LocationSummary* locations = op->GetLocations();
3722 Location first = locations->InAt(0);
3723 Location second = locations->InAt(1);
3724 DCHECK(first.Equals(locations->Out()));
3725
3726 switch (op->GetResultType()) {
3727 case Primitive::kPrimInt: {
Mark P Mendell73945692015-04-29 14:56:17 +00003728 DCHECK(first.IsRegister());
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003729 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003730 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003731 Register second_reg = second.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003732 DCHECK_EQ(ECX, second_reg);
3733 if (op->IsShl()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003734 __ shll(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003735 } else if (op->IsShr()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003736 __ sarl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003737 } else {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003738 __ shrl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003739 }
3740 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003741 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance;
Mark P Mendell73945692015-04-29 14:56:17 +00003742 if (shift == 0) {
3743 return;
3744 }
3745 Immediate imm(shift);
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003746 if (op->IsShl()) {
3747 __ shll(first_reg, imm);
3748 } else if (op->IsShr()) {
3749 __ sarl(first_reg, imm);
3750 } else {
3751 __ shrl(first_reg, imm);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003752 }
3753 }
3754 break;
3755 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003756 case Primitive::kPrimLong: {
Mark P Mendell73945692015-04-29 14:56:17 +00003757 if (second.IsRegister()) {
3758 Register second_reg = second.AsRegister<Register>();
3759 DCHECK_EQ(ECX, second_reg);
3760 if (op->IsShl()) {
3761 GenerateShlLong(first, second_reg);
3762 } else if (op->IsShr()) {
3763 GenerateShrLong(first, second_reg);
3764 } else {
3765 GenerateUShrLong(first, second_reg);
3766 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003767 } else {
Mark P Mendell73945692015-04-29 14:56:17 +00003768 // Shift by a constant.
Roland Levillain5b5b9312016-03-22 14:57:31 +00003769 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance;
Mark P Mendell73945692015-04-29 14:56:17 +00003770 // Nothing to do if the shift is 0, as the input is already the output.
3771 if (shift != 0) {
3772 if (op->IsShl()) {
3773 GenerateShlLong(first, shift);
3774 } else if (op->IsShr()) {
3775 GenerateShrLong(first, shift);
3776 } else {
3777 GenerateUShrLong(first, shift);
3778 }
3779 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003780 }
3781 break;
3782 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003783 default:
3784 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
3785 }
3786}
3787
Mark P Mendell73945692015-04-29 14:56:17 +00003788void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, int shift) {
3789 Register low = loc.AsRegisterPairLow<Register>();
3790 Register high = loc.AsRegisterPairHigh<Register>();
Mark Mendellba56d062015-05-05 21:34:03 -04003791 if (shift == 1) {
3792 // This is just an addition.
3793 __ addl(low, low);
3794 __ adcl(high, high);
3795 } else if (shift == 32) {
Mark P Mendell73945692015-04-29 14:56:17 +00003796 // Shift by 32 is easy. High gets low, and low gets 0.
3797 codegen_->EmitParallelMoves(
3798 loc.ToLow(),
3799 loc.ToHigh(),
3800 Primitive::kPrimInt,
3801 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
3802 loc.ToLow(),
3803 Primitive::kPrimInt);
3804 } else if (shift > 32) {
3805 // Low part becomes 0. High part is low part << (shift-32).
3806 __ movl(high, low);
3807 __ shll(high, Immediate(shift - 32));
3808 __ xorl(low, low);
3809 } else {
3810 // Between 1 and 31.
3811 __ shld(high, low, Immediate(shift));
3812 __ shll(low, Immediate(shift));
3813 }
3814}
3815
Calin Juravle9aec02f2014-11-18 23:06:35 +00003816void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04003817 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00003818 __ shld(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>(), shifter);
3819 __ shll(loc.AsRegisterPairLow<Register>(), shifter);
3820 __ testl(shifter, Immediate(32));
3821 __ j(kEqual, &done);
3822 __ movl(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>());
3823 __ movl(loc.AsRegisterPairLow<Register>(), Immediate(0));
3824 __ Bind(&done);
3825}
3826
Mark P Mendell73945692015-04-29 14:56:17 +00003827void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, int shift) {
3828 Register low = loc.AsRegisterPairLow<Register>();
3829 Register high = loc.AsRegisterPairHigh<Register>();
3830 if (shift == 32) {
3831 // Need to copy the sign.
3832 DCHECK_NE(low, high);
3833 __ movl(low, high);
3834 __ sarl(high, Immediate(31));
3835 } else if (shift > 32) {
3836 DCHECK_NE(low, high);
3837 // High part becomes sign. Low part is shifted by shift - 32.
3838 __ movl(low, high);
3839 __ sarl(high, Immediate(31));
3840 __ sarl(low, Immediate(shift - 32));
3841 } else {
3842 // Between 1 and 31.
3843 __ shrd(low, high, Immediate(shift));
3844 __ sarl(high, Immediate(shift));
3845 }
3846}
3847
Calin Juravle9aec02f2014-11-18 23:06:35 +00003848void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04003849 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00003850 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
3851 __ sarl(loc.AsRegisterPairHigh<Register>(), shifter);
3852 __ testl(shifter, Immediate(32));
3853 __ j(kEqual, &done);
3854 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
3855 __ sarl(loc.AsRegisterPairHigh<Register>(), Immediate(31));
3856 __ Bind(&done);
3857}
3858
Mark P Mendell73945692015-04-29 14:56:17 +00003859void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, int shift) {
3860 Register low = loc.AsRegisterPairLow<Register>();
3861 Register high = loc.AsRegisterPairHigh<Register>();
3862 if (shift == 32) {
3863 // Shift by 32 is easy. Low gets high, and high gets 0.
3864 codegen_->EmitParallelMoves(
3865 loc.ToHigh(),
3866 loc.ToLow(),
3867 Primitive::kPrimInt,
3868 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
3869 loc.ToHigh(),
3870 Primitive::kPrimInt);
3871 } else if (shift > 32) {
3872 // Low part is high >> (shift - 32). High part becomes 0.
3873 __ movl(low, high);
3874 __ shrl(low, Immediate(shift - 32));
3875 __ xorl(high, high);
3876 } else {
3877 // Between 1 and 31.
3878 __ shrd(low, high, Immediate(shift));
3879 __ shrl(high, Immediate(shift));
3880 }
3881}
3882
Calin Juravle9aec02f2014-11-18 23:06:35 +00003883void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04003884 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00003885 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
3886 __ shrl(loc.AsRegisterPairHigh<Register>(), shifter);
3887 __ testl(shifter, Immediate(32));
3888 __ j(kEqual, &done);
3889 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
3890 __ movl(loc.AsRegisterPairHigh<Register>(), Immediate(0));
3891 __ Bind(&done);
3892}
3893
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003894void LocationsBuilderX86::VisitRor(HRor* ror) {
3895 LocationSummary* locations =
3896 new (GetGraph()->GetArena()) LocationSummary(ror, LocationSummary::kNoCall);
3897
3898 switch (ror->GetResultType()) {
3899 case Primitive::kPrimLong:
3900 // Add the temporary needed.
3901 locations->AddTemp(Location::RequiresRegister());
3902 FALLTHROUGH_INTENDED;
3903 case Primitive::kPrimInt:
3904 locations->SetInAt(0, Location::RequiresRegister());
3905 // The shift count needs to be in CL (unless it is a constant).
3906 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, ror->InputAt(1)));
3907 locations->SetOut(Location::SameAsFirstInput());
3908 break;
3909 default:
3910 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
3911 UNREACHABLE();
3912 }
3913}
3914
3915void InstructionCodeGeneratorX86::VisitRor(HRor* ror) {
3916 LocationSummary* locations = ror->GetLocations();
3917 Location first = locations->InAt(0);
3918 Location second = locations->InAt(1);
3919
3920 if (ror->GetResultType() == Primitive::kPrimInt) {
3921 Register first_reg = first.AsRegister<Register>();
3922 if (second.IsRegister()) {
3923 Register second_reg = second.AsRegister<Register>();
3924 __ rorl(first_reg, second_reg);
3925 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003926 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003927 __ rorl(first_reg, imm);
3928 }
3929 return;
3930 }
3931
3932 DCHECK_EQ(ror->GetResultType(), Primitive::kPrimLong);
3933 Register first_reg_lo = first.AsRegisterPairLow<Register>();
3934 Register first_reg_hi = first.AsRegisterPairHigh<Register>();
3935 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
3936 if (second.IsRegister()) {
3937 Register second_reg = second.AsRegister<Register>();
3938 DCHECK_EQ(second_reg, ECX);
3939 __ movl(temp_reg, first_reg_hi);
3940 __ shrd(first_reg_hi, first_reg_lo, second_reg);
3941 __ shrd(first_reg_lo, temp_reg, second_reg);
3942 __ movl(temp_reg, first_reg_hi);
3943 __ testl(second_reg, Immediate(32));
3944 __ cmovl(kNotEqual, first_reg_hi, first_reg_lo);
3945 __ cmovl(kNotEqual, first_reg_lo, temp_reg);
3946 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003947 int32_t shift_amt = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance;
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003948 if (shift_amt == 0) {
3949 // Already fine.
3950 return;
3951 }
3952 if (shift_amt == 32) {
3953 // Just swap.
3954 __ movl(temp_reg, first_reg_lo);
3955 __ movl(first_reg_lo, first_reg_hi);
3956 __ movl(first_reg_hi, temp_reg);
3957 return;
3958 }
3959
3960 Immediate imm(shift_amt);
3961 // Save the constents of the low value.
3962 __ movl(temp_reg, first_reg_lo);
3963
3964 // Shift right into low, feeding bits from high.
3965 __ shrd(first_reg_lo, first_reg_hi, imm);
3966
3967 // Shift right into high, feeding bits from the original low.
3968 __ shrd(first_reg_hi, temp_reg, imm);
3969
3970 // Swap if needed.
3971 if (shift_amt > 32) {
3972 __ movl(temp_reg, first_reg_lo);
3973 __ movl(first_reg_lo, first_reg_hi);
3974 __ movl(first_reg_hi, temp_reg);
3975 }
3976 }
3977}
3978
Calin Juravle9aec02f2014-11-18 23:06:35 +00003979void LocationsBuilderX86::VisitShl(HShl* shl) {
3980 HandleShift(shl);
3981}
3982
3983void InstructionCodeGeneratorX86::VisitShl(HShl* shl) {
3984 HandleShift(shl);
3985}
3986
3987void LocationsBuilderX86::VisitShr(HShr* shr) {
3988 HandleShift(shr);
3989}
3990
3991void InstructionCodeGeneratorX86::VisitShr(HShr* shr) {
3992 HandleShift(shr);
3993}
3994
3995void LocationsBuilderX86::VisitUShr(HUShr* ushr) {
3996 HandleShift(ushr);
3997}
3998
3999void InstructionCodeGeneratorX86::VisitUShr(HUShr* ushr) {
4000 HandleShift(ushr);
4001}
4002
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004003void LocationsBuilderX86::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004004 LocationSummary* locations =
4005 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004006 locations->SetOut(Location::RegisterLocation(EAX));
David Brazdil6de19382016-01-08 17:37:10 +00004007 if (instruction->IsStringAlloc()) {
4008 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
4009 } else {
4010 InvokeRuntimeCallingConvention calling_convention;
4011 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4012 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
4013 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004014}
4015
4016void InstructionCodeGeneratorX86::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01004017 // Note: if heap poisoning is enabled, the entry point takes cares
4018 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00004019 if (instruction->IsStringAlloc()) {
4020 // String is allocated through StringFactory. Call NewEmptyString entry point.
4021 Register temp = instruction->GetLocations()->GetTemp(0).AsRegister<Register>();
4022 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize);
4023 __ fs()->movl(temp, Address::Absolute(QUICK_ENTRY_POINT(pNewEmptyString)));
4024 __ call(Address(temp, code_offset.Int32Value()));
4025 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4026 } else {
4027 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
4028 instruction,
4029 instruction->GetDexPc(),
4030 nullptr);
4031 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
4032 DCHECK(!codegen_->IsLeafMethod());
4033 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004034}
4035
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004036void LocationsBuilderX86::VisitNewArray(HNewArray* instruction) {
4037 LocationSummary* locations =
4038 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4039 locations->SetOut(Location::RegisterLocation(EAX));
4040 InvokeRuntimeCallingConvention calling_convention;
4041 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08004042 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004043 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004044}
4045
4046void InstructionCodeGeneratorX86::VisitNewArray(HNewArray* instruction) {
4047 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004048 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
Roland Levillain4d027112015-07-01 15:41:14 +01004049 // Note: if heap poisoning is enabled, the entry point takes cares
4050 // of poisoning the reference.
Calin Juravle175dc732015-08-25 15:42:32 +01004051 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
4052 instruction,
4053 instruction->GetDexPc(),
4054 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00004055 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004056 DCHECK(!codegen_->IsLeafMethod());
4057}
4058
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004059void LocationsBuilderX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004060 LocationSummary* locations =
4061 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01004062 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
4063 if (location.IsStackSlot()) {
4064 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4065 } else if (location.IsDoubleStackSlot()) {
4066 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004067 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01004068 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004069}
4070
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004071void InstructionCodeGeneratorX86::VisitParameterValue(
4072 HParameterValue* instruction ATTRIBUTE_UNUSED) {
4073}
4074
4075void LocationsBuilderX86::VisitCurrentMethod(HCurrentMethod* instruction) {
4076 LocationSummary* locations =
4077 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4078 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
4079}
4080
4081void InstructionCodeGeneratorX86::VisitCurrentMethod(HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004082}
4083
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004084void LocationsBuilderX86::VisitClassTableGet(HClassTableGet* instruction) {
4085 LocationSummary* locations =
4086 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4087 locations->SetInAt(0, Location::RequiresRegister());
4088 locations->SetOut(Location::RequiresRegister());
4089}
4090
4091void InstructionCodeGeneratorX86::VisitClassTableGet(HClassTableGet* instruction) {
4092 LocationSummary* locations = instruction->GetLocations();
4093 uint32_t method_offset = 0;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004094 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004095 method_offset = mirror::Class::EmbeddedVTableEntryOffset(
4096 instruction->GetIndex(), kX86PointerSize).SizeValue();
4097 } else {
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00004098 __ movl(locations->InAt(0).AsRegister<Register>(),
4099 Address(locations->InAt(0).AsRegister<Register>(),
4100 mirror::Class::ImtPtrOffset(kX86PointerSize).Uint32Value()));
4101 // temp = temp->GetImtEntryAt(method_offset);
4102 method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
4103 instruction->GetIndex() % ImTable::kSize, kX86PointerSize));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004104 }
4105 __ movl(locations->Out().AsRegister<Register>(),
4106 Address(locations->InAt(0).AsRegister<Register>(), method_offset));
4107}
4108
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004109void LocationsBuilderX86::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004110 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004111 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01004112 locations->SetInAt(0, Location::RequiresRegister());
4113 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004114}
4115
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004116void InstructionCodeGeneratorX86::VisitNot(HNot* not_) {
4117 LocationSummary* locations = not_->GetLocations();
Roland Levillain70566432014-10-24 16:20:17 +01004118 Location in = locations->InAt(0);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01004119 Location out = locations->Out();
Roland Levillain70566432014-10-24 16:20:17 +01004120 DCHECK(in.Equals(out));
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00004121 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004122 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00004123 __ notl(out.AsRegister<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004124 break;
4125
4126 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01004127 __ notl(out.AsRegisterPairLow<Register>());
4128 __ notl(out.AsRegisterPairHigh<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004129 break;
4130
4131 default:
4132 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
4133 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004134}
4135
David Brazdil66d126e2015-04-03 16:02:44 +01004136void LocationsBuilderX86::VisitBooleanNot(HBooleanNot* bool_not) {
4137 LocationSummary* locations =
4138 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
4139 locations->SetInAt(0, Location::RequiresRegister());
4140 locations->SetOut(Location::SameAsFirstInput());
4141}
4142
4143void InstructionCodeGeneratorX86::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01004144 LocationSummary* locations = bool_not->GetLocations();
4145 Location in = locations->InAt(0);
4146 Location out = locations->Out();
4147 DCHECK(in.Equals(out));
4148 __ xorl(out.AsRegister<Register>(), Immediate(1));
4149}
4150
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004151void LocationsBuilderX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004152 LocationSummary* locations =
4153 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00004154 switch (compare->InputAt(0)->GetType()) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00004155 case Primitive::kPrimBoolean:
4156 case Primitive::kPrimByte:
4157 case Primitive::kPrimShort:
4158 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08004159 case Primitive::kPrimInt:
Calin Juravleddb7df22014-11-25 20:56:51 +00004160 case Primitive::kPrimLong: {
4161 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravleddb7df22014-11-25 20:56:51 +00004162 locations->SetInAt(1, Location::Any());
4163 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4164 break;
4165 }
4166 case Primitive::kPrimFloat:
4167 case Primitive::kPrimDouble: {
4168 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004169 if (compare->InputAt(1)->IsX86LoadFromConstantTable()) {
4170 DCHECK(compare->InputAt(1)->IsEmittedAtUseSite());
4171 } else if (compare->InputAt(1)->IsConstant()) {
4172 locations->SetInAt(1, Location::RequiresFpuRegister());
4173 } else {
4174 locations->SetInAt(1, Location::Any());
4175 }
Calin Juravleddb7df22014-11-25 20:56:51 +00004176 locations->SetOut(Location::RequiresRegister());
4177 break;
4178 }
4179 default:
4180 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
4181 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004182}
4183
4184void InstructionCodeGeneratorX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004185 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004186 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00004187 Location left = locations->InAt(0);
4188 Location right = locations->InAt(1);
4189
Mark Mendell0c9497d2015-08-21 09:30:05 -04004190 NearLabel less, greater, done;
Aart Bika19616e2016-02-01 18:57:58 -08004191 Condition less_cond = kLess;
4192
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004193 switch (compare->InputAt(0)->GetType()) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00004194 case Primitive::kPrimBoolean:
4195 case Primitive::kPrimByte:
4196 case Primitive::kPrimShort:
4197 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08004198 case Primitive::kPrimInt: {
Mark Mendell0c5b18e2016-02-06 13:58:35 -05004199 GenerateIntCompare(left, right);
Aart Bika19616e2016-02-01 18:57:58 -08004200 break;
4201 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004202 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004203 Register left_low = left.AsRegisterPairLow<Register>();
4204 Register left_high = left.AsRegisterPairHigh<Register>();
4205 int32_t val_low = 0;
4206 int32_t val_high = 0;
4207 bool right_is_const = false;
4208
4209 if (right.IsConstant()) {
4210 DCHECK(right.GetConstant()->IsLongConstant());
4211 right_is_const = true;
4212 int64_t val = right.GetConstant()->AsLongConstant()->GetValue();
4213 val_low = Low32Bits(val);
4214 val_high = High32Bits(val);
4215 }
4216
Calin Juravleddb7df22014-11-25 20:56:51 +00004217 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004218 __ cmpl(left_high, right.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004219 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004220 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004221 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004222 DCHECK(right_is_const) << right;
Aart Bika19616e2016-02-01 18:57:58 -08004223 codegen_->Compare32BitValue(left_high, val_high);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004224 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004225 __ j(kLess, &less); // Signed compare.
4226 __ j(kGreater, &greater); // Signed compare.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004227 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004228 __ cmpl(left_low, right.AsRegisterPairLow<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004229 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004230 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004231 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004232 DCHECK(right_is_const) << right;
Aart Bika19616e2016-02-01 18:57:58 -08004233 codegen_->Compare32BitValue(left_low, val_low);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004234 }
Aart Bika19616e2016-02-01 18:57:58 -08004235 less_cond = kBelow; // for CF (unsigned).
Calin Juravleddb7df22014-11-25 20:56:51 +00004236 break;
4237 }
4238 case Primitive::kPrimFloat: {
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004239 GenerateFPCompare(left, right, compare, false);
Calin Juravleddb7df22014-11-25 20:56:51 +00004240 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08004241 less_cond = kBelow; // for CF (floats).
Calin Juravleddb7df22014-11-25 20:56:51 +00004242 break;
4243 }
4244 case Primitive::kPrimDouble: {
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004245 GenerateFPCompare(left, right, compare, true);
Calin Juravleddb7df22014-11-25 20:56:51 +00004246 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08004247 less_cond = kBelow; // for CF (floats).
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004248 break;
4249 }
4250 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00004251 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004252 }
Aart Bika19616e2016-02-01 18:57:58 -08004253
Calin Juravleddb7df22014-11-25 20:56:51 +00004254 __ movl(out, Immediate(0));
4255 __ j(kEqual, &done);
Aart Bika19616e2016-02-01 18:57:58 -08004256 __ j(less_cond, &less);
Calin Juravleddb7df22014-11-25 20:56:51 +00004257
4258 __ Bind(&greater);
4259 __ movl(out, Immediate(1));
4260 __ jmp(&done);
4261
4262 __ Bind(&less);
4263 __ movl(out, Immediate(-1));
4264
4265 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004266}
4267
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004268void LocationsBuilderX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004269 LocationSummary* locations =
4270 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko372f10e2016-05-17 16:30:10 +01004271 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01004272 locations->SetInAt(i, Location::Any());
4273 }
4274 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004275}
4276
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004277void InstructionCodeGeneratorX86::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004278 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004279}
4280
Roland Levillain7c1559a2015-12-15 10:55:36 +00004281void CodeGeneratorX86::GenerateMemoryBarrier(MemBarrierKind kind) {
Calin Juravle52c48962014-12-16 17:02:57 +00004282 /*
4283 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
4284 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
4285 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
4286 */
4287 switch (kind) {
4288 case MemBarrierKind::kAnyAny: {
Mark P Mendell17077d82015-12-16 19:15:59 +00004289 MemoryFence();
Calin Juravle52c48962014-12-16 17:02:57 +00004290 break;
4291 }
4292 case MemBarrierKind::kAnyStore:
4293 case MemBarrierKind::kLoadAny:
4294 case MemBarrierKind::kStoreStore: {
4295 // nop
4296 break;
4297 }
Mark Mendell7aa04a12016-01-27 22:39:07 -05004298 case MemBarrierKind::kNTStoreStore:
4299 // Non-Temporal Store/Store needs an explicit fence.
4300 MemoryFence(/* non-temporal */ true);
4301 break;
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004302 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004303}
4304
Vladimir Markodc151b22015-10-15 18:02:30 +01004305HInvokeStaticOrDirect::DispatchInfo CodeGeneratorX86::GetSupportedInvokeStaticOrDirectDispatch(
4306 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
4307 MethodReference target_method ATTRIBUTE_UNUSED) {
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00004308 HInvokeStaticOrDirect::DispatchInfo dispatch_info = desired_dispatch_info;
4309
4310 // We disable pc-relative load when there is an irreducible loop, as the optimization
4311 // is incompatible with it.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004312 // TODO: Create as many X86ComputeBaseMethodAddress instructions
4313 // as needed for methods with irreducible loops.
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00004314 if (GetGraph()->HasIrreducibleLoops() &&
4315 (dispatch_info.method_load_kind ==
4316 HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative)) {
4317 dispatch_info.method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod;
4318 }
4319 switch (dispatch_info.code_ptr_location) {
Vladimir Markodc151b22015-10-15 18:02:30 +01004320 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
4321 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
4322 // For direct code, we actually prefer to call via the code pointer from ArtMethod*.
4323 // (Though the direct CALL ptr16:32 is available for consideration).
4324 return HInvokeStaticOrDirect::DispatchInfo {
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00004325 dispatch_info.method_load_kind,
Vladimir Markodc151b22015-10-15 18:02:30 +01004326 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00004327 dispatch_info.method_load_data,
Vladimir Markodc151b22015-10-15 18:02:30 +01004328 0u
4329 };
4330 default:
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00004331 return dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01004332 }
4333}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004334
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004335Register CodeGeneratorX86::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
4336 Register temp) {
4337 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
Vladimir Markoc53c0792015-11-19 15:48:33 +00004338 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004339 if (!invoke->GetLocations()->Intrinsified()) {
4340 return location.AsRegister<Register>();
4341 }
4342 // For intrinsics we allow any location, so it may be on the stack.
4343 if (!location.IsRegister()) {
4344 __ movl(temp, Address(ESP, location.GetStackIndex()));
4345 return temp;
4346 }
4347 // For register locations, check if the register was saved. If so, get it from the stack.
4348 // Note: There is a chance that the register was saved but not overwritten, so we could
4349 // save one load. However, since this is just an intrinsic slow path we prefer this
4350 // simple and more robust approach rather that trying to determine if that's the case.
4351 SlowPathCode* slow_path = GetCurrentSlowPath();
Serguei Katkov288c7a82016-05-16 11:53:15 +06004352 if (slow_path != nullptr) {
4353 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
4354 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
4355 __ movl(temp, Address(ESP, stack_offset));
4356 return temp;
4357 }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004358 }
4359 return location.AsRegister<Register>();
4360}
4361
Serguei Katkov288c7a82016-05-16 11:53:15 +06004362Location CodeGeneratorX86::GenerateCalleeMethodStaticOrDirectCall(HInvokeStaticOrDirect* invoke,
4363 Location temp) {
Vladimir Marko58155012015-08-19 12:49:41 +00004364 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
4365 switch (invoke->GetMethodLoadKind()) {
4366 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
4367 // temp = thread->string_init_entrypoint
4368 __ fs()->movl(temp.AsRegister<Register>(), Address::Absolute(invoke->GetStringInitOffset()));
4369 break;
4370 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00004371 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004372 break;
4373 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
4374 __ movl(temp.AsRegister<Register>(), Immediate(invoke->GetMethodAddress()));
4375 break;
4376 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004377 __ movl(temp.AsRegister<Register>(), Immediate(/* placeholder */ 0));
Vladimir Marko58155012015-08-19 12:49:41 +00004378 method_patches_.emplace_back(invoke->GetTargetMethod());
4379 __ Bind(&method_patches_.back().label); // Bind the label at the end of the "movl" insn.
4380 break;
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004381 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
4382 Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
4383 temp.AsRegister<Register>());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004384 __ movl(temp.AsRegister<Register>(), Address(base_reg, kDummy32BitOffset));
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004385 // Bind a new fixup label at the end of the "movl" insn.
4386 uint32_t offset = invoke->GetDexCacheArrayOffset();
4387 __ Bind(NewPcRelativeDexCacheArrayPatch(*invoke->GetTargetMethod().dex_file, offset));
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004388 break;
4389 }
Vladimir Marko58155012015-08-19 12:49:41 +00004390 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004391 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004392 Register method_reg;
4393 Register reg = temp.AsRegister<Register>();
4394 if (current_method.IsRegister()) {
4395 method_reg = current_method.AsRegister<Register>();
4396 } else {
David Brazdil58282f42016-01-14 12:45:10 +00004397 DCHECK(invoke->GetLocations()->Intrinsified());
Vladimir Marko58155012015-08-19 12:49:41 +00004398 DCHECK(!current_method.IsValid());
4399 method_reg = reg;
4400 __ movl(reg, Address(ESP, kCurrentMethodStackOffset));
4401 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00004402 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +01004403 __ movl(reg, Address(method_reg,
4404 ArtMethod::DexCacheResolvedMethodsOffset(kX86PointerSize).Int32Value()));
Vladimir Marko40ecb122016-04-06 17:33:41 +01004405 // temp = temp[index_in_cache];
4406 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
4407 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Vladimir Marko58155012015-08-19 12:49:41 +00004408 __ movl(reg, Address(reg, CodeGenerator::GetCachePointerOffset(index_in_cache)));
4409 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +01004410 }
Vladimir Marko58155012015-08-19 12:49:41 +00004411 }
Serguei Katkov288c7a82016-05-16 11:53:15 +06004412 return callee_method;
4413}
4414
4415void CodeGeneratorX86::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
4416 Location callee_method = GenerateCalleeMethodStaticOrDirectCall(invoke, temp);
Vladimir Marko58155012015-08-19 12:49:41 +00004417
4418 switch (invoke->GetCodePtrLocation()) {
4419 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
4420 __ call(GetFrameEntryLabel());
4421 break;
4422 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative: {
4423 relative_call_patches_.emplace_back(invoke->GetTargetMethod());
4424 Label* label = &relative_call_patches_.back().label;
4425 __ call(label); // Bind to the patch label, override at link time.
4426 __ Bind(label); // Bind the label at the end of the "call" insn.
4427 break;
4428 }
4429 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
4430 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
Vladimir Markodc151b22015-10-15 18:02:30 +01004431 // Filtered out by GetSupportedInvokeStaticOrDirectDispatch().
4432 LOG(FATAL) << "Unsupported";
4433 UNREACHABLE();
Vladimir Marko58155012015-08-19 12:49:41 +00004434 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
4435 // (callee_method + offset_of_quick_compiled_code)()
4436 __ call(Address(callee_method.AsRegister<Register>(),
4437 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
4438 kX86WordSize).Int32Value()));
4439 break;
Mark Mendell09ed1a32015-03-25 08:30:06 -04004440 }
4441
4442 DCHECK(!IsLeafMethod());
Mark Mendell09ed1a32015-03-25 08:30:06 -04004443}
4444
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004445void CodeGeneratorX86::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_in) {
4446 Register temp = temp_in.AsRegister<Register>();
4447 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
4448 invoke->GetVTableIndex(), kX86PointerSize).Uint32Value();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004449
4450 // Use the calling convention instead of the location of the receiver, as
4451 // intrinsics may have put the receiver in a different register. In the intrinsics
4452 // slow path, the arguments have been moved to the right place, so here we are
4453 // guaranteed that the receiver is the first register of the calling convention.
4454 InvokeDexCallingConvention calling_convention;
4455 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004456 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004457 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004458 __ movl(temp, Address(receiver, class_offset));
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004459 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00004460 // Instead of simply (possibly) unpoisoning `temp` here, we should
4461 // emit a read barrier for the previous class reference load.
4462 // However this is not required in practice, as this is an
4463 // intermediate/temporary reference and because the current
4464 // concurrent copying collector keeps the from-space memory
4465 // intact/accessible until the end of the marking phase (the
4466 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004467 __ MaybeUnpoisonHeapReference(temp);
4468 // temp = temp->GetMethodAt(method_offset);
4469 __ movl(temp, Address(temp, method_offset));
4470 // call temp->GetEntryPoint();
4471 __ call(Address(
4472 temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize).Int32Value()));
4473}
4474
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004475void CodeGeneratorX86::RecordSimplePatch() {
4476 if (GetCompilerOptions().GetIncludePatchInformation()) {
4477 simple_patches_.emplace_back();
4478 __ Bind(&simple_patches_.back());
4479 }
4480}
4481
4482void CodeGeneratorX86::RecordStringPatch(HLoadString* load_string) {
4483 string_patches_.emplace_back(load_string->GetDexFile(), load_string->GetStringIndex());
4484 __ Bind(&string_patches_.back().label);
4485}
4486
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004487void CodeGeneratorX86::RecordTypePatch(HLoadClass* load_class) {
4488 type_patches_.emplace_back(load_class->GetDexFile(), load_class->GetTypeIndex());
4489 __ Bind(&type_patches_.back().label);
4490}
4491
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004492Label* CodeGeneratorX86::NewPcRelativeDexCacheArrayPatch(const DexFile& dex_file,
4493 uint32_t element_offset) {
4494 // Add the patch entry and bind its label at the end of the instruction.
4495 pc_relative_dex_cache_patches_.emplace_back(dex_file, element_offset);
4496 return &pc_relative_dex_cache_patches_.back().label;
4497}
4498
Vladimir Marko58155012015-08-19 12:49:41 +00004499void CodeGeneratorX86::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
4500 DCHECK(linker_patches->empty());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004501 size_t size =
4502 method_patches_.size() +
4503 relative_call_patches_.size() +
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004504 pc_relative_dex_cache_patches_.size() +
4505 simple_patches_.size() +
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004506 string_patches_.size() +
4507 type_patches_.size();
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004508 linker_patches->reserve(size);
4509 // The label points to the end of the "movl" insn but the literal offset for method
4510 // patch needs to point to the embedded constant which occupies the last 4 bytes.
4511 constexpr uint32_t kLabelPositionToLiteralOffsetAdjustment = 4u;
Vladimir Marko58155012015-08-19 12:49:41 +00004512 for (const MethodPatchInfo<Label>& info : method_patches_) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004513 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Marko58155012015-08-19 12:49:41 +00004514 linker_patches->push_back(LinkerPatch::MethodPatch(literal_offset,
4515 info.target_method.dex_file,
4516 info.target_method.dex_method_index));
4517 }
4518 for (const MethodPatchInfo<Label>& info : relative_call_patches_) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004519 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Marko58155012015-08-19 12:49:41 +00004520 linker_patches->push_back(LinkerPatch::RelativeCodePatch(literal_offset,
4521 info.target_method.dex_file,
4522 info.target_method.dex_method_index));
4523 }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004524 for (const PcRelativeDexCacheAccessInfo& info : pc_relative_dex_cache_patches_) {
4525 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
4526 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(literal_offset,
4527 &info.target_dex_file,
4528 GetMethodAddressOffset(),
4529 info.element_offset));
4530 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004531 for (const Label& label : simple_patches_) {
4532 uint32_t literal_offset = label.Position() - kLabelPositionToLiteralOffsetAdjustment;
4533 linker_patches->push_back(LinkerPatch::RecordPosition(literal_offset));
4534 }
4535 if (GetCompilerOptions().GetCompilePic()) {
4536 for (const StringPatchInfo<Label>& info : string_patches_) {
4537 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
4538 linker_patches->push_back(LinkerPatch::RelativeStringPatch(literal_offset,
4539 &info.dex_file,
4540 GetMethodAddressOffset(),
4541 info.string_index));
4542 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004543 for (const TypePatchInfo<Label>& info : type_patches_) {
4544 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
4545 linker_patches->push_back(LinkerPatch::RelativeTypePatch(literal_offset,
4546 &info.dex_file,
4547 GetMethodAddressOffset(),
4548 info.type_index));
4549 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004550 } else {
4551 for (const StringPatchInfo<Label>& info : string_patches_) {
4552 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
4553 linker_patches->push_back(LinkerPatch::StringPatch(literal_offset,
4554 &info.dex_file,
4555 info.string_index));
4556 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004557 for (const TypePatchInfo<Label>& info : type_patches_) {
4558 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
4559 linker_patches->push_back(LinkerPatch::TypePatch(literal_offset,
4560 &info.dex_file,
4561 info.type_index));
4562 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004563 }
Vladimir Marko58155012015-08-19 12:49:41 +00004564}
4565
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004566void CodeGeneratorX86::MarkGCCard(Register temp,
4567 Register card,
4568 Register object,
4569 Register value,
4570 bool value_can_be_null) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004571 NearLabel is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004572 if (value_can_be_null) {
4573 __ testl(value, value);
4574 __ j(kEqual, &is_null);
4575 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004576 __ fs()->movl(card, Address::Absolute(Thread::CardTableOffset<kX86WordSize>().Int32Value()));
4577 __ movl(temp, object);
4578 __ shrl(temp, Immediate(gc::accounting::CardTable::kCardShift));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00004579 __ movb(Address(temp, card, TIMES_1, 0),
4580 X86ManagedRegister::FromCpuRegister(card).AsByteRegister());
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004581 if (value_can_be_null) {
4582 __ Bind(&is_null);
4583 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004584}
4585
Calin Juravle52c48962014-12-16 17:02:57 +00004586void LocationsBuilderX86::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
4587 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain0d5a2812015-11-13 10:07:31 +00004588
4589 bool object_field_get_with_read_barrier =
4590 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004591 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00004592 new (GetGraph()->GetArena()) LocationSummary(instruction,
4593 kEmitCompilerReadBarrier ?
4594 LocationSummary::kCallOnSlowPath :
4595 LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004596 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004597
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004598 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4599 locations->SetOut(Location::RequiresFpuRegister());
4600 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004601 // The output overlaps in case of long: we don't want the low move
4602 // to overwrite the object's location. Likewise, in the case of
4603 // an object field get with read barriers enabled, we do not want
4604 // the move to overwrite the object's location, as we need it to emit
4605 // the read barrier.
4606 locations->SetOut(
4607 Location::RequiresRegister(),
4608 (object_field_get_with_read_barrier || instruction->GetType() == Primitive::kPrimLong) ?
4609 Location::kOutputOverlap :
4610 Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004611 }
Calin Juravle52c48962014-12-16 17:02:57 +00004612
4613 if (field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong)) {
4614 // Long values can be loaded atomically into an XMM using movsd.
Roland Levillain7c1559a2015-12-15 10:55:36 +00004615 // So we use an XMM register as a temp to achieve atomicity (first
4616 // load the temp into the XMM and then copy the XMM into the
4617 // output, 32 bits at a time).
Calin Juravle52c48962014-12-16 17:02:57 +00004618 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain7c1559a2015-12-15 10:55:36 +00004619 } else if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
4620 // We need a temporary register for the read barrier marking slow
4621 // path in CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier.
4622 locations->AddTemp(Location::RequiresRegister());
Calin Juravle52c48962014-12-16 17:02:57 +00004623 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004624}
4625
Calin Juravle52c48962014-12-16 17:02:57 +00004626void InstructionCodeGeneratorX86::HandleFieldGet(HInstruction* instruction,
4627 const FieldInfo& field_info) {
4628 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004629
Calin Juravle52c48962014-12-16 17:02:57 +00004630 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004631 Location base_loc = locations->InAt(0);
4632 Register base = base_loc.AsRegister<Register>();
Calin Juravle52c48962014-12-16 17:02:57 +00004633 Location out = locations->Out();
4634 bool is_volatile = field_info.IsVolatile();
4635 Primitive::Type field_type = field_info.GetFieldType();
4636 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4637
4638 switch (field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004639 case Primitive::kPrimBoolean: {
Calin Juravle52c48962014-12-16 17:02:57 +00004640 __ movzxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004641 break;
4642 }
4643
4644 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00004645 __ movsxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004646 break;
4647 }
4648
4649 case Primitive::kPrimShort: {
Calin Juravle52c48962014-12-16 17:02:57 +00004650 __ movsxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004651 break;
4652 }
4653
4654 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00004655 __ movzxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004656 break;
4657 }
4658
4659 case Primitive::kPrimInt:
Calin Juravle52c48962014-12-16 17:02:57 +00004660 __ movl(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004661 break;
Roland Levillain7c1559a2015-12-15 10:55:36 +00004662
4663 case Primitive::kPrimNot: {
4664 // /* HeapReference<Object> */ out = *(base + offset)
4665 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
4666 Location temp_loc = locations->GetTemp(0);
4667 // Note that a potential implicit null check is handled in this
4668 // CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier call.
4669 codegen_->GenerateFieldLoadWithBakerReadBarrier(
4670 instruction, out, base, offset, temp_loc, /* needs_null_check */ true);
4671 if (is_volatile) {
4672 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4673 }
4674 } else {
4675 __ movl(out.AsRegister<Register>(), Address(base, offset));
4676 codegen_->MaybeRecordImplicitNullCheck(instruction);
4677 if (is_volatile) {
4678 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4679 }
4680 // If read barriers are enabled, emit read barriers other than
4681 // Baker's using a slow path (and also unpoison the loaded
4682 // reference, if heap poisoning is enabled).
4683 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
4684 }
4685 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004686 }
4687
4688 case Primitive::kPrimLong: {
Calin Juravle52c48962014-12-16 17:02:57 +00004689 if (is_volatile) {
4690 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4691 __ movsd(temp, Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004692 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004693 __ movd(out.AsRegisterPairLow<Register>(), temp);
4694 __ psrlq(temp, Immediate(32));
4695 __ movd(out.AsRegisterPairHigh<Register>(), temp);
4696 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004697 DCHECK_NE(base, out.AsRegisterPairLow<Register>());
Calin Juravle52c48962014-12-16 17:02:57 +00004698 __ movl(out.AsRegisterPairLow<Register>(), Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004699 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004700 __ movl(out.AsRegisterPairHigh<Register>(), Address(base, kX86WordSize + offset));
4701 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004702 break;
4703 }
4704
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004705 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00004706 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004707 break;
4708 }
4709
4710 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00004711 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004712 break;
4713 }
4714
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004715 case Primitive::kPrimVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00004716 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004717 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004718 }
Calin Juravle52c48962014-12-16 17:02:57 +00004719
Roland Levillain7c1559a2015-12-15 10:55:36 +00004720 if (field_type == Primitive::kPrimNot || field_type == Primitive::kPrimLong) {
4721 // Potential implicit null checks, in the case of reference or
4722 // long fields, are handled in the previous switch statement.
4723 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00004724 codegen_->MaybeRecordImplicitNullCheck(instruction);
4725 }
4726
Calin Juravle52c48962014-12-16 17:02:57 +00004727 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004728 if (field_type == Primitive::kPrimNot) {
4729 // Memory barriers, in the case of references, are also handled
4730 // in the previous switch statement.
4731 } else {
4732 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4733 }
Roland Levillain4d027112015-07-01 15:41:14 +01004734 }
Calin Juravle52c48962014-12-16 17:02:57 +00004735}
4736
4737void LocationsBuilderX86::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
4738 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4739
4740 LocationSummary* locations =
4741 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4742 locations->SetInAt(0, Location::RequiresRegister());
4743 bool is_volatile = field_info.IsVolatile();
4744 Primitive::Type field_type = field_info.GetFieldType();
4745 bool is_byte_type = (field_type == Primitive::kPrimBoolean)
4746 || (field_type == Primitive::kPrimByte);
4747
4748 // The register allocator does not support multiple
4749 // inputs that die at entry with one in a specific register.
4750 if (is_byte_type) {
4751 // Ensure the value is in a byte register.
4752 locations->SetInAt(1, Location::RegisterLocation(EAX));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004753 } else if (Primitive::IsFloatingPointType(field_type)) {
Mark Mendell81489372015-11-04 11:30:41 -05004754 if (is_volatile && field_type == Primitive::kPrimDouble) {
4755 // In order to satisfy the semantics of volatile, this must be a single instruction store.
4756 locations->SetInAt(1, Location::RequiresFpuRegister());
4757 } else {
4758 locations->SetInAt(1, Location::FpuRegisterOrConstant(instruction->InputAt(1)));
4759 }
4760 } else if (is_volatile && field_type == Primitive::kPrimLong) {
4761 // In order to satisfy the semantics of volatile, this must be a single instruction store.
Calin Juravle52c48962014-12-16 17:02:57 +00004762 locations->SetInAt(1, Location::RequiresRegister());
Mark Mendell81489372015-11-04 11:30:41 -05004763
Calin Juravle52c48962014-12-16 17:02:57 +00004764 // 64bits value can be atomically written to an address with movsd and an XMM register.
4765 // We need two XMM registers because there's no easier way to (bit) copy a register pair
4766 // into a single XMM register (we copy each pair part into the XMMs and then interleave them).
4767 // NB: We could make the register allocator understand fp_reg <-> core_reg moves but given the
4768 // isolated cases when we need this it isn't worth adding the extra complexity.
4769 locations->AddTemp(Location::RequiresFpuRegister());
4770 locations->AddTemp(Location::RequiresFpuRegister());
Mark Mendell81489372015-11-04 11:30:41 -05004771 } else {
4772 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4773
4774 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
4775 // Temporary registers for the write barrier.
4776 locations->AddTemp(Location::RequiresRegister()); // May be used for reference poisoning too.
4777 // Ensure the card is in a byte register.
4778 locations->AddTemp(Location::RegisterLocation(ECX));
4779 }
Calin Juravle52c48962014-12-16 17:02:57 +00004780 }
4781}
4782
4783void InstructionCodeGeneratorX86::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004784 const FieldInfo& field_info,
4785 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00004786 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4787
4788 LocationSummary* locations = instruction->GetLocations();
4789 Register base = locations->InAt(0).AsRegister<Register>();
4790 Location value = locations->InAt(1);
4791 bool is_volatile = field_info.IsVolatile();
4792 Primitive::Type field_type = field_info.GetFieldType();
4793 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01004794 bool needs_write_barrier =
4795 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00004796
4797 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004798 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
Calin Juravle52c48962014-12-16 17:02:57 +00004799 }
4800
Mark Mendell81489372015-11-04 11:30:41 -05004801 bool maybe_record_implicit_null_check_done = false;
4802
Calin Juravle52c48962014-12-16 17:02:57 +00004803 switch (field_type) {
4804 case Primitive::kPrimBoolean:
4805 case Primitive::kPrimByte: {
4806 __ movb(Address(base, offset), value.AsRegister<ByteRegister>());
4807 break;
4808 }
4809
4810 case Primitive::kPrimShort:
4811 case Primitive::kPrimChar: {
Mark Mendell81489372015-11-04 11:30:41 -05004812 if (value.IsConstant()) {
4813 int16_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4814 __ movw(Address(base, offset), Immediate(v));
4815 } else {
4816 __ movw(Address(base, offset), value.AsRegister<Register>());
4817 }
Calin Juravle52c48962014-12-16 17:02:57 +00004818 break;
4819 }
4820
4821 case Primitive::kPrimInt:
4822 case Primitive::kPrimNot: {
Roland Levillain4d027112015-07-01 15:41:14 +01004823 if (kPoisonHeapReferences && needs_write_barrier) {
4824 // Note that in the case where `value` is a null reference,
4825 // we do not enter this block, as the reference does not
4826 // need poisoning.
4827 DCHECK_EQ(field_type, Primitive::kPrimNot);
4828 Register temp = locations->GetTemp(0).AsRegister<Register>();
4829 __ movl(temp, value.AsRegister<Register>());
4830 __ PoisonHeapReference(temp);
4831 __ movl(Address(base, offset), temp);
Mark Mendell81489372015-11-04 11:30:41 -05004832 } else if (value.IsConstant()) {
4833 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4834 __ movl(Address(base, offset), Immediate(v));
Roland Levillain4d027112015-07-01 15:41:14 +01004835 } else {
Nicolas Geoffray03971632016-03-17 10:44:24 +00004836 DCHECK(value.IsRegister()) << value;
Roland Levillain4d027112015-07-01 15:41:14 +01004837 __ movl(Address(base, offset), value.AsRegister<Register>());
4838 }
Calin Juravle52c48962014-12-16 17:02:57 +00004839 break;
4840 }
4841
4842 case Primitive::kPrimLong: {
4843 if (is_volatile) {
4844 XmmRegister temp1 = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4845 XmmRegister temp2 = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
4846 __ movd(temp1, value.AsRegisterPairLow<Register>());
4847 __ movd(temp2, value.AsRegisterPairHigh<Register>());
4848 __ punpckldq(temp1, temp2);
4849 __ movsd(Address(base, offset), temp1);
Calin Juravle77520bc2015-01-12 18:45:46 +00004850 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell81489372015-11-04 11:30:41 -05004851 } else if (value.IsConstant()) {
4852 int64_t v = CodeGenerator::GetInt64ValueOf(value.GetConstant());
4853 __ movl(Address(base, offset), Immediate(Low32Bits(v)));
4854 codegen_->MaybeRecordImplicitNullCheck(instruction);
4855 __ movl(Address(base, kX86WordSize + offset), Immediate(High32Bits(v)));
Calin Juravle52c48962014-12-16 17:02:57 +00004856 } else {
4857 __ movl(Address(base, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00004858 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004859 __ movl(Address(base, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
4860 }
Mark Mendell81489372015-11-04 11:30:41 -05004861 maybe_record_implicit_null_check_done = true;
Calin Juravle52c48962014-12-16 17:02:57 +00004862 break;
4863 }
4864
4865 case Primitive::kPrimFloat: {
Mark Mendell81489372015-11-04 11:30:41 -05004866 if (value.IsConstant()) {
4867 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4868 __ movl(Address(base, offset), Immediate(v));
4869 } else {
4870 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4871 }
Calin Juravle52c48962014-12-16 17:02:57 +00004872 break;
4873 }
4874
4875 case Primitive::kPrimDouble: {
Mark Mendell81489372015-11-04 11:30:41 -05004876 if (value.IsConstant()) {
4877 int64_t v = CodeGenerator::GetInt64ValueOf(value.GetConstant());
4878 __ movl(Address(base, offset), Immediate(Low32Bits(v)));
4879 codegen_->MaybeRecordImplicitNullCheck(instruction);
4880 __ movl(Address(base, kX86WordSize + offset), Immediate(High32Bits(v)));
4881 maybe_record_implicit_null_check_done = true;
4882 } else {
4883 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4884 }
Calin Juravle52c48962014-12-16 17:02:57 +00004885 break;
4886 }
4887
4888 case Primitive::kPrimVoid:
4889 LOG(FATAL) << "Unreachable type " << field_type;
4890 UNREACHABLE();
4891 }
4892
Mark Mendell81489372015-11-04 11:30:41 -05004893 if (!maybe_record_implicit_null_check_done) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004894 codegen_->MaybeRecordImplicitNullCheck(instruction);
4895 }
4896
Roland Levillain4d027112015-07-01 15:41:14 +01004897 if (needs_write_barrier) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004898 Register temp = locations->GetTemp(0).AsRegister<Register>();
4899 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004900 codegen_->MarkGCCard(temp, card, base, value.AsRegister<Register>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00004901 }
4902
Calin Juravle52c48962014-12-16 17:02:57 +00004903 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004904 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
Calin Juravle52c48962014-12-16 17:02:57 +00004905 }
4906}
4907
4908void LocationsBuilderX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4909 HandleFieldGet(instruction, instruction->GetFieldInfo());
4910}
4911
4912void InstructionCodeGeneratorX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4913 HandleFieldGet(instruction, instruction->GetFieldInfo());
4914}
4915
4916void LocationsBuilderX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
4917 HandleFieldSet(instruction, instruction->GetFieldInfo());
4918}
4919
4920void InstructionCodeGeneratorX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004921 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00004922}
4923
4924void LocationsBuilderX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
4925 HandleFieldSet(instruction, instruction->GetFieldInfo());
4926}
4927
4928void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004929 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00004930}
4931
4932void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4933 HandleFieldGet(instruction, instruction->GetFieldInfo());
4934}
4935
4936void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4937 HandleFieldGet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004938}
4939
Calin Juravlee460d1d2015-09-29 04:52:17 +01004940void LocationsBuilderX86::VisitUnresolvedInstanceFieldGet(
4941 HUnresolvedInstanceFieldGet* instruction) {
4942 FieldAccessCallingConventionX86 calling_convention;
4943 codegen_->CreateUnresolvedFieldLocationSummary(
4944 instruction, instruction->GetFieldType(), calling_convention);
4945}
4946
4947void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldGet(
4948 HUnresolvedInstanceFieldGet* instruction) {
4949 FieldAccessCallingConventionX86 calling_convention;
4950 codegen_->GenerateUnresolvedFieldAccess(instruction,
4951 instruction->GetFieldType(),
4952 instruction->GetFieldIndex(),
4953 instruction->GetDexPc(),
4954 calling_convention);
4955}
4956
4957void LocationsBuilderX86::VisitUnresolvedInstanceFieldSet(
4958 HUnresolvedInstanceFieldSet* instruction) {
4959 FieldAccessCallingConventionX86 calling_convention;
4960 codegen_->CreateUnresolvedFieldLocationSummary(
4961 instruction, instruction->GetFieldType(), calling_convention);
4962}
4963
4964void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldSet(
4965 HUnresolvedInstanceFieldSet* instruction) {
4966 FieldAccessCallingConventionX86 calling_convention;
4967 codegen_->GenerateUnresolvedFieldAccess(instruction,
4968 instruction->GetFieldType(),
4969 instruction->GetFieldIndex(),
4970 instruction->GetDexPc(),
4971 calling_convention);
4972}
4973
4974void LocationsBuilderX86::VisitUnresolvedStaticFieldGet(
4975 HUnresolvedStaticFieldGet* instruction) {
4976 FieldAccessCallingConventionX86 calling_convention;
4977 codegen_->CreateUnresolvedFieldLocationSummary(
4978 instruction, instruction->GetFieldType(), calling_convention);
4979}
4980
4981void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldGet(
4982 HUnresolvedStaticFieldGet* instruction) {
4983 FieldAccessCallingConventionX86 calling_convention;
4984 codegen_->GenerateUnresolvedFieldAccess(instruction,
4985 instruction->GetFieldType(),
4986 instruction->GetFieldIndex(),
4987 instruction->GetDexPc(),
4988 calling_convention);
4989}
4990
4991void LocationsBuilderX86::VisitUnresolvedStaticFieldSet(
4992 HUnresolvedStaticFieldSet* instruction) {
4993 FieldAccessCallingConventionX86 calling_convention;
4994 codegen_->CreateUnresolvedFieldLocationSummary(
4995 instruction, instruction->GetFieldType(), calling_convention);
4996}
4997
4998void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldSet(
4999 HUnresolvedStaticFieldSet* instruction) {
5000 FieldAccessCallingConventionX86 calling_convention;
5001 codegen_->GenerateUnresolvedFieldAccess(instruction,
5002 instruction->GetFieldType(),
5003 instruction->GetFieldIndex(),
5004 instruction->GetDexPc(),
5005 calling_convention);
5006}
5007
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005008void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00005009 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
5010 ? LocationSummary::kCallOnSlowPath
5011 : LocationSummary::kNoCall;
5012 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
5013 Location loc = codegen_->IsImplicitNullCheckAllowed(instruction)
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005014 ? Location::RequiresRegister()
5015 : Location::Any();
5016 locations->SetInAt(0, loc);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005017 if (instruction->HasUses()) {
5018 locations->SetOut(Location::SameAsFirstInput());
5019 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005020}
5021
Calin Juravle2ae48182016-03-16 14:05:09 +00005022void CodeGeneratorX86::GenerateImplicitNullCheck(HNullCheck* instruction) {
5023 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005024 return;
5025 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005026 LocationSummary* locations = instruction->GetLocations();
5027 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00005028
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005029 __ testl(EAX, Address(obj.AsRegister<Register>(), 0));
Calin Juravle2ae48182016-03-16 14:05:09 +00005030 RecordPcInfo(instruction, instruction->GetDexPc());
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005031}
5032
Calin Juravle2ae48182016-03-16 14:05:09 +00005033void CodeGeneratorX86::GenerateExplicitNullCheck(HNullCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07005034 SlowPathCode* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00005035 AddSlowPath(slow_path);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005036
5037 LocationSummary* locations = instruction->GetLocations();
5038 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005039
5040 if (obj.IsRegister()) {
Mark Mendell42514f62015-03-31 11:34:22 -04005041 __ testl(obj.AsRegister<Register>(), obj.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005042 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005043 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005044 } else {
5045 DCHECK(obj.IsConstant()) << obj;
David Brazdil77a48ae2015-09-15 12:34:04 +00005046 DCHECK(obj.GetConstant()->IsNullConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005047 __ jmp(slow_path->GetEntryLabel());
5048 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005049 }
5050 __ j(kEqual, slow_path->GetEntryLabel());
5051}
5052
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005053void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00005054 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005055}
5056
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005057void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005058 bool object_array_get_with_read_barrier =
5059 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005060 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00005061 new (GetGraph()->GetArena()) LocationSummary(instruction,
5062 object_array_get_with_read_barrier ?
5063 LocationSummary::kCallOnSlowPath :
5064 LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005065 locations->SetInAt(0, Location::RequiresRegister());
5066 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005067 if (Primitive::IsFloatingPointType(instruction->GetType())) {
5068 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5069 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005070 // The output overlaps in case of long: we don't want the low move
5071 // to overwrite the array's location. Likewise, in the case of an
5072 // object array get with read barriers enabled, we do not want the
5073 // move to overwrite the array's location, as we need it to emit
5074 // the read barrier.
5075 locations->SetOut(
5076 Location::RequiresRegister(),
5077 (instruction->GetType() == Primitive::kPrimLong || object_array_get_with_read_barrier) ?
5078 Location::kOutputOverlap :
5079 Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005080 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00005081 // We need a temporary register for the read barrier marking slow
5082 // path in CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier.
5083 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
5084 locations->AddTemp(Location::RequiresRegister());
5085 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005086}
5087
5088void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
5089 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005090 Location obj_loc = locations->InAt(0);
5091 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005092 Location index = locations->InAt(1);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005093 Location out_loc = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005094 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005095
Calin Juravle77520bc2015-01-12 18:45:46 +00005096 Primitive::Type type = instruction->GetType();
5097 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005098 case Primitive::kPrimBoolean: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005099 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005100 if (index.IsConstant()) {
5101 __ movzxb(out, Address(obj,
5102 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
5103 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005104 __ movzxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005105 }
5106 break;
5107 }
5108
5109 case Primitive::kPrimByte: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005110 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005111 if (index.IsConstant()) {
5112 __ movsxb(out, Address(obj,
5113 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
5114 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005115 __ movsxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005116 }
5117 break;
5118 }
5119
5120 case Primitive::kPrimShort: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005121 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005122 if (index.IsConstant()) {
5123 __ movsxw(out, Address(obj,
5124 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
5125 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005126 __ movsxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005127 }
5128 break;
5129 }
5130
5131 case Primitive::kPrimChar: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005132 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005133 if (index.IsConstant()) {
5134 __ movzxw(out, Address(obj,
5135 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
5136 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005137 __ movzxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005138 }
5139 break;
5140 }
5141
Roland Levillain7c1559a2015-12-15 10:55:36 +00005142 case Primitive::kPrimInt: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005143 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005144 if (index.IsConstant()) {
5145 __ movl(out, Address(obj,
5146 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
5147 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005148 __ movl(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005149 }
5150 break;
5151 }
5152
Roland Levillain7c1559a2015-12-15 10:55:36 +00005153 case Primitive::kPrimNot: {
5154 static_assert(
5155 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
5156 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain7c1559a2015-12-15 10:55:36 +00005157 // /* HeapReference<Object> */ out =
5158 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
5159 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
5160 Location temp = locations->GetTemp(0);
5161 // Note that a potential implicit null check is handled in this
5162 // CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier call.
5163 codegen_->GenerateArrayLoadWithBakerReadBarrier(
5164 instruction, out_loc, obj, data_offset, index, temp, /* needs_null_check */ true);
5165 } else {
5166 Register out = out_loc.AsRegister<Register>();
5167 if (index.IsConstant()) {
5168 uint32_t offset =
5169 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
5170 __ movl(out, Address(obj, offset));
5171 codegen_->MaybeRecordImplicitNullCheck(instruction);
5172 // If read barriers are enabled, emit read barriers other than
5173 // Baker's using a slow path (and also unpoison the loaded
5174 // reference, if heap poisoning is enabled).
5175 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
5176 } else {
5177 __ movl(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
5178 codegen_->MaybeRecordImplicitNullCheck(instruction);
5179 // If read barriers are enabled, emit read barriers other than
5180 // Baker's using a slow path (and also unpoison the loaded
5181 // reference, if heap poisoning is enabled).
5182 codegen_->MaybeGenerateReadBarrierSlow(
5183 instruction, out_loc, out_loc, obj_loc, data_offset, index);
5184 }
5185 }
5186 break;
5187 }
5188
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005189 case Primitive::kPrimLong: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005190 DCHECK_NE(obj, out_loc.AsRegisterPairLow<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005191 if (index.IsConstant()) {
5192 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Roland Levillain7c1559a2015-12-15 10:55:36 +00005193 __ movl(out_loc.AsRegisterPairLow<Register>(), Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005194 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005195 __ movl(out_loc.AsRegisterPairHigh<Register>(), Address(obj, offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005196 } else {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005197 __ movl(out_loc.AsRegisterPairLow<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005198 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005199 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005200 __ movl(out_loc.AsRegisterPairHigh<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005201 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005202 }
5203 break;
5204 }
5205
Mark Mendell7c8d0092015-01-26 11:21:33 -05005206 case Primitive::kPrimFloat: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005207 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Mark Mendell7c8d0092015-01-26 11:21:33 -05005208 if (index.IsConstant()) {
5209 __ movss(out, Address(obj,
5210 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
5211 } else {
5212 __ movss(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
5213 }
5214 break;
5215 }
5216
5217 case Primitive::kPrimDouble: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005218 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Mark Mendell7c8d0092015-01-26 11:21:33 -05005219 if (index.IsConstant()) {
5220 __ movsd(out, Address(obj,
5221 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
5222 } else {
5223 __ movsd(out, Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
5224 }
5225 break;
5226 }
5227
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005228 case Primitive::kPrimVoid:
Calin Juravle77520bc2015-01-12 18:45:46 +00005229 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07005230 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005231 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005232
Roland Levillain7c1559a2015-12-15 10:55:36 +00005233 if (type == Primitive::kPrimNot || type == Primitive::kPrimLong) {
5234 // Potential implicit null checks, in the case of reference or
5235 // long arrays, are handled in the previous switch statement.
5236 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00005237 codegen_->MaybeRecordImplicitNullCheck(instruction);
5238 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005239}
5240
5241void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01005242 Primitive::Type value_type = instruction->GetComponentType();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005243
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005244 bool needs_write_barrier =
5245 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Roland Levillain0d5a2812015-11-13 10:07:31 +00005246 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
5247 bool object_array_set_with_read_barrier =
5248 kEmitCompilerReadBarrier && (value_type == Primitive::kPrimNot);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005249
Nicolas Geoffray39468442014-09-02 15:17:15 +01005250 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
5251 instruction,
Roland Levillain0d5a2812015-11-13 10:07:31 +00005252 (may_need_runtime_call_for_type_check || object_array_set_with_read_barrier) ?
5253 LocationSummary::kCallOnSlowPath :
5254 LocationSummary::kNoCall);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005255
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005256 bool is_byte_type = (value_type == Primitive::kPrimBoolean)
5257 || (value_type == Primitive::kPrimByte);
5258 // We need the inputs to be different than the output in case of long operation.
5259 // In case of a byte operation, the register allocator does not support multiple
5260 // inputs that die at entry with one in a specific register.
5261 locations->SetInAt(0, Location::RequiresRegister());
5262 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
5263 if (is_byte_type) {
5264 // Ensure the value is in a byte register.
5265 locations->SetInAt(2, Location::ByteRegisterOrConstant(EAX, instruction->InputAt(2)));
5266 } else if (Primitive::IsFloatingPointType(value_type)) {
Mark Mendell81489372015-11-04 11:30:41 -05005267 locations->SetInAt(2, Location::FpuRegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005268 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005269 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
5270 }
5271 if (needs_write_barrier) {
5272 // Temporary registers for the write barrier.
5273 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
5274 // Ensure the card is in a byte register.
Roland Levillain4f6b0b52015-11-23 19:29:22 +00005275 locations->AddTemp(Location::RegisterLocation(ECX));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005276 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005277}
5278
5279void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
5280 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005281 Location array_loc = locations->InAt(0);
5282 Register array = array_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005283 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005284 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005285 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005286 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
5287 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5288 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005289 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005290 bool needs_write_barrier =
5291 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005292
5293 switch (value_type) {
5294 case Primitive::kPrimBoolean:
5295 case Primitive::kPrimByte: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005296 uint32_t offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
5297 Address address = index.IsConstant()
5298 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + offset)
5299 : Address(array, index.AsRegister<Register>(), TIMES_1, offset);
5300 if (value.IsRegister()) {
5301 __ movb(address, value.AsRegister<ByteRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005302 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005303 __ movb(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005304 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005305 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005306 break;
5307 }
5308
5309 case Primitive::kPrimShort:
5310 case Primitive::kPrimChar: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005311 uint32_t offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
5312 Address address = index.IsConstant()
5313 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + offset)
5314 : Address(array, index.AsRegister<Register>(), TIMES_2, offset);
5315 if (value.IsRegister()) {
5316 __ movw(address, value.AsRegister<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005317 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005318 __ movw(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005319 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005320 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005321 break;
5322 }
5323
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005324 case Primitive::kPrimNot: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005325 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
5326 Address address = index.IsConstant()
5327 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
5328 : Address(array, index.AsRegister<Register>(), TIMES_4, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005329
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005330 if (!value.IsRegister()) {
5331 // Just setting null.
5332 DCHECK(instruction->InputAt(2)->IsNullConstant());
5333 DCHECK(value.IsConstant()) << value;
5334 __ movl(address, Immediate(0));
Calin Juravle77520bc2015-01-12 18:45:46 +00005335 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005336 DCHECK(!needs_write_barrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005337 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005338 break;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005339 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005340
5341 DCHECK(needs_write_barrier);
5342 Register register_value = value.AsRegister<Register>();
5343 NearLabel done, not_null, do_put;
5344 SlowPathCode* slow_path = nullptr;
5345 Register temp = locations->GetTemp(0).AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005346 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005347 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathX86(instruction);
5348 codegen_->AddSlowPath(slow_path);
5349 if (instruction->GetValueCanBeNull()) {
5350 __ testl(register_value, register_value);
5351 __ j(kNotEqual, &not_null);
5352 __ movl(address, Immediate(0));
5353 codegen_->MaybeRecordImplicitNullCheck(instruction);
5354 __ jmp(&done);
5355 __ Bind(&not_null);
5356 }
5357
Roland Levillain0d5a2812015-11-13 10:07:31 +00005358 if (kEmitCompilerReadBarrier) {
5359 // When read barriers are enabled, the type checking
5360 // instrumentation requires two read barriers:
5361 //
5362 // __ movl(temp2, temp);
5363 // // /* HeapReference<Class> */ temp = temp->component_type_
5364 // __ movl(temp, Address(temp, component_offset));
Roland Levillain7c1559a2015-12-15 10:55:36 +00005365 // codegen_->GenerateReadBarrierSlow(
Roland Levillain0d5a2812015-11-13 10:07:31 +00005366 // instruction, temp_loc, temp_loc, temp2_loc, component_offset);
5367 //
5368 // // /* HeapReference<Class> */ temp2 = register_value->klass_
5369 // __ movl(temp2, Address(register_value, class_offset));
Roland Levillain7c1559a2015-12-15 10:55:36 +00005370 // codegen_->GenerateReadBarrierSlow(
Roland Levillain0d5a2812015-11-13 10:07:31 +00005371 // instruction, temp2_loc, temp2_loc, value, class_offset, temp_loc);
5372 //
5373 // __ cmpl(temp, temp2);
5374 //
5375 // However, the second read barrier may trash `temp`, as it
5376 // is a temporary register, and as such would not be saved
5377 // along with live registers before calling the runtime (nor
5378 // restored afterwards). So in this case, we bail out and
5379 // delegate the work to the array set slow path.
5380 //
5381 // TODO: Extend the register allocator to support a new
5382 // "(locally) live temp" location so as to avoid always
5383 // going into the slow path when read barriers are enabled.
5384 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005385 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005386 // /* HeapReference<Class> */ temp = array->klass_
5387 __ movl(temp, Address(array, class_offset));
5388 codegen_->MaybeRecordImplicitNullCheck(instruction);
5389 __ MaybeUnpoisonHeapReference(temp);
5390
5391 // /* HeapReference<Class> */ temp = temp->component_type_
5392 __ movl(temp, Address(temp, component_offset));
5393 // If heap poisoning is enabled, no need to unpoison `temp`
5394 // nor the object reference in `register_value->klass`, as
5395 // we are comparing two poisoned references.
5396 __ cmpl(temp, Address(register_value, class_offset));
5397
5398 if (instruction->StaticTypeOfArrayIsObjectArray()) {
5399 __ j(kEqual, &do_put);
5400 // If heap poisoning is enabled, the `temp` reference has
5401 // not been unpoisoned yet; unpoison it now.
5402 __ MaybeUnpoisonHeapReference(temp);
5403
5404 // /* HeapReference<Class> */ temp = temp->super_class_
5405 __ movl(temp, Address(temp, super_offset));
5406 // If heap poisoning is enabled, no need to unpoison
5407 // `temp`, as we are comparing against null below.
5408 __ testl(temp, temp);
5409 __ j(kNotEqual, slow_path->GetEntryLabel());
5410 __ Bind(&do_put);
5411 } else {
5412 __ j(kNotEqual, slow_path->GetEntryLabel());
5413 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005414 }
5415 }
5416
5417 if (kPoisonHeapReferences) {
5418 __ movl(temp, register_value);
5419 __ PoisonHeapReference(temp);
5420 __ movl(address, temp);
5421 } else {
5422 __ movl(address, register_value);
5423 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005424 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005425 codegen_->MaybeRecordImplicitNullCheck(instruction);
5426 }
5427
5428 Register card = locations->GetTemp(1).AsRegister<Register>();
5429 codegen_->MarkGCCard(
5430 temp, card, array, value.AsRegister<Register>(), instruction->GetValueCanBeNull());
5431 __ Bind(&done);
5432
5433 if (slow_path != nullptr) {
5434 __ Bind(slow_path->GetExitLabel());
5435 }
5436
5437 break;
5438 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005439
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005440 case Primitive::kPrimInt: {
5441 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
5442 Address address = index.IsConstant()
5443 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
5444 : Address(array, index.AsRegister<Register>(), TIMES_4, offset);
5445 if (value.IsRegister()) {
5446 __ movl(address, value.AsRegister<Register>());
5447 } else {
5448 DCHECK(value.IsConstant()) << value;
5449 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
5450 __ movl(address, Immediate(v));
5451 }
5452 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005453 break;
5454 }
5455
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005456 case Primitive::kPrimLong: {
5457 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005458 if (index.IsConstant()) {
5459 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005460 if (value.IsRegisterPair()) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005461 __ movl(Address(array, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00005462 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005463 __ movl(Address(array, offset + kX86WordSize), value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005464 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005465 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005466 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005467 __ movl(Address(array, offset), Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00005468 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005469 __ movl(Address(array, offset + kX86WordSize), Immediate(High32Bits(val)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005470 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005471 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005472 if (value.IsRegisterPair()) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005473 __ movl(Address(array, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005474 value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00005475 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005476 __ movl(Address(array, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005477 value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005478 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005479 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005480 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005481 __ movl(Address(array, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005482 Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00005483 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005484 __ movl(Address(array, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005485 Immediate(High32Bits(val)));
5486 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005487 }
5488 break;
5489 }
5490
Mark Mendell7c8d0092015-01-26 11:21:33 -05005491 case Primitive::kPrimFloat: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005492 uint32_t offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
5493 Address address = index.IsConstant()
5494 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
5495 : Address(array, index.AsRegister<Register>(), TIMES_4, offset);
Mark Mendell81489372015-11-04 11:30:41 -05005496 if (value.IsFpuRegister()) {
5497 __ movss(address, value.AsFpuRegister<XmmRegister>());
5498 } else {
5499 DCHECK(value.IsConstant());
5500 int32_t v = bit_cast<int32_t, float>(value.GetConstant()->AsFloatConstant()->GetValue());
5501 __ movl(address, Immediate(v));
5502 }
5503 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005504 break;
5505 }
5506
5507 case Primitive::kPrimDouble: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005508 uint32_t offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
5509 Address address = index.IsConstant()
5510 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + offset)
5511 : Address(array, index.AsRegister<Register>(), TIMES_8, offset);
Mark Mendell81489372015-11-04 11:30:41 -05005512 if (value.IsFpuRegister()) {
5513 __ movsd(address, value.AsFpuRegister<XmmRegister>());
5514 } else {
5515 DCHECK(value.IsConstant());
5516 Address address_hi = index.IsConstant() ?
5517 Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) +
5518 offset + kX86WordSize) :
5519 Address(array, index.AsRegister<Register>(), TIMES_8, offset + kX86WordSize);
5520 int64_t v = bit_cast<int64_t, double>(value.GetConstant()->AsDoubleConstant()->GetValue());
5521 __ movl(address, Immediate(Low32Bits(v)));
5522 codegen_->MaybeRecordImplicitNullCheck(instruction);
5523 __ movl(address_hi, Immediate(High32Bits(v)));
5524 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005525 break;
5526 }
5527
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005528 case Primitive::kPrimVoid:
5529 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07005530 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005531 }
5532}
5533
5534void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
5535 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005536 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellee8d9712016-07-12 11:13:15 -04005537 if (!instruction->IsEmittedAtUseSite()) {
5538 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5539 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005540}
5541
5542void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
Mark Mendellee8d9712016-07-12 11:13:15 -04005543 if (instruction->IsEmittedAtUseSite()) {
5544 return;
5545 }
5546
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005547 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01005548 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00005549 Register obj = locations->InAt(0).AsRegister<Register>();
5550 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005551 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005552 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005553}
5554
5555void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00005556 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
5557 ? LocationSummary::kCallOnSlowPath
5558 : LocationSummary::kNoCall;
5559 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Mark Mendellf60c90b2015-03-04 15:12:59 -05005560 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendellee8d9712016-07-12 11:13:15 -04005561 HInstruction* length = instruction->InputAt(1);
5562 if (!length->IsEmittedAtUseSite()) {
5563 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
5564 }
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005565 if (instruction->HasUses()) {
5566 locations->SetOut(Location::SameAsFirstInput());
5567 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005568}
5569
5570void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
5571 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05005572 Location index_loc = locations->InAt(0);
5573 Location length_loc = locations->InAt(1);
Andreas Gampe85b62f22015-09-09 13:15:38 -07005574 SlowPathCode* slow_path =
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01005575 new (GetGraph()->GetArena()) BoundsCheckSlowPathX86(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005576
Mark Mendell99dbd682015-04-22 16:18:52 -04005577 if (length_loc.IsConstant()) {
5578 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
5579 if (index_loc.IsConstant()) {
5580 // BCE will remove the bounds check if we are guarenteed to pass.
5581 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5582 if (index < 0 || index >= length) {
5583 codegen_->AddSlowPath(slow_path);
5584 __ jmp(slow_path->GetEntryLabel());
5585 } else {
5586 // Some optimization after BCE may have generated this, and we should not
5587 // generate a bounds check if it is a valid range.
5588 }
5589 return;
5590 }
5591
5592 // We have to reverse the jump condition because the length is the constant.
5593 Register index_reg = index_loc.AsRegister<Register>();
5594 __ cmpl(index_reg, Immediate(length));
5595 codegen_->AddSlowPath(slow_path);
5596 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005597 } else {
Mark Mendellee8d9712016-07-12 11:13:15 -04005598 HInstruction* array_length = instruction->InputAt(1);
5599 if (array_length->IsEmittedAtUseSite()) {
5600 // Address the length field in the array.
5601 DCHECK(array_length->IsArrayLength());
5602 uint32_t len_offset = CodeGenerator::GetArrayLengthOffset(array_length->AsArrayLength());
5603 Location array_loc = array_length->GetLocations()->InAt(0);
5604 Address array_len(array_loc.AsRegister<Register>(), len_offset);
5605 if (index_loc.IsConstant()) {
5606 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5607 __ cmpl(array_len, Immediate(value));
5608 } else {
5609 __ cmpl(array_len, index_loc.AsRegister<Register>());
5610 }
5611 codegen_->MaybeRecordImplicitNullCheck(array_length);
Mark Mendell99dbd682015-04-22 16:18:52 -04005612 } else {
Mark Mendellee8d9712016-07-12 11:13:15 -04005613 Register length = length_loc.AsRegister<Register>();
5614 if (index_loc.IsConstant()) {
5615 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5616 __ cmpl(length, Immediate(value));
5617 } else {
5618 __ cmpl(length, index_loc.AsRegister<Register>());
5619 }
Mark Mendell99dbd682015-04-22 16:18:52 -04005620 }
5621 codegen_->AddSlowPath(slow_path);
5622 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005623 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005624}
5625
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005626void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005627 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005628}
5629
5630void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005631 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5632}
5633
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005634void LocationsBuilderX86::VisitSuspendCheck(HSuspendCheck* instruction) {
5635 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
5636}
5637
5638void InstructionCodeGeneratorX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005639 HBasicBlock* block = instruction->GetBlock();
5640 if (block->GetLoopInformation() != nullptr) {
5641 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5642 // The back edge will generate the suspend check.
5643 return;
5644 }
5645 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5646 // The goto will generate the suspend check.
5647 return;
5648 }
5649 GenerateSuspendCheck(instruction, nullptr);
5650}
5651
5652void InstructionCodeGeneratorX86::GenerateSuspendCheck(HSuspendCheck* instruction,
5653 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005654 SuspendCheckSlowPathX86* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005655 down_cast<SuspendCheckSlowPathX86*>(instruction->GetSlowPath());
5656 if (slow_path == nullptr) {
5657 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathX86(instruction, successor);
5658 instruction->SetSlowPath(slow_path);
5659 codegen_->AddSlowPath(slow_path);
5660 if (successor != nullptr) {
5661 DCHECK(successor->IsLoopHeader());
5662 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
5663 }
5664 } else {
5665 DCHECK_EQ(slow_path->GetSuccessor(), successor);
5666 }
5667
Roland Levillain7c1559a2015-12-15 10:55:36 +00005668 __ fs()->cmpw(Address::Absolute(Thread::ThreadFlagsOffset<kX86WordSize>().Int32Value()),
5669 Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005670 if (successor == nullptr) {
5671 __ j(kNotEqual, slow_path->GetEntryLabel());
5672 __ Bind(slow_path->GetReturnLabel());
5673 } else {
5674 __ j(kEqual, codegen_->GetLabelOf(successor));
5675 __ jmp(slow_path->GetEntryLabel());
5676 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005677}
5678
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005679X86Assembler* ParallelMoveResolverX86::GetAssembler() const {
5680 return codegen_->GetAssembler();
5681}
5682
Mark Mendell7c8d0092015-01-26 11:21:33 -05005683void ParallelMoveResolverX86::MoveMemoryToMemory32(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005684 ScratchRegisterScope ensure_scratch(
5685 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5686 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5687 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5688 __ movl(temp_reg, Address(ESP, src + stack_offset));
5689 __ movl(Address(ESP, dst + stack_offset), temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005690}
5691
5692void ParallelMoveResolverX86::MoveMemoryToMemory64(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005693 ScratchRegisterScope ensure_scratch(
5694 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5695 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5696 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5697 __ movl(temp_reg, Address(ESP, src + stack_offset));
5698 __ movl(Address(ESP, dst + stack_offset), temp_reg);
5699 __ movl(temp_reg, Address(ESP, src + stack_offset + kX86WordSize));
5700 __ movl(Address(ESP, dst + stack_offset + kX86WordSize), temp_reg);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005701}
5702
5703void ParallelMoveResolverX86::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005704 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005705 Location source = move->GetSource();
5706 Location destination = move->GetDestination();
5707
5708 if (source.IsRegister()) {
5709 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005710 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
David Brazdil74eb1b22015-12-14 11:44:01 +00005711 } else if (destination.IsFpuRegister()) {
5712 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005713 } else {
5714 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00005715 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005716 }
David Brazdil74eb1b22015-12-14 11:44:01 +00005717 } else if (source.IsRegisterPair()) {
5718 size_t elem_size = Primitive::ComponentSize(Primitive::kPrimInt);
5719 // Create stack space for 2 elements.
5720 __ subl(ESP, Immediate(2 * elem_size));
5721 __ movl(Address(ESP, 0), source.AsRegisterPairLow<Register>());
5722 __ movl(Address(ESP, elem_size), source.AsRegisterPairHigh<Register>());
5723 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
5724 // And remove the temporary stack space we allocated.
5725 __ addl(ESP, Immediate(2 * elem_size));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005726 } else if (source.IsFpuRegister()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00005727 if (destination.IsRegister()) {
5728 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
5729 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005730 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
David Brazdil74eb1b22015-12-14 11:44:01 +00005731 } else if (destination.IsRegisterPair()) {
5732 XmmRegister src_reg = source.AsFpuRegister<XmmRegister>();
5733 __ movd(destination.AsRegisterPairLow<Register>(), src_reg);
5734 __ psrlq(src_reg, Immediate(32));
5735 __ movd(destination.AsRegisterPairHigh<Register>(), src_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005736 } else if (destination.IsStackSlot()) {
5737 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
5738 } else {
5739 DCHECK(destination.IsDoubleStackSlot());
5740 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
5741 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005742 } else if (source.IsStackSlot()) {
5743 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005744 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005745 } else if (destination.IsFpuRegister()) {
5746 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005747 } else {
5748 DCHECK(destination.IsStackSlot());
Mark Mendell7c8d0092015-01-26 11:21:33 -05005749 MoveMemoryToMemory32(destination.GetStackIndex(), source.GetStackIndex());
5750 }
5751 } else if (source.IsDoubleStackSlot()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00005752 if (destination.IsRegisterPair()) {
5753 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
5754 __ movl(destination.AsRegisterPairHigh<Register>(),
5755 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
5756 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005757 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
5758 } else {
5759 DCHECK(destination.IsDoubleStackSlot()) << destination;
5760 MoveMemoryToMemory64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005761 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005762 } else if (source.IsConstant()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005763 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00005764 if (constant->IsIntConstant() || constant->IsNullConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05005765 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005766 if (destination.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05005767 if (value == 0) {
5768 __ xorl(destination.AsRegister<Register>(), destination.AsRegister<Register>());
5769 } else {
5770 __ movl(destination.AsRegister<Register>(), Immediate(value));
5771 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005772 } else {
5773 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell09b84632015-02-13 17:48:38 -05005774 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005775 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005776 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005777 float fp_value = constant->AsFloatConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00005778 int32_t value = bit_cast<int32_t, float>(fp_value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005779 Immediate imm(value);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005780 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005781 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
5782 if (value == 0) {
5783 // Easy handling of 0.0.
5784 __ xorps(dest, dest);
5785 } else {
5786 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005787 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5788 Register temp = static_cast<Register>(ensure_scratch.GetRegister());
5789 __ movl(temp, Immediate(value));
5790 __ movd(dest, temp);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005791 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005792 } else {
5793 DCHECK(destination.IsStackSlot()) << destination;
5794 __ movl(Address(ESP, destination.GetStackIndex()), imm);
5795 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005796 } else if (constant->IsLongConstant()) {
5797 int64_t value = constant->AsLongConstant()->GetValue();
5798 int32_t low_value = Low32Bits(value);
5799 int32_t high_value = High32Bits(value);
5800 Immediate low(low_value);
5801 Immediate high(high_value);
5802 if (destination.IsDoubleStackSlot()) {
5803 __ movl(Address(ESP, destination.GetStackIndex()), low);
5804 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
5805 } else {
5806 __ movl(destination.AsRegisterPairLow<Register>(), low);
5807 __ movl(destination.AsRegisterPairHigh<Register>(), high);
5808 }
5809 } else {
5810 DCHECK(constant->IsDoubleConstant());
5811 double dbl_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00005812 int64_t value = bit_cast<int64_t, double>(dbl_value);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005813 int32_t low_value = Low32Bits(value);
5814 int32_t high_value = High32Bits(value);
5815 Immediate low(low_value);
5816 Immediate high(high_value);
5817 if (destination.IsFpuRegister()) {
5818 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
5819 if (value == 0) {
5820 // Easy handling of 0.0.
5821 __ xorpd(dest, dest);
5822 } else {
5823 __ pushl(high);
5824 __ pushl(low);
5825 __ movsd(dest, Address(ESP, 0));
5826 __ addl(ESP, Immediate(8));
5827 }
5828 } else {
5829 DCHECK(destination.IsDoubleStackSlot()) << destination;
5830 __ movl(Address(ESP, destination.GetStackIndex()), low);
5831 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
5832 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005833 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005834 } else {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00005835 LOG(FATAL) << "Unimplemented move: " << destination << " <- " << source;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005836 }
5837}
5838
Mark Mendella5c19ce2015-04-01 12:51:05 -04005839void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005840 Register suggested_scratch = reg == EAX ? EBX : EAX;
5841 ScratchRegisterScope ensure_scratch(
5842 this, reg, suggested_scratch, codegen_->GetNumberOfCoreRegisters());
5843
5844 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5845 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset));
5846 __ movl(Address(ESP, mem + stack_offset), reg);
5847 __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005848}
5849
Mark Mendell7c8d0092015-01-26 11:21:33 -05005850void ParallelMoveResolverX86::Exchange32(XmmRegister reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005851 ScratchRegisterScope ensure_scratch(
5852 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5853
5854 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5855 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5856 __ movl(temp_reg, Address(ESP, mem + stack_offset));
5857 __ movss(Address(ESP, mem + stack_offset), reg);
5858 __ movd(reg, temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005859}
5860
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005861void ParallelMoveResolverX86::Exchange(int mem1, int mem2) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005862 ScratchRegisterScope ensure_scratch1(
5863 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005864
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005865 Register suggested_scratch = ensure_scratch1.GetRegister() == EAX ? EBX : EAX;
5866 ScratchRegisterScope ensure_scratch2(
5867 this, ensure_scratch1.GetRegister(), suggested_scratch, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005868
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005869 int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0;
5870 stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0;
5871 __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset));
5872 __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset));
5873 __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister()));
5874 __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005875}
5876
5877void ParallelMoveResolverX86::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005878 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005879 Location source = move->GetSource();
5880 Location destination = move->GetDestination();
5881
5882 if (source.IsRegister() && destination.IsRegister()) {
Mark Mendell90979812015-07-28 16:41:21 -04005883 // Use XOR swap algorithm to avoid serializing XCHG instruction or using a temporary.
5884 DCHECK_NE(destination.AsRegister<Register>(), source.AsRegister<Register>());
5885 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
5886 __ xorl(source.AsRegister<Register>(), destination.AsRegister<Register>());
5887 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005888 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005889 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005890 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005891 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005892 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
5893 Exchange(destination.GetStackIndex(), source.GetStackIndex());
Mark Mendell7c8d0092015-01-26 11:21:33 -05005894 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
5895 // Use XOR Swap algorithm to avoid a temporary.
5896 DCHECK_NE(source.reg(), destination.reg());
5897 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
5898 __ xorpd(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
5899 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
5900 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
5901 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
5902 } else if (destination.IsFpuRegister() && source.IsStackSlot()) {
5903 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005904 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
5905 // Take advantage of the 16 bytes in the XMM register.
5906 XmmRegister reg = source.AsFpuRegister<XmmRegister>();
5907 Address stack(ESP, destination.GetStackIndex());
5908 // Load the double into the high doubleword.
5909 __ movhpd(reg, stack);
5910
5911 // Store the low double into the destination.
5912 __ movsd(stack, reg);
5913
5914 // Move the high double to the low double.
5915 __ psrldq(reg, Immediate(8));
5916 } else if (destination.IsFpuRegister() && source.IsDoubleStackSlot()) {
5917 // Take advantage of the 16 bytes in the XMM register.
5918 XmmRegister reg = destination.AsFpuRegister<XmmRegister>();
5919 Address stack(ESP, source.GetStackIndex());
5920 // Load the double into the high doubleword.
5921 __ movhpd(reg, stack);
5922
5923 // Store the low double into the destination.
5924 __ movsd(stack, reg);
5925
5926 // Move the high double to the low double.
5927 __ psrldq(reg, Immediate(8));
5928 } else if (destination.IsDoubleStackSlot() && source.IsDoubleStackSlot()) {
5929 Exchange(destination.GetStackIndex(), source.GetStackIndex());
5930 Exchange(destination.GetHighStackIndex(kX86WordSize), source.GetHighStackIndex(kX86WordSize));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005931 } else {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005932 LOG(FATAL) << "Unimplemented: source: " << source << ", destination: " << destination;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005933 }
5934}
5935
5936void ParallelMoveResolverX86::SpillScratch(int reg) {
5937 __ pushl(static_cast<Register>(reg));
5938}
5939
5940void ParallelMoveResolverX86::RestoreScratch(int reg) {
5941 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005942}
5943
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005944HLoadClass::LoadKind CodeGeneratorX86::GetSupportedLoadClassKind(
5945 HLoadClass::LoadKind desired_class_load_kind) {
5946 if (kEmitCompilerReadBarrier) {
5947 switch (desired_class_load_kind) {
5948 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
5949 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
5950 case HLoadClass::LoadKind::kBootImageAddress:
5951 // TODO: Implement for read barrier.
5952 return HLoadClass::LoadKind::kDexCacheViaMethod;
5953 default:
5954 break;
5955 }
5956 }
5957 switch (desired_class_load_kind) {
5958 case HLoadClass::LoadKind::kReferrersClass:
5959 break;
5960 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
5961 DCHECK(!GetCompilerOptions().GetCompilePic());
5962 break;
5963 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
5964 DCHECK(GetCompilerOptions().GetCompilePic());
5965 FALLTHROUGH_INTENDED;
5966 case HLoadClass::LoadKind::kDexCachePcRelative:
5967 DCHECK(!Runtime::Current()->UseJitCompilation()); // Note: boot image is also non-JIT.
5968 // We disable pc-relative load when there is an irreducible loop, as the optimization
5969 // is incompatible with it.
5970 // TODO: Create as many X86ComputeBaseMethodAddress instructions as needed for methods
5971 // with irreducible loops.
5972 if (GetGraph()->HasIrreducibleLoops()) {
5973 return HLoadClass::LoadKind::kDexCacheViaMethod;
5974 }
5975 break;
5976 case HLoadClass::LoadKind::kBootImageAddress:
5977 break;
5978 case HLoadClass::LoadKind::kDexCacheAddress:
5979 DCHECK(Runtime::Current()->UseJitCompilation());
5980 break;
5981 case HLoadClass::LoadKind::kDexCacheViaMethod:
5982 break;
5983 }
5984 return desired_class_load_kind;
5985}
5986
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005987void LocationsBuilderX86::VisitLoadClass(HLoadClass* cls) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005988 if (cls->NeedsAccessCheck()) {
5989 InvokeRuntimeCallingConvention calling_convention;
5990 CodeGenerator::CreateLoadClassLocationSummary(
5991 cls,
5992 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
5993 Location::RegisterLocation(EAX),
5994 /* code_generator_supports_read_barrier */ true);
5995 return;
5996 }
5997
5998 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || kEmitCompilerReadBarrier)
5999 ? LocationSummary::kCallOnSlowPath
6000 : LocationSummary::kNoCall;
6001 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
6002 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
6003 if (load_kind == HLoadClass::LoadKind::kReferrersClass ||
6004 load_kind == HLoadClass::LoadKind::kDexCacheViaMethod ||
6005 load_kind == HLoadClass::LoadKind::kBootImageLinkTimePcRelative ||
6006 load_kind == HLoadClass::LoadKind::kDexCachePcRelative) {
6007 locations->SetInAt(0, Location::RequiresRegister());
6008 }
6009 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006010}
6011
6012void InstructionCodeGeneratorX86::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01006013 LocationSummary* locations = cls->GetLocations();
Calin Juravle98893e12015-10-02 21:05:03 +01006014 if (cls->NeedsAccessCheck()) {
6015 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex());
6016 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pInitializeTypeAndVerifyAccess),
6017 cls,
6018 cls->GetDexPc(),
6019 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00006020 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Calin Juravle580b6092015-10-06 17:35:58 +01006021 return;
6022 }
6023
Roland Levillain0d5a2812015-11-13 10:07:31 +00006024 Location out_loc = locations->Out();
6025 Register out = out_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006026
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006027 bool generate_null_check = false;
6028 switch (cls->GetLoadKind()) {
6029 case HLoadClass::LoadKind::kReferrersClass: {
6030 DCHECK(!cls->CanCallRuntime());
6031 DCHECK(!cls->MustGenerateClinitCheck());
6032 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
6033 Register current_method = locations->InAt(0).AsRegister<Register>();
6034 GenerateGcRootFieldLoad(
6035 cls, out_loc, Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
6036 break;
6037 }
6038 case HLoadClass::LoadKind::kBootImageLinkTimeAddress: {
6039 DCHECK(!kEmitCompilerReadBarrier);
6040 __ movl(out, Immediate(/* placeholder */ 0));
6041 codegen_->RecordTypePatch(cls);
6042 break;
6043 }
6044 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
6045 DCHECK(!kEmitCompilerReadBarrier);
6046 Register method_address = locations->InAt(0).AsRegister<Register>();
6047 __ leal(out, Address(method_address, CodeGeneratorX86::kDummy32BitOffset));
6048 codegen_->RecordTypePatch(cls);
6049 break;
6050 }
6051 case HLoadClass::LoadKind::kBootImageAddress: {
6052 DCHECK(!kEmitCompilerReadBarrier);
6053 DCHECK_NE(cls->GetAddress(), 0u);
6054 uint32_t address = dchecked_integral_cast<uint32_t>(cls->GetAddress());
6055 __ movl(out, Immediate(address));
6056 codegen_->RecordSimplePatch();
6057 break;
6058 }
6059 case HLoadClass::LoadKind::kDexCacheAddress: {
6060 DCHECK_NE(cls->GetAddress(), 0u);
6061 uint32_t address = dchecked_integral_cast<uint32_t>(cls->GetAddress());
6062 // /* GcRoot<mirror::Class> */ out = *address
6063 GenerateGcRootFieldLoad(cls, out_loc, Address::Absolute(address));
6064 generate_null_check = !cls->IsInDexCache();
6065 break;
6066 }
6067 case HLoadClass::LoadKind::kDexCachePcRelative: {
6068 Register base_reg = locations->InAt(0).AsRegister<Register>();
6069 uint32_t offset = cls->GetDexCacheElementOffset();
6070 Label* fixup_label = codegen_->NewPcRelativeDexCacheArrayPatch(cls->GetDexFile(), offset);
6071 // /* GcRoot<mirror::Class> */ out = *(base + offset) /* PC-relative */
6072 GenerateGcRootFieldLoad(
6073 cls, out_loc, Address(base_reg, CodeGeneratorX86::kDummy32BitOffset), fixup_label);
6074 generate_null_check = !cls->IsInDexCache();
6075 break;
6076 }
6077 case HLoadClass::LoadKind::kDexCacheViaMethod: {
6078 // /* GcRoot<mirror::Class>[] */ out =
6079 // current_method.ptr_sized_fields_->dex_cache_resolved_types_
6080 Register current_method = locations->InAt(0).AsRegister<Register>();
6081 __ movl(out, Address(current_method,
6082 ArtMethod::DexCacheResolvedTypesOffset(kX86PointerSize).Int32Value()));
6083 // /* GcRoot<mirror::Class> */ out = out[type_index]
6084 GenerateGcRootFieldLoad(
6085 cls, out_loc, Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
6086 generate_null_check = !cls->IsInDexCache();
6087 break;
6088 }
6089 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006090
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006091 if (generate_null_check || cls->MustGenerateClinitCheck()) {
6092 DCHECK(cls->CanCallRuntime());
6093 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
6094 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
6095 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006096
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006097 if (generate_null_check) {
6098 __ testl(out, out);
6099 __ j(kEqual, slow_path->GetEntryLabel());
6100 }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006101
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006102 if (cls->MustGenerateClinitCheck()) {
6103 GenerateClassInitializationCheck(slow_path, out);
6104 } else {
6105 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006106 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006107 }
6108}
6109
6110void LocationsBuilderX86::VisitClinitCheck(HClinitCheck* check) {
6111 LocationSummary* locations =
6112 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
6113 locations->SetInAt(0, Location::RequiresRegister());
6114 if (check->HasUses()) {
6115 locations->SetOut(Location::SameAsFirstInput());
6116 }
6117}
6118
6119void InstructionCodeGeneratorX86::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006120 // We assume the class to not be null.
Andreas Gampe85b62f22015-09-09 13:15:38 -07006121 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006122 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006123 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00006124 GenerateClassInitializationCheck(slow_path,
6125 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006126}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006127
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006128void InstructionCodeGeneratorX86::GenerateClassInitializationCheck(
Andreas Gampe85b62f22015-09-09 13:15:38 -07006129 SlowPathCode* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006130 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
6131 Immediate(mirror::Class::kStatusInitialized));
6132 __ j(kLess, slow_path->GetEntryLabel());
6133 __ Bind(slow_path->GetExitLabel());
6134 // No need for memory fence, thanks to the X86 memory model.
6135}
6136
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006137HLoadString::LoadKind CodeGeneratorX86::GetSupportedLoadStringKind(
6138 HLoadString::LoadKind desired_string_load_kind) {
6139 if (kEmitCompilerReadBarrier) {
6140 switch (desired_string_load_kind) {
6141 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
6142 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
6143 case HLoadString::LoadKind::kBootImageAddress:
6144 // TODO: Implement for read barrier.
6145 return HLoadString::LoadKind::kDexCacheViaMethod;
6146 default:
6147 break;
6148 }
6149 }
6150 switch (desired_string_load_kind) {
6151 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
6152 DCHECK(!GetCompilerOptions().GetCompilePic());
6153 break;
6154 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
6155 DCHECK(GetCompilerOptions().GetCompilePic());
6156 FALLTHROUGH_INTENDED;
6157 case HLoadString::LoadKind::kDexCachePcRelative:
Calin Juravleffc87072016-04-20 14:22:09 +01006158 DCHECK(!Runtime::Current()->UseJitCompilation()); // Note: boot image is also non-JIT.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006159 // We disable pc-relative load when there is an irreducible loop, as the optimization
6160 // is incompatible with it.
6161 // TODO: Create as many X86ComputeBaseMethodAddress instructions as needed for methods
6162 // with irreducible loops.
6163 if (GetGraph()->HasIrreducibleLoops()) {
6164 return HLoadString::LoadKind::kDexCacheViaMethod;
6165 }
6166 break;
6167 case HLoadString::LoadKind::kBootImageAddress:
6168 break;
6169 case HLoadString::LoadKind::kDexCacheAddress:
Calin Juravleffc87072016-04-20 14:22:09 +01006170 DCHECK(Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006171 break;
6172 case HLoadString::LoadKind::kDexCacheViaMethod:
6173 break;
6174 }
6175 return desired_string_load_kind;
6176}
6177
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006178void LocationsBuilderX86::VisitLoadString(HLoadString* load) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006179 LocationSummary::CallKind call_kind = (load->NeedsEnvironment() || kEmitCompilerReadBarrier)
Nicolas Geoffray917d0162015-11-24 18:25:35 +00006180 ? LocationSummary::kCallOnSlowPath
6181 : LocationSummary::kNoCall;
6182 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006183 HLoadString::LoadKind load_kind = load->GetLoadKind();
6184 if (load_kind == HLoadString::LoadKind::kDexCacheViaMethod ||
6185 load_kind == HLoadString::LoadKind::kBootImageLinkTimePcRelative ||
6186 load_kind == HLoadString::LoadKind::kDexCachePcRelative) {
6187 locations->SetInAt(0, Location::RequiresRegister());
6188 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006189 locations->SetOut(Location::RequiresRegister());
6190}
6191
6192void InstructionCodeGeneratorX86::VisitLoadString(HLoadString* load) {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01006193 LocationSummary* locations = load->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006194 Location out_loc = locations->Out();
6195 Register out = out_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006196
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006197 switch (load->GetLoadKind()) {
6198 case HLoadString::LoadKind::kBootImageLinkTimeAddress: {
6199 DCHECK(!kEmitCompilerReadBarrier);
6200 __ movl(out, Immediate(/* placeholder */ 0));
6201 codegen_->RecordStringPatch(load);
6202 return; // No dex cache slow path.
6203 }
6204 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
6205 DCHECK(!kEmitCompilerReadBarrier);
6206 Register method_address = locations->InAt(0).AsRegister<Register>();
6207 __ leal(out, Address(method_address, CodeGeneratorX86::kDummy32BitOffset));
6208 codegen_->RecordStringPatch(load);
6209 return; // No dex cache slow path.
6210 }
6211 case HLoadString::LoadKind::kBootImageAddress: {
6212 DCHECK(!kEmitCompilerReadBarrier);
6213 DCHECK_NE(load->GetAddress(), 0u);
6214 uint32_t address = dchecked_integral_cast<uint32_t>(load->GetAddress());
6215 __ movl(out, Immediate(address));
6216 codegen_->RecordSimplePatch();
6217 return; // No dex cache slow path.
6218 }
6219 case HLoadString::LoadKind::kDexCacheAddress: {
6220 DCHECK_NE(load->GetAddress(), 0u);
6221 uint32_t address = dchecked_integral_cast<uint32_t>(load->GetAddress());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006222 // /* GcRoot<mirror::String> */ out = *address
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006223 GenerateGcRootFieldLoad(load, out_loc, Address::Absolute(address));
6224 break;
6225 }
6226 case HLoadString::LoadKind::kDexCachePcRelative: {
6227 Register base_reg = locations->InAt(0).AsRegister<Register>();
6228 uint32_t offset = load->GetDexCacheElementOffset();
6229 Label* fixup_label = codegen_->NewPcRelativeDexCacheArrayPatch(load->GetDexFile(), offset);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006230 // /* GcRoot<mirror::String> */ out = *(base + offset) /* PC-relative */
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006231 GenerateGcRootFieldLoad(
6232 load, out_loc, Address(base_reg, CodeGeneratorX86::kDummy32BitOffset), fixup_label);
6233 break;
6234 }
6235 case HLoadString::LoadKind::kDexCacheViaMethod: {
6236 Register current_method = locations->InAt(0).AsRegister<Register>();
6237
6238 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
6239 GenerateGcRootFieldLoad(
6240 load, out_loc, Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
6241
6242 // /* GcRoot<mirror::String>[] */ out = out->dex_cache_strings_
6243 __ movl(out, Address(out, mirror::Class::DexCacheStringsOffset().Int32Value()));
6244 // /* GcRoot<mirror::String> */ out = out[string_index]
6245 GenerateGcRootFieldLoad(
6246 load, out_loc, Address(out, CodeGenerator::GetCacheOffset(load->GetStringIndex())));
6247 break;
6248 }
6249 default:
6250 LOG(FATAL) << "Unexpected load kind: " << load->GetLoadKind();
6251 UNREACHABLE();
6252 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006253
Nicolas Geoffray917d0162015-11-24 18:25:35 +00006254 if (!load->IsInDexCache()) {
6255 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86(load);
6256 codegen_->AddSlowPath(slow_path);
6257 __ testl(out, out);
6258 __ j(kEqual, slow_path->GetEntryLabel());
6259 __ Bind(slow_path->GetExitLabel());
6260 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006261}
6262
David Brazdilcb1c0552015-08-04 16:22:25 +01006263static Address GetExceptionTlsAddress() {
6264 return Address::Absolute(Thread::ExceptionOffset<kX86WordSize>().Int32Value());
6265}
6266
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006267void LocationsBuilderX86::VisitLoadException(HLoadException* load) {
6268 LocationSummary* locations =
6269 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
6270 locations->SetOut(Location::RequiresRegister());
6271}
6272
6273void InstructionCodeGeneratorX86::VisitLoadException(HLoadException* load) {
David Brazdilcb1c0552015-08-04 16:22:25 +01006274 __ fs()->movl(load->GetLocations()->Out().AsRegister<Register>(), GetExceptionTlsAddress());
6275}
6276
6277void LocationsBuilderX86::VisitClearException(HClearException* clear) {
6278 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
6279}
6280
6281void InstructionCodeGeneratorX86::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
6282 __ fs()->movl(GetExceptionTlsAddress(), Immediate(0));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006283}
6284
6285void LocationsBuilderX86::VisitThrow(HThrow* instruction) {
6286 LocationSummary* locations =
6287 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
6288 InvokeRuntimeCallingConvention calling_convention;
6289 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6290}
6291
6292void InstructionCodeGeneratorX86::VisitThrow(HThrow* instruction) {
Alexandre Rames8158f282015-08-07 10:26:17 +01006293 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pDeliverException),
6294 instruction,
6295 instruction->GetDexPc(),
6296 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00006297 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006298}
6299
Roland Levillain7c1559a2015-12-15 10:55:36 +00006300static bool TypeCheckNeedsATemporary(TypeCheckKind type_check_kind) {
6301 return kEmitCompilerReadBarrier &&
6302 (kUseBakerReadBarrier ||
6303 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
6304 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
6305 type_check_kind == TypeCheckKind::kArrayObjectCheck);
6306}
6307
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006308void LocationsBuilderX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006309 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain0d5a2812015-11-13 10:07:31 +00006310 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
6311 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006312 case TypeCheckKind::kExactCheck:
6313 case TypeCheckKind::kAbstractClassCheck:
6314 case TypeCheckKind::kClassHierarchyCheck:
6315 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006316 call_kind =
6317 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006318 break;
6319 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006320 case TypeCheckKind::kUnresolvedCheck:
6321 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006322 call_kind = LocationSummary::kCallOnSlowPath;
6323 break;
6324 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006325
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006326 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006327 locations->SetInAt(0, Location::RequiresRegister());
6328 locations->SetInAt(1, Location::Any());
6329 // Note that TypeCheckSlowPathX86 uses this "out" register too.
6330 locations->SetOut(Location::RequiresRegister());
6331 // When read barriers are enabled, we need a temporary register for
6332 // some cases.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006333 if (TypeCheckNeedsATemporary(type_check_kind)) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006334 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006335 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006336}
6337
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006338void InstructionCodeGeneratorX86::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006339 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006340 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006341 Location obj_loc = locations->InAt(0);
6342 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006343 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006344 Location out_loc = locations->Out();
6345 Register out = out_loc.AsRegister<Register>();
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006346 Location maybe_temp_loc = TypeCheckNeedsATemporary(type_check_kind) ?
Roland Levillain7c1559a2015-12-15 10:55:36 +00006347 locations->GetTemp(0) :
6348 Location::NoLocation();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006349 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006350 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6351 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6352 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Andreas Gampe85b62f22015-09-09 13:15:38 -07006353 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006354 NearLabel done, zero;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006355
6356 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006357 // Avoid null check if we know obj is not null.
6358 if (instruction->MustDoNullCheck()) {
6359 __ testl(obj, obj);
6360 __ j(kEqual, &zero);
6361 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006362
Roland Levillain0d5a2812015-11-13 10:07:31 +00006363 // /* HeapReference<Class> */ out = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006364 GenerateReferenceLoadTwoRegisters(instruction, out_loc, obj_loc, class_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006365
Roland Levillain7c1559a2015-12-15 10:55:36 +00006366 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006367 case TypeCheckKind::kExactCheck: {
6368 if (cls.IsRegister()) {
6369 __ cmpl(out, cls.AsRegister<Register>());
6370 } else {
6371 DCHECK(cls.IsStackSlot()) << cls;
6372 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6373 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006374
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006375 // Classes must be equal for the instanceof to succeed.
6376 __ j(kNotEqual, &zero);
6377 __ movl(out, Immediate(1));
6378 __ jmp(&done);
6379 break;
6380 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006381
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006382 case TypeCheckKind::kAbstractClassCheck: {
6383 // If the class is abstract, we eagerly fetch the super class of the
6384 // object to avoid doing a comparison we know will fail.
6385 NearLabel loop;
6386 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006387 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006388 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006389 __ testl(out, out);
6390 // If `out` is null, we use it for the result, and jump to `done`.
6391 __ j(kEqual, &done);
6392 if (cls.IsRegister()) {
6393 __ cmpl(out, cls.AsRegister<Register>());
6394 } else {
6395 DCHECK(cls.IsStackSlot()) << cls;
6396 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6397 }
6398 __ j(kNotEqual, &loop);
6399 __ movl(out, Immediate(1));
6400 if (zero.IsLinked()) {
6401 __ jmp(&done);
6402 }
6403 break;
6404 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006405
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006406 case TypeCheckKind::kClassHierarchyCheck: {
6407 // Walk over the class hierarchy to find a match.
6408 NearLabel loop, success;
6409 __ Bind(&loop);
6410 if (cls.IsRegister()) {
6411 __ cmpl(out, cls.AsRegister<Register>());
6412 } else {
6413 DCHECK(cls.IsStackSlot()) << cls;
6414 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6415 }
6416 __ j(kEqual, &success);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006417 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006418 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006419 __ testl(out, out);
6420 __ j(kNotEqual, &loop);
6421 // If `out` is null, we use it for the result, and jump to `done`.
6422 __ jmp(&done);
6423 __ Bind(&success);
6424 __ movl(out, Immediate(1));
6425 if (zero.IsLinked()) {
6426 __ jmp(&done);
6427 }
6428 break;
6429 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006430
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006431 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006432 // Do an exact check.
6433 NearLabel exact_check;
6434 if (cls.IsRegister()) {
6435 __ cmpl(out, cls.AsRegister<Register>());
6436 } else {
6437 DCHECK(cls.IsStackSlot()) << cls;
6438 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6439 }
6440 __ j(kEqual, &exact_check);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006441 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006442 // /* HeapReference<Class> */ out = out->component_type_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006443 GenerateReferenceLoadOneRegister(instruction, out_loc, component_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006444 __ testl(out, out);
6445 // If `out` is null, we use it for the result, and jump to `done`.
6446 __ j(kEqual, &done);
6447 __ cmpw(Address(out, primitive_offset), Immediate(Primitive::kPrimNot));
6448 __ j(kNotEqual, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006449 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006450 __ movl(out, Immediate(1));
6451 __ jmp(&done);
6452 break;
6453 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006454
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006455 case TypeCheckKind::kArrayCheck: {
6456 if (cls.IsRegister()) {
6457 __ cmpl(out, cls.AsRegister<Register>());
6458 } else {
6459 DCHECK(cls.IsStackSlot()) << cls;
6460 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6461 }
6462 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain0d5a2812015-11-13 10:07:31 +00006463 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
6464 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006465 codegen_->AddSlowPath(slow_path);
6466 __ j(kNotEqual, slow_path->GetEntryLabel());
6467 __ movl(out, Immediate(1));
6468 if (zero.IsLinked()) {
6469 __ jmp(&done);
6470 }
6471 break;
6472 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006473
Calin Juravle98893e12015-10-02 21:05:03 +01006474 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006475 case TypeCheckKind::kInterfaceCheck: {
6476 // Note that we indeed only call on slow path, but we always go
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006477 // into the slow path for the unresolved and interface check
Roland Levillain0d5a2812015-11-13 10:07:31 +00006478 // cases.
6479 //
6480 // We cannot directly call the InstanceofNonTrivial runtime
6481 // entry point without resorting to a type checking slow path
6482 // here (i.e. by calling InvokeRuntime directly), as it would
6483 // require to assign fixed registers for the inputs of this
6484 // HInstanceOf instruction (following the runtime calling
6485 // convention), which might be cluttered by the potential first
6486 // read barrier emission at the beginning of this method.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006487 //
6488 // TODO: Introduce a new runtime entry point taking the object
6489 // to test (instead of its class) as argument, and let it deal
6490 // with the read barrier issues. This will let us refactor this
6491 // case of the `switch` code as it was previously (with a direct
6492 // call to the runtime not using a type checking slow path).
6493 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006494 DCHECK(locations->OnlyCallsOnSlowPath());
6495 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
6496 /* is_fatal */ false);
6497 codegen_->AddSlowPath(slow_path);
6498 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006499 if (zero.IsLinked()) {
6500 __ jmp(&done);
6501 }
6502 break;
6503 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006504 }
6505
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006506 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006507 __ Bind(&zero);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006508 __ xorl(out, out);
6509 }
6510
6511 if (done.IsLinked()) {
6512 __ Bind(&done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006513 }
6514
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006515 if (slow_path != nullptr) {
6516 __ Bind(slow_path->GetExitLabel());
6517 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006518}
6519
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006520void LocationsBuilderX86::VisitCheckCast(HCheckCast* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006521 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
6522 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006523 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
6524 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006525 case TypeCheckKind::kExactCheck:
6526 case TypeCheckKind::kAbstractClassCheck:
6527 case TypeCheckKind::kClassHierarchyCheck:
6528 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006529 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
6530 LocationSummary::kCallOnSlowPath :
6531 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006532 break;
6533 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006534 case TypeCheckKind::kUnresolvedCheck:
6535 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006536 call_kind = LocationSummary::kCallOnSlowPath;
6537 break;
6538 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006539 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
6540 locations->SetInAt(0, Location::RequiresRegister());
6541 locations->SetInAt(1, Location::Any());
6542 // Note that TypeCheckSlowPathX86 uses this "temp" register too.
6543 locations->AddTemp(Location::RequiresRegister());
6544 // When read barriers are enabled, we need an additional temporary
6545 // register for some cases.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006546 if (TypeCheckNeedsATemporary(type_check_kind)) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006547 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006548 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006549}
6550
6551void InstructionCodeGeneratorX86::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006552 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006553 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006554 Location obj_loc = locations->InAt(0);
6555 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006556 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006557 Location temp_loc = locations->GetTemp(0);
6558 Register temp = temp_loc.AsRegister<Register>();
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006559 Location maybe_temp2_loc = TypeCheckNeedsATemporary(type_check_kind) ?
Roland Levillain7c1559a2015-12-15 10:55:36 +00006560 locations->GetTemp(1) :
6561 Location::NoLocation();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006562 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
6563 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6564 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6565 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006566
Roland Levillain0d5a2812015-11-13 10:07:31 +00006567 bool is_type_check_slow_path_fatal =
6568 (type_check_kind == TypeCheckKind::kExactCheck ||
6569 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
6570 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
6571 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
6572 !instruction->CanThrowIntoCatchBlock();
6573 SlowPathCode* type_check_slow_path =
6574 new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
6575 is_type_check_slow_path_fatal);
6576 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006577
Roland Levillain0d5a2812015-11-13 10:07:31 +00006578 NearLabel done;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006579 // Avoid null check if we know obj is not null.
6580 if (instruction->MustDoNullCheck()) {
6581 __ testl(obj, obj);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006582 __ j(kEqual, &done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006583 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006584
Roland Levillain0d5a2812015-11-13 10:07:31 +00006585 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006586 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006587
Roland Levillain0d5a2812015-11-13 10:07:31 +00006588 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006589 case TypeCheckKind::kExactCheck:
6590 case TypeCheckKind::kArrayCheck: {
6591 if (cls.IsRegister()) {
6592 __ cmpl(temp, cls.AsRegister<Register>());
6593 } else {
6594 DCHECK(cls.IsStackSlot()) << cls;
6595 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6596 }
6597 // Jump to slow path for throwing the exception or doing a
6598 // more involved array check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006599 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006600 break;
6601 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006602
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006603 case TypeCheckKind::kAbstractClassCheck: {
6604 // If the class is abstract, we eagerly fetch the super class of the
6605 // object to avoid doing a comparison we know will fail.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006606 NearLabel loop, compare_classes;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006607 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006608 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006609 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006610
6611 // If the class reference currently in `temp` is not null, jump
6612 // to the `compare_classes` label to compare it with the checked
6613 // class.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006614 __ testl(temp, temp);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006615 __ j(kNotEqual, &compare_classes);
6616 // Otherwise, jump to the slow path to throw the exception.
6617 //
6618 // But before, move back the object's class into `temp` before
6619 // going into the slow path, as it has been overwritten in the
6620 // meantime.
6621 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006622 GenerateReferenceLoadTwoRegisters(
6623 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006624 __ jmp(type_check_slow_path->GetEntryLabel());
6625
6626 __ Bind(&compare_classes);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006627 if (cls.IsRegister()) {
6628 __ cmpl(temp, cls.AsRegister<Register>());
6629 } else {
6630 DCHECK(cls.IsStackSlot()) << cls;
6631 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6632 }
6633 __ j(kNotEqual, &loop);
6634 break;
6635 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006636
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006637 case TypeCheckKind::kClassHierarchyCheck: {
6638 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006639 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006640 __ Bind(&loop);
6641 if (cls.IsRegister()) {
6642 __ cmpl(temp, cls.AsRegister<Register>());
6643 } else {
6644 DCHECK(cls.IsStackSlot()) << cls;
6645 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6646 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006647 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006648
Roland Levillain0d5a2812015-11-13 10:07:31 +00006649 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006650 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006651
6652 // If the class reference currently in `temp` is not null, jump
6653 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006654 __ testl(temp, temp);
6655 __ j(kNotEqual, &loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006656 // Otherwise, jump to the slow path to throw the exception.
6657 //
6658 // But before, move back the object's class into `temp` before
6659 // going into the slow path, as it has been overwritten in the
6660 // meantime.
6661 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006662 GenerateReferenceLoadTwoRegisters(
6663 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006664 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006665 break;
6666 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006667
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006668 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006669 // Do an exact check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006670 NearLabel check_non_primitive_component_type;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006671 if (cls.IsRegister()) {
6672 __ cmpl(temp, cls.AsRegister<Register>());
6673 } else {
6674 DCHECK(cls.IsStackSlot()) << cls;
6675 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6676 }
6677 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006678
6679 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006680 // /* HeapReference<Class> */ temp = temp->component_type_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006681 GenerateReferenceLoadOneRegister(instruction, temp_loc, component_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006682
6683 // If the component type is not null (i.e. the object is indeed
6684 // an array), jump to label `check_non_primitive_component_type`
6685 // to further check that this component type is not a primitive
6686 // type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006687 __ testl(temp, temp);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006688 __ j(kNotEqual, &check_non_primitive_component_type);
6689 // Otherwise, jump to the slow path to throw the exception.
6690 //
6691 // But before, move back the object's class into `temp` before
6692 // going into the slow path, as it has been overwritten in the
6693 // meantime.
6694 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006695 GenerateReferenceLoadTwoRegisters(
6696 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006697 __ jmp(type_check_slow_path->GetEntryLabel());
6698
6699 __ Bind(&check_non_primitive_component_type);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006700 __ cmpw(Address(temp, primitive_offset), Immediate(Primitive::kPrimNot));
Roland Levillain0d5a2812015-11-13 10:07:31 +00006701 __ j(kEqual, &done);
6702 // Same comment as above regarding `temp` and the slow path.
6703 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006704 GenerateReferenceLoadTwoRegisters(
6705 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006706 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006707 break;
6708 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006709
Calin Juravle98893e12015-10-02 21:05:03 +01006710 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006711 case TypeCheckKind::kInterfaceCheck:
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006712 // We always go into the type check slow path for the unresolved
6713 // and interface check cases.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006714 //
6715 // We cannot directly call the CheckCast runtime entry point
6716 // without resorting to a type checking slow path here (i.e. by
6717 // calling InvokeRuntime directly), as it would require to
6718 // assign fixed registers for the inputs of this HInstanceOf
6719 // instruction (following the runtime calling convention), which
6720 // might be cluttered by the potential first read barrier
6721 // emission at the beginning of this method.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006722 //
6723 // TODO: Introduce a new runtime entry point taking the object
6724 // to test (instead of its class) as argument, and let it deal
6725 // with the read barrier issues. This will let us refactor this
6726 // case of the `switch` code as it was previously (with a direct
6727 // call to the runtime not using a type checking slow path).
6728 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006729 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006730 break;
6731 }
6732 __ Bind(&done);
6733
Roland Levillain0d5a2812015-11-13 10:07:31 +00006734 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006735}
6736
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006737void LocationsBuilderX86::VisitMonitorOperation(HMonitorOperation* instruction) {
6738 LocationSummary* locations =
6739 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
6740 InvokeRuntimeCallingConvention calling_convention;
6741 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6742}
6743
6744void InstructionCodeGeneratorX86::VisitMonitorOperation(HMonitorOperation* instruction) {
Alexandre Rames8158f282015-08-07 10:26:17 +01006745 codegen_->InvokeRuntime(instruction->IsEnter() ? QUICK_ENTRY_POINT(pLockObject)
6746 : QUICK_ENTRY_POINT(pUnlockObject),
6747 instruction,
6748 instruction->GetDexPc(),
6749 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00006750 if (instruction->IsEnter()) {
6751 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
6752 } else {
6753 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
6754 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006755}
6756
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006757void LocationsBuilderX86::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
6758void LocationsBuilderX86::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
6759void LocationsBuilderX86::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
6760
6761void LocationsBuilderX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
6762 LocationSummary* locations =
6763 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6764 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
6765 || instruction->GetResultType() == Primitive::kPrimLong);
6766 locations->SetInAt(0, Location::RequiresRegister());
6767 locations->SetInAt(1, Location::Any());
6768 locations->SetOut(Location::SameAsFirstInput());
6769}
6770
6771void InstructionCodeGeneratorX86::VisitAnd(HAnd* instruction) {
6772 HandleBitwiseOperation(instruction);
6773}
6774
6775void InstructionCodeGeneratorX86::VisitOr(HOr* instruction) {
6776 HandleBitwiseOperation(instruction);
6777}
6778
6779void InstructionCodeGeneratorX86::VisitXor(HXor* instruction) {
6780 HandleBitwiseOperation(instruction);
6781}
6782
6783void InstructionCodeGeneratorX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
6784 LocationSummary* locations = instruction->GetLocations();
6785 Location first = locations->InAt(0);
6786 Location second = locations->InAt(1);
6787 DCHECK(first.Equals(locations->Out()));
6788
6789 if (instruction->GetResultType() == Primitive::kPrimInt) {
6790 if (second.IsRegister()) {
6791 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006792 __ andl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006793 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006794 __ orl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006795 } else {
6796 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006797 __ xorl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006798 }
6799 } else if (second.IsConstant()) {
6800 if (instruction->IsAnd()) {
Roland Levillain199f3362014-11-27 17:15:16 +00006801 __ andl(first.AsRegister<Register>(),
6802 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006803 } else if (instruction->IsOr()) {
Roland Levillain199f3362014-11-27 17:15:16 +00006804 __ orl(first.AsRegister<Register>(),
6805 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006806 } else {
6807 DCHECK(instruction->IsXor());
Roland Levillain199f3362014-11-27 17:15:16 +00006808 __ xorl(first.AsRegister<Register>(),
6809 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006810 }
6811 } else {
6812 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006813 __ andl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006814 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006815 __ orl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006816 } else {
6817 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006818 __ xorl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006819 }
6820 }
6821 } else {
6822 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
6823 if (second.IsRegisterPair()) {
6824 if (instruction->IsAnd()) {
6825 __ andl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
6826 __ andl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
6827 } else if (instruction->IsOr()) {
6828 __ orl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
6829 __ orl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
6830 } else {
6831 DCHECK(instruction->IsXor());
6832 __ xorl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
6833 __ xorl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
6834 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006835 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006836 if (instruction->IsAnd()) {
6837 __ andl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
6838 __ andl(first.AsRegisterPairHigh<Register>(),
6839 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
6840 } else if (instruction->IsOr()) {
6841 __ orl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
6842 __ orl(first.AsRegisterPairHigh<Register>(),
6843 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
6844 } else {
6845 DCHECK(instruction->IsXor());
6846 __ xorl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
6847 __ xorl(first.AsRegisterPairHigh<Register>(),
6848 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
6849 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006850 } else {
6851 DCHECK(second.IsConstant()) << second;
6852 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006853 int32_t low_value = Low32Bits(value);
6854 int32_t high_value = High32Bits(value);
6855 Immediate low(low_value);
6856 Immediate high(high_value);
6857 Register first_low = first.AsRegisterPairLow<Register>();
6858 Register first_high = first.AsRegisterPairHigh<Register>();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006859 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006860 if (low_value == 0) {
6861 __ xorl(first_low, first_low);
6862 } else if (low_value != -1) {
6863 __ andl(first_low, low);
6864 }
6865 if (high_value == 0) {
6866 __ xorl(first_high, first_high);
6867 } else if (high_value != -1) {
6868 __ andl(first_high, high);
6869 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006870 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006871 if (low_value != 0) {
6872 __ orl(first_low, low);
6873 }
6874 if (high_value != 0) {
6875 __ orl(first_high, high);
6876 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006877 } else {
6878 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006879 if (low_value != 0) {
6880 __ xorl(first_low, low);
6881 }
6882 if (high_value != 0) {
6883 __ xorl(first_high, high);
6884 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006885 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006886 }
6887 }
6888}
6889
Roland Levillain7c1559a2015-12-15 10:55:36 +00006890void InstructionCodeGeneratorX86::GenerateReferenceLoadOneRegister(HInstruction* instruction,
6891 Location out,
6892 uint32_t offset,
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006893 Location maybe_temp) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006894 Register out_reg = out.AsRegister<Register>();
6895 if (kEmitCompilerReadBarrier) {
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006896 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillain7c1559a2015-12-15 10:55:36 +00006897 if (kUseBakerReadBarrier) {
6898 // Load with fast path based Baker's read barrier.
6899 // /* HeapReference<Object> */ out = *(out + offset)
6900 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006901 instruction, out, out_reg, offset, maybe_temp, /* needs_null_check */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006902 } else {
6903 // Load with slow path based read barrier.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006904 // Save the value of `out` into `maybe_temp` before overwriting it
Roland Levillain7c1559a2015-12-15 10:55:36 +00006905 // in the following move operation, as we will need it for the
6906 // read barrier below.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006907 __ movl(maybe_temp.AsRegister<Register>(), out_reg);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006908 // /* HeapReference<Object> */ out = *(out + offset)
6909 __ movl(out_reg, Address(out_reg, offset));
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006910 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006911 }
6912 } else {
6913 // Plain load with no read barrier.
6914 // /* HeapReference<Object> */ out = *(out + offset)
6915 __ movl(out_reg, Address(out_reg, offset));
6916 __ MaybeUnpoisonHeapReference(out_reg);
6917 }
6918}
6919
6920void InstructionCodeGeneratorX86::GenerateReferenceLoadTwoRegisters(HInstruction* instruction,
6921 Location out,
6922 Location obj,
6923 uint32_t offset,
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006924 Location maybe_temp) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006925 Register out_reg = out.AsRegister<Register>();
6926 Register obj_reg = obj.AsRegister<Register>();
6927 if (kEmitCompilerReadBarrier) {
6928 if (kUseBakerReadBarrier) {
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006929 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillain7c1559a2015-12-15 10:55:36 +00006930 // Load with fast path based Baker's read barrier.
6931 // /* HeapReference<Object> */ out = *(obj + offset)
6932 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006933 instruction, out, obj_reg, offset, maybe_temp, /* needs_null_check */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006934 } else {
6935 // Load with slow path based read barrier.
6936 // /* HeapReference<Object> */ out = *(obj + offset)
6937 __ movl(out_reg, Address(obj_reg, offset));
6938 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
6939 }
6940 } else {
6941 // Plain load with no read barrier.
6942 // /* HeapReference<Object> */ out = *(obj + offset)
6943 __ movl(out_reg, Address(obj_reg, offset));
6944 __ MaybeUnpoisonHeapReference(out_reg);
6945 }
6946}
6947
6948void InstructionCodeGeneratorX86::GenerateGcRootFieldLoad(HInstruction* instruction,
6949 Location root,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006950 const Address& address,
6951 Label* fixup_label) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006952 Register root_reg = root.AsRegister<Register>();
6953 if (kEmitCompilerReadBarrier) {
6954 if (kUseBakerReadBarrier) {
6955 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
6956 // Baker's read barrier are used:
6957 //
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006958 // root = *address;
Roland Levillain7c1559a2015-12-15 10:55:36 +00006959 // if (Thread::Current()->GetIsGcMarking()) {
6960 // root = ReadBarrier::Mark(root)
6961 // }
6962
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006963 // /* GcRoot<mirror::Object> */ root = *address
6964 __ movl(root_reg, address);
6965 if (fixup_label != nullptr) {
6966 __ Bind(fixup_label);
6967 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00006968 static_assert(
6969 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
6970 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
6971 "have different sizes.");
6972 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
6973 "art::mirror::CompressedReference<mirror::Object> and int32_t "
6974 "have different sizes.");
6975
6976 // Slow path used to mark the GC root `root`.
6977 SlowPathCode* slow_path =
6978 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathX86(instruction, root, root);
6979 codegen_->AddSlowPath(slow_path);
6980
6981 __ fs()->cmpl(Address::Absolute(Thread::IsGcMarkingOffset<kX86WordSize>().Int32Value()),
6982 Immediate(0));
6983 __ j(kNotEqual, slow_path->GetEntryLabel());
6984 __ Bind(slow_path->GetExitLabel());
6985 } else {
6986 // GC root loaded through a slow path for read barriers other
6987 // than Baker's.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006988 // /* GcRoot<mirror::Object>* */ root = address
6989 __ leal(root_reg, address);
6990 if (fixup_label != nullptr) {
6991 __ Bind(fixup_label);
6992 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00006993 // /* mirror::Object* */ root = root->Read()
6994 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
6995 }
6996 } else {
6997 // Plain GC root load with no read barrier.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006998 // /* GcRoot<mirror::Object> */ root = *address
6999 __ movl(root_reg, address);
7000 if (fixup_label != nullptr) {
7001 __ Bind(fixup_label);
7002 }
Roland Levillaine3f43ac2016-01-19 15:07:47 +00007003 // Note that GC roots are not affected by heap poisoning, thus we
7004 // do not have to unpoison `root_reg` here.
Roland Levillain7c1559a2015-12-15 10:55:36 +00007005 }
7006}
7007
7008void CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
7009 Location ref,
7010 Register obj,
7011 uint32_t offset,
7012 Location temp,
7013 bool needs_null_check) {
7014 DCHECK(kEmitCompilerReadBarrier);
7015 DCHECK(kUseBakerReadBarrier);
7016
7017 // /* HeapReference<Object> */ ref = *(obj + offset)
7018 Address src(obj, offset);
7019 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, temp, needs_null_check);
7020}
7021
7022void CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
7023 Location ref,
7024 Register obj,
7025 uint32_t data_offset,
7026 Location index,
7027 Location temp,
7028 bool needs_null_check) {
7029 DCHECK(kEmitCompilerReadBarrier);
7030 DCHECK(kUseBakerReadBarrier);
7031
Roland Levillain3d312422016-06-23 13:53:42 +01007032 static_assert(
7033 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
7034 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain7c1559a2015-12-15 10:55:36 +00007035 // /* HeapReference<Object> */ ref =
7036 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
7037 Address src = index.IsConstant() ?
7038 Address(obj, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset) :
7039 Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset);
7040 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, temp, needs_null_check);
7041}
7042
7043void CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
7044 Location ref,
7045 Register obj,
7046 const Address& src,
7047 Location temp,
7048 bool needs_null_check) {
7049 DCHECK(kEmitCompilerReadBarrier);
7050 DCHECK(kUseBakerReadBarrier);
7051
7052 // In slow path based read barriers, the read barrier call is
7053 // inserted after the original load. However, in fast path based
7054 // Baker's read barriers, we need to perform the load of
7055 // mirror::Object::monitor_ *before* the original reference load.
7056 // This load-load ordering is required by the read barrier.
7057 // The fast path/slow path (for Baker's algorithm) should look like:
7058 //
7059 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
7060 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
7061 // HeapReference<Object> ref = *src; // Original reference load.
7062 // bool is_gray = (rb_state == ReadBarrier::gray_ptr_);
7063 // if (is_gray) {
7064 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
7065 // }
7066 //
7067 // Note: the original implementation in ReadBarrier::Barrier is
7068 // slightly more complex as:
7069 // - it implements the load-load fence using a data dependency on
Roland Levillaine3f43ac2016-01-19 15:07:47 +00007070 // the high-bits of rb_state, which are expected to be all zeroes
7071 // (we use CodeGeneratorX86::GenerateMemoryBarrier instead here,
7072 // which is a no-op thanks to the x86 memory model);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007073 // - it performs additional checks that we do not do here for
7074 // performance reasons.
7075
7076 Register ref_reg = ref.AsRegister<Register>();
7077 Register temp_reg = temp.AsRegister<Register>();
7078 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
7079
7080 // /* int32_t */ monitor = obj->monitor_
7081 __ movl(temp_reg, Address(obj, monitor_offset));
7082 if (needs_null_check) {
7083 MaybeRecordImplicitNullCheck(instruction);
7084 }
7085 // /* LockWord */ lock_word = LockWord(monitor)
7086 static_assert(sizeof(LockWord) == sizeof(int32_t),
7087 "art::LockWord and int32_t have different sizes.");
7088 // /* uint32_t */ rb_state = lock_word.ReadBarrierState()
7089 __ shrl(temp_reg, Immediate(LockWord::kReadBarrierStateShift));
7090 __ andl(temp_reg, Immediate(LockWord::kReadBarrierStateMask));
7091 static_assert(
7092 LockWord::kReadBarrierStateMask == ReadBarrier::rb_ptr_mask_,
7093 "art::LockWord::kReadBarrierStateMask is not equal to art::ReadBarrier::rb_ptr_mask_.");
7094
7095 // Load fence to prevent load-load reordering.
7096 // Note that this is a no-op, thanks to the x86 memory model.
7097 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
7098
7099 // The actual reference load.
7100 // /* HeapReference<Object> */ ref = *src
7101 __ movl(ref_reg, src);
7102
7103 // Object* ref = ref_addr->AsMirrorPtr()
7104 __ MaybeUnpoisonHeapReference(ref_reg);
7105
7106 // Slow path used to mark the object `ref` when it is gray.
7107 SlowPathCode* slow_path =
7108 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathX86(instruction, ref, ref);
7109 AddSlowPath(slow_path);
7110
7111 // if (rb_state == ReadBarrier::gray_ptr_)
7112 // ref = ReadBarrier::Mark(ref);
7113 __ cmpl(temp_reg, Immediate(ReadBarrier::gray_ptr_));
7114 __ j(kEqual, slow_path->GetEntryLabel());
7115 __ Bind(slow_path->GetExitLabel());
7116}
7117
7118void CodeGeneratorX86::GenerateReadBarrierSlow(HInstruction* instruction,
7119 Location out,
7120 Location ref,
7121 Location obj,
7122 uint32_t offset,
7123 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007124 DCHECK(kEmitCompilerReadBarrier);
7125
Roland Levillain7c1559a2015-12-15 10:55:36 +00007126 // Insert a slow path based read barrier *after* the reference load.
7127 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00007128 // If heap poisoning is enabled, the unpoisoning of the loaded
7129 // reference will be carried out by the runtime within the slow
7130 // path.
7131 //
7132 // Note that `ref` currently does not get unpoisoned (when heap
7133 // poisoning is enabled), which is alright as the `ref` argument is
7134 // not used by the artReadBarrierSlow entry point.
7135 //
7136 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
7137 SlowPathCode* slow_path = new (GetGraph()->GetArena())
7138 ReadBarrierForHeapReferenceSlowPathX86(instruction, out, ref, obj, offset, index);
7139 AddSlowPath(slow_path);
7140
Roland Levillain0d5a2812015-11-13 10:07:31 +00007141 __ jmp(slow_path->GetEntryLabel());
7142 __ Bind(slow_path->GetExitLabel());
7143}
7144
Roland Levillain7c1559a2015-12-15 10:55:36 +00007145void CodeGeneratorX86::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
7146 Location out,
7147 Location ref,
7148 Location obj,
7149 uint32_t offset,
7150 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007151 if (kEmitCompilerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007152 // Baker's read barriers shall be handled by the fast path
7153 // (CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier).
7154 DCHECK(!kUseBakerReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007155 // If heap poisoning is enabled, unpoisoning will be taken care of
7156 // by the runtime within the slow path.
Roland Levillain7c1559a2015-12-15 10:55:36 +00007157 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007158 } else if (kPoisonHeapReferences) {
7159 __ UnpoisonHeapReference(out.AsRegister<Register>());
7160 }
7161}
7162
Roland Levillain7c1559a2015-12-15 10:55:36 +00007163void CodeGeneratorX86::GenerateReadBarrierForRootSlow(HInstruction* instruction,
7164 Location out,
7165 Location root) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007166 DCHECK(kEmitCompilerReadBarrier);
7167
Roland Levillain7c1559a2015-12-15 10:55:36 +00007168 // Insert a slow path based read barrier *after* the GC root load.
7169 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00007170 // Note that GC roots are not affected by heap poisoning, so we do
7171 // not need to do anything special for this here.
7172 SlowPathCode* slow_path =
7173 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathX86(instruction, out, root);
7174 AddSlowPath(slow_path);
7175
Roland Levillain0d5a2812015-11-13 10:07:31 +00007176 __ jmp(slow_path->GetEntryLabel());
7177 __ Bind(slow_path->GetExitLabel());
7178}
7179
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007180void LocationsBuilderX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00007181 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00007182 LOG(FATAL) << "Unreachable";
7183}
7184
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007185void InstructionCodeGeneratorX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00007186 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00007187 LOG(FATAL) << "Unreachable";
7188}
7189
Mark Mendellfe57faa2015-09-18 09:26:15 -04007190// Simple implementation of packed switch - generate cascaded compare/jumps.
7191void LocationsBuilderX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7192 LocationSummary* locations =
7193 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
7194 locations->SetInAt(0, Location::RequiresRegister());
7195}
7196
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007197void InstructionCodeGeneratorX86::GenPackedSwitchWithCompares(Register value_reg,
7198 int32_t lower_bound,
7199 uint32_t num_entries,
7200 HBasicBlock* switch_block,
7201 HBasicBlock* default_block) {
7202 // Figure out the correct compare values and jump conditions.
7203 // Handle the first compare/branch as a special case because it might
7204 // jump to the default case.
7205 DCHECK_GT(num_entries, 2u);
7206 Condition first_condition;
7207 uint32_t index;
7208 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
7209 if (lower_bound != 0) {
7210 first_condition = kLess;
7211 __ cmpl(value_reg, Immediate(lower_bound));
7212 __ j(first_condition, codegen_->GetLabelOf(default_block));
7213 __ j(kEqual, codegen_->GetLabelOf(successors[0]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007214
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007215 index = 1;
7216 } else {
7217 // Handle all the compare/jumps below.
7218 first_condition = kBelow;
7219 index = 0;
7220 }
7221
7222 // Handle the rest of the compare/jumps.
7223 for (; index + 1 < num_entries; index += 2) {
7224 int32_t compare_to_value = lower_bound + index + 1;
7225 __ cmpl(value_reg, Immediate(compare_to_value));
7226 // Jump to successors[index] if value < case_value[index].
7227 __ j(first_condition, codegen_->GetLabelOf(successors[index]));
7228 // Jump to successors[index + 1] if value == case_value[index + 1].
7229 __ j(kEqual, codegen_->GetLabelOf(successors[index + 1]));
7230 }
7231
7232 if (index != num_entries) {
7233 // There are an odd number of entries. Handle the last one.
7234 DCHECK_EQ(index + 1, num_entries);
7235 __ cmpl(value_reg, Immediate(lower_bound + index));
7236 __ j(kEqual, codegen_->GetLabelOf(successors[index]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007237 }
7238
7239 // And the default for any other value.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007240 if (!codegen_->GoesToNextBlock(switch_block, default_block)) {
7241 __ jmp(codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007242 }
7243}
7244
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007245void InstructionCodeGeneratorX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7246 int32_t lower_bound = switch_instr->GetStartValue();
7247 uint32_t num_entries = switch_instr->GetNumEntries();
7248 LocationSummary* locations = switch_instr->GetLocations();
7249 Register value_reg = locations->InAt(0).AsRegister<Register>();
7250
7251 GenPackedSwitchWithCompares(value_reg,
7252 lower_bound,
7253 num_entries,
7254 switch_instr->GetBlock(),
7255 switch_instr->GetDefaultBlock());
7256}
7257
Mark Mendell805b3b52015-09-18 14:10:29 -04007258void LocationsBuilderX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
7259 LocationSummary* locations =
7260 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
7261 locations->SetInAt(0, Location::RequiresRegister());
7262
7263 // Constant area pointer.
7264 locations->SetInAt(1, Location::RequiresRegister());
7265
7266 // And the temporary we need.
7267 locations->AddTemp(Location::RequiresRegister());
7268}
7269
7270void InstructionCodeGeneratorX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
7271 int32_t lower_bound = switch_instr->GetStartValue();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007272 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendell805b3b52015-09-18 14:10:29 -04007273 LocationSummary* locations = switch_instr->GetLocations();
7274 Register value_reg = locations->InAt(0).AsRegister<Register>();
7275 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
7276
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007277 if (num_entries <= kPackedSwitchJumpTableThreshold) {
7278 GenPackedSwitchWithCompares(value_reg,
7279 lower_bound,
7280 num_entries,
7281 switch_instr->GetBlock(),
7282 default_block);
7283 return;
7284 }
7285
Mark Mendell805b3b52015-09-18 14:10:29 -04007286 // Optimizing has a jump area.
7287 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
7288 Register constant_area = locations->InAt(1).AsRegister<Register>();
7289
7290 // Remove the bias, if needed.
7291 if (lower_bound != 0) {
7292 __ leal(temp_reg, Address(value_reg, -lower_bound));
7293 value_reg = temp_reg;
7294 }
7295
7296 // Is the value in range?
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007297 DCHECK_GE(num_entries, 1u);
Mark Mendell805b3b52015-09-18 14:10:29 -04007298 __ cmpl(value_reg, Immediate(num_entries - 1));
7299 __ j(kAbove, codegen_->GetLabelOf(default_block));
7300
7301 // We are in the range of the table.
7302 // Load (target-constant_area) from the jump table, indexing by the value.
7303 __ movl(temp_reg, codegen_->LiteralCaseTable(switch_instr, constant_area, value_reg));
7304
7305 // Compute the actual target address by adding in constant_area.
7306 __ addl(temp_reg, constant_area);
7307
7308 // And jump.
7309 __ jmp(temp_reg);
7310}
7311
Mark Mendell0616ae02015-04-17 12:49:27 -04007312void LocationsBuilderX86::VisitX86ComputeBaseMethodAddress(
7313 HX86ComputeBaseMethodAddress* insn) {
7314 LocationSummary* locations =
7315 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
7316 locations->SetOut(Location::RequiresRegister());
7317}
7318
7319void InstructionCodeGeneratorX86::VisitX86ComputeBaseMethodAddress(
7320 HX86ComputeBaseMethodAddress* insn) {
7321 LocationSummary* locations = insn->GetLocations();
7322 Register reg = locations->Out().AsRegister<Register>();
7323
7324 // Generate call to next instruction.
7325 Label next_instruction;
7326 __ call(&next_instruction);
7327 __ Bind(&next_instruction);
7328
7329 // Remember this offset for later use with constant area.
7330 codegen_->SetMethodAddressOffset(GetAssembler()->CodeSize());
7331
7332 // Grab the return address off the stack.
7333 __ popl(reg);
7334}
7335
7336void LocationsBuilderX86::VisitX86LoadFromConstantTable(
7337 HX86LoadFromConstantTable* insn) {
7338 LocationSummary* locations =
7339 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
7340
7341 locations->SetInAt(0, Location::RequiresRegister());
7342 locations->SetInAt(1, Location::ConstantLocation(insn->GetConstant()));
7343
7344 // If we don't need to be materialized, we only need the inputs to be set.
David Brazdilb3e773e2016-01-26 11:28:37 +00007345 if (insn->IsEmittedAtUseSite()) {
Mark Mendell0616ae02015-04-17 12:49:27 -04007346 return;
7347 }
7348
7349 switch (insn->GetType()) {
7350 case Primitive::kPrimFloat:
7351 case Primitive::kPrimDouble:
7352 locations->SetOut(Location::RequiresFpuRegister());
7353 break;
7354
7355 case Primitive::kPrimInt:
7356 locations->SetOut(Location::RequiresRegister());
7357 break;
7358
7359 default:
7360 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
7361 }
7362}
7363
7364void InstructionCodeGeneratorX86::VisitX86LoadFromConstantTable(HX86LoadFromConstantTable* insn) {
David Brazdilb3e773e2016-01-26 11:28:37 +00007365 if (insn->IsEmittedAtUseSite()) {
Mark Mendell0616ae02015-04-17 12:49:27 -04007366 return;
7367 }
7368
7369 LocationSummary* locations = insn->GetLocations();
7370 Location out = locations->Out();
7371 Register const_area = locations->InAt(0).AsRegister<Register>();
7372 HConstant *value = insn->GetConstant();
7373
7374 switch (insn->GetType()) {
7375 case Primitive::kPrimFloat:
7376 __ movss(out.AsFpuRegister<XmmRegister>(),
7377 codegen_->LiteralFloatAddress(value->AsFloatConstant()->GetValue(), const_area));
7378 break;
7379
7380 case Primitive::kPrimDouble:
7381 __ movsd(out.AsFpuRegister<XmmRegister>(),
7382 codegen_->LiteralDoubleAddress(value->AsDoubleConstant()->GetValue(), const_area));
7383 break;
7384
7385 case Primitive::kPrimInt:
7386 __ movl(out.AsRegister<Register>(),
7387 codegen_->LiteralInt32Address(value->AsIntConstant()->GetValue(), const_area));
7388 break;
7389
7390 default:
7391 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
7392 }
7393}
7394
Mark Mendell0616ae02015-04-17 12:49:27 -04007395/**
7396 * Class to handle late fixup of offsets into constant area.
7397 */
Vladimir Marko5233f932015-09-29 19:01:15 +01007398class RIPFixup : public AssemblerFixup, public ArenaObject<kArenaAllocCodeGenerator> {
Mark Mendell0616ae02015-04-17 12:49:27 -04007399 public:
Mark Mendell805b3b52015-09-18 14:10:29 -04007400 RIPFixup(CodeGeneratorX86& codegen, size_t offset)
7401 : codegen_(&codegen), offset_into_constant_area_(offset) {}
7402
7403 protected:
7404 void SetOffset(size_t offset) { offset_into_constant_area_ = offset; }
7405
7406 CodeGeneratorX86* codegen_;
Mark Mendell0616ae02015-04-17 12:49:27 -04007407
7408 private:
7409 void Process(const MemoryRegion& region, int pos) OVERRIDE {
7410 // Patch the correct offset for the instruction. The place to patch is the
7411 // last 4 bytes of the instruction.
7412 // The value to patch is the distance from the offset in the constant area
7413 // from the address computed by the HX86ComputeBaseMethodAddress instruction.
Mark Mendell805b3b52015-09-18 14:10:29 -04007414 int32_t constant_offset = codegen_->ConstantAreaStart() + offset_into_constant_area_;
7415 int32_t relative_position = constant_offset - codegen_->GetMethodAddressOffset();;
Mark Mendell0616ae02015-04-17 12:49:27 -04007416
7417 // Patch in the right value.
7418 region.StoreUnaligned<int32_t>(pos - 4, relative_position);
7419 }
7420
Mark Mendell0616ae02015-04-17 12:49:27 -04007421 // Location in constant area that the fixup refers to.
Mark Mendell805b3b52015-09-18 14:10:29 -04007422 int32_t offset_into_constant_area_;
Mark Mendell0616ae02015-04-17 12:49:27 -04007423};
7424
Mark Mendell805b3b52015-09-18 14:10:29 -04007425/**
7426 * Class to handle late fixup of offsets to a jump table that will be created in the
7427 * constant area.
7428 */
7429class JumpTableRIPFixup : public RIPFixup {
7430 public:
7431 JumpTableRIPFixup(CodeGeneratorX86& codegen, HX86PackedSwitch* switch_instr)
7432 : RIPFixup(codegen, static_cast<size_t>(-1)), switch_instr_(switch_instr) {}
7433
7434 void CreateJumpTable() {
7435 X86Assembler* assembler = codegen_->GetAssembler();
7436
7437 // Ensure that the reference to the jump table has the correct offset.
7438 const int32_t offset_in_constant_table = assembler->ConstantAreaSize();
7439 SetOffset(offset_in_constant_table);
7440
7441 // The label values in the jump table are computed relative to the
7442 // instruction addressing the constant area.
7443 const int32_t relative_offset = codegen_->GetMethodAddressOffset();
7444
7445 // Populate the jump table with the correct values for the jump table.
7446 int32_t num_entries = switch_instr_->GetNumEntries();
7447 HBasicBlock* block = switch_instr_->GetBlock();
7448 const ArenaVector<HBasicBlock*>& successors = block->GetSuccessors();
7449 // The value that we want is the target offset - the position of the table.
7450 for (int32_t i = 0; i < num_entries; i++) {
7451 HBasicBlock* b = successors[i];
7452 Label* l = codegen_->GetLabelOf(b);
7453 DCHECK(l->IsBound());
7454 int32_t offset_to_block = l->Position() - relative_offset;
7455 assembler->AppendInt32(offset_to_block);
7456 }
7457 }
7458
7459 private:
7460 const HX86PackedSwitch* switch_instr_;
7461};
7462
7463void CodeGeneratorX86::Finalize(CodeAllocator* allocator) {
7464 // Generate the constant area if needed.
7465 X86Assembler* assembler = GetAssembler();
7466 if (!assembler->IsConstantAreaEmpty() || !fixups_to_jump_tables_.empty()) {
7467 // Align to 4 byte boundary to reduce cache misses, as the data is 4 and 8
7468 // byte values.
7469 assembler->Align(4, 0);
7470 constant_area_start_ = assembler->CodeSize();
7471
7472 // Populate any jump tables.
7473 for (auto jump_table : fixups_to_jump_tables_) {
7474 jump_table->CreateJumpTable();
7475 }
7476
7477 // And now add the constant area to the generated code.
7478 assembler->AddConstantArea();
7479 }
7480
7481 // And finish up.
7482 CodeGenerator::Finalize(allocator);
7483}
7484
Mark Mendell0616ae02015-04-17 12:49:27 -04007485Address CodeGeneratorX86::LiteralDoubleAddress(double v, Register reg) {
7486 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddDouble(v));
7487 return Address(reg, kDummy32BitOffset, fixup);
7488}
7489
7490Address CodeGeneratorX86::LiteralFloatAddress(float v, Register reg) {
7491 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddFloat(v));
7492 return Address(reg, kDummy32BitOffset, fixup);
7493}
7494
7495Address CodeGeneratorX86::LiteralInt32Address(int32_t v, Register reg) {
7496 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt32(v));
7497 return Address(reg, kDummy32BitOffset, fixup);
7498}
7499
7500Address CodeGeneratorX86::LiteralInt64Address(int64_t v, Register reg) {
7501 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt64(v));
7502 return Address(reg, kDummy32BitOffset, fixup);
7503}
7504
Aart Bika19616e2016-02-01 18:57:58 -08007505void CodeGeneratorX86::Load32BitValue(Register dest, int32_t value) {
7506 if (value == 0) {
7507 __ xorl(dest, dest);
7508 } else {
7509 __ movl(dest, Immediate(value));
7510 }
7511}
7512
7513void CodeGeneratorX86::Compare32BitValue(Register dest, int32_t value) {
7514 if (value == 0) {
7515 __ testl(dest, dest);
7516 } else {
7517 __ cmpl(dest, Immediate(value));
7518 }
7519}
7520
Mark Mendell805b3b52015-09-18 14:10:29 -04007521Address CodeGeneratorX86::LiteralCaseTable(HX86PackedSwitch* switch_instr,
7522 Register reg,
7523 Register value) {
7524 // Create a fixup to be used to create and address the jump table.
7525 JumpTableRIPFixup* table_fixup =
7526 new (GetGraph()->GetArena()) JumpTableRIPFixup(*this, switch_instr);
7527
7528 // We have to populate the jump tables.
7529 fixups_to_jump_tables_.push_back(table_fixup);
7530
7531 // We want a scaled address, as we are extracting the correct offset from the table.
7532 return Address(reg, value, TIMES_4, kDummy32BitOffset, table_fixup);
7533}
7534
Andreas Gampe85b62f22015-09-09 13:15:38 -07007535// TODO: target as memory.
7536void CodeGeneratorX86::MoveFromReturnRegister(Location target, Primitive::Type type) {
7537 if (!target.IsValid()) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007538 DCHECK_EQ(type, Primitive::kPrimVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07007539 return;
7540 }
7541
7542 DCHECK_NE(type, Primitive::kPrimVoid);
7543
7544 Location return_loc = InvokeDexCallingConventionVisitorX86().GetReturnLocation(type);
7545 if (target.Equals(return_loc)) {
7546 return;
7547 }
7548
7549 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
7550 // with the else branch.
7551 if (type == Primitive::kPrimLong) {
7552 HParallelMove parallel_move(GetGraph()->GetArena());
7553 parallel_move.AddMove(return_loc.ToLow(), target.ToLow(), Primitive::kPrimInt, nullptr);
7554 parallel_move.AddMove(return_loc.ToHigh(), target.ToHigh(), Primitive::kPrimInt, nullptr);
7555 GetMoveResolver()->EmitNativeCode(&parallel_move);
7556 } else {
7557 // Let the parallel move resolver take care of all of this.
7558 HParallelMove parallel_move(GetGraph()->GetArena());
7559 parallel_move.AddMove(return_loc, target, type, nullptr);
7560 GetMoveResolver()->EmitNativeCode(&parallel_move);
7561 }
7562}
7563
Roland Levillain4d027112015-07-01 15:41:14 +01007564#undef __
7565
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00007566} // namespace x86
7567} // namespace art