blob: 528e94f76b4d81620b461db74e63d25efede9f42 [file] [log] [blame]
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "code_generator_x86.h"
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +010018
Mathieu Chartiere401d142015-04-22 13:56:20 -070019#include "art_method.h"
Guillaume Sanchez0f88e872015-03-30 17:55:45 +010020#include "code_generator_utils.h"
Vladimir Marko58155012015-08-19 12:49:41 +000021#include "compiled_method.h"
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +010022#include "entrypoints/quick/quick_entrypoints.h"
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +000023#include "entrypoints/quick/quick_entrypoints_enum.h"
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +010024#include "gc/accounting/card_table.h"
Mark Mendell09ed1a32015-03-25 08:30:06 -040025#include "intrinsics.h"
26#include "intrinsics_x86.h"
Ian Rogers7e70b002014-10-08 11:47:24 -070027#include "mirror/array-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070028#include "mirror/class-inl.h"
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +010029#include "thread.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000030#include "utils/assembler.h"
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010031#include "utils/stack_checks.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000032#include "utils/x86/assembler_x86.h"
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010033#include "utils/x86/managed_register_x86.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000034
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000035namespace art {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010036
Roland Levillain0d5a2812015-11-13 10:07:31 +000037template<class MirrorType>
38class GcRoot;
39
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000040namespace x86 {
41
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010042static constexpr int kCurrentMethodStackOffset = 0;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +010043static constexpr Register kMethodRegisterArgument = EAX;
Mark Mendell5f874182015-03-04 15:42:45 -050044static constexpr Register kCoreCalleeSaves[] = { EBP, ESI, EDI };
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010045
Mark Mendell24f2dfa2015-01-14 19:51:45 -050046static constexpr int kC2ConditionMask = 0x400;
47
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +000048static constexpr int kFakeReturnRegister = Register(8);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +000049
Chih-Hung Hsiehfba39972016-05-11 11:26:48 -070050// NOLINT on __ macro to suppress wrong warning/fix from clang-tidy.
51#define __ down_cast<X86Assembler*>(codegen->GetAssembler())-> // NOLINT
Calin Juravle175dc732015-08-25 15:42:32 +010052#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kX86WordSize, x).Int32Value()
Nicolas Geoffraye5038322014-07-04 09:41:32 +010053
Andreas Gampe85b62f22015-09-09 13:15:38 -070054class NullCheckSlowPathX86 : public SlowPathCode {
Nicolas Geoffraye5038322014-07-04 09:41:32 +010055 public:
David Srbecky9cd6d372016-02-09 15:24:47 +000056 explicit NullCheckSlowPathX86(HNullCheck* instruction) : SlowPathCode(instruction) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +010057
Alexandre Rames2ed20af2015-03-06 13:55:35 +000058 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames8158f282015-08-07 10:26:17 +010059 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffraye5038322014-07-04 09:41:32 +010060 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +000061 if (instruction_->CanThrowIntoCatchBlock()) {
62 // Live registers will be restored in the catch block if caught.
63 SaveLiveRegisters(codegen, instruction_->GetLocations());
64 }
Alexandre Rames8158f282015-08-07 10:26:17 +010065 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pThrowNullPointer),
66 instruction_,
67 instruction_->GetDexPc(),
68 this);
Roland Levillain888d0672015-11-23 18:53:50 +000069 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +010070 }
71
Alexandre Rames8158f282015-08-07 10:26:17 +010072 bool IsFatal() const OVERRIDE { return true; }
73
Alexandre Rames9931f312015-06-19 14:47:01 +010074 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathX86"; }
75
Nicolas Geoffraye5038322014-07-04 09:41:32 +010076 private:
Nicolas Geoffraye5038322014-07-04 09:41:32 +010077 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathX86);
78};
79
Andreas Gampe85b62f22015-09-09 13:15:38 -070080class DivZeroCheckSlowPathX86 : public SlowPathCode {
Calin Juravled0d48522014-11-04 16:40:20 +000081 public:
David Srbecky9cd6d372016-02-09 15:24:47 +000082 explicit DivZeroCheckSlowPathX86(HDivZeroCheck* instruction) : SlowPathCode(instruction) {}
Calin Juravled0d48522014-11-04 16:40:20 +000083
Alexandre Rames2ed20af2015-03-06 13:55:35 +000084 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames8158f282015-08-07 10:26:17 +010085 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Calin Juravled0d48522014-11-04 16:40:20 +000086 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +000087 if (instruction_->CanThrowIntoCatchBlock()) {
88 // Live registers will be restored in the catch block if caught.
89 SaveLiveRegisters(codegen, instruction_->GetLocations());
90 }
Alexandre Rames8158f282015-08-07 10:26:17 +010091 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pThrowDivZero),
92 instruction_,
93 instruction_->GetDexPc(),
94 this);
Roland Levillain888d0672015-11-23 18:53:50 +000095 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
Calin Juravled0d48522014-11-04 16:40:20 +000096 }
97
Alexandre Rames8158f282015-08-07 10:26:17 +010098 bool IsFatal() const OVERRIDE { return true; }
99
Alexandre Rames9931f312015-06-19 14:47:01 +0100100 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathX86"; }
101
Calin Juravled0d48522014-11-04 16:40:20 +0000102 private:
Calin Juravled0d48522014-11-04 16:40:20 +0000103 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathX86);
104};
105
Andreas Gampe85b62f22015-09-09 13:15:38 -0700106class DivRemMinusOneSlowPathX86 : public SlowPathCode {
Calin Juravled0d48522014-11-04 16:40:20 +0000107 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000108 DivRemMinusOneSlowPathX86(HInstruction* instruction, Register reg, bool is_div)
109 : SlowPathCode(instruction), reg_(reg), is_div_(is_div) {}
Calin Juravled0d48522014-11-04 16:40:20 +0000110
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000111 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +0000112 __ Bind(GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +0000113 if (is_div_) {
114 __ negl(reg_);
115 } else {
116 __ movl(reg_, Immediate(0));
117 }
Calin Juravled0d48522014-11-04 16:40:20 +0000118 __ jmp(GetExitLabel());
119 }
120
Alexandre Rames9931f312015-06-19 14:47:01 +0100121 const char* GetDescription() const OVERRIDE { return "DivRemMinusOneSlowPathX86"; }
122
Calin Juravled0d48522014-11-04 16:40:20 +0000123 private:
124 Register reg_;
Calin Juravlebacfec32014-11-14 15:54:36 +0000125 bool is_div_;
126 DISALLOW_COPY_AND_ASSIGN(DivRemMinusOneSlowPathX86);
Calin Juravled0d48522014-11-04 16:40:20 +0000127};
128
Andreas Gampe85b62f22015-09-09 13:15:38 -0700129class BoundsCheckSlowPathX86 : public SlowPathCode {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100130 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000131 explicit BoundsCheckSlowPathX86(HBoundsCheck* instruction) : SlowPathCode(instruction) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100132
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000133 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100134 LocationSummary* locations = instruction_->GetLocations();
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100135 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100136 __ Bind(GetEntryLabel());
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000137 // We're moving two locations to locations that could overlap, so we need a parallel
138 // move resolver.
David Brazdil77a48ae2015-09-15 12:34:04 +0000139 if (instruction_->CanThrowIntoCatchBlock()) {
140 // Live registers will be restored in the catch block if caught.
141 SaveLiveRegisters(codegen, instruction_->GetLocations());
142 }
Mark Mendellee8d9712016-07-12 11:13:15 -0400143
144 // Are we using an array length from memory?
145 HInstruction* array_length = instruction_->InputAt(1);
146 Location length_loc = locations->InAt(1);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100147 InvokeRuntimeCallingConvention calling_convention;
Mark Mendellee8d9712016-07-12 11:13:15 -0400148 if (array_length->IsArrayLength() && array_length->IsEmittedAtUseSite()) {
149 // Load the array length into our temporary.
150 uint32_t len_offset = CodeGenerator::GetArrayLengthOffset(array_length->AsArrayLength());
151 Location array_loc = array_length->GetLocations()->InAt(0);
152 Address array_len(array_loc.AsRegister<Register>(), len_offset);
153 length_loc = Location::RegisterLocation(calling_convention.GetRegisterAt(1));
154 // Check for conflicts with index.
155 if (length_loc.Equals(locations->InAt(0))) {
156 // We know we aren't using parameter 2.
157 length_loc = Location::RegisterLocation(calling_convention.GetRegisterAt(2));
158 }
159 __ movl(length_loc.AsRegister<Register>(), array_len);
160 }
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000161 x86_codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100162 locations->InAt(0),
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000163 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100164 Primitive::kPrimInt,
Mark Mendellee8d9712016-07-12 11:13:15 -0400165 length_loc,
Nicolas Geoffray90218252015-04-15 11:56:51 +0100166 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
167 Primitive::kPrimInt);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100168 uint32_t entry_point_offset = instruction_->AsBoundsCheck()->IsStringCharAt()
169 ? QUICK_ENTRY_POINT(pThrowStringBounds)
170 : QUICK_ENTRY_POINT(pThrowArrayBounds);
171 x86_codegen->InvokeRuntime(entry_point_offset,
Alexandre Rames8158f282015-08-07 10:26:17 +0100172 instruction_,
173 instruction_->GetDexPc(),
174 this);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100175 CheckEntrypointTypes<kQuickThrowStringBounds, void, int32_t, int32_t>();
Roland Levillain888d0672015-11-23 18:53:50 +0000176 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100177 }
178
Alexandre Rames8158f282015-08-07 10:26:17 +0100179 bool IsFatal() const OVERRIDE { return true; }
180
Alexandre Rames9931f312015-06-19 14:47:01 +0100181 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathX86"; }
182
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100183 private:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100184 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathX86);
185};
186
Andreas Gampe85b62f22015-09-09 13:15:38 -0700187class SuspendCheckSlowPathX86 : public SlowPathCode {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000188 public:
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000189 SuspendCheckSlowPathX86(HSuspendCheck* instruction, HBasicBlock* successor)
David Srbecky9cd6d372016-02-09 15:24:47 +0000190 : SlowPathCode(instruction), successor_(successor) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000191
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000192 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100193 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000194 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000195 SaveLiveRegisters(codegen, instruction_->GetLocations());
Alexandre Rames8158f282015-08-07 10:26:17 +0100196 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pTestSuspend),
197 instruction_,
198 instruction_->GetDexPc(),
199 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000200 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000201 RestoreLiveRegisters(codegen, instruction_->GetLocations());
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100202 if (successor_ == nullptr) {
203 __ jmp(GetReturnLabel());
204 } else {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100205 __ jmp(x86_codegen->GetLabelOf(successor_));
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100206 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000207 }
208
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100209 Label* GetReturnLabel() {
210 DCHECK(successor_ == nullptr);
211 return &return_label_;
212 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000213
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100214 HBasicBlock* GetSuccessor() const {
215 return successor_;
216 }
217
Alexandre Rames9931f312015-06-19 14:47:01 +0100218 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathX86"; }
219
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000220 private:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100221 HBasicBlock* const successor_;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000222 Label return_label_;
223
224 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathX86);
225};
226
Andreas Gampe85b62f22015-09-09 13:15:38 -0700227class LoadStringSlowPathX86 : public SlowPathCode {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000228 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000229 explicit LoadStringSlowPathX86(HLoadString* instruction): SlowPathCode(instruction) {}
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000230
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000231 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000232 LocationSummary* locations = instruction_->GetLocations();
233 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
234
235 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
236 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000237 SaveLiveRegisters(codegen, locations);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000238
239 InvokeRuntimeCallingConvention calling_convention;
David Srbecky9cd6d372016-02-09 15:24:47 +0000240 const uint32_t string_index = instruction_->AsLoadString()->GetStringIndex();
241 __ movl(calling_convention.GetRegisterAt(0), Immediate(string_index));
Alexandre Rames8158f282015-08-07 10:26:17 +0100242 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pResolveString),
243 instruction_,
244 instruction_->GetDexPc(),
245 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000246 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000247 x86_codegen->Move32(locations->Out(), Location::RegisterLocation(EAX));
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000248 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000249
250 __ jmp(GetExitLabel());
251 }
252
Alexandre Rames9931f312015-06-19 14:47:01 +0100253 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathX86"; }
254
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000255 private:
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000256 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathX86);
257};
258
Andreas Gampe85b62f22015-09-09 13:15:38 -0700259class LoadClassSlowPathX86 : public SlowPathCode {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000260 public:
261 LoadClassSlowPathX86(HLoadClass* cls,
262 HInstruction* at,
263 uint32_t dex_pc,
264 bool do_clinit)
David Srbecky9cd6d372016-02-09 15:24:47 +0000265 : SlowPathCode(at), cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000266 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
267 }
268
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000269 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000270 LocationSummary* locations = at_->GetLocations();
271 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
272 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000273 SaveLiveRegisters(codegen, locations);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000274
275 InvokeRuntimeCallingConvention calling_convention;
276 __ movl(calling_convention.GetRegisterAt(0), Immediate(cls_->GetTypeIndex()));
Alexandre Rames8158f282015-08-07 10:26:17 +0100277 x86_codegen->InvokeRuntime(do_clinit_ ? QUICK_ENTRY_POINT(pInitializeStaticStorage)
278 : QUICK_ENTRY_POINT(pInitializeType),
279 at_, dex_pc_, this);
Roland Levillain888d0672015-11-23 18:53:50 +0000280 if (do_clinit_) {
281 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
282 } else {
283 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
284 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000285
286 // Move the class to the desired location.
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000287 Location out = locations->Out();
288 if (out.IsValid()) {
289 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
290 x86_codegen->Move32(out, Location::RegisterLocation(EAX));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000291 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000292
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000293 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000294 __ jmp(GetExitLabel());
295 }
296
Alexandre Rames9931f312015-06-19 14:47:01 +0100297 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathX86"; }
298
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000299 private:
300 // The class this slow path will load.
301 HLoadClass* const cls_;
302
303 // The instruction where this slow path is happening.
304 // (Might be the load class or an initialization check).
305 HInstruction* const at_;
306
307 // The dex PC of `at_`.
308 const uint32_t dex_pc_;
309
310 // Whether to initialize the class.
311 const bool do_clinit_;
312
313 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathX86);
314};
315
Andreas Gampe85b62f22015-09-09 13:15:38 -0700316class TypeCheckSlowPathX86 : public SlowPathCode {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000317 public:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000318 TypeCheckSlowPathX86(HInstruction* instruction, bool is_fatal)
David Srbecky9cd6d372016-02-09 15:24:47 +0000319 : SlowPathCode(instruction), is_fatal_(is_fatal) {}
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000320
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000321 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000322 LocationSummary* locations = instruction_->GetLocations();
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100323 Location object_class = instruction_->IsCheckCast() ? locations->GetTemp(0)
324 : locations->Out();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000325 DCHECK(instruction_->IsCheckCast()
326 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000327
328 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
329 __ Bind(GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000330
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000331 if (!is_fatal_) {
332 SaveLiveRegisters(codegen, locations);
333 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000334
335 // We're moving two locations to locations that could overlap, so we need a parallel
336 // move resolver.
337 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000338 x86_codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100339 locations->InAt(1),
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000340 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100341 Primitive::kPrimNot,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100342 object_class,
Nicolas Geoffray90218252015-04-15 11:56:51 +0100343 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
344 Primitive::kPrimNot);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000345
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000346 if (instruction_->IsInstanceOf()) {
Alexandre Rames8158f282015-08-07 10:26:17 +0100347 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pInstanceofNonTrivial),
348 instruction_,
349 instruction_->GetDexPc(),
350 this);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000351 CheckEntrypointTypes<
Andreas Gampe67409972016-07-19 22:34:53 -0700352 kQuickInstanceofNonTrivial, size_t, const mirror::Class*, const mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000353 } else {
354 DCHECK(instruction_->IsCheckCast());
Alexandre Rames8158f282015-08-07 10:26:17 +0100355 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pCheckCast),
356 instruction_,
357 instruction_->GetDexPc(),
358 this);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000359 CheckEntrypointTypes<kQuickCheckCast, void, const mirror::Class*, const mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000360 }
361
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000362 if (!is_fatal_) {
363 if (instruction_->IsInstanceOf()) {
364 x86_codegen->Move32(locations->Out(), Location::RegisterLocation(EAX));
365 }
366 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray75374372015-09-17 17:12:19 +0000367
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000368 __ jmp(GetExitLabel());
369 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000370 }
371
Alexandre Rames9931f312015-06-19 14:47:01 +0100372 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathX86"; }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000373 bool IsFatal() const OVERRIDE { return is_fatal_; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100374
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000375 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000376 const bool is_fatal_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000377
378 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathX86);
379};
380
Andreas Gampe85b62f22015-09-09 13:15:38 -0700381class DeoptimizationSlowPathX86 : public SlowPathCode {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700382 public:
Aart Bik42249c32016-01-07 15:33:50 -0800383 explicit DeoptimizationSlowPathX86(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000384 : SlowPathCode(instruction) {}
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700385
386 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames8158f282015-08-07 10:26:17 +0100387 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700388 __ Bind(GetEntryLabel());
389 SaveLiveRegisters(codegen, instruction_->GetLocations());
Alexandre Rames8158f282015-08-07 10:26:17 +0100390 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pDeoptimize),
391 instruction_,
392 instruction_->GetDexPc(),
393 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000394 CheckEntrypointTypes<kQuickDeoptimize, void, void>();
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700395 }
396
Alexandre Rames9931f312015-06-19 14:47:01 +0100397 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathX86"; }
398
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700399 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700400 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathX86);
401};
402
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100403class ArraySetSlowPathX86 : public SlowPathCode {
404 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000405 explicit ArraySetSlowPathX86(HInstruction* instruction) : SlowPathCode(instruction) {}
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100406
407 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
408 LocationSummary* locations = instruction_->GetLocations();
409 __ Bind(GetEntryLabel());
410 SaveLiveRegisters(codegen, locations);
411
412 InvokeRuntimeCallingConvention calling_convention;
413 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
414 parallel_move.AddMove(
415 locations->InAt(0),
416 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
417 Primitive::kPrimNot,
418 nullptr);
419 parallel_move.AddMove(
420 locations->InAt(1),
421 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
422 Primitive::kPrimInt,
423 nullptr);
424 parallel_move.AddMove(
425 locations->InAt(2),
426 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
427 Primitive::kPrimNot,
428 nullptr);
429 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
430
431 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
432 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pAputObject),
433 instruction_,
434 instruction_->GetDexPc(),
435 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000436 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100437 RestoreLiveRegisters(codegen, locations);
438 __ jmp(GetExitLabel());
439 }
440
441 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathX86"; }
442
443 private:
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100444 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathX86);
445};
446
Roland Levillain7c1559a2015-12-15 10:55:36 +0000447// Slow path marking an object during a read barrier.
448class ReadBarrierMarkSlowPathX86 : public SlowPathCode {
449 public:
Roland Levillain02b75802016-07-13 11:54:35 +0100450 ReadBarrierMarkSlowPathX86(HInstruction* instruction, Location obj)
451 : SlowPathCode(instruction), obj_(obj) {
Roland Levillain7c1559a2015-12-15 10:55:36 +0000452 DCHECK(kEmitCompilerReadBarrier);
453 }
454
455 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathX86"; }
456
457 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
458 LocationSummary* locations = instruction_->GetLocations();
Roland Levillain02b75802016-07-13 11:54:35 +0100459 Register reg = obj_.AsRegister<Register>();
Roland Levillain7c1559a2015-12-15 10:55:36 +0000460 DCHECK(locations->CanCall());
Roland Levillain02b75802016-07-13 11:54:35 +0100461 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg));
Roland Levillain7c1559a2015-12-15 10:55:36 +0000462 DCHECK(instruction_->IsInstanceFieldGet() ||
463 instruction_->IsStaticFieldGet() ||
464 instruction_->IsArrayGet() ||
465 instruction_->IsLoadClass() ||
466 instruction_->IsLoadString() ||
467 instruction_->IsInstanceOf() ||
Roland Levillain3d312422016-06-23 13:53:42 +0100468 instruction_->IsCheckCast() ||
Roland Levillaindec8f632016-07-22 17:10:06 +0100469 (instruction_->IsInvokeVirtual()) && instruction_->GetLocations()->Intrinsified())
Roland Levillain7c1559a2015-12-15 10:55:36 +0000470 << "Unexpected instruction in read barrier marking slow path: "
471 << instruction_->DebugName();
472
473 __ Bind(GetEntryLabel());
Roland Levillain4359e612016-07-20 11:32:19 +0100474 // No need to save live registers; it's taken care of by the
475 // entrypoint. Also, there is no need to update the stack mask,
476 // as this runtime call will not trigger a garbage collection.
Roland Levillain7c1559a2015-12-15 10:55:36 +0000477 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Roland Levillain02b75802016-07-13 11:54:35 +0100478 DCHECK_NE(reg, ESP);
479 DCHECK(0 <= reg && reg < kNumberOfCpuRegisters) << reg;
480 // "Compact" slow path, saving two moves.
481 //
482 // Instead of using the standard runtime calling convention (input
483 // and output in EAX):
484 //
485 // EAX <- obj
486 // EAX <- ReadBarrierMark(EAX)
487 // obj <- EAX
488 //
489 // we just use rX (the register holding `obj`) as input and output
490 // of a dedicated entrypoint:
491 //
492 // rX <- ReadBarrierMarkRegX(rX)
493 //
494 int32_t entry_point_offset =
495 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kX86WordSize>(reg);
Roland Levillaindec8f632016-07-22 17:10:06 +0100496 // This runtime call does not require a stack map.
497 x86_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
Roland Levillain7c1559a2015-12-15 10:55:36 +0000498 __ jmp(GetExitLabel());
499 }
500
501 private:
Roland Levillain7c1559a2015-12-15 10:55:36 +0000502 const Location obj_;
503
504 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathX86);
505};
506
Roland Levillain0d5a2812015-11-13 10:07:31 +0000507// Slow path generating a read barrier for a heap reference.
508class ReadBarrierForHeapReferenceSlowPathX86 : public SlowPathCode {
509 public:
510 ReadBarrierForHeapReferenceSlowPathX86(HInstruction* instruction,
511 Location out,
512 Location ref,
513 Location obj,
514 uint32_t offset,
515 Location index)
David Srbecky9cd6d372016-02-09 15:24:47 +0000516 : SlowPathCode(instruction),
Roland Levillain0d5a2812015-11-13 10:07:31 +0000517 out_(out),
518 ref_(ref),
519 obj_(obj),
520 offset_(offset),
521 index_(index) {
522 DCHECK(kEmitCompilerReadBarrier);
523 // If `obj` is equal to `out` or `ref`, it means the initial object
524 // has been overwritten by (or after) the heap object reference load
525 // to be instrumented, e.g.:
526 //
527 // __ movl(out, Address(out, offset));
Roland Levillain7c1559a2015-12-15 10:55:36 +0000528 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000529 //
530 // In that case, we have lost the information about the original
531 // object, and the emitted read barrier cannot work properly.
532 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
533 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
534 }
535
536 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
537 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
538 LocationSummary* locations = instruction_->GetLocations();
539 Register reg_out = out_.AsRegister<Register>();
540 DCHECK(locations->CanCall());
541 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
Roland Levillain3d312422016-06-23 13:53:42 +0100542 DCHECK(instruction_->IsInstanceFieldGet() ||
543 instruction_->IsStaticFieldGet() ||
544 instruction_->IsArrayGet() ||
545 instruction_->IsInstanceOf() ||
546 instruction_->IsCheckCast() ||
Roland Levillaindec8f632016-07-22 17:10:06 +0100547 (instruction_->IsInvokeVirtual()) && instruction_->GetLocations()->Intrinsified())
Roland Levillain7c1559a2015-12-15 10:55:36 +0000548 << "Unexpected instruction in read barrier for heap reference slow path: "
549 << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000550
551 __ Bind(GetEntryLabel());
552 SaveLiveRegisters(codegen, locations);
553
554 // We may have to change the index's value, but as `index_` is a
555 // constant member (like other "inputs" of this slow path),
556 // introduce a copy of it, `index`.
557 Location index = index_;
558 if (index_.IsValid()) {
Roland Levillain3d312422016-06-23 13:53:42 +0100559 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
Roland Levillain0d5a2812015-11-13 10:07:31 +0000560 if (instruction_->IsArrayGet()) {
561 // Compute the actual memory offset and store it in `index`.
562 Register index_reg = index_.AsRegister<Register>();
563 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg));
564 if (codegen->IsCoreCalleeSaveRegister(index_reg)) {
565 // We are about to change the value of `index_reg` (see the
566 // calls to art::x86::X86Assembler::shll and
567 // art::x86::X86Assembler::AddImmediate below), but it has
568 // not been saved by the previous call to
569 // art::SlowPathCode::SaveLiveRegisters, as it is a
570 // callee-save register --
571 // art::SlowPathCode::SaveLiveRegisters does not consider
572 // callee-save registers, as it has been designed with the
573 // assumption that callee-save registers are supposed to be
574 // handled by the called function. So, as a callee-save
575 // register, `index_reg` _would_ eventually be saved onto
576 // the stack, but it would be too late: we would have
577 // changed its value earlier. Therefore, we manually save
578 // it here into another freely available register,
579 // `free_reg`, chosen of course among the caller-save
580 // registers (as a callee-save `free_reg` register would
581 // exhibit the same problem).
582 //
583 // Note we could have requested a temporary register from
584 // the register allocator instead; but we prefer not to, as
585 // this is a slow path, and we know we can find a
586 // caller-save register that is available.
587 Register free_reg = FindAvailableCallerSaveRegister(codegen);
588 __ movl(free_reg, index_reg);
589 index_reg = free_reg;
590 index = Location::RegisterLocation(index_reg);
591 } else {
592 // The initial register stored in `index_` has already been
593 // saved in the call to art::SlowPathCode::SaveLiveRegisters
594 // (as it is not a callee-save register), so we can freely
595 // use it.
596 }
597 // Shifting the index value contained in `index_reg` by the scale
598 // factor (2) cannot overflow in practice, as the runtime is
599 // unable to allocate object arrays with a size larger than
600 // 2^26 - 1 (that is, 2^28 - 4 bytes).
601 __ shll(index_reg, Immediate(TIMES_4));
602 static_assert(
603 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
604 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
605 __ AddImmediate(index_reg, Immediate(offset_));
606 } else {
Roland Levillain3d312422016-06-23 13:53:42 +0100607 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
608 // intrinsics, `index_` is not shifted by a scale factor of 2
609 // (as in the case of ArrayGet), as it is actually an offset
610 // to an object field within an object.
611 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000612 DCHECK(instruction_->GetLocations()->Intrinsified());
613 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
614 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
615 << instruction_->AsInvoke()->GetIntrinsic();
616 DCHECK_EQ(offset_, 0U);
617 DCHECK(index_.IsRegisterPair());
618 // UnsafeGet's offset location is a register pair, the low
619 // part contains the correct offset.
620 index = index_.ToLow();
621 }
622 }
623
624 // We're moving two or three locations to locations that could
625 // overlap, so we need a parallel move resolver.
626 InvokeRuntimeCallingConvention calling_convention;
627 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
628 parallel_move.AddMove(ref_,
629 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
630 Primitive::kPrimNot,
631 nullptr);
632 parallel_move.AddMove(obj_,
633 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
634 Primitive::kPrimNot,
635 nullptr);
636 if (index.IsValid()) {
637 parallel_move.AddMove(index,
638 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
639 Primitive::kPrimInt,
640 nullptr);
641 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
642 } else {
643 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
644 __ movl(calling_convention.GetRegisterAt(2), Immediate(offset_));
645 }
646 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pReadBarrierSlow),
647 instruction_,
648 instruction_->GetDexPc(),
649 this);
650 CheckEntrypointTypes<
651 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
652 x86_codegen->Move32(out_, Location::RegisterLocation(EAX));
653
654 RestoreLiveRegisters(codegen, locations);
655 __ jmp(GetExitLabel());
656 }
657
658 const char* GetDescription() const OVERRIDE { return "ReadBarrierForHeapReferenceSlowPathX86"; }
659
660 private:
661 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
662 size_t ref = static_cast<int>(ref_.AsRegister<Register>());
663 size_t obj = static_cast<int>(obj_.AsRegister<Register>());
664 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
665 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
666 return static_cast<Register>(i);
667 }
668 }
669 // We shall never fail to find a free caller-save register, as
670 // there are more than two core caller-save registers on x86
671 // (meaning it is possible to find one which is different from
672 // `ref` and `obj`).
673 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
674 LOG(FATAL) << "Could not find a free caller-save register";
675 UNREACHABLE();
676 }
677
Roland Levillain0d5a2812015-11-13 10:07:31 +0000678 const Location out_;
679 const Location ref_;
680 const Location obj_;
681 const uint32_t offset_;
682 // An additional location containing an index to an array.
683 // Only used for HArrayGet and the UnsafeGetObject &
684 // UnsafeGetObjectVolatile intrinsics.
685 const Location index_;
686
687 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathX86);
688};
689
690// Slow path generating a read barrier for a GC root.
691class ReadBarrierForRootSlowPathX86 : public SlowPathCode {
692 public:
693 ReadBarrierForRootSlowPathX86(HInstruction* instruction, Location out, Location root)
David Srbecky9cd6d372016-02-09 15:24:47 +0000694 : SlowPathCode(instruction), out_(out), root_(root) {
Roland Levillain7c1559a2015-12-15 10:55:36 +0000695 DCHECK(kEmitCompilerReadBarrier);
696 }
Roland Levillain0d5a2812015-11-13 10:07:31 +0000697
698 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
699 LocationSummary* locations = instruction_->GetLocations();
700 Register reg_out = out_.AsRegister<Register>();
701 DCHECK(locations->CanCall());
702 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
Roland Levillain7c1559a2015-12-15 10:55:36 +0000703 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
704 << "Unexpected instruction in read barrier for GC root slow path: "
705 << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000706
707 __ Bind(GetEntryLabel());
708 SaveLiveRegisters(codegen, locations);
709
710 InvokeRuntimeCallingConvention calling_convention;
711 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
712 x86_codegen->Move32(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), root_);
713 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pReadBarrierForRootSlow),
714 instruction_,
715 instruction_->GetDexPc(),
716 this);
717 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
718 x86_codegen->Move32(out_, Location::RegisterLocation(EAX));
719
720 RestoreLiveRegisters(codegen, locations);
721 __ jmp(GetExitLabel());
722 }
723
724 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathX86"; }
725
726 private:
Roland Levillain0d5a2812015-11-13 10:07:31 +0000727 const Location out_;
728 const Location root_;
729
730 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathX86);
731};
732
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100733#undef __
Chih-Hung Hsiehfba39972016-05-11 11:26:48 -0700734// NOLINT on __ macro to suppress wrong warning/fix from clang-tidy.
735#define __ down_cast<X86Assembler*>(GetAssembler())-> /* NOLINT */
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100736
Aart Bike9f37602015-10-09 11:15:55 -0700737inline Condition X86Condition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -0700738 switch (cond) {
739 case kCondEQ: return kEqual;
740 case kCondNE: return kNotEqual;
741 case kCondLT: return kLess;
742 case kCondLE: return kLessEqual;
743 case kCondGT: return kGreater;
744 case kCondGE: return kGreaterEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700745 case kCondB: return kBelow;
746 case kCondBE: return kBelowEqual;
747 case kCondA: return kAbove;
748 case kCondAE: return kAboveEqual;
Dave Allison20dfc792014-06-16 20:44:29 -0700749 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100750 LOG(FATAL) << "Unreachable";
751 UNREACHABLE();
752}
753
Aart Bike9f37602015-10-09 11:15:55 -0700754// Maps signed condition to unsigned condition and FP condition to x86 name.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100755inline Condition X86UnsignedOrFPCondition(IfCondition cond) {
756 switch (cond) {
757 case kCondEQ: return kEqual;
758 case kCondNE: return kNotEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700759 // Signed to unsigned, and FP to x86 name.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100760 case kCondLT: return kBelow;
761 case kCondLE: return kBelowEqual;
762 case kCondGT: return kAbove;
763 case kCondGE: return kAboveEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700764 // Unsigned remain unchanged.
765 case kCondB: return kBelow;
766 case kCondBE: return kBelowEqual;
767 case kCondA: return kAbove;
768 case kCondAE: return kAboveEqual;
Roland Levillain4fa13f62015-07-06 18:11:54 +0100769 }
770 LOG(FATAL) << "Unreachable";
771 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -0700772}
773
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100774void CodeGeneratorX86::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100775 stream << Register(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100776}
777
778void CodeGeneratorX86::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100779 stream << XmmRegister(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100780}
781
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100782size_t CodeGeneratorX86::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
783 __ movl(Address(ESP, stack_index), static_cast<Register>(reg_id));
784 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100785}
786
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100787size_t CodeGeneratorX86::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
788 __ movl(static_cast<Register>(reg_id), Address(ESP, stack_index));
789 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100790}
791
Mark Mendell7c8d0092015-01-26 11:21:33 -0500792size_t CodeGeneratorX86::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
793 __ movsd(Address(ESP, stack_index), XmmRegister(reg_id));
794 return GetFloatingPointSpillSlotSize();
795}
796
797size_t CodeGeneratorX86::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
798 __ movsd(XmmRegister(reg_id), Address(ESP, stack_index));
799 return GetFloatingPointSpillSlotSize();
800}
801
Calin Juravle175dc732015-08-25 15:42:32 +0100802void CodeGeneratorX86::InvokeRuntime(QuickEntrypointEnum entrypoint,
803 HInstruction* instruction,
804 uint32_t dex_pc,
805 SlowPathCode* slow_path) {
806 InvokeRuntime(GetThreadOffset<kX86WordSize>(entrypoint).Int32Value(),
807 instruction,
808 dex_pc,
809 slow_path);
810}
811
812void CodeGeneratorX86::InvokeRuntime(int32_t entry_point_offset,
Alexandre Rames8158f282015-08-07 10:26:17 +0100813 HInstruction* instruction,
814 uint32_t dex_pc,
815 SlowPathCode* slow_path) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +0100816 ValidateInvokeRuntime(instruction, slow_path);
Calin Juravle175dc732015-08-25 15:42:32 +0100817 __ fs()->call(Address::Absolute(entry_point_offset));
Alexandre Rames8158f282015-08-07 10:26:17 +0100818 RecordPcInfo(instruction, dex_pc, slow_path);
Alexandre Rames8158f282015-08-07 10:26:17 +0100819}
820
Roland Levillaindec8f632016-07-22 17:10:06 +0100821void CodeGeneratorX86::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
822 HInstruction* instruction,
823 SlowPathCode* slow_path) {
824 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
825 __ fs()->call(Address::Absolute(entry_point_offset));
826}
827
Mark Mendellfb8d2792015-03-31 22:16:59 -0400828CodeGeneratorX86::CodeGeneratorX86(HGraph* graph,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000829 const X86InstructionSetFeatures& isa_features,
830 const CompilerOptions& compiler_options,
831 OptimizingCompilerStats* stats)
Mark Mendell5f874182015-03-04 15:42:45 -0500832 : CodeGenerator(graph,
833 kNumberOfCpuRegisters,
834 kNumberOfXmmRegisters,
835 kNumberOfRegisterPairs,
836 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
837 arraysize(kCoreCalleeSaves))
838 | (1 << kFakeReturnRegister),
Serban Constantinescuecc43662015-08-13 13:33:12 +0100839 0,
840 compiler_options,
841 stats),
Vladimir Marko225b6462015-09-28 12:17:40 +0100842 block_labels_(nullptr),
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100843 location_builder_(graph, this),
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100844 instruction_visitor_(graph, this),
Mark Mendellfb8d2792015-03-31 22:16:59 -0400845 move_resolver_(graph->GetArena(), this),
Vladimir Marko93205e32016-04-13 11:59:46 +0100846 assembler_(graph->GetArena()),
Vladimir Marko58155012015-08-19 12:49:41 +0000847 isa_features_(isa_features),
Vladimir Marko5233f932015-09-29 19:01:15 +0100848 method_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Mark Mendell805b3b52015-09-18 14:10:29 -0400849 relative_call_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000850 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000851 simple_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
852 string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100853 type_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko93205e32016-04-13 11:59:46 +0100854 constant_area_start_(-1),
855 fixups_to_jump_tables_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
856 method_address_offset_(-1) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000857 // Use a fake return address register to mimic Quick.
858 AddAllocatedRegister(Location::RegisterLocation(kFakeReturnRegister));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100859}
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100860
David Brazdil58282f42016-01-14 12:45:10 +0000861void CodeGeneratorX86::SetupBlockedRegisters() const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100862 // Don't allocate the dalvik style register pair passing.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100863 blocked_register_pairs_[ECX_EDX] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100864
865 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100866 blocked_core_registers_[ESP] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100867
Calin Juravle34bacdf2014-10-07 20:23:36 +0100868 UpdateBlockedPairRegisters();
869}
870
871void CodeGeneratorX86::UpdateBlockedPairRegisters() const {
872 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
873 X86ManagedRegister current =
874 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
875 if (blocked_core_registers_[current.AsRegisterPairLow()]
876 || blocked_core_registers_[current.AsRegisterPairHigh()]) {
877 blocked_register_pairs_[i] = true;
878 }
879 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100880}
881
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100882InstructionCodeGeneratorX86::InstructionCodeGeneratorX86(HGraph* graph, CodeGeneratorX86* codegen)
Aart Bik42249c32016-01-07 15:33:50 -0800883 : InstructionCodeGenerator(graph, codegen),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100884 assembler_(codegen->GetAssembler()),
885 codegen_(codegen) {}
886
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100887static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +0100888 return dwarf::Reg::X86Core(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100889}
890
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000891void CodeGeneratorX86::GenerateFrameEntry() {
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100892 __ cfi().SetCurrentCFAOffset(kX86WordSize); // return address
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000893 __ Bind(&frame_entry_label_);
Roland Levillain199f3362014-11-27 17:15:16 +0000894 bool skip_overflow_check =
895 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000896 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Calin Juravle93edf732015-01-20 20:14:07 +0000897
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000898 if (!skip_overflow_check) {
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100899 __ testl(EAX, Address(ESP, -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86))));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100900 RecordPcInfo(nullptr, 0);
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100901 }
902
Mark Mendell5f874182015-03-04 15:42:45 -0500903 if (HasEmptyFrame()) {
904 return;
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000905 }
Mark Mendell5f874182015-03-04 15:42:45 -0500906
907 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
908 Register reg = kCoreCalleeSaves[i];
909 if (allocated_registers_.ContainsCoreRegister(reg)) {
910 __ pushl(reg);
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100911 __ cfi().AdjustCFAOffset(kX86WordSize);
912 __ cfi().RelOffset(DWARFReg(reg), 0);
Mark Mendell5f874182015-03-04 15:42:45 -0500913 }
914 }
915
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100916 int adjust = GetFrameSize() - FrameEntrySpillSize();
917 __ subl(ESP, Immediate(adjust));
918 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100919 __ movl(Address(ESP, kCurrentMethodStackOffset), kMethodRegisterArgument);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000920}
921
922void CodeGeneratorX86::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +0100923 __ cfi().RememberState();
924 if (!HasEmptyFrame()) {
925 int adjust = GetFrameSize() - FrameEntrySpillSize();
926 __ addl(ESP, Immediate(adjust));
927 __ cfi().AdjustCFAOffset(-adjust);
Mark Mendell5f874182015-03-04 15:42:45 -0500928
David Srbeckyc34dc932015-04-12 09:27:43 +0100929 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
930 Register reg = kCoreCalleeSaves[i];
931 if (allocated_registers_.ContainsCoreRegister(reg)) {
932 __ popl(reg);
933 __ cfi().AdjustCFAOffset(-static_cast<int>(kX86WordSize));
934 __ cfi().Restore(DWARFReg(reg));
935 }
Mark Mendell5f874182015-03-04 15:42:45 -0500936 }
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000937 }
David Srbeckyc34dc932015-04-12 09:27:43 +0100938 __ ret();
939 __ cfi().RestoreState();
940 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000941}
942
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100943void CodeGeneratorX86::Bind(HBasicBlock* block) {
944 __ Bind(GetLabelOf(block));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000945}
946
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100947Location InvokeDexCallingConventionVisitorX86::GetReturnLocation(Primitive::Type type) const {
948 switch (type) {
949 case Primitive::kPrimBoolean:
950 case Primitive::kPrimByte:
951 case Primitive::kPrimChar:
952 case Primitive::kPrimShort:
953 case Primitive::kPrimInt:
954 case Primitive::kPrimNot:
955 return Location::RegisterLocation(EAX);
956
957 case Primitive::kPrimLong:
958 return Location::RegisterPairLocation(EAX, EDX);
959
960 case Primitive::kPrimVoid:
961 return Location::NoLocation();
962
963 case Primitive::kPrimDouble:
964 case Primitive::kPrimFloat:
965 return Location::FpuRegisterLocation(XMM0);
966 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +0100967
968 UNREACHABLE();
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100969}
970
971Location InvokeDexCallingConventionVisitorX86::GetMethodLocation() const {
972 return Location::RegisterLocation(kMethodRegisterArgument);
973}
974
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100975Location InvokeDexCallingConventionVisitorX86::GetNextLocation(Primitive::Type type) {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100976 switch (type) {
977 case Primitive::kPrimBoolean:
978 case Primitive::kPrimByte:
979 case Primitive::kPrimChar:
980 case Primitive::kPrimShort:
981 case Primitive::kPrimInt:
982 case Primitive::kPrimNot: {
983 uint32_t index = gp_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000984 stack_index_++;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100985 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100986 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100987 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000988 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100989 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100990 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100991
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000992 case Primitive::kPrimLong: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100993 uint32_t index = gp_index_;
994 gp_index_ += 2;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000995 stack_index_ += 2;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100996 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100997 X86ManagedRegister pair = X86ManagedRegister::FromRegisterPair(
998 calling_convention.GetRegisterPairAt(index));
999 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001000 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001001 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
1002 }
1003 }
1004
1005 case Primitive::kPrimFloat: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001006 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001007 stack_index_++;
1008 if (index < calling_convention.GetNumberOfFpuRegisters()) {
1009 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
1010 } else {
1011 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
1012 }
1013 }
1014
1015 case Primitive::kPrimDouble: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001016 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001017 stack_index_ += 2;
1018 if (index < calling_convention.GetNumberOfFpuRegisters()) {
1019 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
1020 } else {
1021 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001022 }
1023 }
1024
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001025 case Primitive::kPrimVoid:
1026 LOG(FATAL) << "Unexpected parameter type " << type;
1027 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001028 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00001029 return Location::NoLocation();
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001030}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001031
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001032void CodeGeneratorX86::Move32(Location destination, Location source) {
1033 if (source.Equals(destination)) {
1034 return;
1035 }
1036 if (destination.IsRegister()) {
1037 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001038 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001039 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001040 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001041 } else {
1042 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001043 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001044 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001045 } else if (destination.IsFpuRegister()) {
1046 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001047 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001048 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001049 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001050 } else {
1051 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001052 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001053 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001054 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001055 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001056 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001057 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001058 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001059 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Mark Mendell7c8d0092015-01-26 11:21:33 -05001060 } else if (source.IsConstant()) {
1061 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001062 int32_t value = GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05001063 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001064 } else {
1065 DCHECK(source.IsStackSlot());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001066 __ pushl(Address(ESP, source.GetStackIndex()));
1067 __ popl(Address(ESP, destination.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001068 }
1069 }
1070}
1071
1072void CodeGeneratorX86::Move64(Location destination, Location source) {
1073 if (source.Equals(destination)) {
1074 return;
1075 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001076 if (destination.IsRegisterPair()) {
1077 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001078 EmitParallelMoves(
1079 Location::RegisterLocation(source.AsRegisterPairHigh<Register>()),
1080 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001081 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001082 Location::RegisterLocation(source.AsRegisterPairLow<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001083 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()),
1084 Primitive::kPrimInt);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001085 } else if (source.IsFpuRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001086 XmmRegister src_reg = source.AsFpuRegister<XmmRegister>();
1087 __ movd(destination.AsRegisterPairLow<Register>(), src_reg);
1088 __ psrlq(src_reg, Immediate(32));
1089 __ movd(destination.AsRegisterPairHigh<Register>(), src_reg);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001090 } else {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001091 // No conflict possible, so just do the moves.
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001092 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001093 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
1094 __ movl(destination.AsRegisterPairHigh<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001095 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
1096 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001097 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05001098 if (source.IsFpuRegister()) {
1099 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
1100 } else if (source.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001101 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01001102 } else if (source.IsRegisterPair()) {
1103 size_t elem_size = Primitive::ComponentSize(Primitive::kPrimInt);
1104 // Create stack space for 2 elements.
1105 __ subl(ESP, Immediate(2 * elem_size));
1106 __ movl(Address(ESP, 0), source.AsRegisterPairLow<Register>());
1107 __ movl(Address(ESP, elem_size), source.AsRegisterPairHigh<Register>());
1108 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
1109 // And remove the temporary stack space we allocated.
1110 __ addl(ESP, Immediate(2 * elem_size));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001111 } else {
1112 LOG(FATAL) << "Unimplemented";
1113 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001114 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001115 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001116 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001117 // No conflict possible, so just do the moves.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001118 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegisterPairLow<Register>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001119 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001120 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001121 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001122 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001123 } else if (source.IsConstant()) {
1124 HConstant* constant = source.GetConstant();
1125 int64_t value;
1126 if (constant->IsLongConstant()) {
1127 value = constant->AsLongConstant()->GetValue();
1128 } else {
1129 DCHECK(constant->IsDoubleConstant());
Roland Levillainda4d79b2015-03-24 14:36:11 +00001130 value = bit_cast<int64_t, double>(constant->AsDoubleConstant()->GetValue());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001131 }
1132 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(Low32Bits(value)));
1133 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), Immediate(High32Bits(value)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001134 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001135 DCHECK(source.IsDoubleStackSlot()) << source;
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001136 EmitParallelMoves(
1137 Location::StackSlot(source.GetStackIndex()),
1138 Location::StackSlot(destination.GetStackIndex()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001139 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001140 Location::StackSlot(source.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001141 Location::StackSlot(destination.GetHighStackIndex(kX86WordSize)),
1142 Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001143 }
1144 }
1145}
1146
Calin Juravle175dc732015-08-25 15:42:32 +01001147void CodeGeneratorX86::MoveConstant(Location location, int32_t value) {
1148 DCHECK(location.IsRegister());
1149 __ movl(location.AsRegister<Register>(), Immediate(value));
1150}
1151
Calin Juravlee460d1d2015-09-29 04:52:17 +01001152void CodeGeneratorX86::MoveLocation(Location dst, Location src, Primitive::Type dst_type) {
David Brazdil74eb1b22015-12-14 11:44:01 +00001153 HParallelMove move(GetGraph()->GetArena());
1154 if (dst_type == Primitive::kPrimLong && !src.IsConstant() && !src.IsFpuRegister()) {
1155 move.AddMove(src.ToLow(), dst.ToLow(), Primitive::kPrimInt, nullptr);
1156 move.AddMove(src.ToHigh(), dst.ToHigh(), Primitive::kPrimInt, nullptr);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001157 } else {
David Brazdil74eb1b22015-12-14 11:44:01 +00001158 move.AddMove(src, dst, dst_type, nullptr);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001159 }
David Brazdil74eb1b22015-12-14 11:44:01 +00001160 GetMoveResolver()->EmitNativeCode(&move);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001161}
1162
1163void CodeGeneratorX86::AddLocationAsTemp(Location location, LocationSummary* locations) {
1164 if (location.IsRegister()) {
1165 locations->AddTemp(location);
1166 } else if (location.IsRegisterPair()) {
1167 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
1168 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
1169 } else {
1170 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1171 }
1172}
1173
David Brazdilfc6a86a2015-06-26 10:33:45 +00001174void InstructionCodeGeneratorX86::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001175 DCHECK(!successor->IsExitBlock());
1176
1177 HBasicBlock* block = got->GetBlock();
1178 HInstruction* previous = got->GetPrevious();
1179
1180 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +00001181 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001182 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1183 return;
1184 }
1185
1186 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1187 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1188 }
1189 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001190 __ jmp(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001191 }
1192}
1193
David Brazdilfc6a86a2015-06-26 10:33:45 +00001194void LocationsBuilderX86::VisitGoto(HGoto* got) {
1195 got->SetLocations(nullptr);
1196}
1197
1198void InstructionCodeGeneratorX86::VisitGoto(HGoto* got) {
1199 HandleGoto(got, got->GetSuccessor());
1200}
1201
1202void LocationsBuilderX86::VisitTryBoundary(HTryBoundary* try_boundary) {
1203 try_boundary->SetLocations(nullptr);
1204}
1205
1206void InstructionCodeGeneratorX86::VisitTryBoundary(HTryBoundary* try_boundary) {
1207 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
1208 if (!successor->IsExitBlock()) {
1209 HandleGoto(try_boundary, successor);
1210 }
1211}
1212
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001213void LocationsBuilderX86::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001214 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001215}
1216
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001217void InstructionCodeGeneratorX86::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001218}
1219
Mark Mendell152408f2015-12-31 12:28:50 -05001220template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001221void InstructionCodeGeneratorX86::GenerateFPJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001222 LabelType* true_label,
1223 LabelType* false_label) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001224 if (cond->IsFPConditionTrueIfNaN()) {
1225 __ j(kUnordered, true_label);
1226 } else if (cond->IsFPConditionFalseIfNaN()) {
1227 __ j(kUnordered, false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001228 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001229 __ j(X86UnsignedOrFPCondition(cond->GetCondition()), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001230}
1231
Mark Mendell152408f2015-12-31 12:28:50 -05001232template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001233void InstructionCodeGeneratorX86::GenerateLongComparesAndJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001234 LabelType* true_label,
1235 LabelType* false_label) {
Mark Mendellc4701932015-04-10 13:18:51 -04001236 LocationSummary* locations = cond->GetLocations();
1237 Location left = locations->InAt(0);
1238 Location right = locations->InAt(1);
1239 IfCondition if_cond = cond->GetCondition();
1240
Mark Mendellc4701932015-04-10 13:18:51 -04001241 Register left_high = left.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001242 Register left_low = left.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001243 IfCondition true_high_cond = if_cond;
1244 IfCondition false_high_cond = cond->GetOppositeCondition();
Aart Bike9f37602015-10-09 11:15:55 -07001245 Condition final_condition = X86UnsignedOrFPCondition(if_cond); // unsigned on lower part
Mark Mendellc4701932015-04-10 13:18:51 -04001246
1247 // Set the conditions for the test, remembering that == needs to be
1248 // decided using the low words.
1249 switch (if_cond) {
1250 case kCondEQ:
Mark Mendellc4701932015-04-10 13:18:51 -04001251 case kCondNE:
Roland Levillain4fa13f62015-07-06 18:11:54 +01001252 // Nothing to do.
Mark Mendellc4701932015-04-10 13:18:51 -04001253 break;
1254 case kCondLT:
1255 false_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -04001256 break;
1257 case kCondLE:
1258 true_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -04001259 break;
1260 case kCondGT:
1261 false_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -04001262 break;
1263 case kCondGE:
1264 true_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -04001265 break;
Aart Bike9f37602015-10-09 11:15:55 -07001266 case kCondB:
1267 false_high_cond = kCondA;
1268 break;
1269 case kCondBE:
1270 true_high_cond = kCondB;
1271 break;
1272 case kCondA:
1273 false_high_cond = kCondB;
1274 break;
1275 case kCondAE:
1276 true_high_cond = kCondA;
1277 break;
Mark Mendellc4701932015-04-10 13:18:51 -04001278 }
1279
1280 if (right.IsConstant()) {
1281 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
Mark Mendellc4701932015-04-10 13:18:51 -04001282 int32_t val_high = High32Bits(value);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001283 int32_t val_low = Low32Bits(value);
Mark Mendellc4701932015-04-10 13:18:51 -04001284
Aart Bika19616e2016-02-01 18:57:58 -08001285 codegen_->Compare32BitValue(left_high, val_high);
Mark Mendellc4701932015-04-10 13:18:51 -04001286 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.
Aart Bika19616e2016-02-01 18:57:58 -08001295 codegen_->Compare32BitValue(left_low, val_low);
Mark Mendell8659e842016-02-16 10:41:46 -05001296 } else if (right.IsRegisterPair()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001297 Register right_high = right.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001298 Register right_low = right.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001299
1300 __ cmpl(left_high, right_high);
1301 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001302 __ j(X86Condition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001303 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001304 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001305 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001306 __ j(X86Condition(true_high_cond), true_label);
1307 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001308 }
1309 // Must be equal high, so compare the lows.
1310 __ cmpl(left_low, right_low);
Mark Mendell8659e842016-02-16 10:41:46 -05001311 } else {
1312 DCHECK(right.IsDoubleStackSlot());
1313 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
1314 if (if_cond == kCondNE) {
1315 __ j(X86Condition(true_high_cond), true_label);
1316 } else if (if_cond == kCondEQ) {
1317 __ j(X86Condition(false_high_cond), false_label);
1318 } else {
1319 __ j(X86Condition(true_high_cond), true_label);
1320 __ j(X86Condition(false_high_cond), false_label);
1321 }
1322 // Must be equal high, so compare the lows.
1323 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Mark Mendellc4701932015-04-10 13:18:51 -04001324 }
1325 // The last comparison might be unsigned.
1326 __ j(final_condition, true_label);
1327}
1328
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001329void InstructionCodeGeneratorX86::GenerateFPCompare(Location lhs,
1330 Location rhs,
1331 HInstruction* insn,
1332 bool is_double) {
1333 HX86LoadFromConstantTable* const_area = insn->InputAt(1)->AsX86LoadFromConstantTable();
1334 if (is_double) {
1335 if (rhs.IsFpuRegister()) {
1336 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1337 } else if (const_area != nullptr) {
1338 DCHECK(const_area->IsEmittedAtUseSite());
1339 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(),
1340 codegen_->LiteralDoubleAddress(
1341 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
1342 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
1343 } else {
1344 DCHECK(rhs.IsDoubleStackSlot());
1345 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(), Address(ESP, rhs.GetStackIndex()));
1346 }
1347 } else {
1348 if (rhs.IsFpuRegister()) {
1349 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1350 } else if (const_area != nullptr) {
1351 DCHECK(const_area->IsEmittedAtUseSite());
1352 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(),
1353 codegen_->LiteralFloatAddress(
1354 const_area->GetConstant()->AsFloatConstant()->GetValue(),
1355 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
1356 } else {
1357 DCHECK(rhs.IsStackSlot());
1358 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(), Address(ESP, rhs.GetStackIndex()));
1359 }
1360 }
1361}
1362
Mark Mendell152408f2015-12-31 12:28:50 -05001363template<class LabelType>
David Brazdil0debae72015-11-12 18:37:00 +00001364void InstructionCodeGeneratorX86::GenerateCompareTestAndBranch(HCondition* condition,
Mark Mendell152408f2015-12-31 12:28:50 -05001365 LabelType* true_target_in,
1366 LabelType* false_target_in) {
David Brazdil0debae72015-11-12 18:37:00 +00001367 // Generated branching requires both targets to be explicit. If either of the
1368 // targets is nullptr (fallthrough) use and bind `fallthrough_target` instead.
Mark Mendell152408f2015-12-31 12:28:50 -05001369 LabelType fallthrough_target;
1370 LabelType* true_target = true_target_in == nullptr ? &fallthrough_target : true_target_in;
1371 LabelType* false_target = false_target_in == nullptr ? &fallthrough_target : false_target_in;
David Brazdil0debae72015-11-12 18:37:00 +00001372
Mark Mendellc4701932015-04-10 13:18:51 -04001373 LocationSummary* locations = condition->GetLocations();
1374 Location left = locations->InAt(0);
1375 Location right = locations->InAt(1);
1376
Mark Mendellc4701932015-04-10 13:18:51 -04001377 Primitive::Type type = condition->InputAt(0)->GetType();
1378 switch (type) {
1379 case Primitive::kPrimLong:
1380 GenerateLongComparesAndJumps(condition, true_target, false_target);
1381 break;
1382 case Primitive::kPrimFloat:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001383 GenerateFPCompare(left, right, condition, false);
Mark Mendellc4701932015-04-10 13:18:51 -04001384 GenerateFPJumps(condition, true_target, false_target);
1385 break;
1386 case Primitive::kPrimDouble:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001387 GenerateFPCompare(left, right, condition, true);
Mark Mendellc4701932015-04-10 13:18:51 -04001388 GenerateFPJumps(condition, true_target, false_target);
1389 break;
1390 default:
1391 LOG(FATAL) << "Unexpected compare type " << type;
1392 }
1393
David Brazdil0debae72015-11-12 18:37:00 +00001394 if (false_target != &fallthrough_target) {
Mark Mendellc4701932015-04-10 13:18:51 -04001395 __ jmp(false_target);
1396 }
David Brazdil0debae72015-11-12 18:37:00 +00001397
1398 if (fallthrough_target.IsLinked()) {
1399 __ Bind(&fallthrough_target);
1400 }
Mark Mendellc4701932015-04-10 13:18:51 -04001401}
1402
David Brazdil0debae72015-11-12 18:37:00 +00001403static bool AreEflagsSetFrom(HInstruction* cond, HInstruction* branch) {
1404 // Moves may affect the eflags register (move zero uses xorl), so the EFLAGS
1405 // are set only strictly before `branch`. We can't use the eflags on long/FP
1406 // conditions if they are materialized due to the complex branching.
1407 return cond->IsCondition() &&
1408 cond->GetNext() == branch &&
1409 cond->InputAt(0)->GetType() != Primitive::kPrimLong &&
1410 !Primitive::IsFloatingPointType(cond->InputAt(0)->GetType());
1411}
1412
Mark Mendell152408f2015-12-31 12:28:50 -05001413template<class LabelType>
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001414void InstructionCodeGeneratorX86::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00001415 size_t condition_input_index,
Mark Mendell152408f2015-12-31 12:28:50 -05001416 LabelType* true_target,
1417 LabelType* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00001418 HInstruction* cond = instruction->InputAt(condition_input_index);
1419
1420 if (true_target == nullptr && false_target == nullptr) {
1421 // Nothing to do. The code always falls through.
1422 return;
1423 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00001424 // Constant condition, statically compared against "true" (integer value 1).
1425 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00001426 if (true_target != nullptr) {
1427 __ jmp(true_target);
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001428 }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001429 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00001430 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00001431 if (false_target != nullptr) {
1432 __ jmp(false_target);
1433 }
1434 }
1435 return;
1436 }
1437
1438 // The following code generates these patterns:
1439 // (1) true_target == nullptr && false_target != nullptr
1440 // - opposite condition true => branch to false_target
1441 // (2) true_target != nullptr && false_target == nullptr
1442 // - condition true => branch to true_target
1443 // (3) true_target != nullptr && false_target != nullptr
1444 // - condition true => branch to true_target
1445 // - branch to false_target
1446 if (IsBooleanValueOrMaterializedCondition(cond)) {
1447 if (AreEflagsSetFrom(cond, instruction)) {
1448 if (true_target == nullptr) {
1449 __ j(X86Condition(cond->AsCondition()->GetOppositeCondition()), false_target);
1450 } else {
1451 __ j(X86Condition(cond->AsCondition()->GetCondition()), true_target);
1452 }
1453 } else {
1454 // Materialized condition, compare against 0.
1455 Location lhs = instruction->GetLocations()->InAt(condition_input_index);
1456 if (lhs.IsRegister()) {
1457 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
1458 } else {
1459 __ cmpl(Address(ESP, lhs.GetStackIndex()), Immediate(0));
1460 }
1461 if (true_target == nullptr) {
1462 __ j(kEqual, false_target);
1463 } else {
1464 __ j(kNotEqual, true_target);
1465 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001466 }
1467 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001468 // Condition has not been materialized, use its inputs as the comparison and
1469 // its condition as the branch condition.
Mark Mendellb8b97692015-05-22 16:58:19 -04001470 HCondition* condition = cond->AsCondition();
David Brazdil0debae72015-11-12 18:37:00 +00001471
1472 // If this is a long or FP comparison that has been folded into
1473 // the HCondition, generate the comparison directly.
1474 Primitive::Type type = condition->InputAt(0)->GetType();
1475 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
1476 GenerateCompareTestAndBranch(condition, true_target, false_target);
1477 return;
1478 }
1479
1480 Location lhs = condition->GetLocations()->InAt(0);
1481 Location rhs = condition->GetLocations()->InAt(1);
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001482 // LHS is guaranteed to be in a register (see LocationsBuilderX86::HandleCondition).
David Brazdil0debae72015-11-12 18:37:00 +00001483 if (rhs.IsRegister()) {
1484 __ cmpl(lhs.AsRegister<Register>(), rhs.AsRegister<Register>());
1485 } else if (rhs.IsConstant()) {
1486 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
Aart Bika19616e2016-02-01 18:57:58 -08001487 codegen_->Compare32BitValue(lhs.AsRegister<Register>(), constant);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001488 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001489 __ cmpl(lhs.AsRegister<Register>(), Address(ESP, rhs.GetStackIndex()));
1490 }
1491 if (true_target == nullptr) {
1492 __ j(X86Condition(condition->GetOppositeCondition()), false_target);
1493 } else {
Mark Mendellb8b97692015-05-22 16:58:19 -04001494 __ j(X86Condition(condition->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -07001495 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001496 }
David Brazdil0debae72015-11-12 18:37:00 +00001497
1498 // If neither branch falls through (case 3), the conditional branch to `true_target`
1499 // was already emitted (case 2) and we need to emit a jump to `false_target`.
1500 if (true_target != nullptr && false_target != nullptr) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001501 __ jmp(false_target);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001502 }
1503}
1504
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001505void LocationsBuilderX86::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001506 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
1507 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001508 locations->SetInAt(0, Location::Any());
1509 }
1510}
1511
1512void InstructionCodeGeneratorX86::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001513 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
1514 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
1515 Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
1516 nullptr : codegen_->GetLabelOf(true_successor);
1517 Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
1518 nullptr : codegen_->GetLabelOf(false_successor);
1519 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001520}
1521
1522void LocationsBuilderX86::VisitDeoptimize(HDeoptimize* deoptimize) {
1523 LocationSummary* locations = new (GetGraph()->GetArena())
1524 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
David Brazdil0debae72015-11-12 18:37:00 +00001525 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001526 locations->SetInAt(0, Location::Any());
1527 }
1528}
1529
1530void InstructionCodeGeneratorX86::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08001531 SlowPathCode* slow_path = deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathX86>(deoptimize);
David Brazdil74eb1b22015-12-14 11:44:01 +00001532 GenerateTestAndBranch<Label>(deoptimize,
1533 /* condition_input_index */ 0,
1534 slow_path->GetEntryLabel(),
1535 /* false_target */ nullptr);
1536}
1537
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001538static bool SelectCanUseCMOV(HSelect* select) {
1539 // There are no conditional move instructions for XMMs.
1540 if (Primitive::IsFloatingPointType(select->GetType())) {
1541 return false;
1542 }
1543
1544 // A FP condition doesn't generate the single CC that we need.
1545 // In 32 bit mode, a long condition doesn't generate a single CC either.
1546 HInstruction* condition = select->GetCondition();
1547 if (condition->IsCondition()) {
1548 Primitive::Type compare_type = condition->InputAt(0)->GetType();
1549 if (compare_type == Primitive::kPrimLong ||
1550 Primitive::IsFloatingPointType(compare_type)) {
1551 return false;
1552 }
1553 }
1554
1555 // We can generate a CMOV for this Select.
1556 return true;
1557}
1558
David Brazdil74eb1b22015-12-14 11:44:01 +00001559void LocationsBuilderX86::VisitSelect(HSelect* select) {
1560 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001561 if (Primitive::IsFloatingPointType(select->GetType())) {
David Brazdil74eb1b22015-12-14 11:44:01 +00001562 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001563 locations->SetInAt(1, Location::Any());
David Brazdil74eb1b22015-12-14 11:44:01 +00001564 } else {
1565 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001566 if (SelectCanUseCMOV(select)) {
1567 if (select->InputAt(1)->IsConstant()) {
1568 // Cmov can't handle a constant value.
1569 locations->SetInAt(1, Location::RequiresRegister());
1570 } else {
1571 locations->SetInAt(1, Location::Any());
1572 }
1573 } else {
1574 locations->SetInAt(1, Location::Any());
1575 }
David Brazdil74eb1b22015-12-14 11:44:01 +00001576 }
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001577 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
1578 locations->SetInAt(2, Location::RequiresRegister());
David Brazdil74eb1b22015-12-14 11:44:01 +00001579 }
1580 locations->SetOut(Location::SameAsFirstInput());
1581}
1582
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001583void InstructionCodeGeneratorX86::GenerateIntCompare(Location lhs, Location rhs) {
1584 Register lhs_reg = lhs.AsRegister<Register>();
1585 if (rhs.IsConstant()) {
1586 int32_t value = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
1587 codegen_->Compare32BitValue(lhs_reg, value);
1588 } else if (rhs.IsStackSlot()) {
1589 __ cmpl(lhs_reg, Address(ESP, rhs.GetStackIndex()));
1590 } else {
1591 __ cmpl(lhs_reg, rhs.AsRegister<Register>());
1592 }
1593}
1594
David Brazdil74eb1b22015-12-14 11:44:01 +00001595void InstructionCodeGeneratorX86::VisitSelect(HSelect* select) {
1596 LocationSummary* locations = select->GetLocations();
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001597 DCHECK(locations->InAt(0).Equals(locations->Out()));
1598 if (SelectCanUseCMOV(select)) {
1599 // If both the condition and the source types are integer, we can generate
1600 // a CMOV to implement Select.
1601
1602 HInstruction* select_condition = select->GetCondition();
1603 Condition cond = kNotEqual;
1604
1605 // Figure out how to test the 'condition'.
1606 if (select_condition->IsCondition()) {
1607 HCondition* condition = select_condition->AsCondition();
1608 if (!condition->IsEmittedAtUseSite()) {
1609 // This was a previously materialized condition.
1610 // Can we use the existing condition code?
1611 if (AreEflagsSetFrom(condition, select)) {
1612 // Materialization was the previous instruction. Condition codes are right.
1613 cond = X86Condition(condition->GetCondition());
1614 } else {
1615 // No, we have to recreate the condition code.
1616 Register cond_reg = locations->InAt(2).AsRegister<Register>();
1617 __ testl(cond_reg, cond_reg);
1618 }
1619 } else {
1620 // We can't handle FP or long here.
1621 DCHECK_NE(condition->InputAt(0)->GetType(), Primitive::kPrimLong);
1622 DCHECK(!Primitive::IsFloatingPointType(condition->InputAt(0)->GetType()));
1623 LocationSummary* cond_locations = condition->GetLocations();
1624 GenerateIntCompare(cond_locations->InAt(0), cond_locations->InAt(1));
1625 cond = X86Condition(condition->GetCondition());
1626 }
1627 } else {
1628 // Must be a boolean condition, which needs to be compared to 0.
1629 Register cond_reg = locations->InAt(2).AsRegister<Register>();
1630 __ testl(cond_reg, cond_reg);
1631 }
1632
1633 // If the condition is true, overwrite the output, which already contains false.
1634 Location false_loc = locations->InAt(0);
1635 Location true_loc = locations->InAt(1);
1636 if (select->GetType() == Primitive::kPrimLong) {
1637 // 64 bit conditional move.
1638 Register false_high = false_loc.AsRegisterPairHigh<Register>();
1639 Register false_low = false_loc.AsRegisterPairLow<Register>();
1640 if (true_loc.IsRegisterPair()) {
1641 __ cmovl(cond, false_high, true_loc.AsRegisterPairHigh<Register>());
1642 __ cmovl(cond, false_low, true_loc.AsRegisterPairLow<Register>());
1643 } else {
1644 __ cmovl(cond, false_high, Address(ESP, true_loc.GetHighStackIndex(kX86WordSize)));
1645 __ cmovl(cond, false_low, Address(ESP, true_loc.GetStackIndex()));
1646 }
1647 } else {
1648 // 32 bit conditional move.
1649 Register false_reg = false_loc.AsRegister<Register>();
1650 if (true_loc.IsRegister()) {
1651 __ cmovl(cond, false_reg, true_loc.AsRegister<Register>());
1652 } else {
1653 __ cmovl(cond, false_reg, Address(ESP, true_loc.GetStackIndex()));
1654 }
1655 }
1656 } else {
1657 NearLabel false_target;
1658 GenerateTestAndBranch<NearLabel>(
1659 select, /* condition_input_index */ 2, /* true_target */ nullptr, &false_target);
1660 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
1661 __ Bind(&false_target);
1662 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001663}
1664
David Srbecky0cf44932015-12-09 14:09:59 +00001665void LocationsBuilderX86::VisitNativeDebugInfo(HNativeDebugInfo* info) {
1666 new (GetGraph()->GetArena()) LocationSummary(info);
1667}
1668
David Srbeckyd28f4a02016-03-14 17:14:24 +00001669void InstructionCodeGeneratorX86::VisitNativeDebugInfo(HNativeDebugInfo*) {
1670 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00001671}
1672
1673void CodeGeneratorX86::GenerateNop() {
1674 __ nop();
David Srbecky0cf44932015-12-09 14:09:59 +00001675}
1676
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001677void LocationsBuilderX86::HandleCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001678 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +01001679 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Mark Mendellc4701932015-04-10 13:18:51 -04001680 // Handle the long/FP comparisons made in instruction simplification.
1681 switch (cond->InputAt(0)->GetType()) {
1682 case Primitive::kPrimLong: {
1683 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell8659e842016-02-16 10:41:46 -05001684 locations->SetInAt(1, Location::Any());
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 case Primitive::kPrimFloat:
1691 case Primitive::kPrimDouble: {
1692 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001693 if (cond->InputAt(1)->IsX86LoadFromConstantTable()) {
1694 DCHECK(cond->InputAt(1)->IsEmittedAtUseSite());
1695 } else if (cond->InputAt(1)->IsConstant()) {
1696 locations->SetInAt(1, Location::RequiresFpuRegister());
1697 } else {
1698 locations->SetInAt(1, Location::Any());
1699 }
David Brazdilb3e773e2016-01-26 11:28:37 +00001700 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001701 locations->SetOut(Location::RequiresRegister());
1702 }
1703 break;
1704 }
1705 default:
1706 locations->SetInAt(0, Location::RequiresRegister());
1707 locations->SetInAt(1, Location::Any());
David Brazdilb3e773e2016-01-26 11:28:37 +00001708 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001709 // We need a byte register.
1710 locations->SetOut(Location::RegisterLocation(ECX));
1711 }
1712 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001713 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001714}
1715
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001716void InstructionCodeGeneratorX86::HandleCondition(HCondition* cond) {
David Brazdilb3e773e2016-01-26 11:28:37 +00001717 if (cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001718 return;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001719 }
Mark Mendellc4701932015-04-10 13:18:51 -04001720
1721 LocationSummary* locations = cond->GetLocations();
1722 Location lhs = locations->InAt(0);
1723 Location rhs = locations->InAt(1);
1724 Register reg = locations->Out().AsRegister<Register>();
Mark Mendell152408f2015-12-31 12:28:50 -05001725 NearLabel true_label, false_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001726
1727 switch (cond->InputAt(0)->GetType()) {
1728 default: {
1729 // Integer case.
1730
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01001731 // Clear output register: setb only sets the low byte.
Mark Mendellc4701932015-04-10 13:18:51 -04001732 __ xorl(reg, reg);
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001733 GenerateIntCompare(lhs, rhs);
Aart Bike9f37602015-10-09 11:15:55 -07001734 __ setb(X86Condition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001735 return;
1736 }
1737 case Primitive::kPrimLong:
1738 GenerateLongComparesAndJumps(cond, &true_label, &false_label);
1739 break;
1740 case Primitive::kPrimFloat:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001741 GenerateFPCompare(lhs, rhs, cond, false);
Mark Mendellc4701932015-04-10 13:18:51 -04001742 GenerateFPJumps(cond, &true_label, &false_label);
1743 break;
1744 case Primitive::kPrimDouble:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001745 GenerateFPCompare(lhs, rhs, cond, true);
Mark Mendellc4701932015-04-10 13:18:51 -04001746 GenerateFPJumps(cond, &true_label, &false_label);
1747 break;
1748 }
1749
1750 // Convert the jumps into the result.
Mark Mendell0c9497d2015-08-21 09:30:05 -04001751 NearLabel done_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001752
Roland Levillain4fa13f62015-07-06 18:11:54 +01001753 // False case: result = 0.
Mark Mendellc4701932015-04-10 13:18:51 -04001754 __ Bind(&false_label);
1755 __ xorl(reg, reg);
1756 __ jmp(&done_label);
1757
Roland Levillain4fa13f62015-07-06 18:11:54 +01001758 // True case: result = 1.
Mark Mendellc4701932015-04-10 13:18:51 -04001759 __ Bind(&true_label);
1760 __ movl(reg, Immediate(1));
1761 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07001762}
1763
1764void LocationsBuilderX86::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001765 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001766}
1767
1768void InstructionCodeGeneratorX86::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001769 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001770}
1771
1772void LocationsBuilderX86::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001773 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001774}
1775
1776void InstructionCodeGeneratorX86::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001777 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001778}
1779
1780void LocationsBuilderX86::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001781 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001782}
1783
1784void InstructionCodeGeneratorX86::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001785 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001786}
1787
1788void LocationsBuilderX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001789 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001790}
1791
1792void InstructionCodeGeneratorX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001793 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001794}
1795
1796void LocationsBuilderX86::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001797 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001798}
1799
1800void InstructionCodeGeneratorX86::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001801 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001802}
1803
1804void LocationsBuilderX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001805 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001806}
1807
1808void InstructionCodeGeneratorX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001809 HandleCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001810}
1811
Aart Bike9f37602015-10-09 11:15:55 -07001812void LocationsBuilderX86::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001813 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001814}
1815
1816void InstructionCodeGeneratorX86::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001817 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001818}
1819
1820void LocationsBuilderX86::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001821 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001822}
1823
1824void InstructionCodeGeneratorX86::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001825 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001826}
1827
1828void LocationsBuilderX86::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001829 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001830}
1831
1832void InstructionCodeGeneratorX86::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001833 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001834}
1835
1836void LocationsBuilderX86::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001837 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001838}
1839
1840void InstructionCodeGeneratorX86::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001841 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001842}
1843
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001844void LocationsBuilderX86::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001845 LocationSummary* locations =
1846 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001847 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001848}
1849
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001850void InstructionCodeGeneratorX86::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001851 // Will be generated at use site.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001852}
1853
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001854void LocationsBuilderX86::VisitNullConstant(HNullConstant* constant) {
1855 LocationSummary* locations =
1856 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1857 locations->SetOut(Location::ConstantLocation(constant));
1858}
1859
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001860void InstructionCodeGeneratorX86::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001861 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001862}
1863
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001864void LocationsBuilderX86::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001865 LocationSummary* locations =
1866 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001867 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001868}
1869
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001870void InstructionCodeGeneratorX86::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001871 // Will be generated at use site.
1872}
1873
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001874void LocationsBuilderX86::VisitFloatConstant(HFloatConstant* constant) {
1875 LocationSummary* locations =
1876 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1877 locations->SetOut(Location::ConstantLocation(constant));
1878}
1879
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001880void InstructionCodeGeneratorX86::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001881 // Will be generated at use site.
1882}
1883
1884void LocationsBuilderX86::VisitDoubleConstant(HDoubleConstant* constant) {
1885 LocationSummary* locations =
1886 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1887 locations->SetOut(Location::ConstantLocation(constant));
1888}
1889
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001890void InstructionCodeGeneratorX86::VisitDoubleConstant(HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001891 // Will be generated at use site.
1892}
1893
Calin Juravle27df7582015-04-17 19:12:31 +01001894void LocationsBuilderX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1895 memory_barrier->SetLocations(nullptr);
1896}
1897
1898void InstructionCodeGeneratorX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00001899 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01001900}
1901
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001902void LocationsBuilderX86::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001903 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001904}
1905
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001906void InstructionCodeGeneratorX86::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001907 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001908}
1909
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001910void LocationsBuilderX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001911 LocationSummary* locations =
1912 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001913 switch (ret->InputAt(0)->GetType()) {
1914 case Primitive::kPrimBoolean:
1915 case Primitive::kPrimByte:
1916 case Primitive::kPrimChar:
1917 case Primitive::kPrimShort:
1918 case Primitive::kPrimInt:
1919 case Primitive::kPrimNot:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001920 locations->SetInAt(0, Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001921 break;
1922
1923 case Primitive::kPrimLong:
1924 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001925 0, Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001926 break;
1927
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001928 case Primitive::kPrimFloat:
1929 case Primitive::kPrimDouble:
1930 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001931 0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001932 break;
1933
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001934 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001935 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001936 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001937}
1938
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001939void InstructionCodeGeneratorX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001940 if (kIsDebugBuild) {
1941 switch (ret->InputAt(0)->GetType()) {
1942 case Primitive::kPrimBoolean:
1943 case Primitive::kPrimByte:
1944 case Primitive::kPrimChar:
1945 case Primitive::kPrimShort:
1946 case Primitive::kPrimInt:
1947 case Primitive::kPrimNot:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001948 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<Register>(), EAX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001949 break;
1950
1951 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001952 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairLow<Register>(), EAX);
1953 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairHigh<Register>(), EDX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001954 break;
1955
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001956 case Primitive::kPrimFloat:
1957 case Primitive::kPrimDouble:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001958 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>(), XMM0);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001959 break;
1960
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001961 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001962 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001963 }
1964 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001965 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001966}
1967
Calin Juravle175dc732015-08-25 15:42:32 +01001968void LocationsBuilderX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
1969 // The trampoline uses the same calling convention as dex calling conventions,
1970 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
1971 // the method_idx.
1972 HandleInvoke(invoke);
1973}
1974
1975void InstructionCodeGeneratorX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
1976 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
1977}
1978
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001979void LocationsBuilderX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00001980 // Explicit clinit checks triggered by static invokes must have been pruned by
1981 // art::PrepareForRegisterAllocation.
1982 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001983
Mark Mendellfb8d2792015-03-31 22:16:59 -04001984 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001985 if (intrinsic.TryDispatch(invoke)) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00001986 if (invoke->GetLocations()->CanCall() && invoke->HasPcRelativeDexCache()) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00001987 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00001988 }
Mark Mendell09ed1a32015-03-25 08:30:06 -04001989 return;
1990 }
1991
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001992 HandleInvoke(invoke);
Nicolas Geoffray94015b92015-06-04 18:21:04 +01001993
Vladimir Marko0f7dca42015-11-02 14:36:43 +00001994 // For PC-relative dex cache the invoke has an extra input, the PC-relative address base.
1995 if (invoke->HasPcRelativeDexCache()) {
Vladimir Markob4536b72015-11-24 13:45:23 +00001996 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00001997 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001998}
1999
Mark Mendell09ed1a32015-03-25 08:30:06 -04002000static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86* codegen) {
2001 if (invoke->GetLocations()->Intrinsified()) {
2002 IntrinsicCodeGeneratorX86 intrinsic(codegen);
2003 intrinsic.Dispatch(invoke);
2004 return true;
2005 }
2006 return false;
2007}
2008
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002009void InstructionCodeGeneratorX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002010 // Explicit clinit checks triggered by static invokes must have been pruned by
2011 // art::PrepareForRegisterAllocation.
2012 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002013
Mark Mendell09ed1a32015-03-25 08:30:06 -04002014 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2015 return;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002016 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002017
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002018 LocationSummary* locations = invoke->GetLocations();
Mark Mendell09ed1a32015-03-25 08:30:06 -04002019 codegen_->GenerateStaticOrDirectCall(
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002020 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Mingyao Yang8693fe12015-04-17 16:51:08 -07002021 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002022}
2023
2024void LocationsBuilderX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00002025 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
2026 if (intrinsic.TryDispatch(invoke)) {
2027 return;
2028 }
2029
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002030 HandleInvoke(invoke);
2031}
2032
2033void LocationsBuilderX86::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002034 InvokeDexCallingConventionVisitorX86 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002035 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002036}
2037
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002038void InstructionCodeGeneratorX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002039 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2040 return;
2041 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002042
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002043 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002044 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01002045 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002046}
2047
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002048void LocationsBuilderX86::VisitInvokeInterface(HInvokeInterface* invoke) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002049 // This call to HandleInvoke allocates a temporary (core) register
2050 // which is also used to transfer the hidden argument from FP to
2051 // core register.
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002052 HandleInvoke(invoke);
2053 // Add the hidden argument.
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002054 invoke->GetLocations()->AddTemp(Location::FpuRegisterLocation(XMM7));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002055}
2056
2057void InstructionCodeGeneratorX86::VisitInvokeInterface(HInvokeInterface* invoke) {
2058 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain0d5a2812015-11-13 10:07:31 +00002059 LocationSummary* locations = invoke->GetLocations();
2060 Register temp = locations->GetTemp(0).AsRegister<Register>();
2061 XmmRegister hidden_reg = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002062 Location receiver = locations->InAt(0);
2063 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2064
Roland Levillain0d5a2812015-11-13 10:07:31 +00002065 // Set the hidden argument. This is safe to do this here, as XMM7
2066 // won't be modified thereafter, before the `call` instruction.
2067 DCHECK_EQ(XMM7, hidden_reg);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002068 __ movl(temp, Immediate(invoke->GetDexMethodIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002069 __ movd(hidden_reg, temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002070
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002071 if (receiver.IsStackSlot()) {
2072 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002073 // /* HeapReference<Class> */ temp = temp->klass_
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002074 __ movl(temp, Address(temp, class_offset));
2075 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002076 // /* HeapReference<Class> */ temp = receiver->klass_
Roland Levillain271ab9c2014-11-27 15:23:57 +00002077 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002078 }
Roland Levillain4d027112015-07-01 15:41:14 +01002079 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00002080 // Instead of simply (possibly) unpoisoning `temp` here, we should
2081 // emit a read barrier for the previous class reference load.
2082 // However this is not required in practice, as this is an
2083 // intermediate/temporary reference and because the current
2084 // concurrent copying collector keeps the from-space memory
2085 // intact/accessible until the end of the marking phase (the
2086 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01002087 __ MaybeUnpoisonHeapReference(temp);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002088 // temp = temp->GetAddressOfIMT()
2089 __ movl(temp,
2090 Address(temp, mirror::Class::ImtPtrOffset(kX86PointerSize).Uint32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002091 // temp = temp->GetImtEntryAt(method_offset);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002092 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00002093 invoke->GetImtIndex(), kX86PointerSize));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002094 __ movl(temp, Address(temp, method_offset));
2095 // call temp->GetEntryPoint();
Roland Levillain0d5a2812015-11-13 10:07:31 +00002096 __ call(Address(temp,
2097 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize).Int32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002098
2099 DCHECK(!codegen_->IsLeafMethod());
2100 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2101}
2102
Roland Levillain88cb1752014-10-20 16:36:47 +01002103void LocationsBuilderX86::VisitNeg(HNeg* neg) {
2104 LocationSummary* locations =
2105 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2106 switch (neg->GetResultType()) {
2107 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002108 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01002109 locations->SetInAt(0, Location::RequiresRegister());
2110 locations->SetOut(Location::SameAsFirstInput());
2111 break;
2112
Roland Levillain88cb1752014-10-20 16:36:47 +01002113 case Primitive::kPrimFloat:
Roland Levillain5368c212014-11-27 15:03:41 +00002114 locations->SetInAt(0, Location::RequiresFpuRegister());
2115 locations->SetOut(Location::SameAsFirstInput());
2116 locations->AddTemp(Location::RequiresRegister());
2117 locations->AddTemp(Location::RequiresFpuRegister());
2118 break;
2119
Roland Levillain88cb1752014-10-20 16:36:47 +01002120 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002121 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00002122 locations->SetOut(Location::SameAsFirstInput());
2123 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01002124 break;
2125
2126 default:
2127 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2128 }
2129}
2130
2131void InstructionCodeGeneratorX86::VisitNeg(HNeg* neg) {
2132 LocationSummary* locations = neg->GetLocations();
2133 Location out = locations->Out();
2134 Location in = locations->InAt(0);
2135 switch (neg->GetResultType()) {
2136 case Primitive::kPrimInt:
2137 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002138 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002139 __ negl(out.AsRegister<Register>());
Roland Levillain88cb1752014-10-20 16:36:47 +01002140 break;
2141
2142 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002143 DCHECK(in.IsRegisterPair());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002144 DCHECK(in.Equals(out));
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002145 __ negl(out.AsRegisterPairLow<Register>());
2146 // Negation is similar to subtraction from zero. The least
2147 // significant byte triggers a borrow when it is different from
2148 // zero; to take it into account, add 1 to the most significant
2149 // byte if the carry flag (CF) is set to 1 after the first NEGL
2150 // operation.
2151 __ adcl(out.AsRegisterPairHigh<Register>(), Immediate(0));
2152 __ negl(out.AsRegisterPairHigh<Register>());
2153 break;
2154
Roland Levillain5368c212014-11-27 15:03:41 +00002155 case Primitive::kPrimFloat: {
2156 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002157 Register constant = locations->GetTemp(0).AsRegister<Register>();
2158 XmmRegister mask = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002159 // Implement float negation with an exclusive or with value
2160 // 0x80000000 (mask for bit 31, representing the sign of a
2161 // single-precision floating-point number).
2162 __ movl(constant, Immediate(INT32_C(0x80000000)));
2163 __ movd(mask, constant);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002164 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00002165 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002166 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00002167
Roland Levillain5368c212014-11-27 15:03:41 +00002168 case Primitive::kPrimDouble: {
2169 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002170 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002171 // Implement double negation with an exclusive or with value
2172 // 0x8000000000000000 (mask for bit 63, representing the sign of
2173 // a double-precision floating-point number).
2174 __ LoadLongConstant(mask, INT64_C(0x8000000000000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002175 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01002176 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002177 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002178
2179 default:
2180 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2181 }
2182}
2183
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002184void LocationsBuilderX86::VisitX86FPNeg(HX86FPNeg* neg) {
2185 LocationSummary* locations =
2186 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2187 DCHECK(Primitive::IsFloatingPointType(neg->GetType()));
2188 locations->SetInAt(0, Location::RequiresFpuRegister());
2189 locations->SetInAt(1, Location::RequiresRegister());
2190 locations->SetOut(Location::SameAsFirstInput());
2191 locations->AddTemp(Location::RequiresFpuRegister());
2192}
2193
2194void InstructionCodeGeneratorX86::VisitX86FPNeg(HX86FPNeg* neg) {
2195 LocationSummary* locations = neg->GetLocations();
2196 Location out = locations->Out();
2197 DCHECK(locations->InAt(0).Equals(out));
2198
2199 Register constant_area = locations->InAt(1).AsRegister<Register>();
2200 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
2201 if (neg->GetType() == Primitive::kPrimFloat) {
2202 __ movss(mask, codegen_->LiteralInt32Address(INT32_C(0x80000000), constant_area));
2203 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
2204 } else {
2205 __ movsd(mask, codegen_->LiteralInt64Address(INT64_C(0x8000000000000000), constant_area));
2206 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
2207 }
2208}
2209
Roland Levillaindff1f282014-11-05 14:15:05 +00002210void LocationsBuilderX86::VisitTypeConversion(HTypeConversion* conversion) {
Roland Levillaindff1f282014-11-05 14:15:05 +00002211 Primitive::Type result_type = conversion->GetResultType();
2212 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002213 DCHECK_NE(result_type, input_type);
Roland Levillain624279f2014-12-04 11:54:28 +00002214
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002215 // The float-to-long and double-to-long type conversions rely on a
2216 // call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00002217 LocationSummary::CallKind call_kind =
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002218 ((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
2219 && result_type == Primitive::kPrimLong)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01002220 ? LocationSummary::kCallOnMainOnly
Roland Levillain624279f2014-12-04 11:54:28 +00002221 : LocationSummary::kNoCall;
2222 LocationSummary* locations =
2223 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
2224
David Brazdilb2bd1c52015-03-25 11:17:37 +00002225 // The Java language does not allow treating boolean as an integral type but
2226 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00002227
Roland Levillaindff1f282014-11-05 14:15:05 +00002228 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002229 case Primitive::kPrimByte:
2230 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002231 case Primitive::kPrimLong: {
2232 // Type conversion from long to byte is a result of code transformations.
2233 HInstruction* input = conversion->InputAt(0);
2234 Location input_location = input->IsConstant()
2235 ? Location::ConstantLocation(input->AsConstant())
2236 : Location::RegisterPairLocation(EAX, EDX);
2237 locations->SetInAt(0, input_location);
2238 // Make the output overlap to please the register allocator. This greatly simplifies
2239 // the validation of the linear scan implementation
2240 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2241 break;
2242 }
David Brazdil46e2a392015-03-16 17:31:52 +00002243 case Primitive::kPrimBoolean:
2244 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002245 case Primitive::kPrimShort:
2246 case Primitive::kPrimInt:
2247 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002248 // Processing a Dex `int-to-byte' instruction.
Mark Mendell5f874182015-03-04 15:42:45 -05002249 locations->SetInAt(0, Location::ByteRegisterOrConstant(ECX, conversion->InputAt(0)));
2250 // Make the output overlap to please the register allocator. This greatly simplifies
2251 // the validation of the linear scan implementation
2252 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Roland Levillain51d3fc42014-11-13 14:11:42 +00002253 break;
2254
2255 default:
2256 LOG(FATAL) << "Unexpected type conversion from " << input_type
2257 << " to " << result_type;
2258 }
2259 break;
2260
Roland Levillain01a8d712014-11-14 16:27:39 +00002261 case Primitive::kPrimShort:
2262 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002263 case Primitive::kPrimLong:
2264 // Type conversion from long to short is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002265 case Primitive::kPrimBoolean:
2266 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002267 case Primitive::kPrimByte:
2268 case Primitive::kPrimInt:
2269 case Primitive::kPrimChar:
2270 // Processing a Dex `int-to-short' instruction.
2271 locations->SetInAt(0, Location::Any());
2272 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2273 break;
2274
2275 default:
2276 LOG(FATAL) << "Unexpected type conversion from " << input_type
2277 << " to " << result_type;
2278 }
2279 break;
2280
Roland Levillain946e1432014-11-11 17:35:19 +00002281 case Primitive::kPrimInt:
2282 switch (input_type) {
2283 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002284 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002285 locations->SetInAt(0, Location::Any());
2286 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2287 break;
2288
2289 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00002290 // Processing a Dex `float-to-int' instruction.
2291 locations->SetInAt(0, Location::RequiresFpuRegister());
2292 locations->SetOut(Location::RequiresRegister());
2293 locations->AddTemp(Location::RequiresFpuRegister());
2294 break;
2295
Roland Levillain946e1432014-11-11 17:35:19 +00002296 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002297 // Processing a Dex `double-to-int' instruction.
2298 locations->SetInAt(0, Location::RequiresFpuRegister());
2299 locations->SetOut(Location::RequiresRegister());
2300 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00002301 break;
2302
2303 default:
2304 LOG(FATAL) << "Unexpected type conversion from " << input_type
2305 << " to " << result_type;
2306 }
2307 break;
2308
Roland Levillaindff1f282014-11-05 14:15:05 +00002309 case Primitive::kPrimLong:
2310 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002311 case Primitive::kPrimBoolean:
2312 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002313 case Primitive::kPrimByte:
2314 case Primitive::kPrimShort:
2315 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002316 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002317 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002318 locations->SetInAt(0, Location::RegisterLocation(EAX));
2319 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
2320 break;
2321
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002322 case Primitive::kPrimFloat:
Vladimir Marko949c91f2015-01-27 10:48:44 +00002323 case Primitive::kPrimDouble: {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002324 // Processing a Dex `float-to-long' or 'double-to-long' instruction.
Vladimir Marko949c91f2015-01-27 10:48:44 +00002325 InvokeRuntimeCallingConvention calling_convention;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002326 XmmRegister parameter = calling_convention.GetFpuRegisterAt(0);
2327 locations->SetInAt(0, Location::FpuRegisterLocation(parameter));
2328
Vladimir Marko949c91f2015-01-27 10:48:44 +00002329 // The runtime helper puts the result in EAX, EDX.
2330 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Vladimir Marko949c91f2015-01-27 10:48:44 +00002331 }
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002332 break;
Roland Levillaindff1f282014-11-05 14:15:05 +00002333
2334 default:
2335 LOG(FATAL) << "Unexpected type conversion from " << input_type
2336 << " to " << result_type;
2337 }
2338 break;
2339
Roland Levillain981e4542014-11-14 11:47:14 +00002340 case Primitive::kPrimChar:
2341 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002342 case Primitive::kPrimLong:
2343 // Type conversion from long to char is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002344 case Primitive::kPrimBoolean:
2345 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002346 case Primitive::kPrimByte:
2347 case Primitive::kPrimShort:
2348 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002349 // Processing a Dex `int-to-char' instruction.
2350 locations->SetInAt(0, Location::Any());
2351 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2352 break;
2353
2354 default:
2355 LOG(FATAL) << "Unexpected type conversion from " << input_type
2356 << " to " << result_type;
2357 }
2358 break;
2359
Roland Levillaindff1f282014-11-05 14:15:05 +00002360 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002361 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002362 case Primitive::kPrimBoolean:
2363 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002364 case Primitive::kPrimByte:
2365 case Primitive::kPrimShort:
2366 case Primitive::kPrimInt:
2367 case Primitive::kPrimChar:
2368 // Processing a Dex `int-to-float' instruction.
2369 locations->SetInAt(0, Location::RequiresRegister());
2370 locations->SetOut(Location::RequiresFpuRegister());
2371 break;
2372
2373 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002374 // Processing a Dex `long-to-float' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002375 locations->SetInAt(0, Location::Any());
2376 locations->SetOut(Location::Any());
Roland Levillain6d0e4832014-11-27 18:31:21 +00002377 break;
2378
Roland Levillaincff13742014-11-17 14:32:17 +00002379 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002380 // Processing a Dex `double-to-float' instruction.
2381 locations->SetInAt(0, Location::RequiresFpuRegister());
2382 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002383 break;
2384
2385 default:
2386 LOG(FATAL) << "Unexpected type conversion from " << input_type
2387 << " to " << result_type;
2388 };
2389 break;
2390
Roland Levillaindff1f282014-11-05 14:15:05 +00002391 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002392 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002393 case Primitive::kPrimBoolean:
2394 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002395 case Primitive::kPrimByte:
2396 case Primitive::kPrimShort:
2397 case Primitive::kPrimInt:
2398 case Primitive::kPrimChar:
2399 // Processing a Dex `int-to-double' instruction.
2400 locations->SetInAt(0, Location::RequiresRegister());
2401 locations->SetOut(Location::RequiresFpuRegister());
2402 break;
2403
2404 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002405 // Processing a Dex `long-to-double' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002406 locations->SetInAt(0, Location::Any());
2407 locations->SetOut(Location::Any());
Roland Levillain647b9ed2014-11-27 12:06:00 +00002408 break;
2409
Roland Levillaincff13742014-11-17 14:32:17 +00002410 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002411 // Processing a Dex `float-to-double' instruction.
2412 locations->SetInAt(0, Location::RequiresFpuRegister());
2413 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002414 break;
2415
2416 default:
2417 LOG(FATAL) << "Unexpected type conversion from " << input_type
2418 << " to " << result_type;
2419 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002420 break;
2421
2422 default:
2423 LOG(FATAL) << "Unexpected type conversion from " << input_type
2424 << " to " << result_type;
2425 }
2426}
2427
2428void InstructionCodeGeneratorX86::VisitTypeConversion(HTypeConversion* conversion) {
2429 LocationSummary* locations = conversion->GetLocations();
2430 Location out = locations->Out();
2431 Location in = locations->InAt(0);
2432 Primitive::Type result_type = conversion->GetResultType();
2433 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002434 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00002435 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002436 case Primitive::kPrimByte:
2437 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002438 case Primitive::kPrimLong:
2439 // Type conversion from long to byte is a result of code transformations.
2440 if (in.IsRegisterPair()) {
2441 __ movsxb(out.AsRegister<Register>(), in.AsRegisterPairLow<ByteRegister>());
2442 } else {
2443 DCHECK(in.GetConstant()->IsLongConstant());
2444 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2445 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
2446 }
2447 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002448 case Primitive::kPrimBoolean:
2449 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002450 case Primitive::kPrimShort:
2451 case Primitive::kPrimInt:
2452 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002453 // Processing a Dex `int-to-byte' instruction.
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002454 if (in.IsRegister()) {
2455 __ movsxb(out.AsRegister<Register>(), in.AsRegister<ByteRegister>());
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002456 } else {
2457 DCHECK(in.GetConstant()->IsIntConstant());
2458 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
2459 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
2460 }
Roland Levillain51d3fc42014-11-13 14:11:42 +00002461 break;
2462
2463 default:
2464 LOG(FATAL) << "Unexpected type conversion from " << input_type
2465 << " to " << result_type;
2466 }
2467 break;
2468
Roland Levillain01a8d712014-11-14 16:27:39 +00002469 case Primitive::kPrimShort:
2470 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002471 case Primitive::kPrimLong:
2472 // Type conversion from long to short is a result of code transformations.
2473 if (in.IsRegisterPair()) {
2474 __ movsxw(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
2475 } else if (in.IsDoubleStackSlot()) {
2476 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
2477 } else {
2478 DCHECK(in.GetConstant()->IsLongConstant());
2479 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2480 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
2481 }
2482 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002483 case Primitive::kPrimBoolean:
2484 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002485 case Primitive::kPrimByte:
2486 case Primitive::kPrimInt:
2487 case Primitive::kPrimChar:
2488 // Processing a Dex `int-to-short' instruction.
2489 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002490 __ movsxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain01a8d712014-11-14 16:27:39 +00002491 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002492 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain01a8d712014-11-14 16:27:39 +00002493 } else {
2494 DCHECK(in.GetConstant()->IsIntConstant());
2495 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002496 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
Roland Levillain01a8d712014-11-14 16:27:39 +00002497 }
2498 break;
2499
2500 default:
2501 LOG(FATAL) << "Unexpected type conversion from " << input_type
2502 << " to " << result_type;
2503 }
2504 break;
2505
Roland Levillain946e1432014-11-11 17:35:19 +00002506 case Primitive::kPrimInt:
2507 switch (input_type) {
2508 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002509 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002510 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002511 __ movl(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00002512 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002513 __ movl(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain946e1432014-11-11 17:35:19 +00002514 } else {
2515 DCHECK(in.IsConstant());
2516 DCHECK(in.GetConstant()->IsLongConstant());
2517 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002518 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00002519 }
2520 break;
2521
Roland Levillain3f8f9362014-12-02 17:45:01 +00002522 case Primitive::kPrimFloat: {
2523 // Processing a Dex `float-to-int' instruction.
2524 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2525 Register output = out.AsRegister<Register>();
2526 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002527 NearLabel done, nan;
Roland Levillain3f8f9362014-12-02 17:45:01 +00002528
2529 __ movl(output, Immediate(kPrimIntMax));
2530 // temp = int-to-float(output)
2531 __ cvtsi2ss(temp, output);
2532 // if input >= temp goto done
2533 __ comiss(input, temp);
2534 __ j(kAboveEqual, &done);
2535 // if input == NaN goto nan
2536 __ j(kUnordered, &nan);
2537 // output = float-to-int-truncate(input)
2538 __ cvttss2si(output, input);
2539 __ jmp(&done);
2540 __ Bind(&nan);
2541 // output = 0
2542 __ xorl(output, output);
2543 __ Bind(&done);
2544 break;
2545 }
2546
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002547 case Primitive::kPrimDouble: {
2548 // Processing a Dex `double-to-int' instruction.
2549 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2550 Register output = out.AsRegister<Register>();
2551 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002552 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002553
2554 __ movl(output, Immediate(kPrimIntMax));
2555 // temp = int-to-double(output)
2556 __ cvtsi2sd(temp, output);
2557 // if input >= temp goto done
2558 __ comisd(input, temp);
2559 __ j(kAboveEqual, &done);
2560 // if input == NaN goto nan
2561 __ j(kUnordered, &nan);
2562 // output = double-to-int-truncate(input)
2563 __ cvttsd2si(output, input);
2564 __ jmp(&done);
2565 __ Bind(&nan);
2566 // output = 0
2567 __ xorl(output, output);
2568 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00002569 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002570 }
Roland Levillain946e1432014-11-11 17:35:19 +00002571
2572 default:
2573 LOG(FATAL) << "Unexpected type conversion from " << input_type
2574 << " to " << result_type;
2575 }
2576 break;
2577
Roland Levillaindff1f282014-11-05 14:15:05 +00002578 case Primitive::kPrimLong:
2579 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002580 case Primitive::kPrimBoolean:
2581 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002582 case Primitive::kPrimByte:
2583 case Primitive::kPrimShort:
2584 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002585 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002586 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002587 DCHECK_EQ(out.AsRegisterPairLow<Register>(), EAX);
2588 DCHECK_EQ(out.AsRegisterPairHigh<Register>(), EDX);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002589 DCHECK_EQ(in.AsRegister<Register>(), EAX);
Roland Levillaindff1f282014-11-05 14:15:05 +00002590 __ cdq();
2591 break;
2592
2593 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00002594 // Processing a Dex `float-to-long' instruction.
Alexandre Rames8158f282015-08-07 10:26:17 +01002595 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pF2l),
2596 conversion,
2597 conversion->GetDexPc(),
2598 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00002599 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
Roland Levillain624279f2014-12-04 11:54:28 +00002600 break;
2601
Roland Levillaindff1f282014-11-05 14:15:05 +00002602 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002603 // Processing a Dex `double-to-long' instruction.
Alexandre Rames8158f282015-08-07 10:26:17 +01002604 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pD2l),
2605 conversion,
2606 conversion->GetDexPc(),
2607 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00002608 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
Roland Levillaindff1f282014-11-05 14:15:05 +00002609 break;
2610
2611 default:
2612 LOG(FATAL) << "Unexpected type conversion from " << input_type
2613 << " to " << result_type;
2614 }
2615 break;
2616
Roland Levillain981e4542014-11-14 11:47:14 +00002617 case Primitive::kPrimChar:
2618 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002619 case Primitive::kPrimLong:
2620 // Type conversion from long to short is a result of code transformations.
2621 if (in.IsRegisterPair()) {
2622 __ movzxw(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
2623 } else if (in.IsDoubleStackSlot()) {
2624 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
2625 } else {
2626 DCHECK(in.GetConstant()->IsLongConstant());
2627 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2628 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
2629 }
2630 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002631 case Primitive::kPrimBoolean:
2632 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002633 case Primitive::kPrimByte:
2634 case Primitive::kPrimShort:
2635 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002636 // Processing a Dex `Process a Dex `int-to-char'' instruction.
2637 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002638 __ movzxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain981e4542014-11-14 11:47:14 +00002639 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002640 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain981e4542014-11-14 11:47:14 +00002641 } else {
2642 DCHECK(in.GetConstant()->IsIntConstant());
2643 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002644 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
Roland Levillain981e4542014-11-14 11:47:14 +00002645 }
2646 break;
2647
2648 default:
2649 LOG(FATAL) << "Unexpected type conversion from " << input_type
2650 << " to " << result_type;
2651 }
2652 break;
2653
Roland Levillaindff1f282014-11-05 14:15:05 +00002654 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002655 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002656 case Primitive::kPrimBoolean:
2657 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002658 case Primitive::kPrimByte:
2659 case Primitive::kPrimShort:
2660 case Primitive::kPrimInt:
2661 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002662 // Processing a Dex `int-to-float' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002663 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002664 break;
2665
Roland Levillain6d0e4832014-11-27 18:31:21 +00002666 case Primitive::kPrimLong: {
2667 // Processing a Dex `long-to-float' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002668 size_t adjustment = 0;
Roland Levillain6d0e4832014-11-27 18:31:21 +00002669
Roland Levillain232ade02015-04-20 15:14:36 +01002670 // Create stack space for the call to
2671 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstps below.
2672 // TODO: enhance register allocator to ask for stack temporaries.
2673 if (!in.IsDoubleStackSlot() || !out.IsStackSlot()) {
2674 adjustment = Primitive::ComponentSize(Primitive::kPrimLong);
2675 __ subl(ESP, Immediate(adjustment));
2676 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002677
Roland Levillain232ade02015-04-20 15:14:36 +01002678 // Load the value to the FP stack, using temporaries if needed.
2679 PushOntoFPStack(in, 0, adjustment, false, true);
2680
2681 if (out.IsStackSlot()) {
2682 __ fstps(Address(ESP, out.GetStackIndex() + adjustment));
2683 } else {
2684 __ fstps(Address(ESP, 0));
2685 Location stack_temp = Location::StackSlot(0);
2686 codegen_->Move32(out, stack_temp);
2687 }
2688
2689 // Remove the temporary stack space we allocated.
2690 if (adjustment != 0) {
2691 __ addl(ESP, Immediate(adjustment));
2692 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002693 break;
2694 }
2695
Roland Levillaincff13742014-11-17 14:32:17 +00002696 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002697 // Processing a Dex `double-to-float' instruction.
2698 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002699 break;
2700
2701 default:
2702 LOG(FATAL) << "Unexpected type conversion from " << input_type
2703 << " to " << result_type;
2704 };
2705 break;
2706
Roland Levillaindff1f282014-11-05 14:15:05 +00002707 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002708 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002709 case Primitive::kPrimBoolean:
2710 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002711 case Primitive::kPrimByte:
2712 case Primitive::kPrimShort:
2713 case Primitive::kPrimInt:
2714 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002715 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002716 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002717 break;
2718
Roland Levillain647b9ed2014-11-27 12:06:00 +00002719 case Primitive::kPrimLong: {
2720 // Processing a Dex `long-to-double' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002721 size_t adjustment = 0;
Roland Levillain647b9ed2014-11-27 12:06:00 +00002722
Roland Levillain232ade02015-04-20 15:14:36 +01002723 // Create stack space for the call to
2724 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstpl below.
2725 // TODO: enhance register allocator to ask for stack temporaries.
2726 if (!in.IsDoubleStackSlot() || !out.IsDoubleStackSlot()) {
2727 adjustment = Primitive::ComponentSize(Primitive::kPrimLong);
2728 __ subl(ESP, Immediate(adjustment));
2729 }
2730
2731 // Load the value to the FP stack, using temporaries if needed.
2732 PushOntoFPStack(in, 0, adjustment, false, true);
2733
2734 if (out.IsDoubleStackSlot()) {
2735 __ fstpl(Address(ESP, out.GetStackIndex() + adjustment));
2736 } else {
2737 __ fstpl(Address(ESP, 0));
2738 Location stack_temp = Location::DoubleStackSlot(0);
2739 codegen_->Move64(out, stack_temp);
2740 }
2741
2742 // Remove the temporary stack space we allocated.
2743 if (adjustment != 0) {
2744 __ addl(ESP, Immediate(adjustment));
2745 }
Roland Levillain647b9ed2014-11-27 12:06:00 +00002746 break;
2747 }
2748
Roland Levillaincff13742014-11-17 14:32:17 +00002749 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002750 // Processing a Dex `float-to-double' instruction.
2751 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002752 break;
2753
2754 default:
2755 LOG(FATAL) << "Unexpected type conversion from " << input_type
2756 << " to " << result_type;
2757 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002758 break;
2759
2760 default:
2761 LOG(FATAL) << "Unexpected type conversion from " << input_type
2762 << " to " << result_type;
2763 }
2764}
2765
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002766void LocationsBuilderX86::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002767 LocationSummary* locations =
2768 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002769 switch (add->GetResultType()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002770 case Primitive::kPrimInt: {
2771 locations->SetInAt(0, Location::RequiresRegister());
2772 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
2773 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2774 break;
2775 }
2776
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002777 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002778 locations->SetInAt(0, Location::RequiresRegister());
2779 locations->SetInAt(1, Location::Any());
2780 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002781 break;
2782 }
2783
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002784 case Primitive::kPrimFloat:
2785 case Primitive::kPrimDouble: {
2786 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002787 if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
2788 DCHECK(add->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00002789 } else if (add->InputAt(1)->IsConstant()) {
2790 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002791 } else {
2792 locations->SetInAt(1, Location::Any());
2793 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002794 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002795 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002796 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002797
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002798 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002799 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
2800 break;
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002801 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002802}
2803
2804void InstructionCodeGeneratorX86::VisitAdd(HAdd* add) {
2805 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002806 Location first = locations->InAt(0);
2807 Location second = locations->InAt(1);
Mark Mendell09b84632015-02-13 17:48:38 -05002808 Location out = locations->Out();
2809
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002810 switch (add->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002811 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002812 if (second.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002813 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2814 __ addl(out.AsRegister<Register>(), second.AsRegister<Register>());
Mark Mendell33bf2452015-05-27 10:08:24 -04002815 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
2816 __ addl(out.AsRegister<Register>(), first.AsRegister<Register>());
Mark Mendell09b84632015-02-13 17:48:38 -05002817 } else {
2818 __ leal(out.AsRegister<Register>(), Address(
2819 first.AsRegister<Register>(), second.AsRegister<Register>(), TIMES_1, 0));
2820 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002821 } else if (second.IsConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002822 int32_t value = second.GetConstant()->AsIntConstant()->GetValue();
2823 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2824 __ addl(out.AsRegister<Register>(), Immediate(value));
2825 } else {
2826 __ leal(out.AsRegister<Register>(), Address(first.AsRegister<Register>(), value));
2827 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002828 } else {
Mark Mendell09b84632015-02-13 17:48:38 -05002829 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002830 __ addl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002831 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002832 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002833 }
2834
2835 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002836 if (second.IsRegisterPair()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002837 __ addl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
2838 __ adcl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002839 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002840 __ addl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
2841 __ adcl(first.AsRegisterPairHigh<Register>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002842 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002843 } else {
2844 DCHECK(second.IsConstant()) << second;
2845 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2846 __ addl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
2847 __ adcl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002848 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002849 break;
2850 }
2851
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002852 case Primitive::kPrimFloat: {
2853 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002854 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04002855 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
2856 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00002857 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04002858 __ addss(first.AsFpuRegister<XmmRegister>(),
2859 codegen_->LiteralFloatAddress(
2860 const_area->GetConstant()->AsFloatConstant()->GetValue(),
2861 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2862 } else {
2863 DCHECK(second.IsStackSlot());
2864 __ addss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002865 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002866 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002867 }
2868
2869 case Primitive::kPrimDouble: {
2870 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002871 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04002872 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
2873 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00002874 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04002875 __ addsd(first.AsFpuRegister<XmmRegister>(),
2876 codegen_->LiteralDoubleAddress(
2877 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
2878 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2879 } else {
2880 DCHECK(second.IsDoubleStackSlot());
2881 __ addsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002882 }
2883 break;
2884 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002885
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002886 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002887 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002888 }
2889}
2890
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002891void LocationsBuilderX86::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002892 LocationSummary* locations =
2893 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002894 switch (sub->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002895 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002896 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002897 locations->SetInAt(0, Location::RequiresRegister());
2898 locations->SetInAt(1, Location::Any());
2899 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002900 break;
2901 }
Calin Juravle11351682014-10-23 15:38:15 +01002902 case Primitive::kPrimFloat:
2903 case Primitive::kPrimDouble: {
2904 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002905 if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
2906 DCHECK(sub->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00002907 } else if (sub->InputAt(1)->IsConstant()) {
2908 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002909 } else {
2910 locations->SetInAt(1, Location::Any());
2911 }
Calin Juravle11351682014-10-23 15:38:15 +01002912 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002913 break;
Calin Juravle11351682014-10-23 15:38:15 +01002914 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002915
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002916 default:
Calin Juravle11351682014-10-23 15:38:15 +01002917 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002918 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002919}
2920
2921void InstructionCodeGeneratorX86::VisitSub(HSub* sub) {
2922 LocationSummary* locations = sub->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002923 Location first = locations->InAt(0);
2924 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01002925 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002926 switch (sub->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002927 case Primitive::kPrimInt: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002928 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002929 __ subl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002930 } else if (second.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002931 __ subl(first.AsRegister<Register>(),
2932 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002933 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002934 __ subl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002935 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002936 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002937 }
2938
2939 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002940 if (second.IsRegisterPair()) {
Calin Juravle11351682014-10-23 15:38:15 +01002941 __ subl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
2942 __ sbbl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002943 } else if (second.IsDoubleStackSlot()) {
Calin Juravle11351682014-10-23 15:38:15 +01002944 __ subl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002945 __ sbbl(first.AsRegisterPairHigh<Register>(),
2946 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002947 } else {
2948 DCHECK(second.IsConstant()) << second;
2949 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2950 __ subl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
2951 __ sbbl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002952 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002953 break;
2954 }
2955
Calin Juravle11351682014-10-23 15:38:15 +01002956 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04002957 if (second.IsFpuRegister()) {
2958 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2959 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
2960 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00002961 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04002962 __ subss(first.AsFpuRegister<XmmRegister>(),
2963 codegen_->LiteralFloatAddress(
2964 const_area->GetConstant()->AsFloatConstant()->GetValue(),
2965 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2966 } else {
2967 DCHECK(second.IsStackSlot());
2968 __ subss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
2969 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002970 break;
Calin Juravle11351682014-10-23 15:38:15 +01002971 }
2972
2973 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04002974 if (second.IsFpuRegister()) {
2975 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2976 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
2977 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00002978 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04002979 __ subsd(first.AsFpuRegister<XmmRegister>(),
2980 codegen_->LiteralDoubleAddress(
2981 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
2982 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2983 } else {
2984 DCHECK(second.IsDoubleStackSlot());
2985 __ subsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
2986 }
Calin Juravle11351682014-10-23 15:38:15 +01002987 break;
2988 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002989
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002990 default:
Calin Juravle11351682014-10-23 15:38:15 +01002991 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002992 }
2993}
2994
Calin Juravle34bacdf2014-10-07 20:23:36 +01002995void LocationsBuilderX86::VisitMul(HMul* mul) {
2996 LocationSummary* locations =
2997 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
2998 switch (mul->GetResultType()) {
2999 case Primitive::kPrimInt:
3000 locations->SetInAt(0, Location::RequiresRegister());
3001 locations->SetInAt(1, Location::Any());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003002 if (mul->InputAt(1)->IsIntConstant()) {
3003 // Can use 3 operand multiply.
3004 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3005 } else {
3006 locations->SetOut(Location::SameAsFirstInput());
3007 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003008 break;
3009 case Primitive::kPrimLong: {
3010 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003011 locations->SetInAt(1, Location::Any());
3012 locations->SetOut(Location::SameAsFirstInput());
3013 // Needed for imul on 32bits with 64bits output.
3014 locations->AddTemp(Location::RegisterLocation(EAX));
3015 locations->AddTemp(Location::RegisterLocation(EDX));
3016 break;
3017 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003018 case Primitive::kPrimFloat:
3019 case Primitive::kPrimDouble: {
3020 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003021 if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3022 DCHECK(mul->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003023 } else if (mul->InputAt(1)->IsConstant()) {
3024 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003025 } else {
3026 locations->SetInAt(1, Location::Any());
3027 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003028 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003029 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003030 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003031
3032 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003033 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003034 }
3035}
3036
3037void InstructionCodeGeneratorX86::VisitMul(HMul* mul) {
3038 LocationSummary* locations = mul->GetLocations();
3039 Location first = locations->InAt(0);
3040 Location second = locations->InAt(1);
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003041 Location out = locations->Out();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003042
3043 switch (mul->GetResultType()) {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003044 case Primitive::kPrimInt:
3045 // The constant may have ended up in a register, so test explicitly to avoid
3046 // problems where the output may not be the same as the first operand.
3047 if (mul->InputAt(1)->IsIntConstant()) {
3048 Immediate imm(mul->InputAt(1)->AsIntConstant()->GetValue());
3049 __ imull(out.AsRegister<Register>(), first.AsRegister<Register>(), imm);
3050 } else if (second.IsRegister()) {
3051 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003052 __ imull(first.AsRegister<Register>(), second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003053 } else {
3054 DCHECK(second.IsStackSlot());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003055 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003056 __ imull(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01003057 }
3058 break;
Calin Juravle34bacdf2014-10-07 20:23:36 +01003059
3060 case Primitive::kPrimLong: {
Calin Juravle34bacdf2014-10-07 20:23:36 +01003061 Register in1_hi = first.AsRegisterPairHigh<Register>();
3062 Register in1_lo = first.AsRegisterPairLow<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003063 Register eax = locations->GetTemp(0).AsRegister<Register>();
3064 Register edx = locations->GetTemp(1).AsRegister<Register>();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003065
3066 DCHECK_EQ(EAX, eax);
3067 DCHECK_EQ(EDX, edx);
3068
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003069 // input: in1 - 64 bits, in2 - 64 bits.
Calin Juravle34bacdf2014-10-07 20:23:36 +01003070 // output: in1
3071 // formula: in1.hi : in1.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
3072 // parts: in1.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
3073 // parts: in1.lo = (in1.lo * in2.lo)[31:0]
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003074 if (second.IsConstant()) {
3075 DCHECK(second.GetConstant()->IsLongConstant());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003076
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003077 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3078 int32_t low_value = Low32Bits(value);
3079 int32_t high_value = High32Bits(value);
3080 Immediate low(low_value);
3081 Immediate high(high_value);
3082
3083 __ movl(eax, high);
3084 // eax <- in1.lo * in2.hi
3085 __ imull(eax, in1_lo);
3086 // in1.hi <- in1.hi * in2.lo
3087 __ imull(in1_hi, low);
3088 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3089 __ addl(in1_hi, eax);
3090 // move in2_lo to eax to prepare for double precision
3091 __ movl(eax, low);
3092 // edx:eax <- in1.lo * in2.lo
3093 __ mull(in1_lo);
3094 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3095 __ addl(in1_hi, edx);
3096 // in1.lo <- (in1.lo * in2.lo)[31:0];
3097 __ movl(in1_lo, eax);
3098 } else if (second.IsRegisterPair()) {
3099 Register in2_hi = second.AsRegisterPairHigh<Register>();
3100 Register in2_lo = second.AsRegisterPairLow<Register>();
3101
3102 __ movl(eax, in2_hi);
3103 // eax <- in1.lo * in2.hi
3104 __ imull(eax, in1_lo);
3105 // in1.hi <- in1.hi * in2.lo
3106 __ imull(in1_hi, in2_lo);
3107 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3108 __ addl(in1_hi, eax);
3109 // move in1_lo to eax to prepare for double precision
3110 __ movl(eax, in1_lo);
3111 // edx:eax <- in1.lo * in2.lo
3112 __ mull(in2_lo);
3113 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3114 __ addl(in1_hi, edx);
3115 // in1.lo <- (in1.lo * in2.lo)[31:0];
3116 __ movl(in1_lo, eax);
3117 } else {
3118 DCHECK(second.IsDoubleStackSlot()) << second;
3119 Address in2_hi(ESP, second.GetHighStackIndex(kX86WordSize));
3120 Address in2_lo(ESP, second.GetStackIndex());
3121
3122 __ movl(eax, in2_hi);
3123 // eax <- in1.lo * in2.hi
3124 __ imull(eax, in1_lo);
3125 // in1.hi <- in1.hi * in2.lo
3126 __ imull(in1_hi, in2_lo);
3127 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3128 __ addl(in1_hi, eax);
3129 // move in1_lo to eax to prepare for double precision
3130 __ movl(eax, in1_lo);
3131 // edx:eax <- in1.lo * in2.lo
3132 __ mull(in2_lo);
3133 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3134 __ addl(in1_hi, edx);
3135 // in1.lo <- (in1.lo * in2.lo)[31:0];
3136 __ movl(in1_lo, eax);
3137 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003138
3139 break;
3140 }
3141
Calin Juravleb5bfa962014-10-21 18:02:24 +01003142 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003143 DCHECK(first.Equals(locations->Out()));
3144 if (second.IsFpuRegister()) {
3145 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3146 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3147 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003148 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003149 __ mulss(first.AsFpuRegister<XmmRegister>(),
3150 codegen_->LiteralFloatAddress(
3151 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3152 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3153 } else {
3154 DCHECK(second.IsStackSlot());
3155 __ mulss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3156 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003157 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003158 }
3159
3160 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003161 DCHECK(first.Equals(locations->Out()));
3162 if (second.IsFpuRegister()) {
3163 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3164 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3165 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003166 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003167 __ mulsd(first.AsFpuRegister<XmmRegister>(),
3168 codegen_->LiteralDoubleAddress(
3169 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3170 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3171 } else {
3172 DCHECK(second.IsDoubleStackSlot());
3173 __ mulsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3174 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003175 break;
3176 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003177
3178 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003179 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003180 }
3181}
3182
Roland Levillain232ade02015-04-20 15:14:36 +01003183void InstructionCodeGeneratorX86::PushOntoFPStack(Location source,
3184 uint32_t temp_offset,
3185 uint32_t stack_adjustment,
3186 bool is_fp,
3187 bool is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003188 if (source.IsStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01003189 DCHECK(!is_wide);
3190 if (is_fp) {
3191 __ flds(Address(ESP, source.GetStackIndex() + stack_adjustment));
3192 } else {
3193 __ filds(Address(ESP, source.GetStackIndex() + stack_adjustment));
3194 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003195 } else if (source.IsDoubleStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01003196 DCHECK(is_wide);
3197 if (is_fp) {
3198 __ fldl(Address(ESP, source.GetStackIndex() + stack_adjustment));
3199 } else {
3200 __ fildl(Address(ESP, source.GetStackIndex() + stack_adjustment));
3201 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003202 } else {
3203 // Write the value to the temporary location on the stack and load to FP stack.
Roland Levillain232ade02015-04-20 15:14:36 +01003204 if (!is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003205 Location stack_temp = Location::StackSlot(temp_offset);
3206 codegen_->Move32(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01003207 if (is_fp) {
3208 __ flds(Address(ESP, temp_offset));
3209 } else {
3210 __ filds(Address(ESP, temp_offset));
3211 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003212 } else {
3213 Location stack_temp = Location::DoubleStackSlot(temp_offset);
3214 codegen_->Move64(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01003215 if (is_fp) {
3216 __ fldl(Address(ESP, temp_offset));
3217 } else {
3218 __ fildl(Address(ESP, temp_offset));
3219 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003220 }
3221 }
3222}
3223
3224void InstructionCodeGeneratorX86::GenerateRemFP(HRem *rem) {
3225 Primitive::Type type = rem->GetResultType();
3226 bool is_float = type == Primitive::kPrimFloat;
3227 size_t elem_size = Primitive::ComponentSize(type);
3228 LocationSummary* locations = rem->GetLocations();
3229 Location first = locations->InAt(0);
3230 Location second = locations->InAt(1);
3231 Location out = locations->Out();
3232
3233 // Create stack space for 2 elements.
3234 // TODO: enhance register allocator to ask for stack temporaries.
3235 __ subl(ESP, Immediate(2 * elem_size));
3236
3237 // Load the values to the FP stack in reverse order, using temporaries if needed.
Roland Levillain232ade02015-04-20 15:14:36 +01003238 const bool is_wide = !is_float;
3239 PushOntoFPStack(second, elem_size, 2 * elem_size, /* is_fp */ true, is_wide);
3240 PushOntoFPStack(first, 0, 2 * elem_size, /* is_fp */ true, is_wide);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003241
3242 // Loop doing FPREM until we stabilize.
Mark Mendell0c9497d2015-08-21 09:30:05 -04003243 NearLabel retry;
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003244 __ Bind(&retry);
3245 __ fprem();
3246
3247 // Move FP status to AX.
3248 __ fstsw();
3249
3250 // And see if the argument reduction is complete. This is signaled by the
3251 // C2 FPU flag bit set to 0.
3252 __ andl(EAX, Immediate(kC2ConditionMask));
3253 __ j(kNotEqual, &retry);
3254
3255 // We have settled on the final value. Retrieve it into an XMM register.
3256 // Store FP top of stack to real stack.
3257 if (is_float) {
3258 __ fsts(Address(ESP, 0));
3259 } else {
3260 __ fstl(Address(ESP, 0));
3261 }
3262
3263 // Pop the 2 items from the FP stack.
3264 __ fucompp();
3265
3266 // Load the value from the stack into an XMM register.
3267 DCHECK(out.IsFpuRegister()) << out;
3268 if (is_float) {
3269 __ movss(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
3270 } else {
3271 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
3272 }
3273
3274 // And remove the temporary stack space we allocated.
3275 __ addl(ESP, Immediate(2 * elem_size));
3276}
3277
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003278
3279void InstructionCodeGeneratorX86::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3280 DCHECK(instruction->IsDiv() || instruction->IsRem());
3281
3282 LocationSummary* locations = instruction->GetLocations();
3283 DCHECK(locations->InAt(1).IsConstant());
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003284 DCHECK(locations->InAt(1).GetConstant()->IsIntConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003285
3286 Register out_register = locations->Out().AsRegister<Register>();
3287 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003288 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003289
3290 DCHECK(imm == 1 || imm == -1);
3291
3292 if (instruction->IsRem()) {
3293 __ xorl(out_register, out_register);
3294 } else {
3295 __ movl(out_register, input_register);
3296 if (imm == -1) {
3297 __ negl(out_register);
3298 }
3299 }
3300}
3301
3302
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003303void InstructionCodeGeneratorX86::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003304 LocationSummary* locations = instruction->GetLocations();
3305
3306 Register out_register = locations->Out().AsRegister<Register>();
3307 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003308 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003309 DCHECK(IsPowerOfTwo(AbsOrMin(imm)));
3310 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003311
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003312 Register num = locations->GetTemp(0).AsRegister<Register>();
3313
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003314 __ leal(num, Address(input_register, abs_imm - 1));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003315 __ testl(input_register, input_register);
3316 __ cmovl(kGreaterEqual, num, input_register);
3317 int shift = CTZ(imm);
3318 __ sarl(num, Immediate(shift));
3319
3320 if (imm < 0) {
3321 __ negl(num);
3322 }
3323
3324 __ movl(out_register, num);
3325}
3326
3327void InstructionCodeGeneratorX86::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3328 DCHECK(instruction->IsDiv() || instruction->IsRem());
3329
3330 LocationSummary* locations = instruction->GetLocations();
3331 int imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
3332
3333 Register eax = locations->InAt(0).AsRegister<Register>();
3334 Register out = locations->Out().AsRegister<Register>();
3335 Register num;
3336 Register edx;
3337
3338 if (instruction->IsDiv()) {
3339 edx = locations->GetTemp(0).AsRegister<Register>();
3340 num = locations->GetTemp(1).AsRegister<Register>();
3341 } else {
3342 edx = locations->Out().AsRegister<Register>();
3343 num = locations->GetTemp(0).AsRegister<Register>();
3344 }
3345
3346 DCHECK_EQ(EAX, eax);
3347 DCHECK_EQ(EDX, edx);
3348 if (instruction->IsDiv()) {
3349 DCHECK_EQ(EAX, out);
3350 } else {
3351 DCHECK_EQ(EDX, out);
3352 }
3353
3354 int64_t magic;
3355 int shift;
3356 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
3357
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003358 // Save the numerator.
3359 __ movl(num, eax);
3360
3361 // EAX = magic
3362 __ movl(eax, Immediate(magic));
3363
3364 // EDX:EAX = magic * numerator
3365 __ imull(num);
3366
3367 if (imm > 0 && magic < 0) {
3368 // EDX += num
3369 __ addl(edx, num);
3370 } else if (imm < 0 && magic > 0) {
3371 __ subl(edx, num);
3372 }
3373
3374 // Shift if needed.
3375 if (shift != 0) {
3376 __ sarl(edx, Immediate(shift));
3377 }
3378
3379 // EDX += 1 if EDX < 0
3380 __ movl(eax, edx);
3381 __ shrl(edx, Immediate(31));
3382 __ addl(edx, eax);
3383
3384 if (instruction->IsRem()) {
3385 __ movl(eax, num);
3386 __ imull(edx, Immediate(imm));
3387 __ subl(eax, edx);
3388 __ movl(edx, eax);
3389 } else {
3390 __ movl(eax, edx);
3391 }
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003392}
3393
Calin Juravlebacfec32014-11-14 15:54:36 +00003394void InstructionCodeGeneratorX86::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3395 DCHECK(instruction->IsDiv() || instruction->IsRem());
3396
3397 LocationSummary* locations = instruction->GetLocations();
3398 Location out = locations->Out();
3399 Location first = locations->InAt(0);
3400 Location second = locations->InAt(1);
3401 bool is_div = instruction->IsDiv();
3402
3403 switch (instruction->GetResultType()) {
3404 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003405 DCHECK_EQ(EAX, first.AsRegister<Register>());
3406 DCHECK_EQ(is_div ? EAX : EDX, out.AsRegister<Register>());
Calin Juravlebacfec32014-11-14 15:54:36 +00003407
Vladimir Marko13c86fd2015-11-11 12:37:46 +00003408 if (second.IsConstant()) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003409 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003410
3411 if (imm == 0) {
3412 // Do not generate anything for 0. DivZeroCheck would forbid any generated code.
3413 } else if (imm == 1 || imm == -1) {
3414 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003415 } else if (is_div && IsPowerOfTwo(AbsOrMin(imm))) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003416 DivByPowerOfTwo(instruction->AsDiv());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003417 } else {
3418 DCHECK(imm <= -2 || imm >= 2);
3419 GenerateDivRemWithAnyConstant(instruction);
3420 }
3421 } else {
David Srbecky9cd6d372016-02-09 15:24:47 +00003422 SlowPathCode* slow_path = new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86(
3423 instruction, out.AsRegister<Register>(), is_div);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003424 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00003425
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003426 Register second_reg = second.AsRegister<Register>();
3427 // 0x80000000/-1 triggers an arithmetic exception!
3428 // Dividing by -1 is actually negation and -0x800000000 = 0x80000000 so
3429 // it's safe to just use negl instead of more complex comparisons.
Calin Juravlebacfec32014-11-14 15:54:36 +00003430
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003431 __ cmpl(second_reg, Immediate(-1));
3432 __ j(kEqual, slow_path->GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +00003433
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003434 // edx:eax <- sign-extended of eax
3435 __ cdq();
3436 // eax = quotient, edx = remainder
3437 __ idivl(second_reg);
3438 __ Bind(slow_path->GetExitLabel());
3439 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003440 break;
3441 }
3442
3443 case Primitive::kPrimLong: {
3444 InvokeRuntimeCallingConvention calling_convention;
3445 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
3446 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
3447 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
3448 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
3449 DCHECK_EQ(EAX, out.AsRegisterPairLow<Register>());
3450 DCHECK_EQ(EDX, out.AsRegisterPairHigh<Register>());
3451
3452 if (is_div) {
Alexandre Rames8158f282015-08-07 10:26:17 +01003453 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLdiv),
3454 instruction,
3455 instruction->GetDexPc(),
3456 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003457 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003458 } else {
Alexandre Rames8158f282015-08-07 10:26:17 +01003459 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLmod),
3460 instruction,
3461 instruction->GetDexPc(),
3462 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003463 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003464 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003465 break;
3466 }
3467
3468 default:
3469 LOG(FATAL) << "Unexpected type for GenerateDivRemIntegral " << instruction->GetResultType();
3470 }
3471}
3472
Calin Juravle7c4954d2014-10-28 16:57:40 +00003473void LocationsBuilderX86::VisitDiv(HDiv* div) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003474 LocationSummary::CallKind call_kind = (div->GetResultType() == Primitive::kPrimLong)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003475 ? LocationSummary::kCallOnMainOnly
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003476 : LocationSummary::kNoCall;
3477 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
3478
Calin Juravle7c4954d2014-10-28 16:57:40 +00003479 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00003480 case Primitive::kPrimInt: {
3481 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003482 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00003483 locations->SetOut(Location::SameAsFirstInput());
3484 // Intel uses edx:eax as the dividend.
3485 locations->AddTemp(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003486 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3487 // which enforces results to be in EAX and EDX, things are simpler if we use EAX also as
3488 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003489 if (div->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003490 locations->AddTemp(Location::RequiresRegister());
3491 }
Calin Juravled0d48522014-11-04 16:40:20 +00003492 break;
3493 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003494 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003495 InvokeRuntimeCallingConvention calling_convention;
3496 locations->SetInAt(0, Location::RegisterPairLocation(
3497 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3498 locations->SetInAt(1, Location::RegisterPairLocation(
3499 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3500 // Runtime helper puts the result in EAX, EDX.
3501 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Calin Juravle7c4954d2014-10-28 16:57:40 +00003502 break;
3503 }
3504 case Primitive::kPrimFloat:
3505 case Primitive::kPrimDouble: {
3506 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003507 if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3508 DCHECK(div->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003509 } else if (div->InputAt(1)->IsConstant()) {
3510 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003511 } else {
3512 locations->SetInAt(1, Location::Any());
3513 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003514 locations->SetOut(Location::SameAsFirstInput());
3515 break;
3516 }
3517
3518 default:
3519 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3520 }
3521}
3522
3523void InstructionCodeGeneratorX86::VisitDiv(HDiv* div) {
3524 LocationSummary* locations = div->GetLocations();
3525 Location first = locations->InAt(0);
3526 Location second = locations->InAt(1);
Calin Juravle7c4954d2014-10-28 16:57:40 +00003527
3528 switch (div->GetResultType()) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003529 case Primitive::kPrimInt:
Calin Juravle7c4954d2014-10-28 16:57:40 +00003530 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003531 GenerateDivRemIntegral(div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00003532 break;
3533 }
3534
3535 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003536 if (second.IsFpuRegister()) {
3537 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3538 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3539 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003540 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003541 __ divss(first.AsFpuRegister<XmmRegister>(),
3542 codegen_->LiteralFloatAddress(
3543 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3544 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3545 } else {
3546 DCHECK(second.IsStackSlot());
3547 __ divss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3548 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003549 break;
3550 }
3551
3552 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003553 if (second.IsFpuRegister()) {
3554 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3555 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3556 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003557 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003558 __ divsd(first.AsFpuRegister<XmmRegister>(),
3559 codegen_->LiteralDoubleAddress(
3560 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3561 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3562 } else {
3563 DCHECK(second.IsDoubleStackSlot());
3564 __ divsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3565 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003566 break;
3567 }
3568
3569 default:
3570 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3571 }
3572}
3573
Calin Juravlebacfec32014-11-14 15:54:36 +00003574void LocationsBuilderX86::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00003575 Primitive::Type type = rem->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003576
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003577 LocationSummary::CallKind call_kind = (rem->GetResultType() == Primitive::kPrimLong)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003578 ? LocationSummary::kCallOnMainOnly
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003579 : LocationSummary::kNoCall;
3580 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
Calin Juravlebacfec32014-11-14 15:54:36 +00003581
Calin Juravled2ec87d2014-12-08 14:24:46 +00003582 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003583 case Primitive::kPrimInt: {
3584 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003585 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00003586 locations->SetOut(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003587 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3588 // which enforces results to be in EAX and EDX, things are simpler if we use EDX also as
3589 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003590 if (rem->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003591 locations->AddTemp(Location::RequiresRegister());
3592 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003593 break;
3594 }
3595 case Primitive::kPrimLong: {
3596 InvokeRuntimeCallingConvention calling_convention;
3597 locations->SetInAt(0, Location::RegisterPairLocation(
3598 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3599 locations->SetInAt(1, Location::RegisterPairLocation(
3600 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3601 // Runtime helper puts the result in EAX, EDX.
3602 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
3603 break;
3604 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003605 case Primitive::kPrimDouble:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003606 case Primitive::kPrimFloat: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003607 locations->SetInAt(0, Location::Any());
3608 locations->SetInAt(1, Location::Any());
3609 locations->SetOut(Location::RequiresFpuRegister());
3610 locations->AddTemp(Location::RegisterLocation(EAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00003611 break;
3612 }
3613
3614 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003615 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003616 }
3617}
3618
3619void InstructionCodeGeneratorX86::VisitRem(HRem* rem) {
3620 Primitive::Type type = rem->GetResultType();
3621 switch (type) {
3622 case Primitive::kPrimInt:
3623 case Primitive::kPrimLong: {
3624 GenerateDivRemIntegral(rem);
3625 break;
3626 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003627 case Primitive::kPrimFloat:
Calin Juravlebacfec32014-11-14 15:54:36 +00003628 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003629 GenerateRemFP(rem);
Calin Juravlebacfec32014-11-14 15:54:36 +00003630 break;
3631 }
3632 default:
3633 LOG(FATAL) << "Unexpected rem type " << type;
3634 }
3635}
3636
Calin Juravled0d48522014-11-04 16:40:20 +00003637void LocationsBuilderX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00003638 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
3639 ? LocationSummary::kCallOnSlowPath
3640 : LocationSummary::kNoCall;
3641 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003642 switch (instruction->GetType()) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003643 case Primitive::kPrimBoolean:
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003644 case Primitive::kPrimByte:
3645 case Primitive::kPrimChar:
3646 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003647 case Primitive::kPrimInt: {
3648 locations->SetInAt(0, Location::Any());
3649 break;
3650 }
3651 case Primitive::kPrimLong: {
3652 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
3653 if (!instruction->IsConstant()) {
3654 locations->AddTemp(Location::RequiresRegister());
3655 }
3656 break;
3657 }
3658 default:
3659 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
3660 }
Calin Juravled0d48522014-11-04 16:40:20 +00003661 if (instruction->HasUses()) {
3662 locations->SetOut(Location::SameAsFirstInput());
3663 }
3664}
3665
3666void InstructionCodeGeneratorX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003667 SlowPathCode* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86(instruction);
Calin Juravled0d48522014-11-04 16:40:20 +00003668 codegen_->AddSlowPath(slow_path);
3669
3670 LocationSummary* locations = instruction->GetLocations();
3671 Location value = locations->InAt(0);
3672
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003673 switch (instruction->GetType()) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003674 case Primitive::kPrimBoolean:
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003675 case Primitive::kPrimByte:
3676 case Primitive::kPrimChar:
3677 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003678 case Primitive::kPrimInt: {
3679 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003680 __ testl(value.AsRegister<Register>(), value.AsRegister<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003681 __ j(kEqual, slow_path->GetEntryLabel());
3682 } else if (value.IsStackSlot()) {
3683 __ cmpl(Address(ESP, value.GetStackIndex()), Immediate(0));
3684 __ j(kEqual, slow_path->GetEntryLabel());
3685 } else {
3686 DCHECK(value.IsConstant()) << value;
3687 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
3688 __ jmp(slow_path->GetEntryLabel());
3689 }
3690 }
3691 break;
Calin Juravled0d48522014-11-04 16:40:20 +00003692 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003693 case Primitive::kPrimLong: {
3694 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003695 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003696 __ movl(temp, value.AsRegisterPairLow<Register>());
3697 __ orl(temp, value.AsRegisterPairHigh<Register>());
3698 __ j(kEqual, slow_path->GetEntryLabel());
3699 } else {
3700 DCHECK(value.IsConstant()) << value;
3701 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
3702 __ jmp(slow_path->GetEntryLabel());
3703 }
3704 }
3705 break;
3706 }
3707 default:
3708 LOG(FATAL) << "Unexpected type for HDivZeroCheck" << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00003709 }
Calin Juravled0d48522014-11-04 16:40:20 +00003710}
3711
Calin Juravle9aec02f2014-11-18 23:06:35 +00003712void LocationsBuilderX86::HandleShift(HBinaryOperation* op) {
3713 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3714
3715 LocationSummary* locations =
3716 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
3717
3718 switch (op->GetResultType()) {
Mark P Mendell73945692015-04-29 14:56:17 +00003719 case Primitive::kPrimInt:
Calin Juravle9aec02f2014-11-18 23:06:35 +00003720 case Primitive::kPrimLong: {
Mark P Mendell73945692015-04-29 14:56:17 +00003721 // Can't have Location::Any() and output SameAsFirstInput()
Calin Juravle9aec02f2014-11-18 23:06:35 +00003722 locations->SetInAt(0, Location::RequiresRegister());
Mark P Mendell73945692015-04-29 14:56:17 +00003723 // The shift count needs to be in CL or a constant.
3724 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, op->InputAt(1)));
Calin Juravle9aec02f2014-11-18 23:06:35 +00003725 locations->SetOut(Location::SameAsFirstInput());
3726 break;
3727 }
3728 default:
3729 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
3730 }
3731}
3732
3733void InstructionCodeGeneratorX86::HandleShift(HBinaryOperation* op) {
3734 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3735
3736 LocationSummary* locations = op->GetLocations();
3737 Location first = locations->InAt(0);
3738 Location second = locations->InAt(1);
3739 DCHECK(first.Equals(locations->Out()));
3740
3741 switch (op->GetResultType()) {
3742 case Primitive::kPrimInt: {
Mark P Mendell73945692015-04-29 14:56:17 +00003743 DCHECK(first.IsRegister());
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003744 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003745 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003746 Register second_reg = second.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003747 DCHECK_EQ(ECX, second_reg);
3748 if (op->IsShl()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003749 __ shll(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003750 } else if (op->IsShr()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003751 __ sarl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003752 } else {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003753 __ shrl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003754 }
3755 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003756 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance;
Mark P Mendell73945692015-04-29 14:56:17 +00003757 if (shift == 0) {
3758 return;
3759 }
3760 Immediate imm(shift);
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003761 if (op->IsShl()) {
3762 __ shll(first_reg, imm);
3763 } else if (op->IsShr()) {
3764 __ sarl(first_reg, imm);
3765 } else {
3766 __ shrl(first_reg, imm);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003767 }
3768 }
3769 break;
3770 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003771 case Primitive::kPrimLong: {
Mark P Mendell73945692015-04-29 14:56:17 +00003772 if (second.IsRegister()) {
3773 Register second_reg = second.AsRegister<Register>();
3774 DCHECK_EQ(ECX, second_reg);
3775 if (op->IsShl()) {
3776 GenerateShlLong(first, second_reg);
3777 } else if (op->IsShr()) {
3778 GenerateShrLong(first, second_reg);
3779 } else {
3780 GenerateUShrLong(first, second_reg);
3781 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003782 } else {
Mark P Mendell73945692015-04-29 14:56:17 +00003783 // Shift by a constant.
Roland Levillain5b5b9312016-03-22 14:57:31 +00003784 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance;
Mark P Mendell73945692015-04-29 14:56:17 +00003785 // Nothing to do if the shift is 0, as the input is already the output.
3786 if (shift != 0) {
3787 if (op->IsShl()) {
3788 GenerateShlLong(first, shift);
3789 } else if (op->IsShr()) {
3790 GenerateShrLong(first, shift);
3791 } else {
3792 GenerateUShrLong(first, shift);
3793 }
3794 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003795 }
3796 break;
3797 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003798 default:
3799 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
3800 }
3801}
3802
Mark P Mendell73945692015-04-29 14:56:17 +00003803void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, int shift) {
3804 Register low = loc.AsRegisterPairLow<Register>();
3805 Register high = loc.AsRegisterPairHigh<Register>();
Mark Mendellba56d062015-05-05 21:34:03 -04003806 if (shift == 1) {
3807 // This is just an addition.
3808 __ addl(low, low);
3809 __ adcl(high, high);
3810 } else if (shift == 32) {
Mark P Mendell73945692015-04-29 14:56:17 +00003811 // Shift by 32 is easy. High gets low, and low gets 0.
3812 codegen_->EmitParallelMoves(
3813 loc.ToLow(),
3814 loc.ToHigh(),
3815 Primitive::kPrimInt,
3816 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
3817 loc.ToLow(),
3818 Primitive::kPrimInt);
3819 } else if (shift > 32) {
3820 // Low part becomes 0. High part is low part << (shift-32).
3821 __ movl(high, low);
3822 __ shll(high, Immediate(shift - 32));
3823 __ xorl(low, low);
3824 } else {
3825 // Between 1 and 31.
3826 __ shld(high, low, Immediate(shift));
3827 __ shll(low, Immediate(shift));
3828 }
3829}
3830
Calin Juravle9aec02f2014-11-18 23:06:35 +00003831void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04003832 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00003833 __ shld(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>(), shifter);
3834 __ shll(loc.AsRegisterPairLow<Register>(), shifter);
3835 __ testl(shifter, Immediate(32));
3836 __ j(kEqual, &done);
3837 __ movl(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>());
3838 __ movl(loc.AsRegisterPairLow<Register>(), Immediate(0));
3839 __ Bind(&done);
3840}
3841
Mark P Mendell73945692015-04-29 14:56:17 +00003842void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, int shift) {
3843 Register low = loc.AsRegisterPairLow<Register>();
3844 Register high = loc.AsRegisterPairHigh<Register>();
3845 if (shift == 32) {
3846 // Need to copy the sign.
3847 DCHECK_NE(low, high);
3848 __ movl(low, high);
3849 __ sarl(high, Immediate(31));
3850 } else if (shift > 32) {
3851 DCHECK_NE(low, high);
3852 // High part becomes sign. Low part is shifted by shift - 32.
3853 __ movl(low, high);
3854 __ sarl(high, Immediate(31));
3855 __ sarl(low, Immediate(shift - 32));
3856 } else {
3857 // Between 1 and 31.
3858 __ shrd(low, high, Immediate(shift));
3859 __ sarl(high, Immediate(shift));
3860 }
3861}
3862
Calin Juravle9aec02f2014-11-18 23:06:35 +00003863void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04003864 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00003865 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
3866 __ sarl(loc.AsRegisterPairHigh<Register>(), shifter);
3867 __ testl(shifter, Immediate(32));
3868 __ j(kEqual, &done);
3869 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
3870 __ sarl(loc.AsRegisterPairHigh<Register>(), Immediate(31));
3871 __ Bind(&done);
3872}
3873
Mark P Mendell73945692015-04-29 14:56:17 +00003874void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, int shift) {
3875 Register low = loc.AsRegisterPairLow<Register>();
3876 Register high = loc.AsRegisterPairHigh<Register>();
3877 if (shift == 32) {
3878 // Shift by 32 is easy. Low gets high, and high gets 0.
3879 codegen_->EmitParallelMoves(
3880 loc.ToHigh(),
3881 loc.ToLow(),
3882 Primitive::kPrimInt,
3883 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
3884 loc.ToHigh(),
3885 Primitive::kPrimInt);
3886 } else if (shift > 32) {
3887 // Low part is high >> (shift - 32). High part becomes 0.
3888 __ movl(low, high);
3889 __ shrl(low, Immediate(shift - 32));
3890 __ xorl(high, high);
3891 } else {
3892 // Between 1 and 31.
3893 __ shrd(low, high, Immediate(shift));
3894 __ shrl(high, Immediate(shift));
3895 }
3896}
3897
Calin Juravle9aec02f2014-11-18 23:06:35 +00003898void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04003899 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00003900 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
3901 __ shrl(loc.AsRegisterPairHigh<Register>(), shifter);
3902 __ testl(shifter, Immediate(32));
3903 __ j(kEqual, &done);
3904 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
3905 __ movl(loc.AsRegisterPairHigh<Register>(), Immediate(0));
3906 __ Bind(&done);
3907}
3908
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003909void LocationsBuilderX86::VisitRor(HRor* ror) {
3910 LocationSummary* locations =
3911 new (GetGraph()->GetArena()) LocationSummary(ror, LocationSummary::kNoCall);
3912
3913 switch (ror->GetResultType()) {
3914 case Primitive::kPrimLong:
3915 // Add the temporary needed.
3916 locations->AddTemp(Location::RequiresRegister());
3917 FALLTHROUGH_INTENDED;
3918 case Primitive::kPrimInt:
3919 locations->SetInAt(0, Location::RequiresRegister());
3920 // The shift count needs to be in CL (unless it is a constant).
3921 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, ror->InputAt(1)));
3922 locations->SetOut(Location::SameAsFirstInput());
3923 break;
3924 default:
3925 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
3926 UNREACHABLE();
3927 }
3928}
3929
3930void InstructionCodeGeneratorX86::VisitRor(HRor* ror) {
3931 LocationSummary* locations = ror->GetLocations();
3932 Location first = locations->InAt(0);
3933 Location second = locations->InAt(1);
3934
3935 if (ror->GetResultType() == Primitive::kPrimInt) {
3936 Register first_reg = first.AsRegister<Register>();
3937 if (second.IsRegister()) {
3938 Register second_reg = second.AsRegister<Register>();
3939 __ rorl(first_reg, second_reg);
3940 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003941 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003942 __ rorl(first_reg, imm);
3943 }
3944 return;
3945 }
3946
3947 DCHECK_EQ(ror->GetResultType(), Primitive::kPrimLong);
3948 Register first_reg_lo = first.AsRegisterPairLow<Register>();
3949 Register first_reg_hi = first.AsRegisterPairHigh<Register>();
3950 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
3951 if (second.IsRegister()) {
3952 Register second_reg = second.AsRegister<Register>();
3953 DCHECK_EQ(second_reg, ECX);
3954 __ movl(temp_reg, first_reg_hi);
3955 __ shrd(first_reg_hi, first_reg_lo, second_reg);
3956 __ shrd(first_reg_lo, temp_reg, second_reg);
3957 __ movl(temp_reg, first_reg_hi);
3958 __ testl(second_reg, Immediate(32));
3959 __ cmovl(kNotEqual, first_reg_hi, first_reg_lo);
3960 __ cmovl(kNotEqual, first_reg_lo, temp_reg);
3961 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003962 int32_t shift_amt = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance;
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003963 if (shift_amt == 0) {
3964 // Already fine.
3965 return;
3966 }
3967 if (shift_amt == 32) {
3968 // Just swap.
3969 __ movl(temp_reg, first_reg_lo);
3970 __ movl(first_reg_lo, first_reg_hi);
3971 __ movl(first_reg_hi, temp_reg);
3972 return;
3973 }
3974
3975 Immediate imm(shift_amt);
3976 // Save the constents of the low value.
3977 __ movl(temp_reg, first_reg_lo);
3978
3979 // Shift right into low, feeding bits from high.
3980 __ shrd(first_reg_lo, first_reg_hi, imm);
3981
3982 // Shift right into high, feeding bits from the original low.
3983 __ shrd(first_reg_hi, temp_reg, imm);
3984
3985 // Swap if needed.
3986 if (shift_amt > 32) {
3987 __ movl(temp_reg, first_reg_lo);
3988 __ movl(first_reg_lo, first_reg_hi);
3989 __ movl(first_reg_hi, temp_reg);
3990 }
3991 }
3992}
3993
Calin Juravle9aec02f2014-11-18 23:06:35 +00003994void LocationsBuilderX86::VisitShl(HShl* shl) {
3995 HandleShift(shl);
3996}
3997
3998void InstructionCodeGeneratorX86::VisitShl(HShl* shl) {
3999 HandleShift(shl);
4000}
4001
4002void LocationsBuilderX86::VisitShr(HShr* shr) {
4003 HandleShift(shr);
4004}
4005
4006void InstructionCodeGeneratorX86::VisitShr(HShr* shr) {
4007 HandleShift(shr);
4008}
4009
4010void LocationsBuilderX86::VisitUShr(HUShr* ushr) {
4011 HandleShift(ushr);
4012}
4013
4014void InstructionCodeGeneratorX86::VisitUShr(HUShr* ushr) {
4015 HandleShift(ushr);
4016}
4017
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004018void LocationsBuilderX86::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004019 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004020 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004021 locations->SetOut(Location::RegisterLocation(EAX));
David Brazdil6de19382016-01-08 17:37:10 +00004022 if (instruction->IsStringAlloc()) {
4023 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
4024 } else {
4025 InvokeRuntimeCallingConvention calling_convention;
4026 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4027 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
4028 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004029}
4030
4031void InstructionCodeGeneratorX86::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01004032 // Note: if heap poisoning is enabled, the entry point takes cares
4033 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00004034 if (instruction->IsStringAlloc()) {
4035 // String is allocated through StringFactory. Call NewEmptyString entry point.
4036 Register temp = instruction->GetLocations()->GetTemp(0).AsRegister<Register>();
4037 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize);
4038 __ fs()->movl(temp, Address::Absolute(QUICK_ENTRY_POINT(pNewEmptyString)));
4039 __ call(Address(temp, code_offset.Int32Value()));
4040 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4041 } else {
4042 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
4043 instruction,
4044 instruction->GetDexPc(),
4045 nullptr);
4046 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
4047 DCHECK(!codegen_->IsLeafMethod());
4048 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004049}
4050
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004051void LocationsBuilderX86::VisitNewArray(HNewArray* instruction) {
4052 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004053 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004054 locations->SetOut(Location::RegisterLocation(EAX));
4055 InvokeRuntimeCallingConvention calling_convention;
4056 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08004057 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004058 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004059}
4060
4061void InstructionCodeGeneratorX86::VisitNewArray(HNewArray* instruction) {
4062 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004063 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
Roland Levillain4d027112015-07-01 15:41:14 +01004064 // Note: if heap poisoning is enabled, the entry point takes cares
4065 // of poisoning the reference.
Calin Juravle175dc732015-08-25 15:42:32 +01004066 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
4067 instruction,
4068 instruction->GetDexPc(),
4069 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00004070 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004071 DCHECK(!codegen_->IsLeafMethod());
4072}
4073
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004074void LocationsBuilderX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004075 LocationSummary* locations =
4076 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01004077 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
4078 if (location.IsStackSlot()) {
4079 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4080 } else if (location.IsDoubleStackSlot()) {
4081 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004082 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01004083 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004084}
4085
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004086void InstructionCodeGeneratorX86::VisitParameterValue(
4087 HParameterValue* instruction ATTRIBUTE_UNUSED) {
4088}
4089
4090void LocationsBuilderX86::VisitCurrentMethod(HCurrentMethod* instruction) {
4091 LocationSummary* locations =
4092 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4093 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
4094}
4095
4096void InstructionCodeGeneratorX86::VisitCurrentMethod(HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004097}
4098
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004099void LocationsBuilderX86::VisitClassTableGet(HClassTableGet* instruction) {
4100 LocationSummary* locations =
4101 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4102 locations->SetInAt(0, Location::RequiresRegister());
4103 locations->SetOut(Location::RequiresRegister());
4104}
4105
4106void InstructionCodeGeneratorX86::VisitClassTableGet(HClassTableGet* instruction) {
4107 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004108 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004109 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004110 instruction->GetIndex(), kX86PointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004111 __ movl(locations->Out().AsRegister<Register>(),
4112 Address(locations->InAt(0).AsRegister<Register>(), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004113 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004114 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00004115 instruction->GetIndex(), kX86PointerSize));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004116 __ movl(locations->Out().AsRegister<Register>(),
4117 Address(locations->InAt(0).AsRegister<Register>(),
4118 mirror::Class::ImtPtrOffset(kX86PointerSize).Uint32Value()));
4119 // temp = temp->GetImtEntryAt(method_offset);
4120 __ movl(locations->Out().AsRegister<Register>(),
4121 Address(locations->Out().AsRegister<Register>(), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004122 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004123}
4124
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004125void LocationsBuilderX86::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004126 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004127 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01004128 locations->SetInAt(0, Location::RequiresRegister());
4129 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004130}
4131
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004132void InstructionCodeGeneratorX86::VisitNot(HNot* not_) {
4133 LocationSummary* locations = not_->GetLocations();
Roland Levillain70566432014-10-24 16:20:17 +01004134 Location in = locations->InAt(0);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01004135 Location out = locations->Out();
Roland Levillain70566432014-10-24 16:20:17 +01004136 DCHECK(in.Equals(out));
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00004137 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004138 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00004139 __ notl(out.AsRegister<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004140 break;
4141
4142 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01004143 __ notl(out.AsRegisterPairLow<Register>());
4144 __ notl(out.AsRegisterPairHigh<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004145 break;
4146
4147 default:
4148 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
4149 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004150}
4151
David Brazdil66d126e2015-04-03 16:02:44 +01004152void LocationsBuilderX86::VisitBooleanNot(HBooleanNot* bool_not) {
4153 LocationSummary* locations =
4154 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
4155 locations->SetInAt(0, Location::RequiresRegister());
4156 locations->SetOut(Location::SameAsFirstInput());
4157}
4158
4159void InstructionCodeGeneratorX86::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01004160 LocationSummary* locations = bool_not->GetLocations();
4161 Location in = locations->InAt(0);
4162 Location out = locations->Out();
4163 DCHECK(in.Equals(out));
4164 __ xorl(out.AsRegister<Register>(), Immediate(1));
4165}
4166
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004167void LocationsBuilderX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004168 LocationSummary* locations =
4169 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00004170 switch (compare->InputAt(0)->GetType()) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00004171 case Primitive::kPrimBoolean:
4172 case Primitive::kPrimByte:
4173 case Primitive::kPrimShort:
4174 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08004175 case Primitive::kPrimInt:
Calin Juravleddb7df22014-11-25 20:56:51 +00004176 case Primitive::kPrimLong: {
4177 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravleddb7df22014-11-25 20:56:51 +00004178 locations->SetInAt(1, Location::Any());
4179 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4180 break;
4181 }
4182 case Primitive::kPrimFloat:
4183 case Primitive::kPrimDouble: {
4184 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004185 if (compare->InputAt(1)->IsX86LoadFromConstantTable()) {
4186 DCHECK(compare->InputAt(1)->IsEmittedAtUseSite());
4187 } else if (compare->InputAt(1)->IsConstant()) {
4188 locations->SetInAt(1, Location::RequiresFpuRegister());
4189 } else {
4190 locations->SetInAt(1, Location::Any());
4191 }
Calin Juravleddb7df22014-11-25 20:56:51 +00004192 locations->SetOut(Location::RequiresRegister());
4193 break;
4194 }
4195 default:
4196 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
4197 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004198}
4199
4200void InstructionCodeGeneratorX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004201 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004202 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00004203 Location left = locations->InAt(0);
4204 Location right = locations->InAt(1);
4205
Mark Mendell0c9497d2015-08-21 09:30:05 -04004206 NearLabel less, greater, done;
Aart Bika19616e2016-02-01 18:57:58 -08004207 Condition less_cond = kLess;
4208
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004209 switch (compare->InputAt(0)->GetType()) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00004210 case Primitive::kPrimBoolean:
4211 case Primitive::kPrimByte:
4212 case Primitive::kPrimShort:
4213 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08004214 case Primitive::kPrimInt: {
Mark Mendell0c5b18e2016-02-06 13:58:35 -05004215 GenerateIntCompare(left, right);
Aart Bika19616e2016-02-01 18:57:58 -08004216 break;
4217 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004218 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004219 Register left_low = left.AsRegisterPairLow<Register>();
4220 Register left_high = left.AsRegisterPairHigh<Register>();
4221 int32_t val_low = 0;
4222 int32_t val_high = 0;
4223 bool right_is_const = false;
4224
4225 if (right.IsConstant()) {
4226 DCHECK(right.GetConstant()->IsLongConstant());
4227 right_is_const = true;
4228 int64_t val = right.GetConstant()->AsLongConstant()->GetValue();
4229 val_low = Low32Bits(val);
4230 val_high = High32Bits(val);
4231 }
4232
Calin Juravleddb7df22014-11-25 20:56:51 +00004233 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004234 __ cmpl(left_high, right.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004235 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004236 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004237 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004238 DCHECK(right_is_const) << right;
Aart Bika19616e2016-02-01 18:57:58 -08004239 codegen_->Compare32BitValue(left_high, val_high);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004240 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004241 __ j(kLess, &less); // Signed compare.
4242 __ j(kGreater, &greater); // Signed compare.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004243 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004244 __ cmpl(left_low, right.AsRegisterPairLow<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004245 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004246 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004247 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004248 DCHECK(right_is_const) << right;
Aart Bika19616e2016-02-01 18:57:58 -08004249 codegen_->Compare32BitValue(left_low, val_low);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004250 }
Aart Bika19616e2016-02-01 18:57:58 -08004251 less_cond = kBelow; // for CF (unsigned).
Calin Juravleddb7df22014-11-25 20:56:51 +00004252 break;
4253 }
4254 case Primitive::kPrimFloat: {
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004255 GenerateFPCompare(left, right, compare, false);
Calin Juravleddb7df22014-11-25 20:56:51 +00004256 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08004257 less_cond = kBelow; // for CF (floats).
Calin Juravleddb7df22014-11-25 20:56:51 +00004258 break;
4259 }
4260 case Primitive::kPrimDouble: {
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004261 GenerateFPCompare(left, right, compare, true);
Calin Juravleddb7df22014-11-25 20:56:51 +00004262 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08004263 less_cond = kBelow; // for CF (floats).
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004264 break;
4265 }
4266 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00004267 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004268 }
Aart Bika19616e2016-02-01 18:57:58 -08004269
Calin Juravleddb7df22014-11-25 20:56:51 +00004270 __ movl(out, Immediate(0));
4271 __ j(kEqual, &done);
Aart Bika19616e2016-02-01 18:57:58 -08004272 __ j(less_cond, &less);
Calin Juravleddb7df22014-11-25 20:56:51 +00004273
4274 __ Bind(&greater);
4275 __ movl(out, Immediate(1));
4276 __ jmp(&done);
4277
4278 __ Bind(&less);
4279 __ movl(out, Immediate(-1));
4280
4281 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004282}
4283
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004284void LocationsBuilderX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004285 LocationSummary* locations =
4286 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko372f10e2016-05-17 16:30:10 +01004287 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01004288 locations->SetInAt(i, Location::Any());
4289 }
4290 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004291}
4292
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004293void InstructionCodeGeneratorX86::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004294 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004295}
4296
Roland Levillain7c1559a2015-12-15 10:55:36 +00004297void CodeGeneratorX86::GenerateMemoryBarrier(MemBarrierKind kind) {
Calin Juravle52c48962014-12-16 17:02:57 +00004298 /*
4299 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
4300 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
4301 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
4302 */
4303 switch (kind) {
4304 case MemBarrierKind::kAnyAny: {
Mark P Mendell17077d82015-12-16 19:15:59 +00004305 MemoryFence();
Calin Juravle52c48962014-12-16 17:02:57 +00004306 break;
4307 }
4308 case MemBarrierKind::kAnyStore:
4309 case MemBarrierKind::kLoadAny:
4310 case MemBarrierKind::kStoreStore: {
4311 // nop
4312 break;
4313 }
Mark Mendell7aa04a12016-01-27 22:39:07 -05004314 case MemBarrierKind::kNTStoreStore:
4315 // Non-Temporal Store/Store needs an explicit fence.
4316 MemoryFence(/* non-temporal */ true);
4317 break;
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004318 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004319}
4320
Vladimir Markodc151b22015-10-15 18:02:30 +01004321HInvokeStaticOrDirect::DispatchInfo CodeGeneratorX86::GetSupportedInvokeStaticOrDirectDispatch(
4322 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
4323 MethodReference target_method ATTRIBUTE_UNUSED) {
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00004324 HInvokeStaticOrDirect::DispatchInfo dispatch_info = desired_dispatch_info;
4325
4326 // We disable pc-relative load when there is an irreducible loop, as the optimization
4327 // is incompatible with it.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004328 // TODO: Create as many X86ComputeBaseMethodAddress instructions
4329 // as needed for methods with irreducible loops.
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00004330 if (GetGraph()->HasIrreducibleLoops() &&
4331 (dispatch_info.method_load_kind ==
4332 HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative)) {
4333 dispatch_info.method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod;
4334 }
4335 switch (dispatch_info.code_ptr_location) {
Vladimir Markodc151b22015-10-15 18:02:30 +01004336 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
4337 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
4338 // For direct code, we actually prefer to call via the code pointer from ArtMethod*.
4339 // (Though the direct CALL ptr16:32 is available for consideration).
4340 return HInvokeStaticOrDirect::DispatchInfo {
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00004341 dispatch_info.method_load_kind,
Vladimir Markodc151b22015-10-15 18:02:30 +01004342 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00004343 dispatch_info.method_load_data,
Vladimir Markodc151b22015-10-15 18:02:30 +01004344 0u
4345 };
4346 default:
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00004347 return dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01004348 }
4349}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004350
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004351Register CodeGeneratorX86::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
4352 Register temp) {
4353 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
Vladimir Markoc53c0792015-11-19 15:48:33 +00004354 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004355 if (!invoke->GetLocations()->Intrinsified()) {
4356 return location.AsRegister<Register>();
4357 }
4358 // For intrinsics we allow any location, so it may be on the stack.
4359 if (!location.IsRegister()) {
4360 __ movl(temp, Address(ESP, location.GetStackIndex()));
4361 return temp;
4362 }
4363 // For register locations, check if the register was saved. If so, get it from the stack.
4364 // Note: There is a chance that the register was saved but not overwritten, so we could
4365 // save one load. However, since this is just an intrinsic slow path we prefer this
4366 // simple and more robust approach rather that trying to determine if that's the case.
4367 SlowPathCode* slow_path = GetCurrentSlowPath();
Serguei Katkov288c7a82016-05-16 11:53:15 +06004368 if (slow_path != nullptr) {
4369 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
4370 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
4371 __ movl(temp, Address(ESP, stack_offset));
4372 return temp;
4373 }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004374 }
4375 return location.AsRegister<Register>();
4376}
4377
Serguei Katkov288c7a82016-05-16 11:53:15 +06004378Location CodeGeneratorX86::GenerateCalleeMethodStaticOrDirectCall(HInvokeStaticOrDirect* invoke,
4379 Location temp) {
Vladimir Marko58155012015-08-19 12:49:41 +00004380 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
4381 switch (invoke->GetMethodLoadKind()) {
4382 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
4383 // temp = thread->string_init_entrypoint
4384 __ fs()->movl(temp.AsRegister<Register>(), Address::Absolute(invoke->GetStringInitOffset()));
4385 break;
4386 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00004387 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004388 break;
4389 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
4390 __ movl(temp.AsRegister<Register>(), Immediate(invoke->GetMethodAddress()));
4391 break;
4392 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004393 __ movl(temp.AsRegister<Register>(), Immediate(/* placeholder */ 0));
Vladimir Marko58155012015-08-19 12:49:41 +00004394 method_patches_.emplace_back(invoke->GetTargetMethod());
4395 __ Bind(&method_patches_.back().label); // Bind the label at the end of the "movl" insn.
4396 break;
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004397 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
4398 Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
4399 temp.AsRegister<Register>());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004400 __ movl(temp.AsRegister<Register>(), Address(base_reg, kDummy32BitOffset));
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004401 // Bind a new fixup label at the end of the "movl" insn.
4402 uint32_t offset = invoke->GetDexCacheArrayOffset();
4403 __ Bind(NewPcRelativeDexCacheArrayPatch(*invoke->GetTargetMethod().dex_file, offset));
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004404 break;
4405 }
Vladimir Marko58155012015-08-19 12:49:41 +00004406 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004407 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004408 Register method_reg;
4409 Register reg = temp.AsRegister<Register>();
4410 if (current_method.IsRegister()) {
4411 method_reg = current_method.AsRegister<Register>();
4412 } else {
David Brazdil58282f42016-01-14 12:45:10 +00004413 DCHECK(invoke->GetLocations()->Intrinsified());
Vladimir Marko58155012015-08-19 12:49:41 +00004414 DCHECK(!current_method.IsValid());
4415 method_reg = reg;
4416 __ movl(reg, Address(ESP, kCurrentMethodStackOffset));
4417 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00004418 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +01004419 __ movl(reg, Address(method_reg,
4420 ArtMethod::DexCacheResolvedMethodsOffset(kX86PointerSize).Int32Value()));
Vladimir Marko40ecb122016-04-06 17:33:41 +01004421 // temp = temp[index_in_cache];
4422 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
4423 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Vladimir Marko58155012015-08-19 12:49:41 +00004424 __ movl(reg, Address(reg, CodeGenerator::GetCachePointerOffset(index_in_cache)));
4425 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +01004426 }
Vladimir Marko58155012015-08-19 12:49:41 +00004427 }
Serguei Katkov288c7a82016-05-16 11:53:15 +06004428 return callee_method;
4429}
4430
4431void CodeGeneratorX86::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
4432 Location callee_method = GenerateCalleeMethodStaticOrDirectCall(invoke, temp);
Vladimir Marko58155012015-08-19 12:49:41 +00004433
4434 switch (invoke->GetCodePtrLocation()) {
4435 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
4436 __ call(GetFrameEntryLabel());
4437 break;
4438 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative: {
4439 relative_call_patches_.emplace_back(invoke->GetTargetMethod());
4440 Label* label = &relative_call_patches_.back().label;
4441 __ call(label); // Bind to the patch label, override at link time.
4442 __ Bind(label); // Bind the label at the end of the "call" insn.
4443 break;
4444 }
4445 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
4446 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
Vladimir Markodc151b22015-10-15 18:02:30 +01004447 // Filtered out by GetSupportedInvokeStaticOrDirectDispatch().
4448 LOG(FATAL) << "Unsupported";
4449 UNREACHABLE();
Vladimir Marko58155012015-08-19 12:49:41 +00004450 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
4451 // (callee_method + offset_of_quick_compiled_code)()
4452 __ call(Address(callee_method.AsRegister<Register>(),
4453 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
4454 kX86WordSize).Int32Value()));
4455 break;
Mark Mendell09ed1a32015-03-25 08:30:06 -04004456 }
4457
4458 DCHECK(!IsLeafMethod());
Mark Mendell09ed1a32015-03-25 08:30:06 -04004459}
4460
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004461void CodeGeneratorX86::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_in) {
4462 Register temp = temp_in.AsRegister<Register>();
4463 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
4464 invoke->GetVTableIndex(), kX86PointerSize).Uint32Value();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004465
4466 // Use the calling convention instead of the location of the receiver, as
4467 // intrinsics may have put the receiver in a different register. In the intrinsics
4468 // slow path, the arguments have been moved to the right place, so here we are
4469 // guaranteed that the receiver is the first register of the calling convention.
4470 InvokeDexCallingConvention calling_convention;
4471 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004472 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004473 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004474 __ movl(temp, Address(receiver, class_offset));
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004475 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00004476 // Instead of simply (possibly) unpoisoning `temp` here, we should
4477 // emit a read barrier for the previous class reference load.
4478 // However this is not required in practice, as this is an
4479 // intermediate/temporary reference and because the current
4480 // concurrent copying collector keeps the from-space memory
4481 // intact/accessible until the end of the marking phase (the
4482 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004483 __ MaybeUnpoisonHeapReference(temp);
4484 // temp = temp->GetMethodAt(method_offset);
4485 __ movl(temp, Address(temp, method_offset));
4486 // call temp->GetEntryPoint();
4487 __ call(Address(
4488 temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize).Int32Value()));
4489}
4490
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004491void CodeGeneratorX86::RecordSimplePatch() {
4492 if (GetCompilerOptions().GetIncludePatchInformation()) {
4493 simple_patches_.emplace_back();
4494 __ Bind(&simple_patches_.back());
4495 }
4496}
4497
4498void CodeGeneratorX86::RecordStringPatch(HLoadString* load_string) {
4499 string_patches_.emplace_back(load_string->GetDexFile(), load_string->GetStringIndex());
4500 __ Bind(&string_patches_.back().label);
4501}
4502
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004503void CodeGeneratorX86::RecordTypePatch(HLoadClass* load_class) {
4504 type_patches_.emplace_back(load_class->GetDexFile(), load_class->GetTypeIndex());
4505 __ Bind(&type_patches_.back().label);
4506}
4507
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004508Label* CodeGeneratorX86::NewPcRelativeDexCacheArrayPatch(const DexFile& dex_file,
4509 uint32_t element_offset) {
4510 // Add the patch entry and bind its label at the end of the instruction.
4511 pc_relative_dex_cache_patches_.emplace_back(dex_file, element_offset);
4512 return &pc_relative_dex_cache_patches_.back().label;
4513}
4514
Vladimir Marko58155012015-08-19 12:49:41 +00004515void CodeGeneratorX86::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
4516 DCHECK(linker_patches->empty());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004517 size_t size =
4518 method_patches_.size() +
4519 relative_call_patches_.size() +
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004520 pc_relative_dex_cache_patches_.size() +
4521 simple_patches_.size() +
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004522 string_patches_.size() +
4523 type_patches_.size();
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004524 linker_patches->reserve(size);
4525 // The label points to the end of the "movl" insn but the literal offset for method
4526 // patch needs to point to the embedded constant which occupies the last 4 bytes.
4527 constexpr uint32_t kLabelPositionToLiteralOffsetAdjustment = 4u;
Vladimir Marko58155012015-08-19 12:49:41 +00004528 for (const MethodPatchInfo<Label>& info : method_patches_) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004529 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Marko58155012015-08-19 12:49:41 +00004530 linker_patches->push_back(LinkerPatch::MethodPatch(literal_offset,
4531 info.target_method.dex_file,
4532 info.target_method.dex_method_index));
4533 }
4534 for (const MethodPatchInfo<Label>& info : relative_call_patches_) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004535 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Marko58155012015-08-19 12:49:41 +00004536 linker_patches->push_back(LinkerPatch::RelativeCodePatch(literal_offset,
4537 info.target_method.dex_file,
4538 info.target_method.dex_method_index));
4539 }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004540 for (const PcRelativeDexCacheAccessInfo& info : pc_relative_dex_cache_patches_) {
4541 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
4542 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(literal_offset,
4543 &info.target_dex_file,
4544 GetMethodAddressOffset(),
4545 info.element_offset));
4546 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004547 for (const Label& label : simple_patches_) {
4548 uint32_t literal_offset = label.Position() - kLabelPositionToLiteralOffsetAdjustment;
4549 linker_patches->push_back(LinkerPatch::RecordPosition(literal_offset));
4550 }
4551 if (GetCompilerOptions().GetCompilePic()) {
4552 for (const StringPatchInfo<Label>& info : string_patches_) {
4553 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
4554 linker_patches->push_back(LinkerPatch::RelativeStringPatch(literal_offset,
4555 &info.dex_file,
4556 GetMethodAddressOffset(),
4557 info.string_index));
4558 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004559 for (const TypePatchInfo<Label>& info : type_patches_) {
4560 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
4561 linker_patches->push_back(LinkerPatch::RelativeTypePatch(literal_offset,
4562 &info.dex_file,
4563 GetMethodAddressOffset(),
4564 info.type_index));
4565 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004566 } else {
4567 for (const StringPatchInfo<Label>& info : string_patches_) {
4568 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
4569 linker_patches->push_back(LinkerPatch::StringPatch(literal_offset,
4570 &info.dex_file,
4571 info.string_index));
4572 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004573 for (const TypePatchInfo<Label>& info : type_patches_) {
4574 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
4575 linker_patches->push_back(LinkerPatch::TypePatch(literal_offset,
4576 &info.dex_file,
4577 info.type_index));
4578 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004579 }
Vladimir Marko58155012015-08-19 12:49:41 +00004580}
4581
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004582void CodeGeneratorX86::MarkGCCard(Register temp,
4583 Register card,
4584 Register object,
4585 Register value,
4586 bool value_can_be_null) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004587 NearLabel is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004588 if (value_can_be_null) {
4589 __ testl(value, value);
4590 __ j(kEqual, &is_null);
4591 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004592 __ fs()->movl(card, Address::Absolute(Thread::CardTableOffset<kX86WordSize>().Int32Value()));
4593 __ movl(temp, object);
4594 __ shrl(temp, Immediate(gc::accounting::CardTable::kCardShift));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00004595 __ movb(Address(temp, card, TIMES_1, 0),
4596 X86ManagedRegister::FromCpuRegister(card).AsByteRegister());
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004597 if (value_can_be_null) {
4598 __ Bind(&is_null);
4599 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004600}
4601
Calin Juravle52c48962014-12-16 17:02:57 +00004602void LocationsBuilderX86::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
4603 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain0d5a2812015-11-13 10:07:31 +00004604
4605 bool object_field_get_with_read_barrier =
4606 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004607 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00004608 new (GetGraph()->GetArena()) LocationSummary(instruction,
4609 kEmitCompilerReadBarrier ?
4610 LocationSummary::kCallOnSlowPath :
4611 LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004612 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004613
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004614 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4615 locations->SetOut(Location::RequiresFpuRegister());
4616 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004617 // The output overlaps in case of long: we don't want the low move
4618 // to overwrite the object's location. Likewise, in the case of
4619 // an object field get with read barriers enabled, we do not want
4620 // the move to overwrite the object's location, as we need it to emit
4621 // the read barrier.
4622 locations->SetOut(
4623 Location::RequiresRegister(),
4624 (object_field_get_with_read_barrier || instruction->GetType() == Primitive::kPrimLong) ?
4625 Location::kOutputOverlap :
4626 Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004627 }
Calin Juravle52c48962014-12-16 17:02:57 +00004628
4629 if (field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong)) {
4630 // Long values can be loaded atomically into an XMM using movsd.
Roland Levillain7c1559a2015-12-15 10:55:36 +00004631 // So we use an XMM register as a temp to achieve atomicity (first
4632 // load the temp into the XMM and then copy the XMM into the
4633 // output, 32 bits at a time).
Calin Juravle52c48962014-12-16 17:02:57 +00004634 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain7c1559a2015-12-15 10:55:36 +00004635 } else if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
4636 // We need a temporary register for the read barrier marking slow
4637 // path in CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier.
4638 locations->AddTemp(Location::RequiresRegister());
Calin Juravle52c48962014-12-16 17:02:57 +00004639 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004640}
4641
Calin Juravle52c48962014-12-16 17:02:57 +00004642void InstructionCodeGeneratorX86::HandleFieldGet(HInstruction* instruction,
4643 const FieldInfo& field_info) {
4644 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004645
Calin Juravle52c48962014-12-16 17:02:57 +00004646 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004647 Location base_loc = locations->InAt(0);
4648 Register base = base_loc.AsRegister<Register>();
Calin Juravle52c48962014-12-16 17:02:57 +00004649 Location out = locations->Out();
4650 bool is_volatile = field_info.IsVolatile();
4651 Primitive::Type field_type = field_info.GetFieldType();
4652 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4653
4654 switch (field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004655 case Primitive::kPrimBoolean: {
Calin Juravle52c48962014-12-16 17:02:57 +00004656 __ movzxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004657 break;
4658 }
4659
4660 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00004661 __ movsxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004662 break;
4663 }
4664
4665 case Primitive::kPrimShort: {
Calin Juravle52c48962014-12-16 17:02:57 +00004666 __ movsxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004667 break;
4668 }
4669
4670 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00004671 __ movzxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004672 break;
4673 }
4674
4675 case Primitive::kPrimInt:
Calin Juravle52c48962014-12-16 17:02:57 +00004676 __ movl(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004677 break;
Roland Levillain7c1559a2015-12-15 10:55:36 +00004678
4679 case Primitive::kPrimNot: {
4680 // /* HeapReference<Object> */ out = *(base + offset)
4681 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
4682 Location temp_loc = locations->GetTemp(0);
4683 // Note that a potential implicit null check is handled in this
4684 // CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier call.
4685 codegen_->GenerateFieldLoadWithBakerReadBarrier(
4686 instruction, out, base, offset, temp_loc, /* needs_null_check */ true);
4687 if (is_volatile) {
4688 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4689 }
4690 } else {
4691 __ movl(out.AsRegister<Register>(), Address(base, offset));
4692 codegen_->MaybeRecordImplicitNullCheck(instruction);
4693 if (is_volatile) {
4694 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4695 }
4696 // If read barriers are enabled, emit read barriers other than
4697 // Baker's using a slow path (and also unpoison the loaded
4698 // reference, if heap poisoning is enabled).
4699 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
4700 }
4701 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004702 }
4703
4704 case Primitive::kPrimLong: {
Calin Juravle52c48962014-12-16 17:02:57 +00004705 if (is_volatile) {
4706 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4707 __ movsd(temp, Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004708 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004709 __ movd(out.AsRegisterPairLow<Register>(), temp);
4710 __ psrlq(temp, Immediate(32));
4711 __ movd(out.AsRegisterPairHigh<Register>(), temp);
4712 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004713 DCHECK_NE(base, out.AsRegisterPairLow<Register>());
Calin Juravle52c48962014-12-16 17:02:57 +00004714 __ movl(out.AsRegisterPairLow<Register>(), Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004715 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004716 __ movl(out.AsRegisterPairHigh<Register>(), Address(base, kX86WordSize + offset));
4717 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004718 break;
4719 }
4720
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004721 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00004722 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004723 break;
4724 }
4725
4726 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00004727 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004728 break;
4729 }
4730
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004731 case Primitive::kPrimVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00004732 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004733 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004734 }
Calin Juravle52c48962014-12-16 17:02:57 +00004735
Roland Levillain7c1559a2015-12-15 10:55:36 +00004736 if (field_type == Primitive::kPrimNot || field_type == Primitive::kPrimLong) {
4737 // Potential implicit null checks, in the case of reference or
4738 // long fields, are handled in the previous switch statement.
4739 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00004740 codegen_->MaybeRecordImplicitNullCheck(instruction);
4741 }
4742
Calin Juravle52c48962014-12-16 17:02:57 +00004743 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004744 if (field_type == Primitive::kPrimNot) {
4745 // Memory barriers, in the case of references, are also handled
4746 // in the previous switch statement.
4747 } else {
4748 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4749 }
Roland Levillain4d027112015-07-01 15:41:14 +01004750 }
Calin Juravle52c48962014-12-16 17:02:57 +00004751}
4752
4753void LocationsBuilderX86::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
4754 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4755
4756 LocationSummary* locations =
4757 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4758 locations->SetInAt(0, Location::RequiresRegister());
4759 bool is_volatile = field_info.IsVolatile();
4760 Primitive::Type field_type = field_info.GetFieldType();
4761 bool is_byte_type = (field_type == Primitive::kPrimBoolean)
4762 || (field_type == Primitive::kPrimByte);
4763
4764 // The register allocator does not support multiple
4765 // inputs that die at entry with one in a specific register.
4766 if (is_byte_type) {
4767 // Ensure the value is in a byte register.
4768 locations->SetInAt(1, Location::RegisterLocation(EAX));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004769 } else if (Primitive::IsFloatingPointType(field_type)) {
Mark Mendell81489372015-11-04 11:30:41 -05004770 if (is_volatile && field_type == Primitive::kPrimDouble) {
4771 // In order to satisfy the semantics of volatile, this must be a single instruction store.
4772 locations->SetInAt(1, Location::RequiresFpuRegister());
4773 } else {
4774 locations->SetInAt(1, Location::FpuRegisterOrConstant(instruction->InputAt(1)));
4775 }
4776 } else if (is_volatile && field_type == Primitive::kPrimLong) {
4777 // In order to satisfy the semantics of volatile, this must be a single instruction store.
Calin Juravle52c48962014-12-16 17:02:57 +00004778 locations->SetInAt(1, Location::RequiresRegister());
Mark Mendell81489372015-11-04 11:30:41 -05004779
Calin Juravle52c48962014-12-16 17:02:57 +00004780 // 64bits value can be atomically written to an address with movsd and an XMM register.
4781 // We need two XMM registers because there's no easier way to (bit) copy a register pair
4782 // into a single XMM register (we copy each pair part into the XMMs and then interleave them).
4783 // NB: We could make the register allocator understand fp_reg <-> core_reg moves but given the
4784 // isolated cases when we need this it isn't worth adding the extra complexity.
4785 locations->AddTemp(Location::RequiresFpuRegister());
4786 locations->AddTemp(Location::RequiresFpuRegister());
Mark Mendell81489372015-11-04 11:30:41 -05004787 } else {
4788 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4789
4790 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
4791 // Temporary registers for the write barrier.
4792 locations->AddTemp(Location::RequiresRegister()); // May be used for reference poisoning too.
4793 // Ensure the card is in a byte register.
4794 locations->AddTemp(Location::RegisterLocation(ECX));
4795 }
Calin Juravle52c48962014-12-16 17:02:57 +00004796 }
4797}
4798
4799void InstructionCodeGeneratorX86::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004800 const FieldInfo& field_info,
4801 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00004802 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4803
4804 LocationSummary* locations = instruction->GetLocations();
4805 Register base = locations->InAt(0).AsRegister<Register>();
4806 Location value = locations->InAt(1);
4807 bool is_volatile = field_info.IsVolatile();
4808 Primitive::Type field_type = field_info.GetFieldType();
4809 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01004810 bool needs_write_barrier =
4811 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00004812
4813 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004814 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
Calin Juravle52c48962014-12-16 17:02:57 +00004815 }
4816
Mark Mendell81489372015-11-04 11:30:41 -05004817 bool maybe_record_implicit_null_check_done = false;
4818
Calin Juravle52c48962014-12-16 17:02:57 +00004819 switch (field_type) {
4820 case Primitive::kPrimBoolean:
4821 case Primitive::kPrimByte: {
4822 __ movb(Address(base, offset), value.AsRegister<ByteRegister>());
4823 break;
4824 }
4825
4826 case Primitive::kPrimShort:
4827 case Primitive::kPrimChar: {
Mark Mendell81489372015-11-04 11:30:41 -05004828 if (value.IsConstant()) {
4829 int16_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4830 __ movw(Address(base, offset), Immediate(v));
4831 } else {
4832 __ movw(Address(base, offset), value.AsRegister<Register>());
4833 }
Calin Juravle52c48962014-12-16 17:02:57 +00004834 break;
4835 }
4836
4837 case Primitive::kPrimInt:
4838 case Primitive::kPrimNot: {
Roland Levillain4d027112015-07-01 15:41:14 +01004839 if (kPoisonHeapReferences && needs_write_barrier) {
4840 // Note that in the case where `value` is a null reference,
4841 // we do not enter this block, as the reference does not
4842 // need poisoning.
4843 DCHECK_EQ(field_type, Primitive::kPrimNot);
4844 Register temp = locations->GetTemp(0).AsRegister<Register>();
4845 __ movl(temp, value.AsRegister<Register>());
4846 __ PoisonHeapReference(temp);
4847 __ movl(Address(base, offset), temp);
Mark Mendell81489372015-11-04 11:30:41 -05004848 } else if (value.IsConstant()) {
4849 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4850 __ movl(Address(base, offset), Immediate(v));
Roland Levillain4d027112015-07-01 15:41:14 +01004851 } else {
Nicolas Geoffray03971632016-03-17 10:44:24 +00004852 DCHECK(value.IsRegister()) << value;
Roland Levillain4d027112015-07-01 15:41:14 +01004853 __ movl(Address(base, offset), value.AsRegister<Register>());
4854 }
Calin Juravle52c48962014-12-16 17:02:57 +00004855 break;
4856 }
4857
4858 case Primitive::kPrimLong: {
4859 if (is_volatile) {
4860 XmmRegister temp1 = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4861 XmmRegister temp2 = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
4862 __ movd(temp1, value.AsRegisterPairLow<Register>());
4863 __ movd(temp2, value.AsRegisterPairHigh<Register>());
4864 __ punpckldq(temp1, temp2);
4865 __ movsd(Address(base, offset), temp1);
Calin Juravle77520bc2015-01-12 18:45:46 +00004866 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell81489372015-11-04 11:30:41 -05004867 } else if (value.IsConstant()) {
4868 int64_t v = CodeGenerator::GetInt64ValueOf(value.GetConstant());
4869 __ movl(Address(base, offset), Immediate(Low32Bits(v)));
4870 codegen_->MaybeRecordImplicitNullCheck(instruction);
4871 __ movl(Address(base, kX86WordSize + offset), Immediate(High32Bits(v)));
Calin Juravle52c48962014-12-16 17:02:57 +00004872 } else {
4873 __ movl(Address(base, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00004874 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004875 __ movl(Address(base, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
4876 }
Mark Mendell81489372015-11-04 11:30:41 -05004877 maybe_record_implicit_null_check_done = true;
Calin Juravle52c48962014-12-16 17:02:57 +00004878 break;
4879 }
4880
4881 case Primitive::kPrimFloat: {
Mark Mendell81489372015-11-04 11:30:41 -05004882 if (value.IsConstant()) {
4883 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4884 __ movl(Address(base, offset), Immediate(v));
4885 } else {
4886 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4887 }
Calin Juravle52c48962014-12-16 17:02:57 +00004888 break;
4889 }
4890
4891 case Primitive::kPrimDouble: {
Mark Mendell81489372015-11-04 11:30:41 -05004892 if (value.IsConstant()) {
4893 int64_t v = CodeGenerator::GetInt64ValueOf(value.GetConstant());
4894 __ movl(Address(base, offset), Immediate(Low32Bits(v)));
4895 codegen_->MaybeRecordImplicitNullCheck(instruction);
4896 __ movl(Address(base, kX86WordSize + offset), Immediate(High32Bits(v)));
4897 maybe_record_implicit_null_check_done = true;
4898 } else {
4899 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4900 }
Calin Juravle52c48962014-12-16 17:02:57 +00004901 break;
4902 }
4903
4904 case Primitive::kPrimVoid:
4905 LOG(FATAL) << "Unreachable type " << field_type;
4906 UNREACHABLE();
4907 }
4908
Mark Mendell81489372015-11-04 11:30:41 -05004909 if (!maybe_record_implicit_null_check_done) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004910 codegen_->MaybeRecordImplicitNullCheck(instruction);
4911 }
4912
Roland Levillain4d027112015-07-01 15:41:14 +01004913 if (needs_write_barrier) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004914 Register temp = locations->GetTemp(0).AsRegister<Register>();
4915 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004916 codegen_->MarkGCCard(temp, card, base, value.AsRegister<Register>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00004917 }
4918
Calin Juravle52c48962014-12-16 17:02:57 +00004919 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004920 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
Calin Juravle52c48962014-12-16 17:02:57 +00004921 }
4922}
4923
4924void LocationsBuilderX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4925 HandleFieldGet(instruction, instruction->GetFieldInfo());
4926}
4927
4928void InstructionCodeGeneratorX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4929 HandleFieldGet(instruction, instruction->GetFieldInfo());
4930}
4931
4932void LocationsBuilderX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
4933 HandleFieldSet(instruction, instruction->GetFieldInfo());
4934}
4935
4936void InstructionCodeGeneratorX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004937 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00004938}
4939
4940void LocationsBuilderX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
4941 HandleFieldSet(instruction, instruction->GetFieldInfo());
4942}
4943
4944void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004945 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00004946}
4947
4948void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4949 HandleFieldGet(instruction, instruction->GetFieldInfo());
4950}
4951
4952void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4953 HandleFieldGet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004954}
4955
Calin Juravlee460d1d2015-09-29 04:52:17 +01004956void LocationsBuilderX86::VisitUnresolvedInstanceFieldGet(
4957 HUnresolvedInstanceFieldGet* instruction) {
4958 FieldAccessCallingConventionX86 calling_convention;
4959 codegen_->CreateUnresolvedFieldLocationSummary(
4960 instruction, instruction->GetFieldType(), calling_convention);
4961}
4962
4963void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldGet(
4964 HUnresolvedInstanceFieldGet* instruction) {
4965 FieldAccessCallingConventionX86 calling_convention;
4966 codegen_->GenerateUnresolvedFieldAccess(instruction,
4967 instruction->GetFieldType(),
4968 instruction->GetFieldIndex(),
4969 instruction->GetDexPc(),
4970 calling_convention);
4971}
4972
4973void LocationsBuilderX86::VisitUnresolvedInstanceFieldSet(
4974 HUnresolvedInstanceFieldSet* instruction) {
4975 FieldAccessCallingConventionX86 calling_convention;
4976 codegen_->CreateUnresolvedFieldLocationSummary(
4977 instruction, instruction->GetFieldType(), calling_convention);
4978}
4979
4980void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldSet(
4981 HUnresolvedInstanceFieldSet* instruction) {
4982 FieldAccessCallingConventionX86 calling_convention;
4983 codegen_->GenerateUnresolvedFieldAccess(instruction,
4984 instruction->GetFieldType(),
4985 instruction->GetFieldIndex(),
4986 instruction->GetDexPc(),
4987 calling_convention);
4988}
4989
4990void LocationsBuilderX86::VisitUnresolvedStaticFieldGet(
4991 HUnresolvedStaticFieldGet* instruction) {
4992 FieldAccessCallingConventionX86 calling_convention;
4993 codegen_->CreateUnresolvedFieldLocationSummary(
4994 instruction, instruction->GetFieldType(), calling_convention);
4995}
4996
4997void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldGet(
4998 HUnresolvedStaticFieldGet* instruction) {
4999 FieldAccessCallingConventionX86 calling_convention;
5000 codegen_->GenerateUnresolvedFieldAccess(instruction,
5001 instruction->GetFieldType(),
5002 instruction->GetFieldIndex(),
5003 instruction->GetDexPc(),
5004 calling_convention);
5005}
5006
5007void LocationsBuilderX86::VisitUnresolvedStaticFieldSet(
5008 HUnresolvedStaticFieldSet* instruction) {
5009 FieldAccessCallingConventionX86 calling_convention;
5010 codegen_->CreateUnresolvedFieldLocationSummary(
5011 instruction, instruction->GetFieldType(), calling_convention);
5012}
5013
5014void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldSet(
5015 HUnresolvedStaticFieldSet* instruction) {
5016 FieldAccessCallingConventionX86 calling_convention;
5017 codegen_->GenerateUnresolvedFieldAccess(instruction,
5018 instruction->GetFieldType(),
5019 instruction->GetFieldIndex(),
5020 instruction->GetDexPc(),
5021 calling_convention);
5022}
5023
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005024void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00005025 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
5026 ? LocationSummary::kCallOnSlowPath
5027 : LocationSummary::kNoCall;
5028 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
5029 Location loc = codegen_->IsImplicitNullCheckAllowed(instruction)
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005030 ? Location::RequiresRegister()
5031 : Location::Any();
5032 locations->SetInAt(0, loc);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005033 if (instruction->HasUses()) {
5034 locations->SetOut(Location::SameAsFirstInput());
5035 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005036}
5037
Calin Juravle2ae48182016-03-16 14:05:09 +00005038void CodeGeneratorX86::GenerateImplicitNullCheck(HNullCheck* instruction) {
5039 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005040 return;
5041 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005042 LocationSummary* locations = instruction->GetLocations();
5043 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00005044
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005045 __ testl(EAX, Address(obj.AsRegister<Register>(), 0));
Calin Juravle2ae48182016-03-16 14:05:09 +00005046 RecordPcInfo(instruction, instruction->GetDexPc());
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005047}
5048
Calin Juravle2ae48182016-03-16 14:05:09 +00005049void CodeGeneratorX86::GenerateExplicitNullCheck(HNullCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07005050 SlowPathCode* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00005051 AddSlowPath(slow_path);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005052
5053 LocationSummary* locations = instruction->GetLocations();
5054 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005055
5056 if (obj.IsRegister()) {
Mark Mendell42514f62015-03-31 11:34:22 -04005057 __ testl(obj.AsRegister<Register>(), obj.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005058 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005059 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005060 } else {
5061 DCHECK(obj.IsConstant()) << obj;
David Brazdil77a48ae2015-09-15 12:34:04 +00005062 DCHECK(obj.GetConstant()->IsNullConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005063 __ jmp(slow_path->GetEntryLabel());
5064 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005065 }
5066 __ j(kEqual, slow_path->GetEntryLabel());
5067}
5068
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005069void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00005070 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005071}
5072
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005073void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005074 bool object_array_get_with_read_barrier =
5075 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005076 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00005077 new (GetGraph()->GetArena()) LocationSummary(instruction,
5078 object_array_get_with_read_barrier ?
5079 LocationSummary::kCallOnSlowPath :
5080 LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005081 locations->SetInAt(0, Location::RequiresRegister());
5082 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005083 if (Primitive::IsFloatingPointType(instruction->GetType())) {
5084 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5085 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005086 // The output overlaps in case of long: we don't want the low move
5087 // to overwrite the array's location. Likewise, in the case of an
5088 // object array get with read barriers enabled, we do not want the
5089 // move to overwrite the array's location, as we need it to emit
5090 // the read barrier.
5091 locations->SetOut(
5092 Location::RequiresRegister(),
5093 (instruction->GetType() == Primitive::kPrimLong || object_array_get_with_read_barrier) ?
5094 Location::kOutputOverlap :
5095 Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005096 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00005097 // We need a temporary register for the read barrier marking slow
5098 // path in CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier.
5099 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
5100 locations->AddTemp(Location::RequiresRegister());
5101 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005102}
5103
5104void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
5105 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005106 Location obj_loc = locations->InAt(0);
5107 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005108 Location index = locations->InAt(1);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005109 Location out_loc = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005110 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005111
Calin Juravle77520bc2015-01-12 18:45:46 +00005112 Primitive::Type type = instruction->GetType();
5113 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005114 case Primitive::kPrimBoolean: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005115 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005116 if (index.IsConstant()) {
5117 __ movzxb(out, Address(obj,
5118 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
5119 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005120 __ movzxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005121 }
5122 break;
5123 }
5124
5125 case Primitive::kPrimByte: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005126 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005127 if (index.IsConstant()) {
5128 __ movsxb(out, Address(obj,
5129 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
5130 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005131 __ movsxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005132 }
5133 break;
5134 }
5135
5136 case Primitive::kPrimShort: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005137 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005138 if (index.IsConstant()) {
5139 __ movsxw(out, Address(obj,
5140 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
5141 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005142 __ movsxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005143 }
5144 break;
5145 }
5146
5147 case Primitive::kPrimChar: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005148 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005149 if (index.IsConstant()) {
5150 __ movzxw(out, Address(obj,
5151 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
5152 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005153 __ movzxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005154 }
5155 break;
5156 }
5157
Roland Levillain7c1559a2015-12-15 10:55:36 +00005158 case Primitive::kPrimInt: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005159 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005160 if (index.IsConstant()) {
5161 __ movl(out, Address(obj,
5162 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
5163 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005164 __ movl(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005165 }
5166 break;
5167 }
5168
Roland Levillain7c1559a2015-12-15 10:55:36 +00005169 case Primitive::kPrimNot: {
5170 static_assert(
5171 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
5172 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain7c1559a2015-12-15 10:55:36 +00005173 // /* HeapReference<Object> */ out =
5174 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
5175 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
5176 Location temp = locations->GetTemp(0);
5177 // Note that a potential implicit null check is handled in this
5178 // CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier call.
5179 codegen_->GenerateArrayLoadWithBakerReadBarrier(
5180 instruction, out_loc, obj, data_offset, index, temp, /* needs_null_check */ true);
5181 } else {
5182 Register out = out_loc.AsRegister<Register>();
5183 if (index.IsConstant()) {
5184 uint32_t offset =
5185 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
5186 __ movl(out, Address(obj, offset));
5187 codegen_->MaybeRecordImplicitNullCheck(instruction);
5188 // If read barriers are enabled, emit read barriers other than
5189 // Baker's using a slow path (and also unpoison the loaded
5190 // reference, if heap poisoning is enabled).
5191 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
5192 } else {
5193 __ movl(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
5194 codegen_->MaybeRecordImplicitNullCheck(instruction);
5195 // If read barriers are enabled, emit read barriers other than
5196 // Baker's using a slow path (and also unpoison the loaded
5197 // reference, if heap poisoning is enabled).
5198 codegen_->MaybeGenerateReadBarrierSlow(
5199 instruction, out_loc, out_loc, obj_loc, data_offset, index);
5200 }
5201 }
5202 break;
5203 }
5204
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005205 case Primitive::kPrimLong: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005206 DCHECK_NE(obj, out_loc.AsRegisterPairLow<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005207 if (index.IsConstant()) {
5208 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Roland Levillain7c1559a2015-12-15 10:55:36 +00005209 __ movl(out_loc.AsRegisterPairLow<Register>(), Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005210 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005211 __ movl(out_loc.AsRegisterPairHigh<Register>(), Address(obj, offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005212 } else {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005213 __ movl(out_loc.AsRegisterPairLow<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005214 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005215 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005216 __ movl(out_loc.AsRegisterPairHigh<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005217 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005218 }
5219 break;
5220 }
5221
Mark Mendell7c8d0092015-01-26 11:21:33 -05005222 case Primitive::kPrimFloat: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005223 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Mark Mendell7c8d0092015-01-26 11:21:33 -05005224 if (index.IsConstant()) {
5225 __ movss(out, Address(obj,
5226 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
5227 } else {
5228 __ movss(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
5229 }
5230 break;
5231 }
5232
5233 case Primitive::kPrimDouble: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005234 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Mark Mendell7c8d0092015-01-26 11:21:33 -05005235 if (index.IsConstant()) {
5236 __ movsd(out, Address(obj,
5237 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
5238 } else {
5239 __ movsd(out, Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
5240 }
5241 break;
5242 }
5243
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005244 case Primitive::kPrimVoid:
Calin Juravle77520bc2015-01-12 18:45:46 +00005245 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07005246 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005247 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005248
Roland Levillain7c1559a2015-12-15 10:55:36 +00005249 if (type == Primitive::kPrimNot || type == Primitive::kPrimLong) {
5250 // Potential implicit null checks, in the case of reference or
5251 // long arrays, are handled in the previous switch statement.
5252 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00005253 codegen_->MaybeRecordImplicitNullCheck(instruction);
5254 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005255}
5256
5257void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01005258 Primitive::Type value_type = instruction->GetComponentType();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005259
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005260 bool needs_write_barrier =
5261 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Roland Levillain0d5a2812015-11-13 10:07:31 +00005262 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
5263 bool object_array_set_with_read_barrier =
5264 kEmitCompilerReadBarrier && (value_type == Primitive::kPrimNot);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005265
Nicolas Geoffray39468442014-09-02 15:17:15 +01005266 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
5267 instruction,
Roland Levillain0d5a2812015-11-13 10:07:31 +00005268 (may_need_runtime_call_for_type_check || object_array_set_with_read_barrier) ?
5269 LocationSummary::kCallOnSlowPath :
5270 LocationSummary::kNoCall);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005271
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005272 bool is_byte_type = (value_type == Primitive::kPrimBoolean)
5273 || (value_type == Primitive::kPrimByte);
5274 // We need the inputs to be different than the output in case of long operation.
5275 // In case of a byte operation, the register allocator does not support multiple
5276 // inputs that die at entry with one in a specific register.
5277 locations->SetInAt(0, Location::RequiresRegister());
5278 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
5279 if (is_byte_type) {
5280 // Ensure the value is in a byte register.
5281 locations->SetInAt(2, Location::ByteRegisterOrConstant(EAX, instruction->InputAt(2)));
5282 } else if (Primitive::IsFloatingPointType(value_type)) {
Mark Mendell81489372015-11-04 11:30:41 -05005283 locations->SetInAt(2, Location::FpuRegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005284 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005285 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
5286 }
5287 if (needs_write_barrier) {
5288 // Temporary registers for the write barrier.
5289 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
5290 // Ensure the card is in a byte register.
Roland Levillain4f6b0b52015-11-23 19:29:22 +00005291 locations->AddTemp(Location::RegisterLocation(ECX));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005292 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005293}
5294
5295void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
5296 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005297 Location array_loc = locations->InAt(0);
5298 Register array = array_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005299 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005300 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005301 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005302 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
5303 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5304 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005305 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005306 bool needs_write_barrier =
5307 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005308
5309 switch (value_type) {
5310 case Primitive::kPrimBoolean:
5311 case Primitive::kPrimByte: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005312 uint32_t offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
5313 Address address = index.IsConstant()
5314 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + offset)
5315 : Address(array, index.AsRegister<Register>(), TIMES_1, offset);
5316 if (value.IsRegister()) {
5317 __ movb(address, value.AsRegister<ByteRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005318 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005319 __ movb(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005320 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005321 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005322 break;
5323 }
5324
5325 case Primitive::kPrimShort:
5326 case Primitive::kPrimChar: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005327 uint32_t offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
5328 Address address = index.IsConstant()
5329 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + offset)
5330 : Address(array, index.AsRegister<Register>(), TIMES_2, offset);
5331 if (value.IsRegister()) {
5332 __ movw(address, value.AsRegister<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005333 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005334 __ movw(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005335 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005336 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005337 break;
5338 }
5339
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005340 case Primitive::kPrimNot: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005341 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
5342 Address address = index.IsConstant()
5343 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
5344 : Address(array, index.AsRegister<Register>(), TIMES_4, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005345
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005346 if (!value.IsRegister()) {
5347 // Just setting null.
5348 DCHECK(instruction->InputAt(2)->IsNullConstant());
5349 DCHECK(value.IsConstant()) << value;
5350 __ movl(address, Immediate(0));
Calin Juravle77520bc2015-01-12 18:45:46 +00005351 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005352 DCHECK(!needs_write_barrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005353 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005354 break;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005355 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005356
5357 DCHECK(needs_write_barrier);
5358 Register register_value = value.AsRegister<Register>();
5359 NearLabel done, not_null, do_put;
5360 SlowPathCode* slow_path = nullptr;
5361 Register temp = locations->GetTemp(0).AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005362 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005363 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathX86(instruction);
5364 codegen_->AddSlowPath(slow_path);
5365 if (instruction->GetValueCanBeNull()) {
5366 __ testl(register_value, register_value);
5367 __ j(kNotEqual, &not_null);
5368 __ movl(address, Immediate(0));
5369 codegen_->MaybeRecordImplicitNullCheck(instruction);
5370 __ jmp(&done);
5371 __ Bind(&not_null);
5372 }
5373
Roland Levillain0d5a2812015-11-13 10:07:31 +00005374 if (kEmitCompilerReadBarrier) {
5375 // When read barriers are enabled, the type checking
5376 // instrumentation requires two read barriers:
5377 //
5378 // __ movl(temp2, temp);
5379 // // /* HeapReference<Class> */ temp = temp->component_type_
5380 // __ movl(temp, Address(temp, component_offset));
Roland Levillain7c1559a2015-12-15 10:55:36 +00005381 // codegen_->GenerateReadBarrierSlow(
Roland Levillain0d5a2812015-11-13 10:07:31 +00005382 // instruction, temp_loc, temp_loc, temp2_loc, component_offset);
5383 //
5384 // // /* HeapReference<Class> */ temp2 = register_value->klass_
5385 // __ movl(temp2, Address(register_value, class_offset));
Roland Levillain7c1559a2015-12-15 10:55:36 +00005386 // codegen_->GenerateReadBarrierSlow(
Roland Levillain0d5a2812015-11-13 10:07:31 +00005387 // instruction, temp2_loc, temp2_loc, value, class_offset, temp_loc);
5388 //
5389 // __ cmpl(temp, temp2);
5390 //
5391 // However, the second read barrier may trash `temp`, as it
5392 // is a temporary register, and as such would not be saved
5393 // along with live registers before calling the runtime (nor
5394 // restored afterwards). So in this case, we bail out and
5395 // delegate the work to the array set slow path.
5396 //
5397 // TODO: Extend the register allocator to support a new
5398 // "(locally) live temp" location so as to avoid always
5399 // going into the slow path when read barriers are enabled.
5400 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005401 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005402 // /* HeapReference<Class> */ temp = array->klass_
5403 __ movl(temp, Address(array, class_offset));
5404 codegen_->MaybeRecordImplicitNullCheck(instruction);
5405 __ MaybeUnpoisonHeapReference(temp);
5406
5407 // /* HeapReference<Class> */ temp = temp->component_type_
5408 __ movl(temp, Address(temp, component_offset));
5409 // If heap poisoning is enabled, no need to unpoison `temp`
5410 // nor the object reference in `register_value->klass`, as
5411 // we are comparing two poisoned references.
5412 __ cmpl(temp, Address(register_value, class_offset));
5413
5414 if (instruction->StaticTypeOfArrayIsObjectArray()) {
5415 __ j(kEqual, &do_put);
5416 // If heap poisoning is enabled, the `temp` reference has
5417 // not been unpoisoned yet; unpoison it now.
5418 __ MaybeUnpoisonHeapReference(temp);
5419
5420 // /* HeapReference<Class> */ temp = temp->super_class_
5421 __ movl(temp, Address(temp, super_offset));
5422 // If heap poisoning is enabled, no need to unpoison
5423 // `temp`, as we are comparing against null below.
5424 __ testl(temp, temp);
5425 __ j(kNotEqual, slow_path->GetEntryLabel());
5426 __ Bind(&do_put);
5427 } else {
5428 __ j(kNotEqual, slow_path->GetEntryLabel());
5429 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005430 }
5431 }
5432
5433 if (kPoisonHeapReferences) {
5434 __ movl(temp, register_value);
5435 __ PoisonHeapReference(temp);
5436 __ movl(address, temp);
5437 } else {
5438 __ movl(address, register_value);
5439 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005440 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005441 codegen_->MaybeRecordImplicitNullCheck(instruction);
5442 }
5443
5444 Register card = locations->GetTemp(1).AsRegister<Register>();
5445 codegen_->MarkGCCard(
5446 temp, card, array, value.AsRegister<Register>(), instruction->GetValueCanBeNull());
5447 __ Bind(&done);
5448
5449 if (slow_path != nullptr) {
5450 __ Bind(slow_path->GetExitLabel());
5451 }
5452
5453 break;
5454 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005455
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005456 case Primitive::kPrimInt: {
5457 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
5458 Address address = index.IsConstant()
5459 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
5460 : Address(array, index.AsRegister<Register>(), TIMES_4, offset);
5461 if (value.IsRegister()) {
5462 __ movl(address, value.AsRegister<Register>());
5463 } else {
5464 DCHECK(value.IsConstant()) << value;
5465 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
5466 __ movl(address, Immediate(v));
5467 }
5468 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005469 break;
5470 }
5471
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005472 case Primitive::kPrimLong: {
5473 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005474 if (index.IsConstant()) {
5475 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005476 if (value.IsRegisterPair()) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005477 __ movl(Address(array, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00005478 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005479 __ movl(Address(array, offset + kX86WordSize), value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005480 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005481 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005482 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005483 __ movl(Address(array, offset), Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00005484 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005485 __ movl(Address(array, offset + kX86WordSize), Immediate(High32Bits(val)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005486 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005487 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005488 if (value.IsRegisterPair()) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005489 __ movl(Address(array, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005490 value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00005491 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005492 __ movl(Address(array, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005493 value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005494 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005495 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005496 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005497 __ movl(Address(array, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005498 Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00005499 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005500 __ movl(Address(array, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005501 Immediate(High32Bits(val)));
5502 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005503 }
5504 break;
5505 }
5506
Mark Mendell7c8d0092015-01-26 11:21:33 -05005507 case Primitive::kPrimFloat: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005508 uint32_t offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
5509 Address address = index.IsConstant()
5510 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
5511 : Address(array, index.AsRegister<Register>(), TIMES_4, offset);
Mark Mendell81489372015-11-04 11:30:41 -05005512 if (value.IsFpuRegister()) {
5513 __ movss(address, value.AsFpuRegister<XmmRegister>());
5514 } else {
5515 DCHECK(value.IsConstant());
5516 int32_t v = bit_cast<int32_t, float>(value.GetConstant()->AsFloatConstant()->GetValue());
5517 __ movl(address, Immediate(v));
5518 }
5519 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005520 break;
5521 }
5522
5523 case Primitive::kPrimDouble: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005524 uint32_t offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
5525 Address address = index.IsConstant()
5526 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + offset)
5527 : Address(array, index.AsRegister<Register>(), TIMES_8, offset);
Mark Mendell81489372015-11-04 11:30:41 -05005528 if (value.IsFpuRegister()) {
5529 __ movsd(address, value.AsFpuRegister<XmmRegister>());
5530 } else {
5531 DCHECK(value.IsConstant());
5532 Address address_hi = index.IsConstant() ?
5533 Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) +
5534 offset + kX86WordSize) :
5535 Address(array, index.AsRegister<Register>(), TIMES_8, offset + kX86WordSize);
5536 int64_t v = bit_cast<int64_t, double>(value.GetConstant()->AsDoubleConstant()->GetValue());
5537 __ movl(address, Immediate(Low32Bits(v)));
5538 codegen_->MaybeRecordImplicitNullCheck(instruction);
5539 __ movl(address_hi, Immediate(High32Bits(v)));
5540 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005541 break;
5542 }
5543
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005544 case Primitive::kPrimVoid:
5545 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07005546 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005547 }
5548}
5549
5550void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
5551 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005552 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellee8d9712016-07-12 11:13:15 -04005553 if (!instruction->IsEmittedAtUseSite()) {
5554 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5555 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005556}
5557
5558void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
Mark Mendellee8d9712016-07-12 11:13:15 -04005559 if (instruction->IsEmittedAtUseSite()) {
5560 return;
5561 }
5562
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005563 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01005564 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00005565 Register obj = locations->InAt(0).AsRegister<Register>();
5566 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005567 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005568 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005569}
5570
5571void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00005572 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
5573 ? LocationSummary::kCallOnSlowPath
5574 : LocationSummary::kNoCall;
5575 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Mark Mendellf60c90b2015-03-04 15:12:59 -05005576 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendellee8d9712016-07-12 11:13:15 -04005577 HInstruction* length = instruction->InputAt(1);
5578 if (!length->IsEmittedAtUseSite()) {
5579 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
5580 }
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005581 if (instruction->HasUses()) {
5582 locations->SetOut(Location::SameAsFirstInput());
5583 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005584}
5585
5586void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
5587 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05005588 Location index_loc = locations->InAt(0);
5589 Location length_loc = locations->InAt(1);
Andreas Gampe85b62f22015-09-09 13:15:38 -07005590 SlowPathCode* slow_path =
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01005591 new (GetGraph()->GetArena()) BoundsCheckSlowPathX86(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005592
Mark Mendell99dbd682015-04-22 16:18:52 -04005593 if (length_loc.IsConstant()) {
5594 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
5595 if (index_loc.IsConstant()) {
5596 // BCE will remove the bounds check if we are guarenteed to pass.
5597 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5598 if (index < 0 || index >= length) {
5599 codegen_->AddSlowPath(slow_path);
5600 __ jmp(slow_path->GetEntryLabel());
5601 } else {
5602 // Some optimization after BCE may have generated this, and we should not
5603 // generate a bounds check if it is a valid range.
5604 }
5605 return;
5606 }
5607
5608 // We have to reverse the jump condition because the length is the constant.
5609 Register index_reg = index_loc.AsRegister<Register>();
5610 __ cmpl(index_reg, Immediate(length));
5611 codegen_->AddSlowPath(slow_path);
5612 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005613 } else {
Mark Mendellee8d9712016-07-12 11:13:15 -04005614 HInstruction* array_length = instruction->InputAt(1);
5615 if (array_length->IsEmittedAtUseSite()) {
5616 // Address the length field in the array.
5617 DCHECK(array_length->IsArrayLength());
5618 uint32_t len_offset = CodeGenerator::GetArrayLengthOffset(array_length->AsArrayLength());
5619 Location array_loc = array_length->GetLocations()->InAt(0);
5620 Address array_len(array_loc.AsRegister<Register>(), len_offset);
5621 if (index_loc.IsConstant()) {
5622 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5623 __ cmpl(array_len, Immediate(value));
5624 } else {
5625 __ cmpl(array_len, index_loc.AsRegister<Register>());
5626 }
5627 codegen_->MaybeRecordImplicitNullCheck(array_length);
Mark Mendell99dbd682015-04-22 16:18:52 -04005628 } else {
Mark Mendellee8d9712016-07-12 11:13:15 -04005629 Register length = length_loc.AsRegister<Register>();
5630 if (index_loc.IsConstant()) {
5631 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5632 __ cmpl(length, Immediate(value));
5633 } else {
5634 __ cmpl(length, index_loc.AsRegister<Register>());
5635 }
Mark Mendell99dbd682015-04-22 16:18:52 -04005636 }
5637 codegen_->AddSlowPath(slow_path);
5638 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005639 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005640}
5641
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005642void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005643 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005644}
5645
5646void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005647 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5648}
5649
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005650void LocationsBuilderX86::VisitSuspendCheck(HSuspendCheck* instruction) {
5651 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
5652}
5653
5654void InstructionCodeGeneratorX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005655 HBasicBlock* block = instruction->GetBlock();
5656 if (block->GetLoopInformation() != nullptr) {
5657 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5658 // The back edge will generate the suspend check.
5659 return;
5660 }
5661 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5662 // The goto will generate the suspend check.
5663 return;
5664 }
5665 GenerateSuspendCheck(instruction, nullptr);
5666}
5667
5668void InstructionCodeGeneratorX86::GenerateSuspendCheck(HSuspendCheck* instruction,
5669 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005670 SuspendCheckSlowPathX86* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005671 down_cast<SuspendCheckSlowPathX86*>(instruction->GetSlowPath());
5672 if (slow_path == nullptr) {
5673 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathX86(instruction, successor);
5674 instruction->SetSlowPath(slow_path);
5675 codegen_->AddSlowPath(slow_path);
5676 if (successor != nullptr) {
5677 DCHECK(successor->IsLoopHeader());
5678 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
5679 }
5680 } else {
5681 DCHECK_EQ(slow_path->GetSuccessor(), successor);
5682 }
5683
Roland Levillain7c1559a2015-12-15 10:55:36 +00005684 __ fs()->cmpw(Address::Absolute(Thread::ThreadFlagsOffset<kX86WordSize>().Int32Value()),
5685 Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005686 if (successor == nullptr) {
5687 __ j(kNotEqual, slow_path->GetEntryLabel());
5688 __ Bind(slow_path->GetReturnLabel());
5689 } else {
5690 __ j(kEqual, codegen_->GetLabelOf(successor));
5691 __ jmp(slow_path->GetEntryLabel());
5692 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005693}
5694
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005695X86Assembler* ParallelMoveResolverX86::GetAssembler() const {
5696 return codegen_->GetAssembler();
5697}
5698
Mark Mendell7c8d0092015-01-26 11:21:33 -05005699void ParallelMoveResolverX86::MoveMemoryToMemory32(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005700 ScratchRegisterScope ensure_scratch(
5701 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5702 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5703 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5704 __ movl(temp_reg, Address(ESP, src + stack_offset));
5705 __ movl(Address(ESP, dst + stack_offset), temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005706}
5707
5708void ParallelMoveResolverX86::MoveMemoryToMemory64(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005709 ScratchRegisterScope ensure_scratch(
5710 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5711 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5712 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5713 __ movl(temp_reg, Address(ESP, src + stack_offset));
5714 __ movl(Address(ESP, dst + stack_offset), temp_reg);
5715 __ movl(temp_reg, Address(ESP, src + stack_offset + kX86WordSize));
5716 __ movl(Address(ESP, dst + stack_offset + kX86WordSize), temp_reg);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005717}
5718
5719void ParallelMoveResolverX86::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005720 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005721 Location source = move->GetSource();
5722 Location destination = move->GetDestination();
5723
5724 if (source.IsRegister()) {
5725 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005726 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
David Brazdil74eb1b22015-12-14 11:44:01 +00005727 } else if (destination.IsFpuRegister()) {
5728 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005729 } else {
5730 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00005731 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005732 }
David Brazdil74eb1b22015-12-14 11:44:01 +00005733 } else if (source.IsRegisterPair()) {
5734 size_t elem_size = Primitive::ComponentSize(Primitive::kPrimInt);
5735 // Create stack space for 2 elements.
5736 __ subl(ESP, Immediate(2 * elem_size));
5737 __ movl(Address(ESP, 0), source.AsRegisterPairLow<Register>());
5738 __ movl(Address(ESP, elem_size), source.AsRegisterPairHigh<Register>());
5739 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
5740 // And remove the temporary stack space we allocated.
5741 __ addl(ESP, Immediate(2 * elem_size));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005742 } else if (source.IsFpuRegister()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00005743 if (destination.IsRegister()) {
5744 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
5745 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005746 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
David Brazdil74eb1b22015-12-14 11:44:01 +00005747 } else if (destination.IsRegisterPair()) {
5748 XmmRegister src_reg = source.AsFpuRegister<XmmRegister>();
5749 __ movd(destination.AsRegisterPairLow<Register>(), src_reg);
5750 __ psrlq(src_reg, Immediate(32));
5751 __ movd(destination.AsRegisterPairHigh<Register>(), src_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005752 } else if (destination.IsStackSlot()) {
5753 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
5754 } else {
5755 DCHECK(destination.IsDoubleStackSlot());
5756 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
5757 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005758 } else if (source.IsStackSlot()) {
5759 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005760 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005761 } else if (destination.IsFpuRegister()) {
5762 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005763 } else {
5764 DCHECK(destination.IsStackSlot());
Mark Mendell7c8d0092015-01-26 11:21:33 -05005765 MoveMemoryToMemory32(destination.GetStackIndex(), source.GetStackIndex());
5766 }
5767 } else if (source.IsDoubleStackSlot()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00005768 if (destination.IsRegisterPair()) {
5769 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
5770 __ movl(destination.AsRegisterPairHigh<Register>(),
5771 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
5772 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005773 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
5774 } else {
5775 DCHECK(destination.IsDoubleStackSlot()) << destination;
5776 MoveMemoryToMemory64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005777 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005778 } else if (source.IsConstant()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005779 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00005780 if (constant->IsIntConstant() || constant->IsNullConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05005781 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005782 if (destination.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05005783 if (value == 0) {
5784 __ xorl(destination.AsRegister<Register>(), destination.AsRegister<Register>());
5785 } else {
5786 __ movl(destination.AsRegister<Register>(), Immediate(value));
5787 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005788 } else {
5789 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell09b84632015-02-13 17:48:38 -05005790 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005791 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005792 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005793 float fp_value = constant->AsFloatConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00005794 int32_t value = bit_cast<int32_t, float>(fp_value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005795 Immediate imm(value);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005796 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005797 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
5798 if (value == 0) {
5799 // Easy handling of 0.0.
5800 __ xorps(dest, dest);
5801 } else {
5802 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005803 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5804 Register temp = static_cast<Register>(ensure_scratch.GetRegister());
5805 __ movl(temp, Immediate(value));
5806 __ movd(dest, temp);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005807 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005808 } else {
5809 DCHECK(destination.IsStackSlot()) << destination;
5810 __ movl(Address(ESP, destination.GetStackIndex()), imm);
5811 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005812 } else if (constant->IsLongConstant()) {
5813 int64_t value = constant->AsLongConstant()->GetValue();
5814 int32_t low_value = Low32Bits(value);
5815 int32_t high_value = High32Bits(value);
5816 Immediate low(low_value);
5817 Immediate high(high_value);
5818 if (destination.IsDoubleStackSlot()) {
5819 __ movl(Address(ESP, destination.GetStackIndex()), low);
5820 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
5821 } else {
5822 __ movl(destination.AsRegisterPairLow<Register>(), low);
5823 __ movl(destination.AsRegisterPairHigh<Register>(), high);
5824 }
5825 } else {
5826 DCHECK(constant->IsDoubleConstant());
5827 double dbl_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00005828 int64_t value = bit_cast<int64_t, double>(dbl_value);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005829 int32_t low_value = Low32Bits(value);
5830 int32_t high_value = High32Bits(value);
5831 Immediate low(low_value);
5832 Immediate high(high_value);
5833 if (destination.IsFpuRegister()) {
5834 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
5835 if (value == 0) {
5836 // Easy handling of 0.0.
5837 __ xorpd(dest, dest);
5838 } else {
5839 __ pushl(high);
5840 __ pushl(low);
5841 __ movsd(dest, Address(ESP, 0));
5842 __ addl(ESP, Immediate(8));
5843 }
5844 } else {
5845 DCHECK(destination.IsDoubleStackSlot()) << destination;
5846 __ movl(Address(ESP, destination.GetStackIndex()), low);
5847 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
5848 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005849 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005850 } else {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00005851 LOG(FATAL) << "Unimplemented move: " << destination << " <- " << source;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005852 }
5853}
5854
Mark Mendella5c19ce2015-04-01 12:51:05 -04005855void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005856 Register suggested_scratch = reg == EAX ? EBX : EAX;
5857 ScratchRegisterScope ensure_scratch(
5858 this, reg, suggested_scratch, codegen_->GetNumberOfCoreRegisters());
5859
5860 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5861 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset));
5862 __ movl(Address(ESP, mem + stack_offset), reg);
5863 __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005864}
5865
Mark Mendell7c8d0092015-01-26 11:21:33 -05005866void ParallelMoveResolverX86::Exchange32(XmmRegister reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005867 ScratchRegisterScope ensure_scratch(
5868 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5869
5870 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5871 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5872 __ movl(temp_reg, Address(ESP, mem + stack_offset));
5873 __ movss(Address(ESP, mem + stack_offset), reg);
5874 __ movd(reg, temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005875}
5876
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005877void ParallelMoveResolverX86::Exchange(int mem1, int mem2) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005878 ScratchRegisterScope ensure_scratch1(
5879 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005880
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005881 Register suggested_scratch = ensure_scratch1.GetRegister() == EAX ? EBX : EAX;
5882 ScratchRegisterScope ensure_scratch2(
5883 this, ensure_scratch1.GetRegister(), suggested_scratch, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005884
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005885 int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0;
5886 stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0;
5887 __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset));
5888 __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset));
5889 __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister()));
5890 __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005891}
5892
5893void ParallelMoveResolverX86::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005894 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005895 Location source = move->GetSource();
5896 Location destination = move->GetDestination();
5897
5898 if (source.IsRegister() && destination.IsRegister()) {
Mark Mendell90979812015-07-28 16:41:21 -04005899 // Use XOR swap algorithm to avoid serializing XCHG instruction or using a temporary.
5900 DCHECK_NE(destination.AsRegister<Register>(), source.AsRegister<Register>());
5901 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
5902 __ xorl(source.AsRegister<Register>(), destination.AsRegister<Register>());
5903 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005904 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005905 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005906 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005907 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005908 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
5909 Exchange(destination.GetStackIndex(), source.GetStackIndex());
Mark Mendell7c8d0092015-01-26 11:21:33 -05005910 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
5911 // Use XOR Swap algorithm to avoid a temporary.
5912 DCHECK_NE(source.reg(), destination.reg());
5913 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
5914 __ xorpd(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
5915 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
5916 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
5917 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
5918 } else if (destination.IsFpuRegister() && source.IsStackSlot()) {
5919 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005920 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
5921 // Take advantage of the 16 bytes in the XMM register.
5922 XmmRegister reg = source.AsFpuRegister<XmmRegister>();
5923 Address stack(ESP, destination.GetStackIndex());
5924 // Load the double into the high doubleword.
5925 __ movhpd(reg, stack);
5926
5927 // Store the low double into the destination.
5928 __ movsd(stack, reg);
5929
5930 // Move the high double to the low double.
5931 __ psrldq(reg, Immediate(8));
5932 } else if (destination.IsFpuRegister() && source.IsDoubleStackSlot()) {
5933 // Take advantage of the 16 bytes in the XMM register.
5934 XmmRegister reg = destination.AsFpuRegister<XmmRegister>();
5935 Address stack(ESP, source.GetStackIndex());
5936 // Load the double into the high doubleword.
5937 __ movhpd(reg, stack);
5938
5939 // Store the low double into the destination.
5940 __ movsd(stack, reg);
5941
5942 // Move the high double to the low double.
5943 __ psrldq(reg, Immediate(8));
5944 } else if (destination.IsDoubleStackSlot() && source.IsDoubleStackSlot()) {
5945 Exchange(destination.GetStackIndex(), source.GetStackIndex());
5946 Exchange(destination.GetHighStackIndex(kX86WordSize), source.GetHighStackIndex(kX86WordSize));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005947 } else {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005948 LOG(FATAL) << "Unimplemented: source: " << source << ", destination: " << destination;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005949 }
5950}
5951
5952void ParallelMoveResolverX86::SpillScratch(int reg) {
5953 __ pushl(static_cast<Register>(reg));
5954}
5955
5956void ParallelMoveResolverX86::RestoreScratch(int reg) {
5957 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005958}
5959
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005960HLoadClass::LoadKind CodeGeneratorX86::GetSupportedLoadClassKind(
5961 HLoadClass::LoadKind desired_class_load_kind) {
5962 if (kEmitCompilerReadBarrier) {
5963 switch (desired_class_load_kind) {
5964 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
5965 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
5966 case HLoadClass::LoadKind::kBootImageAddress:
5967 // TODO: Implement for read barrier.
5968 return HLoadClass::LoadKind::kDexCacheViaMethod;
5969 default:
5970 break;
5971 }
5972 }
5973 switch (desired_class_load_kind) {
5974 case HLoadClass::LoadKind::kReferrersClass:
5975 break;
5976 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
5977 DCHECK(!GetCompilerOptions().GetCompilePic());
5978 break;
5979 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
5980 DCHECK(GetCompilerOptions().GetCompilePic());
5981 FALLTHROUGH_INTENDED;
5982 case HLoadClass::LoadKind::kDexCachePcRelative:
5983 DCHECK(!Runtime::Current()->UseJitCompilation()); // Note: boot image is also non-JIT.
5984 // We disable pc-relative load when there is an irreducible loop, as the optimization
5985 // is incompatible with it.
5986 // TODO: Create as many X86ComputeBaseMethodAddress instructions as needed for methods
5987 // with irreducible loops.
5988 if (GetGraph()->HasIrreducibleLoops()) {
5989 return HLoadClass::LoadKind::kDexCacheViaMethod;
5990 }
5991 break;
5992 case HLoadClass::LoadKind::kBootImageAddress:
5993 break;
5994 case HLoadClass::LoadKind::kDexCacheAddress:
5995 DCHECK(Runtime::Current()->UseJitCompilation());
5996 break;
5997 case HLoadClass::LoadKind::kDexCacheViaMethod:
5998 break;
5999 }
6000 return desired_class_load_kind;
6001}
6002
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006003void LocationsBuilderX86::VisitLoadClass(HLoadClass* cls) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006004 if (cls->NeedsAccessCheck()) {
6005 InvokeRuntimeCallingConvention calling_convention;
6006 CodeGenerator::CreateLoadClassLocationSummary(
6007 cls,
6008 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
6009 Location::RegisterLocation(EAX),
6010 /* code_generator_supports_read_barrier */ true);
6011 return;
6012 }
6013
6014 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || kEmitCompilerReadBarrier)
6015 ? LocationSummary::kCallOnSlowPath
6016 : LocationSummary::kNoCall;
6017 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
6018 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
6019 if (load_kind == HLoadClass::LoadKind::kReferrersClass ||
6020 load_kind == HLoadClass::LoadKind::kDexCacheViaMethod ||
6021 load_kind == HLoadClass::LoadKind::kBootImageLinkTimePcRelative ||
6022 load_kind == HLoadClass::LoadKind::kDexCachePcRelative) {
6023 locations->SetInAt(0, Location::RequiresRegister());
6024 }
6025 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006026}
6027
6028void InstructionCodeGeneratorX86::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01006029 LocationSummary* locations = cls->GetLocations();
Calin Juravle98893e12015-10-02 21:05:03 +01006030 if (cls->NeedsAccessCheck()) {
6031 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex());
6032 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pInitializeTypeAndVerifyAccess),
6033 cls,
6034 cls->GetDexPc(),
6035 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00006036 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Calin Juravle580b6092015-10-06 17:35:58 +01006037 return;
6038 }
6039
Roland Levillain0d5a2812015-11-13 10:07:31 +00006040 Location out_loc = locations->Out();
6041 Register out = out_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006042
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006043 bool generate_null_check = false;
6044 switch (cls->GetLoadKind()) {
6045 case HLoadClass::LoadKind::kReferrersClass: {
6046 DCHECK(!cls->CanCallRuntime());
6047 DCHECK(!cls->MustGenerateClinitCheck());
6048 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
6049 Register current_method = locations->InAt(0).AsRegister<Register>();
6050 GenerateGcRootFieldLoad(
6051 cls, out_loc, Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
6052 break;
6053 }
6054 case HLoadClass::LoadKind::kBootImageLinkTimeAddress: {
6055 DCHECK(!kEmitCompilerReadBarrier);
6056 __ movl(out, Immediate(/* placeholder */ 0));
6057 codegen_->RecordTypePatch(cls);
6058 break;
6059 }
6060 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
6061 DCHECK(!kEmitCompilerReadBarrier);
6062 Register method_address = locations->InAt(0).AsRegister<Register>();
6063 __ leal(out, Address(method_address, CodeGeneratorX86::kDummy32BitOffset));
6064 codegen_->RecordTypePatch(cls);
6065 break;
6066 }
6067 case HLoadClass::LoadKind::kBootImageAddress: {
6068 DCHECK(!kEmitCompilerReadBarrier);
6069 DCHECK_NE(cls->GetAddress(), 0u);
6070 uint32_t address = dchecked_integral_cast<uint32_t>(cls->GetAddress());
6071 __ movl(out, Immediate(address));
6072 codegen_->RecordSimplePatch();
6073 break;
6074 }
6075 case HLoadClass::LoadKind::kDexCacheAddress: {
6076 DCHECK_NE(cls->GetAddress(), 0u);
6077 uint32_t address = dchecked_integral_cast<uint32_t>(cls->GetAddress());
6078 // /* GcRoot<mirror::Class> */ out = *address
6079 GenerateGcRootFieldLoad(cls, out_loc, Address::Absolute(address));
6080 generate_null_check = !cls->IsInDexCache();
6081 break;
6082 }
6083 case HLoadClass::LoadKind::kDexCachePcRelative: {
6084 Register base_reg = locations->InAt(0).AsRegister<Register>();
6085 uint32_t offset = cls->GetDexCacheElementOffset();
6086 Label* fixup_label = codegen_->NewPcRelativeDexCacheArrayPatch(cls->GetDexFile(), offset);
6087 // /* GcRoot<mirror::Class> */ out = *(base + offset) /* PC-relative */
6088 GenerateGcRootFieldLoad(
6089 cls, out_loc, Address(base_reg, CodeGeneratorX86::kDummy32BitOffset), fixup_label);
6090 generate_null_check = !cls->IsInDexCache();
6091 break;
6092 }
6093 case HLoadClass::LoadKind::kDexCacheViaMethod: {
6094 // /* GcRoot<mirror::Class>[] */ out =
6095 // current_method.ptr_sized_fields_->dex_cache_resolved_types_
6096 Register current_method = locations->InAt(0).AsRegister<Register>();
6097 __ movl(out, Address(current_method,
6098 ArtMethod::DexCacheResolvedTypesOffset(kX86PointerSize).Int32Value()));
6099 // /* GcRoot<mirror::Class> */ out = out[type_index]
6100 GenerateGcRootFieldLoad(
6101 cls, out_loc, Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
6102 generate_null_check = !cls->IsInDexCache();
6103 break;
6104 }
6105 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006106
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006107 if (generate_null_check || cls->MustGenerateClinitCheck()) {
6108 DCHECK(cls->CanCallRuntime());
6109 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
6110 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
6111 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006112
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006113 if (generate_null_check) {
6114 __ testl(out, out);
6115 __ j(kEqual, slow_path->GetEntryLabel());
6116 }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006117
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006118 if (cls->MustGenerateClinitCheck()) {
6119 GenerateClassInitializationCheck(slow_path, out);
6120 } else {
6121 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006122 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006123 }
6124}
6125
6126void LocationsBuilderX86::VisitClinitCheck(HClinitCheck* check) {
6127 LocationSummary* locations =
6128 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
6129 locations->SetInAt(0, Location::RequiresRegister());
6130 if (check->HasUses()) {
6131 locations->SetOut(Location::SameAsFirstInput());
6132 }
6133}
6134
6135void InstructionCodeGeneratorX86::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006136 // We assume the class to not be null.
Andreas Gampe85b62f22015-09-09 13:15:38 -07006137 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006138 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006139 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00006140 GenerateClassInitializationCheck(slow_path,
6141 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006142}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006143
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006144void InstructionCodeGeneratorX86::GenerateClassInitializationCheck(
Andreas Gampe85b62f22015-09-09 13:15:38 -07006145 SlowPathCode* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006146 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
6147 Immediate(mirror::Class::kStatusInitialized));
6148 __ j(kLess, slow_path->GetEntryLabel());
6149 __ Bind(slow_path->GetExitLabel());
6150 // No need for memory fence, thanks to the X86 memory model.
6151}
6152
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006153HLoadString::LoadKind CodeGeneratorX86::GetSupportedLoadStringKind(
6154 HLoadString::LoadKind desired_string_load_kind) {
6155 if (kEmitCompilerReadBarrier) {
6156 switch (desired_string_load_kind) {
6157 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
6158 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
6159 case HLoadString::LoadKind::kBootImageAddress:
6160 // TODO: Implement for read barrier.
6161 return HLoadString::LoadKind::kDexCacheViaMethod;
6162 default:
6163 break;
6164 }
6165 }
6166 switch (desired_string_load_kind) {
6167 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
6168 DCHECK(!GetCompilerOptions().GetCompilePic());
6169 break;
6170 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
6171 DCHECK(GetCompilerOptions().GetCompilePic());
6172 FALLTHROUGH_INTENDED;
6173 case HLoadString::LoadKind::kDexCachePcRelative:
Calin Juravleffc87072016-04-20 14:22:09 +01006174 DCHECK(!Runtime::Current()->UseJitCompilation()); // Note: boot image is also non-JIT.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006175 // We disable pc-relative load when there is an irreducible loop, as the optimization
6176 // is incompatible with it.
6177 // TODO: Create as many X86ComputeBaseMethodAddress instructions as needed for methods
6178 // with irreducible loops.
6179 if (GetGraph()->HasIrreducibleLoops()) {
6180 return HLoadString::LoadKind::kDexCacheViaMethod;
6181 }
6182 break;
6183 case HLoadString::LoadKind::kBootImageAddress:
6184 break;
6185 case HLoadString::LoadKind::kDexCacheAddress:
Calin Juravleffc87072016-04-20 14:22:09 +01006186 DCHECK(Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006187 break;
6188 case HLoadString::LoadKind::kDexCacheViaMethod:
6189 break;
6190 }
6191 return desired_string_load_kind;
6192}
6193
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006194void LocationsBuilderX86::VisitLoadString(HLoadString* load) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006195 LocationSummary::CallKind call_kind = (load->NeedsEnvironment() || kEmitCompilerReadBarrier)
Nicolas Geoffray917d0162015-11-24 18:25:35 +00006196 ? LocationSummary::kCallOnSlowPath
6197 : LocationSummary::kNoCall;
6198 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006199 HLoadString::LoadKind load_kind = load->GetLoadKind();
6200 if (load_kind == HLoadString::LoadKind::kDexCacheViaMethod ||
6201 load_kind == HLoadString::LoadKind::kBootImageLinkTimePcRelative ||
6202 load_kind == HLoadString::LoadKind::kDexCachePcRelative) {
6203 locations->SetInAt(0, Location::RequiresRegister());
6204 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006205 locations->SetOut(Location::RequiresRegister());
6206}
6207
6208void InstructionCodeGeneratorX86::VisitLoadString(HLoadString* load) {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01006209 LocationSummary* locations = load->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006210 Location out_loc = locations->Out();
6211 Register out = out_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006212
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006213 switch (load->GetLoadKind()) {
6214 case HLoadString::LoadKind::kBootImageLinkTimeAddress: {
6215 DCHECK(!kEmitCompilerReadBarrier);
6216 __ movl(out, Immediate(/* placeholder */ 0));
6217 codegen_->RecordStringPatch(load);
6218 return; // No dex cache slow path.
6219 }
6220 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
6221 DCHECK(!kEmitCompilerReadBarrier);
6222 Register method_address = locations->InAt(0).AsRegister<Register>();
6223 __ leal(out, Address(method_address, CodeGeneratorX86::kDummy32BitOffset));
6224 codegen_->RecordStringPatch(load);
6225 return; // No dex cache slow path.
6226 }
6227 case HLoadString::LoadKind::kBootImageAddress: {
6228 DCHECK(!kEmitCompilerReadBarrier);
6229 DCHECK_NE(load->GetAddress(), 0u);
6230 uint32_t address = dchecked_integral_cast<uint32_t>(load->GetAddress());
6231 __ movl(out, Immediate(address));
6232 codegen_->RecordSimplePatch();
6233 return; // No dex cache slow path.
6234 }
6235 case HLoadString::LoadKind::kDexCacheAddress: {
6236 DCHECK_NE(load->GetAddress(), 0u);
6237 uint32_t address = dchecked_integral_cast<uint32_t>(load->GetAddress());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006238 // /* GcRoot<mirror::String> */ out = *address
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006239 GenerateGcRootFieldLoad(load, out_loc, Address::Absolute(address));
6240 break;
6241 }
6242 case HLoadString::LoadKind::kDexCachePcRelative: {
6243 Register base_reg = locations->InAt(0).AsRegister<Register>();
6244 uint32_t offset = load->GetDexCacheElementOffset();
6245 Label* fixup_label = codegen_->NewPcRelativeDexCacheArrayPatch(load->GetDexFile(), offset);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006246 // /* GcRoot<mirror::String> */ out = *(base + offset) /* PC-relative */
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006247 GenerateGcRootFieldLoad(
6248 load, out_loc, Address(base_reg, CodeGeneratorX86::kDummy32BitOffset), fixup_label);
6249 break;
6250 }
6251 case HLoadString::LoadKind::kDexCacheViaMethod: {
6252 Register current_method = locations->InAt(0).AsRegister<Register>();
6253
6254 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
6255 GenerateGcRootFieldLoad(
6256 load, out_loc, Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
6257
6258 // /* GcRoot<mirror::String>[] */ out = out->dex_cache_strings_
6259 __ movl(out, Address(out, mirror::Class::DexCacheStringsOffset().Int32Value()));
6260 // /* GcRoot<mirror::String> */ out = out[string_index]
6261 GenerateGcRootFieldLoad(
6262 load, out_loc, Address(out, CodeGenerator::GetCacheOffset(load->GetStringIndex())));
6263 break;
6264 }
6265 default:
6266 LOG(FATAL) << "Unexpected load kind: " << load->GetLoadKind();
6267 UNREACHABLE();
6268 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006269
Nicolas Geoffray917d0162015-11-24 18:25:35 +00006270 if (!load->IsInDexCache()) {
6271 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86(load);
6272 codegen_->AddSlowPath(slow_path);
6273 __ testl(out, out);
6274 __ j(kEqual, slow_path->GetEntryLabel());
6275 __ Bind(slow_path->GetExitLabel());
6276 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006277}
6278
David Brazdilcb1c0552015-08-04 16:22:25 +01006279static Address GetExceptionTlsAddress() {
6280 return Address::Absolute(Thread::ExceptionOffset<kX86WordSize>().Int32Value());
6281}
6282
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006283void LocationsBuilderX86::VisitLoadException(HLoadException* load) {
6284 LocationSummary* locations =
6285 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
6286 locations->SetOut(Location::RequiresRegister());
6287}
6288
6289void InstructionCodeGeneratorX86::VisitLoadException(HLoadException* load) {
David Brazdilcb1c0552015-08-04 16:22:25 +01006290 __ fs()->movl(load->GetLocations()->Out().AsRegister<Register>(), GetExceptionTlsAddress());
6291}
6292
6293void LocationsBuilderX86::VisitClearException(HClearException* clear) {
6294 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
6295}
6296
6297void InstructionCodeGeneratorX86::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
6298 __ fs()->movl(GetExceptionTlsAddress(), Immediate(0));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006299}
6300
6301void LocationsBuilderX86::VisitThrow(HThrow* instruction) {
6302 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006303 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006304 InvokeRuntimeCallingConvention calling_convention;
6305 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6306}
6307
6308void InstructionCodeGeneratorX86::VisitThrow(HThrow* instruction) {
Alexandre Rames8158f282015-08-07 10:26:17 +01006309 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pDeliverException),
6310 instruction,
6311 instruction->GetDexPc(),
6312 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00006313 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006314}
6315
Roland Levillain7c1559a2015-12-15 10:55:36 +00006316static bool TypeCheckNeedsATemporary(TypeCheckKind type_check_kind) {
6317 return kEmitCompilerReadBarrier &&
6318 (kUseBakerReadBarrier ||
6319 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
6320 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
6321 type_check_kind == TypeCheckKind::kArrayObjectCheck);
6322}
6323
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006324void LocationsBuilderX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006325 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain0d5a2812015-11-13 10:07:31 +00006326 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
6327 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006328 case TypeCheckKind::kExactCheck:
6329 case TypeCheckKind::kAbstractClassCheck:
6330 case TypeCheckKind::kClassHierarchyCheck:
6331 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006332 call_kind =
6333 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006334 break;
6335 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006336 case TypeCheckKind::kUnresolvedCheck:
6337 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006338 call_kind = LocationSummary::kCallOnSlowPath;
6339 break;
6340 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006341
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006342 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006343 locations->SetInAt(0, Location::RequiresRegister());
6344 locations->SetInAt(1, Location::Any());
6345 // Note that TypeCheckSlowPathX86 uses this "out" register too.
6346 locations->SetOut(Location::RequiresRegister());
6347 // When read barriers are enabled, we need a temporary register for
6348 // some cases.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006349 if (TypeCheckNeedsATemporary(type_check_kind)) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006350 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006351 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006352}
6353
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006354void InstructionCodeGeneratorX86::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006355 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006356 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006357 Location obj_loc = locations->InAt(0);
6358 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006359 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006360 Location out_loc = locations->Out();
6361 Register out = out_loc.AsRegister<Register>();
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006362 Location maybe_temp_loc = TypeCheckNeedsATemporary(type_check_kind) ?
Roland Levillain7c1559a2015-12-15 10:55:36 +00006363 locations->GetTemp(0) :
6364 Location::NoLocation();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006365 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006366 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6367 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6368 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Andreas Gampe85b62f22015-09-09 13:15:38 -07006369 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006370 NearLabel done, zero;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006371
6372 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006373 // Avoid null check if we know obj is not null.
6374 if (instruction->MustDoNullCheck()) {
6375 __ testl(obj, obj);
6376 __ j(kEqual, &zero);
6377 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006378
Roland Levillain0d5a2812015-11-13 10:07:31 +00006379 // /* HeapReference<Class> */ out = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006380 GenerateReferenceLoadTwoRegisters(instruction, out_loc, obj_loc, class_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006381
Roland Levillain7c1559a2015-12-15 10:55:36 +00006382 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006383 case TypeCheckKind::kExactCheck: {
6384 if (cls.IsRegister()) {
6385 __ cmpl(out, cls.AsRegister<Register>());
6386 } else {
6387 DCHECK(cls.IsStackSlot()) << cls;
6388 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6389 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006390
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006391 // Classes must be equal for the instanceof to succeed.
6392 __ j(kNotEqual, &zero);
6393 __ movl(out, Immediate(1));
6394 __ jmp(&done);
6395 break;
6396 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006397
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006398 case TypeCheckKind::kAbstractClassCheck: {
6399 // If the class is abstract, we eagerly fetch the super class of the
6400 // object to avoid doing a comparison we know will fail.
6401 NearLabel loop;
6402 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006403 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006404 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006405 __ testl(out, out);
6406 // If `out` is null, we use it for the result, and jump to `done`.
6407 __ j(kEqual, &done);
6408 if (cls.IsRegister()) {
6409 __ cmpl(out, cls.AsRegister<Register>());
6410 } else {
6411 DCHECK(cls.IsStackSlot()) << cls;
6412 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6413 }
6414 __ j(kNotEqual, &loop);
6415 __ movl(out, Immediate(1));
6416 if (zero.IsLinked()) {
6417 __ jmp(&done);
6418 }
6419 break;
6420 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006421
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006422 case TypeCheckKind::kClassHierarchyCheck: {
6423 // Walk over the class hierarchy to find a match.
6424 NearLabel loop, success;
6425 __ Bind(&loop);
6426 if (cls.IsRegister()) {
6427 __ cmpl(out, cls.AsRegister<Register>());
6428 } else {
6429 DCHECK(cls.IsStackSlot()) << cls;
6430 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6431 }
6432 __ j(kEqual, &success);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006433 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006434 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006435 __ testl(out, out);
6436 __ j(kNotEqual, &loop);
6437 // If `out` is null, we use it for the result, and jump to `done`.
6438 __ jmp(&done);
6439 __ Bind(&success);
6440 __ movl(out, Immediate(1));
6441 if (zero.IsLinked()) {
6442 __ jmp(&done);
6443 }
6444 break;
6445 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006446
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006447 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006448 // Do an exact check.
6449 NearLabel exact_check;
6450 if (cls.IsRegister()) {
6451 __ cmpl(out, cls.AsRegister<Register>());
6452 } else {
6453 DCHECK(cls.IsStackSlot()) << cls;
6454 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6455 }
6456 __ j(kEqual, &exact_check);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006457 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006458 // /* HeapReference<Class> */ out = out->component_type_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006459 GenerateReferenceLoadOneRegister(instruction, out_loc, component_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006460 __ testl(out, out);
6461 // If `out` is null, we use it for the result, and jump to `done`.
6462 __ j(kEqual, &done);
6463 __ cmpw(Address(out, primitive_offset), Immediate(Primitive::kPrimNot));
6464 __ j(kNotEqual, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006465 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006466 __ movl(out, Immediate(1));
6467 __ jmp(&done);
6468 break;
6469 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006470
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006471 case TypeCheckKind::kArrayCheck: {
6472 if (cls.IsRegister()) {
6473 __ cmpl(out, cls.AsRegister<Register>());
6474 } else {
6475 DCHECK(cls.IsStackSlot()) << cls;
6476 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6477 }
6478 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain0d5a2812015-11-13 10:07:31 +00006479 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
6480 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006481 codegen_->AddSlowPath(slow_path);
6482 __ j(kNotEqual, slow_path->GetEntryLabel());
6483 __ movl(out, Immediate(1));
6484 if (zero.IsLinked()) {
6485 __ jmp(&done);
6486 }
6487 break;
6488 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006489
Calin Juravle98893e12015-10-02 21:05:03 +01006490 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006491 case TypeCheckKind::kInterfaceCheck: {
6492 // Note that we indeed only call on slow path, but we always go
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006493 // into the slow path for the unresolved and interface check
Roland Levillain0d5a2812015-11-13 10:07:31 +00006494 // cases.
6495 //
6496 // We cannot directly call the InstanceofNonTrivial runtime
6497 // entry point without resorting to a type checking slow path
6498 // here (i.e. by calling InvokeRuntime directly), as it would
6499 // require to assign fixed registers for the inputs of this
6500 // HInstanceOf instruction (following the runtime calling
6501 // convention), which might be cluttered by the potential first
6502 // read barrier emission at the beginning of this method.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006503 //
6504 // TODO: Introduce a new runtime entry point taking the object
6505 // to test (instead of its class) as argument, and let it deal
6506 // with the read barrier issues. This will let us refactor this
6507 // case of the `switch` code as it was previously (with a direct
6508 // call to the runtime not using a type checking slow path).
6509 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006510 DCHECK(locations->OnlyCallsOnSlowPath());
6511 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
6512 /* is_fatal */ false);
6513 codegen_->AddSlowPath(slow_path);
6514 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006515 if (zero.IsLinked()) {
6516 __ jmp(&done);
6517 }
6518 break;
6519 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006520 }
6521
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006522 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006523 __ Bind(&zero);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006524 __ xorl(out, out);
6525 }
6526
6527 if (done.IsLinked()) {
6528 __ Bind(&done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006529 }
6530
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006531 if (slow_path != nullptr) {
6532 __ Bind(slow_path->GetExitLabel());
6533 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006534}
6535
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006536void LocationsBuilderX86::VisitCheckCast(HCheckCast* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006537 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
6538 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006539 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
6540 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006541 case TypeCheckKind::kExactCheck:
6542 case TypeCheckKind::kAbstractClassCheck:
6543 case TypeCheckKind::kClassHierarchyCheck:
6544 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006545 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
6546 LocationSummary::kCallOnSlowPath :
6547 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006548 break;
6549 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006550 case TypeCheckKind::kUnresolvedCheck:
6551 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006552 call_kind = LocationSummary::kCallOnSlowPath;
6553 break;
6554 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006555 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
6556 locations->SetInAt(0, Location::RequiresRegister());
6557 locations->SetInAt(1, Location::Any());
6558 // Note that TypeCheckSlowPathX86 uses this "temp" register too.
6559 locations->AddTemp(Location::RequiresRegister());
6560 // When read barriers are enabled, we need an additional temporary
6561 // register for some cases.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006562 if (TypeCheckNeedsATemporary(type_check_kind)) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006563 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006564 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006565}
6566
6567void InstructionCodeGeneratorX86::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006568 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006569 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006570 Location obj_loc = locations->InAt(0);
6571 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006572 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006573 Location temp_loc = locations->GetTemp(0);
6574 Register temp = temp_loc.AsRegister<Register>();
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006575 Location maybe_temp2_loc = TypeCheckNeedsATemporary(type_check_kind) ?
Roland Levillain7c1559a2015-12-15 10:55:36 +00006576 locations->GetTemp(1) :
6577 Location::NoLocation();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006578 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
6579 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6580 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6581 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006582
Roland Levillain0d5a2812015-11-13 10:07:31 +00006583 bool is_type_check_slow_path_fatal =
6584 (type_check_kind == TypeCheckKind::kExactCheck ||
6585 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
6586 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
6587 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
6588 !instruction->CanThrowIntoCatchBlock();
6589 SlowPathCode* type_check_slow_path =
6590 new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
6591 is_type_check_slow_path_fatal);
6592 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006593
Roland Levillain0d5a2812015-11-13 10:07:31 +00006594 NearLabel done;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006595 // Avoid null check if we know obj is not null.
6596 if (instruction->MustDoNullCheck()) {
6597 __ testl(obj, obj);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006598 __ j(kEqual, &done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006599 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006600
Roland Levillain0d5a2812015-11-13 10:07:31 +00006601 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006602 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006603
Roland Levillain0d5a2812015-11-13 10:07:31 +00006604 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006605 case TypeCheckKind::kExactCheck:
6606 case TypeCheckKind::kArrayCheck: {
6607 if (cls.IsRegister()) {
6608 __ cmpl(temp, cls.AsRegister<Register>());
6609 } else {
6610 DCHECK(cls.IsStackSlot()) << cls;
6611 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6612 }
6613 // Jump to slow path for throwing the exception or doing a
6614 // more involved array check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006615 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006616 break;
6617 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006618
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006619 case TypeCheckKind::kAbstractClassCheck: {
6620 // If the class is abstract, we eagerly fetch the super class of the
6621 // object to avoid doing a comparison we know will fail.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006622 NearLabel loop, compare_classes;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006623 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006624 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006625 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006626
6627 // If the class reference currently in `temp` is not null, jump
6628 // to the `compare_classes` label to compare it with the checked
6629 // class.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006630 __ testl(temp, temp);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006631 __ j(kNotEqual, &compare_classes);
6632 // Otherwise, jump to the slow path to throw the exception.
6633 //
6634 // But before, move back the object's class into `temp` before
6635 // going into the slow path, as it has been overwritten in the
6636 // meantime.
6637 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006638 GenerateReferenceLoadTwoRegisters(
6639 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006640 __ jmp(type_check_slow_path->GetEntryLabel());
6641
6642 __ Bind(&compare_classes);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006643 if (cls.IsRegister()) {
6644 __ cmpl(temp, cls.AsRegister<Register>());
6645 } else {
6646 DCHECK(cls.IsStackSlot()) << cls;
6647 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6648 }
6649 __ j(kNotEqual, &loop);
6650 break;
6651 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006652
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006653 case TypeCheckKind::kClassHierarchyCheck: {
6654 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006655 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006656 __ Bind(&loop);
6657 if (cls.IsRegister()) {
6658 __ cmpl(temp, cls.AsRegister<Register>());
6659 } else {
6660 DCHECK(cls.IsStackSlot()) << cls;
6661 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6662 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006663 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006664
Roland Levillain0d5a2812015-11-13 10:07:31 +00006665 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006666 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006667
6668 // If the class reference currently in `temp` is not null, jump
6669 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006670 __ testl(temp, temp);
6671 __ j(kNotEqual, &loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006672 // Otherwise, jump to the slow path to throw the exception.
6673 //
6674 // But before, move back the object's class into `temp` before
6675 // going into the slow path, as it has been overwritten in the
6676 // meantime.
6677 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006678 GenerateReferenceLoadTwoRegisters(
6679 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006680 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006681 break;
6682 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006683
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006684 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006685 // Do an exact check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006686 NearLabel check_non_primitive_component_type;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006687 if (cls.IsRegister()) {
6688 __ cmpl(temp, cls.AsRegister<Register>());
6689 } else {
6690 DCHECK(cls.IsStackSlot()) << cls;
6691 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6692 }
6693 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006694
6695 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006696 // /* HeapReference<Class> */ temp = temp->component_type_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006697 GenerateReferenceLoadOneRegister(instruction, temp_loc, component_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006698
6699 // If the component type is not null (i.e. the object is indeed
6700 // an array), jump to label `check_non_primitive_component_type`
6701 // to further check that this component type is not a primitive
6702 // type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006703 __ testl(temp, temp);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006704 __ j(kNotEqual, &check_non_primitive_component_type);
6705 // Otherwise, jump to the slow path to throw the exception.
6706 //
6707 // But before, move back the object's class into `temp` before
6708 // going into the slow path, as it has been overwritten in the
6709 // meantime.
6710 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006711 GenerateReferenceLoadTwoRegisters(
6712 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006713 __ jmp(type_check_slow_path->GetEntryLabel());
6714
6715 __ Bind(&check_non_primitive_component_type);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006716 __ cmpw(Address(temp, primitive_offset), Immediate(Primitive::kPrimNot));
Roland Levillain0d5a2812015-11-13 10:07:31 +00006717 __ j(kEqual, &done);
6718 // Same comment as above regarding `temp` and the slow path.
6719 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006720 GenerateReferenceLoadTwoRegisters(
6721 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006722 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006723 break;
6724 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006725
Calin Juravle98893e12015-10-02 21:05:03 +01006726 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006727 case TypeCheckKind::kInterfaceCheck:
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006728 // We always go into the type check slow path for the unresolved
6729 // and interface check cases.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006730 //
6731 // We cannot directly call the CheckCast runtime entry point
6732 // without resorting to a type checking slow path here (i.e. by
6733 // calling InvokeRuntime directly), as it would require to
6734 // assign fixed registers for the inputs of this HInstanceOf
6735 // instruction (following the runtime calling convention), which
6736 // might be cluttered by the potential first read barrier
6737 // emission at the beginning of this method.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006738 //
6739 // TODO: Introduce a new runtime entry point taking the object
6740 // to test (instead of its class) as argument, and let it deal
6741 // with the read barrier issues. This will let us refactor this
6742 // case of the `switch` code as it was previously (with a direct
6743 // call to the runtime not using a type checking slow path).
6744 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006745 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006746 break;
6747 }
6748 __ Bind(&done);
6749
Roland Levillain0d5a2812015-11-13 10:07:31 +00006750 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006751}
6752
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006753void LocationsBuilderX86::VisitMonitorOperation(HMonitorOperation* instruction) {
6754 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006755 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006756 InvokeRuntimeCallingConvention calling_convention;
6757 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6758}
6759
6760void InstructionCodeGeneratorX86::VisitMonitorOperation(HMonitorOperation* instruction) {
Alexandre Rames8158f282015-08-07 10:26:17 +01006761 codegen_->InvokeRuntime(instruction->IsEnter() ? QUICK_ENTRY_POINT(pLockObject)
6762 : QUICK_ENTRY_POINT(pUnlockObject),
6763 instruction,
6764 instruction->GetDexPc(),
6765 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00006766 if (instruction->IsEnter()) {
6767 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
6768 } else {
6769 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
6770 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006771}
6772
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006773void LocationsBuilderX86::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
6774void LocationsBuilderX86::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
6775void LocationsBuilderX86::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
6776
6777void LocationsBuilderX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
6778 LocationSummary* locations =
6779 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6780 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
6781 || instruction->GetResultType() == Primitive::kPrimLong);
6782 locations->SetInAt(0, Location::RequiresRegister());
6783 locations->SetInAt(1, Location::Any());
6784 locations->SetOut(Location::SameAsFirstInput());
6785}
6786
6787void InstructionCodeGeneratorX86::VisitAnd(HAnd* instruction) {
6788 HandleBitwiseOperation(instruction);
6789}
6790
6791void InstructionCodeGeneratorX86::VisitOr(HOr* instruction) {
6792 HandleBitwiseOperation(instruction);
6793}
6794
6795void InstructionCodeGeneratorX86::VisitXor(HXor* instruction) {
6796 HandleBitwiseOperation(instruction);
6797}
6798
6799void InstructionCodeGeneratorX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
6800 LocationSummary* locations = instruction->GetLocations();
6801 Location first = locations->InAt(0);
6802 Location second = locations->InAt(1);
6803 DCHECK(first.Equals(locations->Out()));
6804
6805 if (instruction->GetResultType() == Primitive::kPrimInt) {
6806 if (second.IsRegister()) {
6807 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006808 __ andl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006809 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006810 __ orl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006811 } else {
6812 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006813 __ xorl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006814 }
6815 } else if (second.IsConstant()) {
6816 if (instruction->IsAnd()) {
Roland Levillain199f3362014-11-27 17:15:16 +00006817 __ andl(first.AsRegister<Register>(),
6818 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006819 } else if (instruction->IsOr()) {
Roland Levillain199f3362014-11-27 17:15:16 +00006820 __ orl(first.AsRegister<Register>(),
6821 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006822 } else {
6823 DCHECK(instruction->IsXor());
Roland Levillain199f3362014-11-27 17:15:16 +00006824 __ xorl(first.AsRegister<Register>(),
6825 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006826 }
6827 } else {
6828 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006829 __ andl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006830 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006831 __ orl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006832 } else {
6833 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006834 __ xorl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006835 }
6836 }
6837 } else {
6838 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
6839 if (second.IsRegisterPair()) {
6840 if (instruction->IsAnd()) {
6841 __ andl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
6842 __ andl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
6843 } else if (instruction->IsOr()) {
6844 __ orl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
6845 __ orl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
6846 } else {
6847 DCHECK(instruction->IsXor());
6848 __ xorl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
6849 __ xorl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
6850 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006851 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006852 if (instruction->IsAnd()) {
6853 __ andl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
6854 __ andl(first.AsRegisterPairHigh<Register>(),
6855 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
6856 } else if (instruction->IsOr()) {
6857 __ orl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
6858 __ orl(first.AsRegisterPairHigh<Register>(),
6859 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
6860 } else {
6861 DCHECK(instruction->IsXor());
6862 __ xorl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
6863 __ xorl(first.AsRegisterPairHigh<Register>(),
6864 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
6865 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006866 } else {
6867 DCHECK(second.IsConstant()) << second;
6868 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006869 int32_t low_value = Low32Bits(value);
6870 int32_t high_value = High32Bits(value);
6871 Immediate low(low_value);
6872 Immediate high(high_value);
6873 Register first_low = first.AsRegisterPairLow<Register>();
6874 Register first_high = first.AsRegisterPairHigh<Register>();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006875 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006876 if (low_value == 0) {
6877 __ xorl(first_low, first_low);
6878 } else if (low_value != -1) {
6879 __ andl(first_low, low);
6880 }
6881 if (high_value == 0) {
6882 __ xorl(first_high, first_high);
6883 } else if (high_value != -1) {
6884 __ andl(first_high, high);
6885 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006886 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006887 if (low_value != 0) {
6888 __ orl(first_low, low);
6889 }
6890 if (high_value != 0) {
6891 __ orl(first_high, high);
6892 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006893 } else {
6894 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006895 if (low_value != 0) {
6896 __ xorl(first_low, low);
6897 }
6898 if (high_value != 0) {
6899 __ xorl(first_high, high);
6900 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006901 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006902 }
6903 }
6904}
6905
Roland Levillain7c1559a2015-12-15 10:55:36 +00006906void InstructionCodeGeneratorX86::GenerateReferenceLoadOneRegister(HInstruction* instruction,
6907 Location out,
6908 uint32_t offset,
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006909 Location maybe_temp) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006910 Register out_reg = out.AsRegister<Register>();
6911 if (kEmitCompilerReadBarrier) {
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006912 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillain7c1559a2015-12-15 10:55:36 +00006913 if (kUseBakerReadBarrier) {
6914 // Load with fast path based Baker's read barrier.
6915 // /* HeapReference<Object> */ out = *(out + offset)
6916 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006917 instruction, out, out_reg, offset, maybe_temp, /* needs_null_check */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006918 } else {
6919 // Load with slow path based read barrier.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006920 // Save the value of `out` into `maybe_temp` before overwriting it
Roland Levillain7c1559a2015-12-15 10:55:36 +00006921 // in the following move operation, as we will need it for the
6922 // read barrier below.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006923 __ movl(maybe_temp.AsRegister<Register>(), out_reg);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006924 // /* HeapReference<Object> */ out = *(out + offset)
6925 __ movl(out_reg, Address(out_reg, offset));
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006926 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006927 }
6928 } else {
6929 // Plain load with no read barrier.
6930 // /* HeapReference<Object> */ out = *(out + offset)
6931 __ movl(out_reg, Address(out_reg, offset));
6932 __ MaybeUnpoisonHeapReference(out_reg);
6933 }
6934}
6935
6936void InstructionCodeGeneratorX86::GenerateReferenceLoadTwoRegisters(HInstruction* instruction,
6937 Location out,
6938 Location obj,
6939 uint32_t offset,
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006940 Location maybe_temp) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006941 Register out_reg = out.AsRegister<Register>();
6942 Register obj_reg = obj.AsRegister<Register>();
6943 if (kEmitCompilerReadBarrier) {
6944 if (kUseBakerReadBarrier) {
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006945 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillain7c1559a2015-12-15 10:55:36 +00006946 // Load with fast path based Baker's read barrier.
6947 // /* HeapReference<Object> */ out = *(obj + offset)
6948 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006949 instruction, out, obj_reg, offset, maybe_temp, /* needs_null_check */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006950 } else {
6951 // Load with slow path based read barrier.
6952 // /* HeapReference<Object> */ out = *(obj + offset)
6953 __ movl(out_reg, Address(obj_reg, offset));
6954 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
6955 }
6956 } else {
6957 // Plain load with no read barrier.
6958 // /* HeapReference<Object> */ out = *(obj + offset)
6959 __ movl(out_reg, Address(obj_reg, offset));
6960 __ MaybeUnpoisonHeapReference(out_reg);
6961 }
6962}
6963
6964void InstructionCodeGeneratorX86::GenerateGcRootFieldLoad(HInstruction* instruction,
6965 Location root,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006966 const Address& address,
6967 Label* fixup_label) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006968 Register root_reg = root.AsRegister<Register>();
6969 if (kEmitCompilerReadBarrier) {
6970 if (kUseBakerReadBarrier) {
6971 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
6972 // Baker's read barrier are used:
6973 //
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006974 // root = *address;
Roland Levillain7c1559a2015-12-15 10:55:36 +00006975 // if (Thread::Current()->GetIsGcMarking()) {
6976 // root = ReadBarrier::Mark(root)
6977 // }
6978
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006979 // /* GcRoot<mirror::Object> */ root = *address
6980 __ movl(root_reg, address);
6981 if (fixup_label != nullptr) {
6982 __ Bind(fixup_label);
6983 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00006984 static_assert(
6985 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
6986 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
6987 "have different sizes.");
6988 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
6989 "art::mirror::CompressedReference<mirror::Object> and int32_t "
6990 "have different sizes.");
6991
6992 // Slow path used to mark the GC root `root`.
6993 SlowPathCode* slow_path =
Roland Levillain02b75802016-07-13 11:54:35 +01006994 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathX86(instruction, root);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006995 codegen_->AddSlowPath(slow_path);
6996
6997 __ fs()->cmpl(Address::Absolute(Thread::IsGcMarkingOffset<kX86WordSize>().Int32Value()),
6998 Immediate(0));
6999 __ j(kNotEqual, slow_path->GetEntryLabel());
7000 __ Bind(slow_path->GetExitLabel());
7001 } else {
7002 // GC root loaded through a slow path for read barriers other
7003 // than Baker's.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007004 // /* GcRoot<mirror::Object>* */ root = address
7005 __ leal(root_reg, address);
7006 if (fixup_label != nullptr) {
7007 __ Bind(fixup_label);
7008 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00007009 // /* mirror::Object* */ root = root->Read()
7010 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
7011 }
7012 } else {
7013 // Plain GC root load with no read barrier.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007014 // /* GcRoot<mirror::Object> */ root = *address
7015 __ movl(root_reg, address);
7016 if (fixup_label != nullptr) {
7017 __ Bind(fixup_label);
7018 }
Roland Levillaine3f43ac2016-01-19 15:07:47 +00007019 // Note that GC roots are not affected by heap poisoning, thus we
7020 // do not have to unpoison `root_reg` here.
Roland Levillain7c1559a2015-12-15 10:55:36 +00007021 }
7022}
7023
7024void CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
7025 Location ref,
7026 Register obj,
7027 uint32_t offset,
7028 Location temp,
7029 bool needs_null_check) {
7030 DCHECK(kEmitCompilerReadBarrier);
7031 DCHECK(kUseBakerReadBarrier);
7032
7033 // /* HeapReference<Object> */ ref = *(obj + offset)
7034 Address src(obj, offset);
7035 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, temp, needs_null_check);
7036}
7037
7038void CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
7039 Location ref,
7040 Register obj,
7041 uint32_t data_offset,
7042 Location index,
7043 Location temp,
7044 bool needs_null_check) {
7045 DCHECK(kEmitCompilerReadBarrier);
7046 DCHECK(kUseBakerReadBarrier);
7047
Roland Levillain3d312422016-06-23 13:53:42 +01007048 static_assert(
7049 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
7050 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain7c1559a2015-12-15 10:55:36 +00007051 // /* HeapReference<Object> */ ref =
7052 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
7053 Address src = index.IsConstant() ?
7054 Address(obj, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset) :
7055 Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset);
7056 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, temp, needs_null_check);
7057}
7058
7059void CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
7060 Location ref,
7061 Register obj,
7062 const Address& src,
7063 Location temp,
7064 bool needs_null_check) {
7065 DCHECK(kEmitCompilerReadBarrier);
7066 DCHECK(kUseBakerReadBarrier);
7067
7068 // In slow path based read barriers, the read barrier call is
7069 // inserted after the original load. However, in fast path based
7070 // Baker's read barriers, we need to perform the load of
7071 // mirror::Object::monitor_ *before* the original reference load.
7072 // This load-load ordering is required by the read barrier.
7073 // The fast path/slow path (for Baker's algorithm) should look like:
7074 //
7075 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
7076 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
7077 // HeapReference<Object> ref = *src; // Original reference load.
7078 // bool is_gray = (rb_state == ReadBarrier::gray_ptr_);
7079 // if (is_gray) {
7080 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
7081 // }
7082 //
7083 // Note: the original implementation in ReadBarrier::Barrier is
7084 // slightly more complex as:
7085 // - it implements the load-load fence using a data dependency on
Roland Levillaine3f43ac2016-01-19 15:07:47 +00007086 // the high-bits of rb_state, which are expected to be all zeroes
7087 // (we use CodeGeneratorX86::GenerateMemoryBarrier instead here,
7088 // which is a no-op thanks to the x86 memory model);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007089 // - it performs additional checks that we do not do here for
7090 // performance reasons.
7091
7092 Register ref_reg = ref.AsRegister<Register>();
7093 Register temp_reg = temp.AsRegister<Register>();
7094 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
7095
7096 // /* int32_t */ monitor = obj->monitor_
7097 __ movl(temp_reg, Address(obj, monitor_offset));
7098 if (needs_null_check) {
7099 MaybeRecordImplicitNullCheck(instruction);
7100 }
7101 // /* LockWord */ lock_word = LockWord(monitor)
7102 static_assert(sizeof(LockWord) == sizeof(int32_t),
7103 "art::LockWord and int32_t have different sizes.");
7104 // /* uint32_t */ rb_state = lock_word.ReadBarrierState()
7105 __ shrl(temp_reg, Immediate(LockWord::kReadBarrierStateShift));
7106 __ andl(temp_reg, Immediate(LockWord::kReadBarrierStateMask));
7107 static_assert(
7108 LockWord::kReadBarrierStateMask == ReadBarrier::rb_ptr_mask_,
7109 "art::LockWord::kReadBarrierStateMask is not equal to art::ReadBarrier::rb_ptr_mask_.");
7110
7111 // Load fence to prevent load-load reordering.
7112 // Note that this is a no-op, thanks to the x86 memory model.
7113 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
7114
7115 // The actual reference load.
7116 // /* HeapReference<Object> */ ref = *src
7117 __ movl(ref_reg, src);
7118
7119 // Object* ref = ref_addr->AsMirrorPtr()
7120 __ MaybeUnpoisonHeapReference(ref_reg);
7121
7122 // Slow path used to mark the object `ref` when it is gray.
7123 SlowPathCode* slow_path =
Roland Levillain02b75802016-07-13 11:54:35 +01007124 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathX86(instruction, ref);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007125 AddSlowPath(slow_path);
7126
7127 // if (rb_state == ReadBarrier::gray_ptr_)
7128 // ref = ReadBarrier::Mark(ref);
7129 __ cmpl(temp_reg, Immediate(ReadBarrier::gray_ptr_));
7130 __ j(kEqual, slow_path->GetEntryLabel());
7131 __ Bind(slow_path->GetExitLabel());
7132}
7133
7134void CodeGeneratorX86::GenerateReadBarrierSlow(HInstruction* instruction,
7135 Location out,
7136 Location ref,
7137 Location obj,
7138 uint32_t offset,
7139 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007140 DCHECK(kEmitCompilerReadBarrier);
7141
Roland Levillain7c1559a2015-12-15 10:55:36 +00007142 // Insert a slow path based read barrier *after* the reference load.
7143 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00007144 // If heap poisoning is enabled, the unpoisoning of the loaded
7145 // reference will be carried out by the runtime within the slow
7146 // path.
7147 //
7148 // Note that `ref` currently does not get unpoisoned (when heap
7149 // poisoning is enabled), which is alright as the `ref` argument is
7150 // not used by the artReadBarrierSlow entry point.
7151 //
7152 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
7153 SlowPathCode* slow_path = new (GetGraph()->GetArena())
7154 ReadBarrierForHeapReferenceSlowPathX86(instruction, out, ref, obj, offset, index);
7155 AddSlowPath(slow_path);
7156
Roland Levillain0d5a2812015-11-13 10:07:31 +00007157 __ jmp(slow_path->GetEntryLabel());
7158 __ Bind(slow_path->GetExitLabel());
7159}
7160
Roland Levillain7c1559a2015-12-15 10:55:36 +00007161void CodeGeneratorX86::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
7162 Location out,
7163 Location ref,
7164 Location obj,
7165 uint32_t offset,
7166 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007167 if (kEmitCompilerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007168 // Baker's read barriers shall be handled by the fast path
7169 // (CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier).
7170 DCHECK(!kUseBakerReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007171 // If heap poisoning is enabled, unpoisoning will be taken care of
7172 // by the runtime within the slow path.
Roland Levillain7c1559a2015-12-15 10:55:36 +00007173 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007174 } else if (kPoisonHeapReferences) {
7175 __ UnpoisonHeapReference(out.AsRegister<Register>());
7176 }
7177}
7178
Roland Levillain7c1559a2015-12-15 10:55:36 +00007179void CodeGeneratorX86::GenerateReadBarrierForRootSlow(HInstruction* instruction,
7180 Location out,
7181 Location root) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007182 DCHECK(kEmitCompilerReadBarrier);
7183
Roland Levillain7c1559a2015-12-15 10:55:36 +00007184 // Insert a slow path based read barrier *after* the GC root load.
7185 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00007186 // Note that GC roots are not affected by heap poisoning, so we do
7187 // not need to do anything special for this here.
7188 SlowPathCode* slow_path =
7189 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathX86(instruction, out, root);
7190 AddSlowPath(slow_path);
7191
Roland Levillain0d5a2812015-11-13 10:07:31 +00007192 __ jmp(slow_path->GetEntryLabel());
7193 __ Bind(slow_path->GetExitLabel());
7194}
7195
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007196void LocationsBuilderX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00007197 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00007198 LOG(FATAL) << "Unreachable";
7199}
7200
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007201void InstructionCodeGeneratorX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00007202 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00007203 LOG(FATAL) << "Unreachable";
7204}
7205
Mark Mendellfe57faa2015-09-18 09:26:15 -04007206// Simple implementation of packed switch - generate cascaded compare/jumps.
7207void LocationsBuilderX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7208 LocationSummary* locations =
7209 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
7210 locations->SetInAt(0, Location::RequiresRegister());
7211}
7212
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007213void InstructionCodeGeneratorX86::GenPackedSwitchWithCompares(Register value_reg,
7214 int32_t lower_bound,
7215 uint32_t num_entries,
7216 HBasicBlock* switch_block,
7217 HBasicBlock* default_block) {
7218 // Figure out the correct compare values and jump conditions.
7219 // Handle the first compare/branch as a special case because it might
7220 // jump to the default case.
7221 DCHECK_GT(num_entries, 2u);
7222 Condition first_condition;
7223 uint32_t index;
7224 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
7225 if (lower_bound != 0) {
7226 first_condition = kLess;
7227 __ cmpl(value_reg, Immediate(lower_bound));
7228 __ j(first_condition, codegen_->GetLabelOf(default_block));
7229 __ j(kEqual, codegen_->GetLabelOf(successors[0]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007230
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007231 index = 1;
7232 } else {
7233 // Handle all the compare/jumps below.
7234 first_condition = kBelow;
7235 index = 0;
7236 }
7237
7238 // Handle the rest of the compare/jumps.
7239 for (; index + 1 < num_entries; index += 2) {
7240 int32_t compare_to_value = lower_bound + index + 1;
7241 __ cmpl(value_reg, Immediate(compare_to_value));
7242 // Jump to successors[index] if value < case_value[index].
7243 __ j(first_condition, codegen_->GetLabelOf(successors[index]));
7244 // Jump to successors[index + 1] if value == case_value[index + 1].
7245 __ j(kEqual, codegen_->GetLabelOf(successors[index + 1]));
7246 }
7247
7248 if (index != num_entries) {
7249 // There are an odd number of entries. Handle the last one.
7250 DCHECK_EQ(index + 1, num_entries);
7251 __ cmpl(value_reg, Immediate(lower_bound + index));
7252 __ j(kEqual, codegen_->GetLabelOf(successors[index]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007253 }
7254
7255 // And the default for any other value.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007256 if (!codegen_->GoesToNextBlock(switch_block, default_block)) {
7257 __ jmp(codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007258 }
7259}
7260
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007261void InstructionCodeGeneratorX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7262 int32_t lower_bound = switch_instr->GetStartValue();
7263 uint32_t num_entries = switch_instr->GetNumEntries();
7264 LocationSummary* locations = switch_instr->GetLocations();
7265 Register value_reg = locations->InAt(0).AsRegister<Register>();
7266
7267 GenPackedSwitchWithCompares(value_reg,
7268 lower_bound,
7269 num_entries,
7270 switch_instr->GetBlock(),
7271 switch_instr->GetDefaultBlock());
7272}
7273
Mark Mendell805b3b52015-09-18 14:10:29 -04007274void LocationsBuilderX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
7275 LocationSummary* locations =
7276 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
7277 locations->SetInAt(0, Location::RequiresRegister());
7278
7279 // Constant area pointer.
7280 locations->SetInAt(1, Location::RequiresRegister());
7281
7282 // And the temporary we need.
7283 locations->AddTemp(Location::RequiresRegister());
7284}
7285
7286void InstructionCodeGeneratorX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
7287 int32_t lower_bound = switch_instr->GetStartValue();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007288 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendell805b3b52015-09-18 14:10:29 -04007289 LocationSummary* locations = switch_instr->GetLocations();
7290 Register value_reg = locations->InAt(0).AsRegister<Register>();
7291 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
7292
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007293 if (num_entries <= kPackedSwitchJumpTableThreshold) {
7294 GenPackedSwitchWithCompares(value_reg,
7295 lower_bound,
7296 num_entries,
7297 switch_instr->GetBlock(),
7298 default_block);
7299 return;
7300 }
7301
Mark Mendell805b3b52015-09-18 14:10:29 -04007302 // Optimizing has a jump area.
7303 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
7304 Register constant_area = locations->InAt(1).AsRegister<Register>();
7305
7306 // Remove the bias, if needed.
7307 if (lower_bound != 0) {
7308 __ leal(temp_reg, Address(value_reg, -lower_bound));
7309 value_reg = temp_reg;
7310 }
7311
7312 // Is the value in range?
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007313 DCHECK_GE(num_entries, 1u);
Mark Mendell805b3b52015-09-18 14:10:29 -04007314 __ cmpl(value_reg, Immediate(num_entries - 1));
7315 __ j(kAbove, codegen_->GetLabelOf(default_block));
7316
7317 // We are in the range of the table.
7318 // Load (target-constant_area) from the jump table, indexing by the value.
7319 __ movl(temp_reg, codegen_->LiteralCaseTable(switch_instr, constant_area, value_reg));
7320
7321 // Compute the actual target address by adding in constant_area.
7322 __ addl(temp_reg, constant_area);
7323
7324 // And jump.
7325 __ jmp(temp_reg);
7326}
7327
Mark Mendell0616ae02015-04-17 12:49:27 -04007328void LocationsBuilderX86::VisitX86ComputeBaseMethodAddress(
7329 HX86ComputeBaseMethodAddress* insn) {
7330 LocationSummary* locations =
7331 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
7332 locations->SetOut(Location::RequiresRegister());
7333}
7334
7335void InstructionCodeGeneratorX86::VisitX86ComputeBaseMethodAddress(
7336 HX86ComputeBaseMethodAddress* insn) {
7337 LocationSummary* locations = insn->GetLocations();
7338 Register reg = locations->Out().AsRegister<Register>();
7339
7340 // Generate call to next instruction.
7341 Label next_instruction;
7342 __ call(&next_instruction);
7343 __ Bind(&next_instruction);
7344
7345 // Remember this offset for later use with constant area.
7346 codegen_->SetMethodAddressOffset(GetAssembler()->CodeSize());
7347
7348 // Grab the return address off the stack.
7349 __ popl(reg);
7350}
7351
7352void LocationsBuilderX86::VisitX86LoadFromConstantTable(
7353 HX86LoadFromConstantTable* insn) {
7354 LocationSummary* locations =
7355 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
7356
7357 locations->SetInAt(0, Location::RequiresRegister());
7358 locations->SetInAt(1, Location::ConstantLocation(insn->GetConstant()));
7359
7360 // If we don't need to be materialized, we only need the inputs to be set.
David Brazdilb3e773e2016-01-26 11:28:37 +00007361 if (insn->IsEmittedAtUseSite()) {
Mark Mendell0616ae02015-04-17 12:49:27 -04007362 return;
7363 }
7364
7365 switch (insn->GetType()) {
7366 case Primitive::kPrimFloat:
7367 case Primitive::kPrimDouble:
7368 locations->SetOut(Location::RequiresFpuRegister());
7369 break;
7370
7371 case Primitive::kPrimInt:
7372 locations->SetOut(Location::RequiresRegister());
7373 break;
7374
7375 default:
7376 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
7377 }
7378}
7379
7380void InstructionCodeGeneratorX86::VisitX86LoadFromConstantTable(HX86LoadFromConstantTable* insn) {
David Brazdilb3e773e2016-01-26 11:28:37 +00007381 if (insn->IsEmittedAtUseSite()) {
Mark Mendell0616ae02015-04-17 12:49:27 -04007382 return;
7383 }
7384
7385 LocationSummary* locations = insn->GetLocations();
7386 Location out = locations->Out();
7387 Register const_area = locations->InAt(0).AsRegister<Register>();
7388 HConstant *value = insn->GetConstant();
7389
7390 switch (insn->GetType()) {
7391 case Primitive::kPrimFloat:
7392 __ movss(out.AsFpuRegister<XmmRegister>(),
7393 codegen_->LiteralFloatAddress(value->AsFloatConstant()->GetValue(), const_area));
7394 break;
7395
7396 case Primitive::kPrimDouble:
7397 __ movsd(out.AsFpuRegister<XmmRegister>(),
7398 codegen_->LiteralDoubleAddress(value->AsDoubleConstant()->GetValue(), const_area));
7399 break;
7400
7401 case Primitive::kPrimInt:
7402 __ movl(out.AsRegister<Register>(),
7403 codegen_->LiteralInt32Address(value->AsIntConstant()->GetValue(), const_area));
7404 break;
7405
7406 default:
7407 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
7408 }
7409}
7410
Mark Mendell0616ae02015-04-17 12:49:27 -04007411/**
7412 * Class to handle late fixup of offsets into constant area.
7413 */
Vladimir Marko5233f932015-09-29 19:01:15 +01007414class RIPFixup : public AssemblerFixup, public ArenaObject<kArenaAllocCodeGenerator> {
Mark Mendell0616ae02015-04-17 12:49:27 -04007415 public:
Mark Mendell805b3b52015-09-18 14:10:29 -04007416 RIPFixup(CodeGeneratorX86& codegen, size_t offset)
7417 : codegen_(&codegen), offset_into_constant_area_(offset) {}
7418
7419 protected:
7420 void SetOffset(size_t offset) { offset_into_constant_area_ = offset; }
7421
7422 CodeGeneratorX86* codegen_;
Mark Mendell0616ae02015-04-17 12:49:27 -04007423
7424 private:
7425 void Process(const MemoryRegion& region, int pos) OVERRIDE {
7426 // Patch the correct offset for the instruction. The place to patch is the
7427 // last 4 bytes of the instruction.
7428 // The value to patch is the distance from the offset in the constant area
7429 // from the address computed by the HX86ComputeBaseMethodAddress instruction.
Mark Mendell805b3b52015-09-18 14:10:29 -04007430 int32_t constant_offset = codegen_->ConstantAreaStart() + offset_into_constant_area_;
7431 int32_t relative_position = constant_offset - codegen_->GetMethodAddressOffset();;
Mark Mendell0616ae02015-04-17 12:49:27 -04007432
7433 // Patch in the right value.
7434 region.StoreUnaligned<int32_t>(pos - 4, relative_position);
7435 }
7436
Mark Mendell0616ae02015-04-17 12:49:27 -04007437 // Location in constant area that the fixup refers to.
Mark Mendell805b3b52015-09-18 14:10:29 -04007438 int32_t offset_into_constant_area_;
Mark Mendell0616ae02015-04-17 12:49:27 -04007439};
7440
Mark Mendell805b3b52015-09-18 14:10:29 -04007441/**
7442 * Class to handle late fixup of offsets to a jump table that will be created in the
7443 * constant area.
7444 */
7445class JumpTableRIPFixup : public RIPFixup {
7446 public:
7447 JumpTableRIPFixup(CodeGeneratorX86& codegen, HX86PackedSwitch* switch_instr)
7448 : RIPFixup(codegen, static_cast<size_t>(-1)), switch_instr_(switch_instr) {}
7449
7450 void CreateJumpTable() {
7451 X86Assembler* assembler = codegen_->GetAssembler();
7452
7453 // Ensure that the reference to the jump table has the correct offset.
7454 const int32_t offset_in_constant_table = assembler->ConstantAreaSize();
7455 SetOffset(offset_in_constant_table);
7456
7457 // The label values in the jump table are computed relative to the
7458 // instruction addressing the constant area.
7459 const int32_t relative_offset = codegen_->GetMethodAddressOffset();
7460
7461 // Populate the jump table with the correct values for the jump table.
7462 int32_t num_entries = switch_instr_->GetNumEntries();
7463 HBasicBlock* block = switch_instr_->GetBlock();
7464 const ArenaVector<HBasicBlock*>& successors = block->GetSuccessors();
7465 // The value that we want is the target offset - the position of the table.
7466 for (int32_t i = 0; i < num_entries; i++) {
7467 HBasicBlock* b = successors[i];
7468 Label* l = codegen_->GetLabelOf(b);
7469 DCHECK(l->IsBound());
7470 int32_t offset_to_block = l->Position() - relative_offset;
7471 assembler->AppendInt32(offset_to_block);
7472 }
7473 }
7474
7475 private:
7476 const HX86PackedSwitch* switch_instr_;
7477};
7478
7479void CodeGeneratorX86::Finalize(CodeAllocator* allocator) {
7480 // Generate the constant area if needed.
7481 X86Assembler* assembler = GetAssembler();
7482 if (!assembler->IsConstantAreaEmpty() || !fixups_to_jump_tables_.empty()) {
7483 // Align to 4 byte boundary to reduce cache misses, as the data is 4 and 8
7484 // byte values.
7485 assembler->Align(4, 0);
7486 constant_area_start_ = assembler->CodeSize();
7487
7488 // Populate any jump tables.
7489 for (auto jump_table : fixups_to_jump_tables_) {
7490 jump_table->CreateJumpTable();
7491 }
7492
7493 // And now add the constant area to the generated code.
7494 assembler->AddConstantArea();
7495 }
7496
7497 // And finish up.
7498 CodeGenerator::Finalize(allocator);
7499}
7500
Mark Mendell0616ae02015-04-17 12:49:27 -04007501Address CodeGeneratorX86::LiteralDoubleAddress(double v, Register reg) {
7502 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddDouble(v));
7503 return Address(reg, kDummy32BitOffset, fixup);
7504}
7505
7506Address CodeGeneratorX86::LiteralFloatAddress(float v, Register reg) {
7507 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddFloat(v));
7508 return Address(reg, kDummy32BitOffset, fixup);
7509}
7510
7511Address CodeGeneratorX86::LiteralInt32Address(int32_t v, Register reg) {
7512 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt32(v));
7513 return Address(reg, kDummy32BitOffset, fixup);
7514}
7515
7516Address CodeGeneratorX86::LiteralInt64Address(int64_t v, Register reg) {
7517 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt64(v));
7518 return Address(reg, kDummy32BitOffset, fixup);
7519}
7520
Aart Bika19616e2016-02-01 18:57:58 -08007521void CodeGeneratorX86::Load32BitValue(Register dest, int32_t value) {
7522 if (value == 0) {
7523 __ xorl(dest, dest);
7524 } else {
7525 __ movl(dest, Immediate(value));
7526 }
7527}
7528
7529void CodeGeneratorX86::Compare32BitValue(Register dest, int32_t value) {
7530 if (value == 0) {
7531 __ testl(dest, dest);
7532 } else {
7533 __ cmpl(dest, Immediate(value));
7534 }
7535}
7536
Mark Mendell805b3b52015-09-18 14:10:29 -04007537Address CodeGeneratorX86::LiteralCaseTable(HX86PackedSwitch* switch_instr,
7538 Register reg,
7539 Register value) {
7540 // Create a fixup to be used to create and address the jump table.
7541 JumpTableRIPFixup* table_fixup =
7542 new (GetGraph()->GetArena()) JumpTableRIPFixup(*this, switch_instr);
7543
7544 // We have to populate the jump tables.
7545 fixups_to_jump_tables_.push_back(table_fixup);
7546
7547 // We want a scaled address, as we are extracting the correct offset from the table.
7548 return Address(reg, value, TIMES_4, kDummy32BitOffset, table_fixup);
7549}
7550
Andreas Gampe85b62f22015-09-09 13:15:38 -07007551// TODO: target as memory.
7552void CodeGeneratorX86::MoveFromReturnRegister(Location target, Primitive::Type type) {
7553 if (!target.IsValid()) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007554 DCHECK_EQ(type, Primitive::kPrimVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07007555 return;
7556 }
7557
7558 DCHECK_NE(type, Primitive::kPrimVoid);
7559
7560 Location return_loc = InvokeDexCallingConventionVisitorX86().GetReturnLocation(type);
7561 if (target.Equals(return_loc)) {
7562 return;
7563 }
7564
7565 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
7566 // with the else branch.
7567 if (type == Primitive::kPrimLong) {
7568 HParallelMove parallel_move(GetGraph()->GetArena());
7569 parallel_move.AddMove(return_loc.ToLow(), target.ToLow(), Primitive::kPrimInt, nullptr);
7570 parallel_move.AddMove(return_loc.ToHigh(), target.ToHigh(), Primitive::kPrimInt, nullptr);
7571 GetMoveResolver()->EmitNativeCode(&parallel_move);
7572 } else {
7573 // Let the parallel move resolver take care of all of this.
7574 HParallelMove parallel_move(GetGraph()->GetArena());
7575 parallel_move.AddMove(return_loc, target, type, nullptr);
7576 GetMoveResolver()->EmitNativeCode(&parallel_move);
7577 }
7578}
7579
Roland Levillain4d027112015-07-01 15:41:14 +01007580#undef __
7581
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00007582} // namespace x86
7583} // namespace art