blob: 4689ccb05ce29683eecbbc30febb00b3a242afad [file] [log] [blame]
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "code_generator_x86.h"
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +010018
Mathieu Chartiere401d142015-04-22 13:56:20 -070019#include "art_method.h"
Guillaume Sanchez0f88e872015-03-30 17:55:45 +010020#include "code_generator_utils.h"
Vladimir Marko58155012015-08-19 12:49:41 +000021#include "compiled_method.h"
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +010022#include "entrypoints/quick/quick_entrypoints.h"
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +000023#include "entrypoints/quick/quick_entrypoints_enum.h"
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +010024#include "gc/accounting/card_table.h"
Mark Mendell09ed1a32015-03-25 08:30:06 -040025#include "intrinsics.h"
26#include "intrinsics_x86.h"
Ian Rogers7e70b002014-10-08 11:47:24 -070027#include "mirror/array-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070028#include "mirror/class-inl.h"
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +010029#include "thread.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000030#include "utils/assembler.h"
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010031#include "utils/stack_checks.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000032#include "utils/x86/assembler_x86.h"
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010033#include "utils/x86/managed_register_x86.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000034
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000035namespace art {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010036
Roland Levillain0d5a2812015-11-13 10:07:31 +000037template<class MirrorType>
38class GcRoot;
39
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000040namespace x86 {
41
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010042static constexpr int kCurrentMethodStackOffset = 0;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +010043static constexpr Register kMethodRegisterArgument = EAX;
Mark Mendell5f874182015-03-04 15:42:45 -050044static constexpr Register kCoreCalleeSaves[] = { EBP, ESI, EDI };
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010045
Mark Mendell24f2dfa2015-01-14 19:51:45 -050046static constexpr int kC2ConditionMask = 0x400;
47
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +000048static constexpr int kFakeReturnRegister = Register(8);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +000049
Roland Levillain7cbd27f2016-08-11 23:53:33 +010050// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
51#define __ down_cast<X86Assembler*>(codegen->GetAssembler())-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -070052#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kX86PointerSize, x).Int32Value()
Nicolas Geoffraye5038322014-07-04 09:41:32 +010053
Andreas Gampe85b62f22015-09-09 13:15:38 -070054class NullCheckSlowPathX86 : public SlowPathCode {
Nicolas Geoffraye5038322014-07-04 09:41:32 +010055 public:
David Srbecky9cd6d372016-02-09 15:24:47 +000056 explicit NullCheckSlowPathX86(HNullCheck* instruction) : SlowPathCode(instruction) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +010057
Alexandre Rames2ed20af2015-03-06 13:55:35 +000058 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames8158f282015-08-07 10:26:17 +010059 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffraye5038322014-07-04 09:41:32 +010060 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +000061 if (instruction_->CanThrowIntoCatchBlock()) {
62 // Live registers will be restored in the catch block if caught.
63 SaveLiveRegisters(codegen, instruction_->GetLocations());
64 }
Serban Constantinescuba45db02016-07-12 22:53:02 +010065 x86_codegen->InvokeRuntime(kQuickThrowNullPointer,
Alexandre Rames8158f282015-08-07 10:26:17 +010066 instruction_,
67 instruction_->GetDexPc(),
68 this);
Roland Levillain888d0672015-11-23 18:53:50 +000069 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +010070 }
71
Alexandre Rames8158f282015-08-07 10:26:17 +010072 bool IsFatal() const OVERRIDE { return true; }
73
Alexandre Rames9931f312015-06-19 14:47:01 +010074 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathX86"; }
75
Nicolas Geoffraye5038322014-07-04 09:41:32 +010076 private:
Nicolas Geoffraye5038322014-07-04 09:41:32 +010077 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathX86);
78};
79
Andreas Gampe85b62f22015-09-09 13:15:38 -070080class DivZeroCheckSlowPathX86 : public SlowPathCode {
Calin Juravled0d48522014-11-04 16:40:20 +000081 public:
David Srbecky9cd6d372016-02-09 15:24:47 +000082 explicit DivZeroCheckSlowPathX86(HDivZeroCheck* instruction) : SlowPathCode(instruction) {}
Calin Juravled0d48522014-11-04 16:40:20 +000083
Alexandre Rames2ed20af2015-03-06 13:55:35 +000084 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames8158f282015-08-07 10:26:17 +010085 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Calin Juravled0d48522014-11-04 16:40:20 +000086 __ Bind(GetEntryLabel());
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 }
Serban Constantinescuba45db02016-07-12 22:53:02 +010091 x86_codegen->InvokeRuntime(kQuickThrowDivZero, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +000092 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
Calin Juravled0d48522014-11-04 16:40:20 +000093 }
94
Alexandre Rames8158f282015-08-07 10:26:17 +010095 bool IsFatal() const OVERRIDE { return true; }
96
Alexandre Rames9931f312015-06-19 14:47:01 +010097 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathX86"; }
98
Calin Juravled0d48522014-11-04 16:40:20 +000099 private:
Calin Juravled0d48522014-11-04 16:40:20 +0000100 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathX86);
101};
102
Andreas Gampe85b62f22015-09-09 13:15:38 -0700103class DivRemMinusOneSlowPathX86 : public SlowPathCode {
Calin Juravled0d48522014-11-04 16:40:20 +0000104 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000105 DivRemMinusOneSlowPathX86(HInstruction* instruction, Register reg, bool is_div)
106 : SlowPathCode(instruction), reg_(reg), is_div_(is_div) {}
Calin Juravled0d48522014-11-04 16:40:20 +0000107
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000108 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +0000109 __ Bind(GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +0000110 if (is_div_) {
111 __ negl(reg_);
112 } else {
113 __ movl(reg_, Immediate(0));
114 }
Calin Juravled0d48522014-11-04 16:40:20 +0000115 __ jmp(GetExitLabel());
116 }
117
Alexandre Rames9931f312015-06-19 14:47:01 +0100118 const char* GetDescription() const OVERRIDE { return "DivRemMinusOneSlowPathX86"; }
119
Calin Juravled0d48522014-11-04 16:40:20 +0000120 private:
121 Register reg_;
Calin Juravlebacfec32014-11-14 15:54:36 +0000122 bool is_div_;
123 DISALLOW_COPY_AND_ASSIGN(DivRemMinusOneSlowPathX86);
Calin Juravled0d48522014-11-04 16:40:20 +0000124};
125
Andreas Gampe85b62f22015-09-09 13:15:38 -0700126class BoundsCheckSlowPathX86 : public SlowPathCode {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100127 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000128 explicit BoundsCheckSlowPathX86(HBoundsCheck* instruction) : SlowPathCode(instruction) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100129
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000130 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100131 LocationSummary* locations = instruction_->GetLocations();
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100132 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100133 __ Bind(GetEntryLabel());
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000134 // We're moving two locations to locations that could overlap, so we need a parallel
135 // move resolver.
David Brazdil77a48ae2015-09-15 12:34:04 +0000136 if (instruction_->CanThrowIntoCatchBlock()) {
137 // Live registers will be restored in the catch block if caught.
138 SaveLiveRegisters(codegen, instruction_->GetLocations());
139 }
Mark Mendellee8d9712016-07-12 11:13:15 -0400140
141 // Are we using an array length from memory?
142 HInstruction* array_length = instruction_->InputAt(1);
143 Location length_loc = locations->InAt(1);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100144 InvokeRuntimeCallingConvention calling_convention;
Mark Mendellee8d9712016-07-12 11:13:15 -0400145 if (array_length->IsArrayLength() && array_length->IsEmittedAtUseSite()) {
146 // Load the array length into our temporary.
147 uint32_t len_offset = CodeGenerator::GetArrayLengthOffset(array_length->AsArrayLength());
148 Location array_loc = array_length->GetLocations()->InAt(0);
149 Address array_len(array_loc.AsRegister<Register>(), len_offset);
150 length_loc = Location::RegisterLocation(calling_convention.GetRegisterAt(1));
151 // Check for conflicts with index.
152 if (length_loc.Equals(locations->InAt(0))) {
153 // We know we aren't using parameter 2.
154 length_loc = Location::RegisterLocation(calling_convention.GetRegisterAt(2));
155 }
156 __ movl(length_loc.AsRegister<Register>(), array_len);
157 }
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000158 x86_codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100159 locations->InAt(0),
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000160 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100161 Primitive::kPrimInt,
Mark Mendellee8d9712016-07-12 11:13:15 -0400162 length_loc,
Nicolas Geoffray90218252015-04-15 11:56:51 +0100163 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
164 Primitive::kPrimInt);
Serban Constantinescuba45db02016-07-12 22:53:02 +0100165 QuickEntrypointEnum entrypoint = instruction_->AsBoundsCheck()->IsStringCharAt()
166 ? kQuickThrowStringBounds
167 : kQuickThrowArrayBounds;
168 x86_codegen->InvokeRuntime(entrypoint, instruction_, instruction_->GetDexPc(), this);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100169 CheckEntrypointTypes<kQuickThrowStringBounds, void, int32_t, int32_t>();
Roland Levillain888d0672015-11-23 18:53:50 +0000170 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100171 }
172
Alexandre Rames8158f282015-08-07 10:26:17 +0100173 bool IsFatal() const OVERRIDE { return true; }
174
Alexandre Rames9931f312015-06-19 14:47:01 +0100175 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathX86"; }
176
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100177 private:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100178 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathX86);
179};
180
Andreas Gampe85b62f22015-09-09 13:15:38 -0700181class SuspendCheckSlowPathX86 : public SlowPathCode {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000182 public:
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000183 SuspendCheckSlowPathX86(HSuspendCheck* instruction, HBasicBlock* successor)
David Srbecky9cd6d372016-02-09 15:24:47 +0000184 : SlowPathCode(instruction), successor_(successor) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000185
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000186 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100187 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000188 __ Bind(GetEntryLabel());
Serban Constantinescuba45db02016-07-12 22:53:02 +0100189 x86_codegen->InvokeRuntime(kQuickTestSuspend, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000190 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100191 if (successor_ == nullptr) {
192 __ jmp(GetReturnLabel());
193 } else {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100194 __ jmp(x86_codegen->GetLabelOf(successor_));
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100195 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000196 }
197
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100198 Label* GetReturnLabel() {
199 DCHECK(successor_ == nullptr);
200 return &return_label_;
201 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000202
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100203 HBasicBlock* GetSuccessor() const {
204 return successor_;
205 }
206
Alexandre Rames9931f312015-06-19 14:47:01 +0100207 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathX86"; }
208
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000209 private:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100210 HBasicBlock* const successor_;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000211 Label return_label_;
212
213 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathX86);
214};
215
Andreas Gampe85b62f22015-09-09 13:15:38 -0700216class LoadStringSlowPathX86 : public SlowPathCode {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000217 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000218 explicit LoadStringSlowPathX86(HLoadString* instruction): SlowPathCode(instruction) {}
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000219
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000220 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000221 LocationSummary* locations = instruction_->GetLocations();
222 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
223
224 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
225 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000226 SaveLiveRegisters(codegen, locations);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000227
228 InvokeRuntimeCallingConvention calling_convention;
David Srbecky9cd6d372016-02-09 15:24:47 +0000229 const uint32_t string_index = instruction_->AsLoadString()->GetStringIndex();
230 __ movl(calling_convention.GetRegisterAt(0), Immediate(string_index));
Serban Constantinescuba45db02016-07-12 22:53:02 +0100231 x86_codegen->InvokeRuntime(kQuickResolveString, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000232 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000233 x86_codegen->Move32(locations->Out(), Location::RegisterLocation(EAX));
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000234 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000235
236 __ jmp(GetExitLabel());
237 }
238
Alexandre Rames9931f312015-06-19 14:47:01 +0100239 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathX86"; }
240
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000241 private:
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000242 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathX86);
243};
244
Andreas Gampe85b62f22015-09-09 13:15:38 -0700245class LoadClassSlowPathX86 : public SlowPathCode {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000246 public:
247 LoadClassSlowPathX86(HLoadClass* cls,
248 HInstruction* at,
249 uint32_t dex_pc,
250 bool do_clinit)
David Srbecky9cd6d372016-02-09 15:24:47 +0000251 : SlowPathCode(at), cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000252 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
253 }
254
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000255 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000256 LocationSummary* locations = at_->GetLocations();
257 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
258 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000259 SaveLiveRegisters(codegen, locations);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000260
261 InvokeRuntimeCallingConvention calling_convention;
262 __ movl(calling_convention.GetRegisterAt(0), Immediate(cls_->GetTypeIndex()));
Serban Constantinescuba45db02016-07-12 22:53:02 +0100263 x86_codegen->InvokeRuntime(do_clinit_ ? kQuickInitializeStaticStorage
264 : kQuickInitializeType,
Alexandre Rames8158f282015-08-07 10:26:17 +0100265 at_, dex_pc_, this);
Roland Levillain888d0672015-11-23 18:53:50 +0000266 if (do_clinit_) {
267 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
268 } else {
269 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
270 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000271
272 // Move the class to the desired location.
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000273 Location out = locations->Out();
274 if (out.IsValid()) {
275 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
276 x86_codegen->Move32(out, Location::RegisterLocation(EAX));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000277 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000278
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000279 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000280 __ jmp(GetExitLabel());
281 }
282
Alexandre Rames9931f312015-06-19 14:47:01 +0100283 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathX86"; }
284
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000285 private:
286 // The class this slow path will load.
287 HLoadClass* const cls_;
288
289 // The instruction where this slow path is happening.
290 // (Might be the load class or an initialization check).
291 HInstruction* const at_;
292
293 // The dex PC of `at_`.
294 const uint32_t dex_pc_;
295
296 // Whether to initialize the class.
297 const bool do_clinit_;
298
299 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathX86);
300};
301
Andreas Gampe85b62f22015-09-09 13:15:38 -0700302class TypeCheckSlowPathX86 : public SlowPathCode {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000303 public:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000304 TypeCheckSlowPathX86(HInstruction* instruction, bool is_fatal)
David Srbecky9cd6d372016-02-09 15:24:47 +0000305 : SlowPathCode(instruction), is_fatal_(is_fatal) {}
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000306
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000307 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000308 LocationSummary* locations = instruction_->GetLocations();
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100309 Location object_class = instruction_->IsCheckCast() ? locations->GetTemp(0)
310 : locations->Out();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000311 DCHECK(instruction_->IsCheckCast()
312 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000313
314 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
315 __ Bind(GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000316
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000317 if (!is_fatal_) {
318 SaveLiveRegisters(codegen, locations);
319 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000320
321 // We're moving two locations to locations that could overlap, so we need a parallel
322 // move resolver.
323 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000324 x86_codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100325 locations->InAt(1),
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000326 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100327 Primitive::kPrimNot,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100328 object_class,
Nicolas Geoffray90218252015-04-15 11:56:51 +0100329 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
330 Primitive::kPrimNot);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000331
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000332 if (instruction_->IsInstanceOf()) {
Serban Constantinescuba45db02016-07-12 22:53:02 +0100333 x86_codegen->InvokeRuntime(kQuickInstanceofNonTrivial,
Alexandre Rames8158f282015-08-07 10:26:17 +0100334 instruction_,
335 instruction_->GetDexPc(),
336 this);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000337 CheckEntrypointTypes<
Andreas Gampe67409972016-07-19 22:34:53 -0700338 kQuickInstanceofNonTrivial, size_t, const mirror::Class*, const mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000339 } else {
340 DCHECK(instruction_->IsCheckCast());
Serban Constantinescuba45db02016-07-12 22:53:02 +0100341 x86_codegen->InvokeRuntime(kQuickCheckCast, instruction_, instruction_->GetDexPc(), this);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000342 CheckEntrypointTypes<kQuickCheckCast, void, const mirror::Class*, const mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000343 }
344
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000345 if (!is_fatal_) {
346 if (instruction_->IsInstanceOf()) {
347 x86_codegen->Move32(locations->Out(), Location::RegisterLocation(EAX));
348 }
349 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray75374372015-09-17 17:12:19 +0000350
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000351 __ jmp(GetExitLabel());
352 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000353 }
354
Alexandre Rames9931f312015-06-19 14:47:01 +0100355 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathX86"; }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000356 bool IsFatal() const OVERRIDE { return is_fatal_; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100357
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000358 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000359 const bool is_fatal_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000360
361 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathX86);
362};
363
Andreas Gampe85b62f22015-09-09 13:15:38 -0700364class DeoptimizationSlowPathX86 : public SlowPathCode {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700365 public:
Aart Bik42249c32016-01-07 15:33:50 -0800366 explicit DeoptimizationSlowPathX86(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000367 : SlowPathCode(instruction) {}
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700368
369 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames8158f282015-08-07 10:26:17 +0100370 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700371 __ Bind(GetEntryLabel());
Serban Constantinescuba45db02016-07-12 22:53:02 +0100372 x86_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000373 CheckEntrypointTypes<kQuickDeoptimize, void, void>();
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700374 }
375
Alexandre Rames9931f312015-06-19 14:47:01 +0100376 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathX86"; }
377
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700378 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700379 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathX86);
380};
381
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100382class ArraySetSlowPathX86 : public SlowPathCode {
383 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000384 explicit ArraySetSlowPathX86(HInstruction* instruction) : SlowPathCode(instruction) {}
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100385
386 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
387 LocationSummary* locations = instruction_->GetLocations();
388 __ Bind(GetEntryLabel());
389 SaveLiveRegisters(codegen, locations);
390
391 InvokeRuntimeCallingConvention calling_convention;
392 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
393 parallel_move.AddMove(
394 locations->InAt(0),
395 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
396 Primitive::kPrimNot,
397 nullptr);
398 parallel_move.AddMove(
399 locations->InAt(1),
400 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
401 Primitive::kPrimInt,
402 nullptr);
403 parallel_move.AddMove(
404 locations->InAt(2),
405 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
406 Primitive::kPrimNot,
407 nullptr);
408 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
409
410 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Serban Constantinescuba45db02016-07-12 22:53:02 +0100411 x86_codegen->InvokeRuntime(kQuickAputObject, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000412 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100413 RestoreLiveRegisters(codegen, locations);
414 __ jmp(GetExitLabel());
415 }
416
417 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathX86"; }
418
419 private:
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100420 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathX86);
421};
422
Roland Levillain7c1559a2015-12-15 10:55:36 +0000423// Slow path marking an object during a read barrier.
424class ReadBarrierMarkSlowPathX86 : public SlowPathCode {
425 public:
Vladimir Marko953437b2016-08-24 08:30:46 +0000426 ReadBarrierMarkSlowPathX86(HInstruction* instruction, Location obj, bool unpoison)
427 : SlowPathCode(instruction), obj_(obj), unpoison_(unpoison) {
Roland Levillain7c1559a2015-12-15 10:55:36 +0000428 DCHECK(kEmitCompilerReadBarrier);
429 }
430
431 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathX86"; }
432
433 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
434 LocationSummary* locations = instruction_->GetLocations();
Roland Levillain02b75802016-07-13 11:54:35 +0100435 Register reg = obj_.AsRegister<Register>();
Roland Levillain7c1559a2015-12-15 10:55:36 +0000436 DCHECK(locations->CanCall());
Roland Levillain02b75802016-07-13 11:54:35 +0100437 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg));
Roland Levillain7c1559a2015-12-15 10:55:36 +0000438 DCHECK(instruction_->IsInstanceFieldGet() ||
439 instruction_->IsStaticFieldGet() ||
440 instruction_->IsArrayGet() ||
Roland Levillain16d9f942016-08-25 17:27:56 +0100441 instruction_->IsArraySet() ||
Roland Levillain7c1559a2015-12-15 10:55:36 +0000442 instruction_->IsLoadClass() ||
443 instruction_->IsLoadString() ||
444 instruction_->IsInstanceOf() ||
Roland Levillain3d312422016-06-23 13:53:42 +0100445 instruction_->IsCheckCast() ||
Roland Levillain0b671c02016-08-19 12:02:34 +0100446 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()) ||
447 (instruction_->IsInvokeStaticOrDirect() && instruction_->GetLocations()->Intrinsified()))
Roland Levillain7c1559a2015-12-15 10:55:36 +0000448 << "Unexpected instruction in read barrier marking slow path: "
449 << instruction_->DebugName();
450
451 __ Bind(GetEntryLabel());
Vladimir Marko953437b2016-08-24 08:30:46 +0000452 if (unpoison_) {
453 // Object* ref = ref_addr->AsMirrorPtr()
454 __ MaybeUnpoisonHeapReference(reg);
455 }
Roland Levillain4359e612016-07-20 11:32:19 +0100456 // No need to save live registers; it's taken care of by the
457 // entrypoint. Also, there is no need to update the stack mask,
458 // as this runtime call will not trigger a garbage collection.
Roland Levillain7c1559a2015-12-15 10:55:36 +0000459 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Roland Levillain02b75802016-07-13 11:54:35 +0100460 DCHECK_NE(reg, ESP);
461 DCHECK(0 <= reg && reg < kNumberOfCpuRegisters) << reg;
462 // "Compact" slow path, saving two moves.
463 //
464 // Instead of using the standard runtime calling convention (input
465 // and output in EAX):
466 //
467 // EAX <- obj
468 // EAX <- ReadBarrierMark(EAX)
469 // obj <- EAX
470 //
471 // we just use rX (the register holding `obj`) as input and output
472 // of a dedicated entrypoint:
473 //
474 // rX <- ReadBarrierMarkRegX(rX)
475 //
476 int32_t entry_point_offset =
Andreas Gampe542451c2016-07-26 09:02:02 -0700477 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kX86PointerSize>(reg);
Roland Levillaindec8f632016-07-22 17:10:06 +0100478 // This runtime call does not require a stack map.
479 x86_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
Roland Levillain7c1559a2015-12-15 10:55:36 +0000480 __ jmp(GetExitLabel());
481 }
482
483 private:
Roland Levillain7c1559a2015-12-15 10:55:36 +0000484 const Location obj_;
Vladimir Marko953437b2016-08-24 08:30:46 +0000485 const bool unpoison_;
Roland Levillain7c1559a2015-12-15 10:55:36 +0000486
487 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathX86);
488};
489
Roland Levillain0d5a2812015-11-13 10:07:31 +0000490// Slow path generating a read barrier for a heap reference.
491class ReadBarrierForHeapReferenceSlowPathX86 : public SlowPathCode {
492 public:
493 ReadBarrierForHeapReferenceSlowPathX86(HInstruction* instruction,
494 Location out,
495 Location ref,
496 Location obj,
497 uint32_t offset,
498 Location index)
David Srbecky9cd6d372016-02-09 15:24:47 +0000499 : SlowPathCode(instruction),
Roland Levillain0d5a2812015-11-13 10:07:31 +0000500 out_(out),
501 ref_(ref),
502 obj_(obj),
503 offset_(offset),
504 index_(index) {
505 DCHECK(kEmitCompilerReadBarrier);
506 // If `obj` is equal to `out` or `ref`, it means the initial object
507 // has been overwritten by (or after) the heap object reference load
508 // to be instrumented, e.g.:
509 //
510 // __ movl(out, Address(out, offset));
Roland Levillain7c1559a2015-12-15 10:55:36 +0000511 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000512 //
513 // In that case, we have lost the information about the original
514 // object, and the emitted read barrier cannot work properly.
515 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
516 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
517 }
518
519 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
520 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
521 LocationSummary* locations = instruction_->GetLocations();
522 Register reg_out = out_.AsRegister<Register>();
523 DCHECK(locations->CanCall());
524 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
Roland Levillain3d312422016-06-23 13:53:42 +0100525 DCHECK(instruction_->IsInstanceFieldGet() ||
526 instruction_->IsStaticFieldGet() ||
527 instruction_->IsArrayGet() ||
528 instruction_->IsInstanceOf() ||
529 instruction_->IsCheckCast() ||
Roland Levillaindec8f632016-07-22 17:10:06 +0100530 (instruction_->IsInvokeVirtual()) && instruction_->GetLocations()->Intrinsified())
Roland Levillain7c1559a2015-12-15 10:55:36 +0000531 << "Unexpected instruction in read barrier for heap reference slow path: "
532 << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000533
534 __ Bind(GetEntryLabel());
535 SaveLiveRegisters(codegen, locations);
536
537 // We may have to change the index's value, but as `index_` is a
538 // constant member (like other "inputs" of this slow path),
539 // introduce a copy of it, `index`.
540 Location index = index_;
541 if (index_.IsValid()) {
Roland Levillain3d312422016-06-23 13:53:42 +0100542 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
Roland Levillain0d5a2812015-11-13 10:07:31 +0000543 if (instruction_->IsArrayGet()) {
544 // Compute the actual memory offset and store it in `index`.
545 Register index_reg = index_.AsRegister<Register>();
546 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg));
547 if (codegen->IsCoreCalleeSaveRegister(index_reg)) {
548 // We are about to change the value of `index_reg` (see the
549 // calls to art::x86::X86Assembler::shll and
550 // art::x86::X86Assembler::AddImmediate below), but it has
551 // not been saved by the previous call to
552 // art::SlowPathCode::SaveLiveRegisters, as it is a
553 // callee-save register --
554 // art::SlowPathCode::SaveLiveRegisters does not consider
555 // callee-save registers, as it has been designed with the
556 // assumption that callee-save registers are supposed to be
557 // handled by the called function. So, as a callee-save
558 // register, `index_reg` _would_ eventually be saved onto
559 // the stack, but it would be too late: we would have
560 // changed its value earlier. Therefore, we manually save
561 // it here into another freely available register,
562 // `free_reg`, chosen of course among the caller-save
563 // registers (as a callee-save `free_reg` register would
564 // exhibit the same problem).
565 //
566 // Note we could have requested a temporary register from
567 // the register allocator instead; but we prefer not to, as
568 // this is a slow path, and we know we can find a
569 // caller-save register that is available.
570 Register free_reg = FindAvailableCallerSaveRegister(codegen);
571 __ movl(free_reg, index_reg);
572 index_reg = free_reg;
573 index = Location::RegisterLocation(index_reg);
574 } else {
575 // The initial register stored in `index_` has already been
576 // saved in the call to art::SlowPathCode::SaveLiveRegisters
577 // (as it is not a callee-save register), so we can freely
578 // use it.
579 }
580 // Shifting the index value contained in `index_reg` by the scale
581 // factor (2) cannot overflow in practice, as the runtime is
582 // unable to allocate object arrays with a size larger than
583 // 2^26 - 1 (that is, 2^28 - 4 bytes).
584 __ shll(index_reg, Immediate(TIMES_4));
585 static_assert(
586 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
587 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
588 __ AddImmediate(index_reg, Immediate(offset_));
589 } else {
Roland Levillain3d312422016-06-23 13:53:42 +0100590 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
591 // intrinsics, `index_` is not shifted by a scale factor of 2
592 // (as in the case of ArrayGet), as it is actually an offset
593 // to an object field within an object.
594 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000595 DCHECK(instruction_->GetLocations()->Intrinsified());
596 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
597 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
598 << instruction_->AsInvoke()->GetIntrinsic();
599 DCHECK_EQ(offset_, 0U);
600 DCHECK(index_.IsRegisterPair());
601 // UnsafeGet's offset location is a register pair, the low
602 // part contains the correct offset.
603 index = index_.ToLow();
604 }
605 }
606
607 // We're moving two or three locations to locations that could
608 // overlap, so we need a parallel move resolver.
609 InvokeRuntimeCallingConvention calling_convention;
610 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
611 parallel_move.AddMove(ref_,
612 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
613 Primitive::kPrimNot,
614 nullptr);
615 parallel_move.AddMove(obj_,
616 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
617 Primitive::kPrimNot,
618 nullptr);
619 if (index.IsValid()) {
620 parallel_move.AddMove(index,
621 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
622 Primitive::kPrimInt,
623 nullptr);
624 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
625 } else {
626 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
627 __ movl(calling_convention.GetRegisterAt(2), Immediate(offset_));
628 }
Serban Constantinescuba45db02016-07-12 22:53:02 +0100629 x86_codegen->InvokeRuntime(kQuickReadBarrierSlow, instruction_, instruction_->GetDexPc(), this);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000630 CheckEntrypointTypes<
631 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
632 x86_codegen->Move32(out_, Location::RegisterLocation(EAX));
633
634 RestoreLiveRegisters(codegen, locations);
635 __ jmp(GetExitLabel());
636 }
637
638 const char* GetDescription() const OVERRIDE { return "ReadBarrierForHeapReferenceSlowPathX86"; }
639
640 private:
641 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
642 size_t ref = static_cast<int>(ref_.AsRegister<Register>());
643 size_t obj = static_cast<int>(obj_.AsRegister<Register>());
644 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
645 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
646 return static_cast<Register>(i);
647 }
648 }
649 // We shall never fail to find a free caller-save register, as
650 // there are more than two core caller-save registers on x86
651 // (meaning it is possible to find one which is different from
652 // `ref` and `obj`).
653 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
654 LOG(FATAL) << "Could not find a free caller-save register";
655 UNREACHABLE();
656 }
657
Roland Levillain0d5a2812015-11-13 10:07:31 +0000658 const Location out_;
659 const Location ref_;
660 const Location obj_;
661 const uint32_t offset_;
662 // An additional location containing an index to an array.
663 // Only used for HArrayGet and the UnsafeGetObject &
664 // UnsafeGetObjectVolatile intrinsics.
665 const Location index_;
666
667 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathX86);
668};
669
670// Slow path generating a read barrier for a GC root.
671class ReadBarrierForRootSlowPathX86 : public SlowPathCode {
672 public:
673 ReadBarrierForRootSlowPathX86(HInstruction* instruction, Location out, Location root)
David Srbecky9cd6d372016-02-09 15:24:47 +0000674 : SlowPathCode(instruction), out_(out), root_(root) {
Roland Levillain7c1559a2015-12-15 10:55:36 +0000675 DCHECK(kEmitCompilerReadBarrier);
676 }
Roland Levillain0d5a2812015-11-13 10:07:31 +0000677
678 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
679 LocationSummary* locations = instruction_->GetLocations();
680 Register reg_out = out_.AsRegister<Register>();
681 DCHECK(locations->CanCall());
682 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
Roland Levillain7c1559a2015-12-15 10:55:36 +0000683 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
684 << "Unexpected instruction in read barrier for GC root slow path: "
685 << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000686
687 __ Bind(GetEntryLabel());
688 SaveLiveRegisters(codegen, locations);
689
690 InvokeRuntimeCallingConvention calling_convention;
691 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
692 x86_codegen->Move32(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), root_);
Serban Constantinescuba45db02016-07-12 22:53:02 +0100693 x86_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000694 instruction_,
695 instruction_->GetDexPc(),
696 this);
697 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
698 x86_codegen->Move32(out_, Location::RegisterLocation(EAX));
699
700 RestoreLiveRegisters(codegen, locations);
701 __ jmp(GetExitLabel());
702 }
703
704 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathX86"; }
705
706 private:
Roland Levillain0d5a2812015-11-13 10:07:31 +0000707 const Location out_;
708 const Location root_;
709
710 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathX86);
711};
712
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100713#undef __
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100714// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
715#define __ down_cast<X86Assembler*>(GetAssembler())-> // NOLINT
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100716
Aart Bike9f37602015-10-09 11:15:55 -0700717inline Condition X86Condition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -0700718 switch (cond) {
719 case kCondEQ: return kEqual;
720 case kCondNE: return kNotEqual;
721 case kCondLT: return kLess;
722 case kCondLE: return kLessEqual;
723 case kCondGT: return kGreater;
724 case kCondGE: return kGreaterEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700725 case kCondB: return kBelow;
726 case kCondBE: return kBelowEqual;
727 case kCondA: return kAbove;
728 case kCondAE: return kAboveEqual;
Dave Allison20dfc792014-06-16 20:44:29 -0700729 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100730 LOG(FATAL) << "Unreachable";
731 UNREACHABLE();
732}
733
Aart Bike9f37602015-10-09 11:15:55 -0700734// Maps signed condition to unsigned condition and FP condition to x86 name.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100735inline Condition X86UnsignedOrFPCondition(IfCondition cond) {
736 switch (cond) {
737 case kCondEQ: return kEqual;
738 case kCondNE: return kNotEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700739 // Signed to unsigned, and FP to x86 name.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100740 case kCondLT: return kBelow;
741 case kCondLE: return kBelowEqual;
742 case kCondGT: return kAbove;
743 case kCondGE: return kAboveEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700744 // Unsigned remain unchanged.
745 case kCondB: return kBelow;
746 case kCondBE: return kBelowEqual;
747 case kCondA: return kAbove;
748 case kCondAE: return kAboveEqual;
Roland Levillain4fa13f62015-07-06 18:11:54 +0100749 }
750 LOG(FATAL) << "Unreachable";
751 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -0700752}
753
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100754void CodeGeneratorX86::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100755 stream << Register(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100756}
757
758void CodeGeneratorX86::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100759 stream << XmmRegister(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100760}
761
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100762size_t CodeGeneratorX86::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
763 __ movl(Address(ESP, stack_index), static_cast<Register>(reg_id));
764 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100765}
766
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100767size_t CodeGeneratorX86::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
768 __ movl(static_cast<Register>(reg_id), Address(ESP, stack_index));
769 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100770}
771
Mark Mendell7c8d0092015-01-26 11:21:33 -0500772size_t CodeGeneratorX86::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
773 __ movsd(Address(ESP, stack_index), XmmRegister(reg_id));
774 return GetFloatingPointSpillSlotSize();
775}
776
777size_t CodeGeneratorX86::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
778 __ movsd(XmmRegister(reg_id), Address(ESP, stack_index));
779 return GetFloatingPointSpillSlotSize();
780}
781
Calin Juravle175dc732015-08-25 15:42:32 +0100782void CodeGeneratorX86::InvokeRuntime(QuickEntrypointEnum entrypoint,
783 HInstruction* instruction,
784 uint32_t dex_pc,
785 SlowPathCode* slow_path) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +0100786 ValidateInvokeRuntime(instruction, slow_path);
Serban Constantinescuba45db02016-07-12 22:53:02 +0100787 GenerateInvokeRuntime(GetThreadOffset<kX86PointerSize>(entrypoint).Int32Value());
788 if (EntrypointRequiresStackMap(entrypoint)) {
789 RecordPcInfo(instruction, dex_pc, slow_path);
790 }
Alexandre Rames8158f282015-08-07 10:26:17 +0100791}
792
Roland Levillaindec8f632016-07-22 17:10:06 +0100793void CodeGeneratorX86::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
794 HInstruction* instruction,
795 SlowPathCode* slow_path) {
796 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
Serban Constantinescuba45db02016-07-12 22:53:02 +0100797 GenerateInvokeRuntime(entry_point_offset);
798}
799
800void CodeGeneratorX86::GenerateInvokeRuntime(int32_t entry_point_offset) {
Roland Levillaindec8f632016-07-22 17:10:06 +0100801 __ fs()->call(Address::Absolute(entry_point_offset));
802}
803
Mark Mendellfb8d2792015-03-31 22:16:59 -0400804CodeGeneratorX86::CodeGeneratorX86(HGraph* graph,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000805 const X86InstructionSetFeatures& isa_features,
806 const CompilerOptions& compiler_options,
807 OptimizingCompilerStats* stats)
Mark Mendell5f874182015-03-04 15:42:45 -0500808 : CodeGenerator(graph,
809 kNumberOfCpuRegisters,
810 kNumberOfXmmRegisters,
811 kNumberOfRegisterPairs,
812 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
813 arraysize(kCoreCalleeSaves))
814 | (1 << kFakeReturnRegister),
Serban Constantinescuecc43662015-08-13 13:33:12 +0100815 0,
816 compiler_options,
817 stats),
Vladimir Marko225b6462015-09-28 12:17:40 +0100818 block_labels_(nullptr),
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100819 location_builder_(graph, this),
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100820 instruction_visitor_(graph, this),
Mark Mendellfb8d2792015-03-31 22:16:59 -0400821 move_resolver_(graph->GetArena(), this),
Vladimir Marko93205e32016-04-13 11:59:46 +0100822 assembler_(graph->GetArena()),
Vladimir Marko58155012015-08-19 12:49:41 +0000823 isa_features_(isa_features),
Vladimir Marko5233f932015-09-29 19:01:15 +0100824 method_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Mark Mendell805b3b52015-09-18 14:10:29 -0400825 relative_call_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000826 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000827 simple_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
828 string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100829 type_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko93205e32016-04-13 11:59:46 +0100830 constant_area_start_(-1),
831 fixups_to_jump_tables_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
832 method_address_offset_(-1) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000833 // Use a fake return address register to mimic Quick.
834 AddAllocatedRegister(Location::RegisterLocation(kFakeReturnRegister));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100835}
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100836
David Brazdil58282f42016-01-14 12:45:10 +0000837void CodeGeneratorX86::SetupBlockedRegisters() const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100838 // Don't allocate the dalvik style register pair passing.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100839 blocked_register_pairs_[ECX_EDX] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100840
841 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100842 blocked_core_registers_[ESP] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100843
Calin Juravle34bacdf2014-10-07 20:23:36 +0100844 UpdateBlockedPairRegisters();
845}
846
847void CodeGeneratorX86::UpdateBlockedPairRegisters() const {
848 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
849 X86ManagedRegister current =
850 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
851 if (blocked_core_registers_[current.AsRegisterPairLow()]
852 || blocked_core_registers_[current.AsRegisterPairHigh()]) {
853 blocked_register_pairs_[i] = true;
854 }
855 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100856}
857
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100858InstructionCodeGeneratorX86::InstructionCodeGeneratorX86(HGraph* graph, CodeGeneratorX86* codegen)
Aart Bik42249c32016-01-07 15:33:50 -0800859 : InstructionCodeGenerator(graph, codegen),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100860 assembler_(codegen->GetAssembler()),
861 codegen_(codegen) {}
862
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100863static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +0100864 return dwarf::Reg::X86Core(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100865}
866
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000867void CodeGeneratorX86::GenerateFrameEntry() {
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100868 __ cfi().SetCurrentCFAOffset(kX86WordSize); // return address
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000869 __ Bind(&frame_entry_label_);
Roland Levillain199f3362014-11-27 17:15:16 +0000870 bool skip_overflow_check =
871 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000872 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Calin Juravle93edf732015-01-20 20:14:07 +0000873
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000874 if (!skip_overflow_check) {
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100875 __ testl(EAX, Address(ESP, -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86))));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100876 RecordPcInfo(nullptr, 0);
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100877 }
878
Mark Mendell5f874182015-03-04 15:42:45 -0500879 if (HasEmptyFrame()) {
880 return;
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000881 }
Mark Mendell5f874182015-03-04 15:42:45 -0500882
883 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
884 Register reg = kCoreCalleeSaves[i];
885 if (allocated_registers_.ContainsCoreRegister(reg)) {
886 __ pushl(reg);
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100887 __ cfi().AdjustCFAOffset(kX86WordSize);
888 __ cfi().RelOffset(DWARFReg(reg), 0);
Mark Mendell5f874182015-03-04 15:42:45 -0500889 }
890 }
891
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100892 int adjust = GetFrameSize() - FrameEntrySpillSize();
893 __ subl(ESP, Immediate(adjust));
894 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100895 __ movl(Address(ESP, kCurrentMethodStackOffset), kMethodRegisterArgument);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000896}
897
898void CodeGeneratorX86::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +0100899 __ cfi().RememberState();
900 if (!HasEmptyFrame()) {
901 int adjust = GetFrameSize() - FrameEntrySpillSize();
902 __ addl(ESP, Immediate(adjust));
903 __ cfi().AdjustCFAOffset(-adjust);
Mark Mendell5f874182015-03-04 15:42:45 -0500904
David Srbeckyc34dc932015-04-12 09:27:43 +0100905 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
906 Register reg = kCoreCalleeSaves[i];
907 if (allocated_registers_.ContainsCoreRegister(reg)) {
908 __ popl(reg);
909 __ cfi().AdjustCFAOffset(-static_cast<int>(kX86WordSize));
910 __ cfi().Restore(DWARFReg(reg));
911 }
Mark Mendell5f874182015-03-04 15:42:45 -0500912 }
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000913 }
David Srbeckyc34dc932015-04-12 09:27:43 +0100914 __ ret();
915 __ cfi().RestoreState();
916 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000917}
918
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100919void CodeGeneratorX86::Bind(HBasicBlock* block) {
920 __ Bind(GetLabelOf(block));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000921}
922
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100923Location InvokeDexCallingConventionVisitorX86::GetReturnLocation(Primitive::Type type) const {
924 switch (type) {
925 case Primitive::kPrimBoolean:
926 case Primitive::kPrimByte:
927 case Primitive::kPrimChar:
928 case Primitive::kPrimShort:
929 case Primitive::kPrimInt:
930 case Primitive::kPrimNot:
931 return Location::RegisterLocation(EAX);
932
933 case Primitive::kPrimLong:
934 return Location::RegisterPairLocation(EAX, EDX);
935
936 case Primitive::kPrimVoid:
937 return Location::NoLocation();
938
939 case Primitive::kPrimDouble:
940 case Primitive::kPrimFloat:
941 return Location::FpuRegisterLocation(XMM0);
942 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +0100943
944 UNREACHABLE();
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100945}
946
947Location InvokeDexCallingConventionVisitorX86::GetMethodLocation() const {
948 return Location::RegisterLocation(kMethodRegisterArgument);
949}
950
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100951Location InvokeDexCallingConventionVisitorX86::GetNextLocation(Primitive::Type type) {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100952 switch (type) {
953 case Primitive::kPrimBoolean:
954 case Primitive::kPrimByte:
955 case Primitive::kPrimChar:
956 case Primitive::kPrimShort:
957 case Primitive::kPrimInt:
958 case Primitive::kPrimNot: {
959 uint32_t index = gp_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000960 stack_index_++;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100961 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100962 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100963 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000964 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100965 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100966 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100967
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000968 case Primitive::kPrimLong: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100969 uint32_t index = gp_index_;
970 gp_index_ += 2;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000971 stack_index_ += 2;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100972 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100973 X86ManagedRegister pair = X86ManagedRegister::FromRegisterPair(
974 calling_convention.GetRegisterPairAt(index));
975 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100976 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000977 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
978 }
979 }
980
981 case Primitive::kPrimFloat: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100982 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000983 stack_index_++;
984 if (index < calling_convention.GetNumberOfFpuRegisters()) {
985 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
986 } else {
987 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
988 }
989 }
990
991 case Primitive::kPrimDouble: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100992 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000993 stack_index_ += 2;
994 if (index < calling_convention.GetNumberOfFpuRegisters()) {
995 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
996 } else {
997 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100998 }
999 }
1000
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001001 case Primitive::kPrimVoid:
1002 LOG(FATAL) << "Unexpected parameter type " << type;
1003 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001004 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00001005 return Location::NoLocation();
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001006}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001007
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001008void CodeGeneratorX86::Move32(Location destination, Location source) {
1009 if (source.Equals(destination)) {
1010 return;
1011 }
1012 if (destination.IsRegister()) {
1013 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001014 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001015 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001016 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001017 } else {
1018 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001019 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001020 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001021 } else if (destination.IsFpuRegister()) {
1022 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001023 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001024 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001025 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001026 } else {
1027 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001028 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001029 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001030 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001031 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001032 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001033 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001034 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001035 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Mark Mendell7c8d0092015-01-26 11:21:33 -05001036 } else if (source.IsConstant()) {
1037 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001038 int32_t value = GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05001039 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001040 } else {
1041 DCHECK(source.IsStackSlot());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001042 __ pushl(Address(ESP, source.GetStackIndex()));
1043 __ popl(Address(ESP, destination.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001044 }
1045 }
1046}
1047
1048void CodeGeneratorX86::Move64(Location destination, Location source) {
1049 if (source.Equals(destination)) {
1050 return;
1051 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001052 if (destination.IsRegisterPair()) {
1053 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001054 EmitParallelMoves(
1055 Location::RegisterLocation(source.AsRegisterPairHigh<Register>()),
1056 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001057 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001058 Location::RegisterLocation(source.AsRegisterPairLow<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001059 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()),
1060 Primitive::kPrimInt);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001061 } else if (source.IsFpuRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001062 XmmRegister src_reg = source.AsFpuRegister<XmmRegister>();
1063 __ movd(destination.AsRegisterPairLow<Register>(), src_reg);
1064 __ psrlq(src_reg, Immediate(32));
1065 __ movd(destination.AsRegisterPairHigh<Register>(), src_reg);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001066 } else {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001067 // No conflict possible, so just do the moves.
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001068 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001069 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
1070 __ movl(destination.AsRegisterPairHigh<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001071 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
1072 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001073 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05001074 if (source.IsFpuRegister()) {
1075 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
1076 } else if (source.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001077 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01001078 } else if (source.IsRegisterPair()) {
1079 size_t elem_size = Primitive::ComponentSize(Primitive::kPrimInt);
1080 // Create stack space for 2 elements.
1081 __ subl(ESP, Immediate(2 * elem_size));
1082 __ movl(Address(ESP, 0), source.AsRegisterPairLow<Register>());
1083 __ movl(Address(ESP, elem_size), source.AsRegisterPairHigh<Register>());
1084 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
1085 // And remove the temporary stack space we allocated.
1086 __ addl(ESP, Immediate(2 * elem_size));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001087 } else {
1088 LOG(FATAL) << "Unimplemented";
1089 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001090 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001091 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001092 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001093 // No conflict possible, so just do the moves.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001094 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegisterPairLow<Register>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001095 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001096 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001097 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001098 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001099 } else if (source.IsConstant()) {
1100 HConstant* constant = source.GetConstant();
1101 int64_t value;
1102 if (constant->IsLongConstant()) {
1103 value = constant->AsLongConstant()->GetValue();
1104 } else {
1105 DCHECK(constant->IsDoubleConstant());
Roland Levillainda4d79b2015-03-24 14:36:11 +00001106 value = bit_cast<int64_t, double>(constant->AsDoubleConstant()->GetValue());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001107 }
1108 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(Low32Bits(value)));
1109 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), Immediate(High32Bits(value)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001110 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001111 DCHECK(source.IsDoubleStackSlot()) << source;
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001112 EmitParallelMoves(
1113 Location::StackSlot(source.GetStackIndex()),
1114 Location::StackSlot(destination.GetStackIndex()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001115 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001116 Location::StackSlot(source.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001117 Location::StackSlot(destination.GetHighStackIndex(kX86WordSize)),
1118 Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001119 }
1120 }
1121}
1122
Calin Juravle175dc732015-08-25 15:42:32 +01001123void CodeGeneratorX86::MoveConstant(Location location, int32_t value) {
1124 DCHECK(location.IsRegister());
1125 __ movl(location.AsRegister<Register>(), Immediate(value));
1126}
1127
Calin Juravlee460d1d2015-09-29 04:52:17 +01001128void CodeGeneratorX86::MoveLocation(Location dst, Location src, Primitive::Type dst_type) {
David Brazdil74eb1b22015-12-14 11:44:01 +00001129 HParallelMove move(GetGraph()->GetArena());
1130 if (dst_type == Primitive::kPrimLong && !src.IsConstant() && !src.IsFpuRegister()) {
1131 move.AddMove(src.ToLow(), dst.ToLow(), Primitive::kPrimInt, nullptr);
1132 move.AddMove(src.ToHigh(), dst.ToHigh(), Primitive::kPrimInt, nullptr);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001133 } else {
David Brazdil74eb1b22015-12-14 11:44:01 +00001134 move.AddMove(src, dst, dst_type, nullptr);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001135 }
David Brazdil74eb1b22015-12-14 11:44:01 +00001136 GetMoveResolver()->EmitNativeCode(&move);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001137}
1138
1139void CodeGeneratorX86::AddLocationAsTemp(Location location, LocationSummary* locations) {
1140 if (location.IsRegister()) {
1141 locations->AddTemp(location);
1142 } else if (location.IsRegisterPair()) {
1143 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
1144 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
1145 } else {
1146 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1147 }
1148}
1149
David Brazdilfc6a86a2015-06-26 10:33:45 +00001150void InstructionCodeGeneratorX86::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001151 DCHECK(!successor->IsExitBlock());
1152
1153 HBasicBlock* block = got->GetBlock();
1154 HInstruction* previous = got->GetPrevious();
1155
1156 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +00001157 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001158 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1159 return;
1160 }
1161
1162 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1163 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1164 }
1165 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001166 __ jmp(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001167 }
1168}
1169
David Brazdilfc6a86a2015-06-26 10:33:45 +00001170void LocationsBuilderX86::VisitGoto(HGoto* got) {
1171 got->SetLocations(nullptr);
1172}
1173
1174void InstructionCodeGeneratorX86::VisitGoto(HGoto* got) {
1175 HandleGoto(got, got->GetSuccessor());
1176}
1177
1178void LocationsBuilderX86::VisitTryBoundary(HTryBoundary* try_boundary) {
1179 try_boundary->SetLocations(nullptr);
1180}
1181
1182void InstructionCodeGeneratorX86::VisitTryBoundary(HTryBoundary* try_boundary) {
1183 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
1184 if (!successor->IsExitBlock()) {
1185 HandleGoto(try_boundary, successor);
1186 }
1187}
1188
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001189void LocationsBuilderX86::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001190 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001191}
1192
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001193void InstructionCodeGeneratorX86::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001194}
1195
Mark Mendell152408f2015-12-31 12:28:50 -05001196template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001197void InstructionCodeGeneratorX86::GenerateFPJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001198 LabelType* true_label,
1199 LabelType* false_label) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001200 if (cond->IsFPConditionTrueIfNaN()) {
1201 __ j(kUnordered, true_label);
1202 } else if (cond->IsFPConditionFalseIfNaN()) {
1203 __ j(kUnordered, false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001204 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001205 __ j(X86UnsignedOrFPCondition(cond->GetCondition()), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001206}
1207
Mark Mendell152408f2015-12-31 12:28:50 -05001208template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001209void InstructionCodeGeneratorX86::GenerateLongComparesAndJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001210 LabelType* true_label,
1211 LabelType* false_label) {
Mark Mendellc4701932015-04-10 13:18:51 -04001212 LocationSummary* locations = cond->GetLocations();
1213 Location left = locations->InAt(0);
1214 Location right = locations->InAt(1);
1215 IfCondition if_cond = cond->GetCondition();
1216
Mark Mendellc4701932015-04-10 13:18:51 -04001217 Register left_high = left.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001218 Register left_low = left.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001219 IfCondition true_high_cond = if_cond;
1220 IfCondition false_high_cond = cond->GetOppositeCondition();
Aart Bike9f37602015-10-09 11:15:55 -07001221 Condition final_condition = X86UnsignedOrFPCondition(if_cond); // unsigned on lower part
Mark Mendellc4701932015-04-10 13:18:51 -04001222
1223 // Set the conditions for the test, remembering that == needs to be
1224 // decided using the low words.
1225 switch (if_cond) {
1226 case kCondEQ:
Mark Mendellc4701932015-04-10 13:18:51 -04001227 case kCondNE:
Roland Levillain4fa13f62015-07-06 18:11:54 +01001228 // Nothing to do.
Mark Mendellc4701932015-04-10 13:18:51 -04001229 break;
1230 case kCondLT:
1231 false_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -04001232 break;
1233 case kCondLE:
1234 true_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -04001235 break;
1236 case kCondGT:
1237 false_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -04001238 break;
1239 case kCondGE:
1240 true_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -04001241 break;
Aart Bike9f37602015-10-09 11:15:55 -07001242 case kCondB:
1243 false_high_cond = kCondA;
1244 break;
1245 case kCondBE:
1246 true_high_cond = kCondB;
1247 break;
1248 case kCondA:
1249 false_high_cond = kCondB;
1250 break;
1251 case kCondAE:
1252 true_high_cond = kCondA;
1253 break;
Mark Mendellc4701932015-04-10 13:18:51 -04001254 }
1255
1256 if (right.IsConstant()) {
1257 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
Mark Mendellc4701932015-04-10 13:18:51 -04001258 int32_t val_high = High32Bits(value);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001259 int32_t val_low = Low32Bits(value);
Mark Mendellc4701932015-04-10 13:18:51 -04001260
Aart Bika19616e2016-02-01 18:57:58 -08001261 codegen_->Compare32BitValue(left_high, val_high);
Mark Mendellc4701932015-04-10 13:18:51 -04001262 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001263 __ j(X86Condition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001264 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001265 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001266 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001267 __ j(X86Condition(true_high_cond), true_label);
1268 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001269 }
1270 // Must be equal high, so compare the lows.
Aart Bika19616e2016-02-01 18:57:58 -08001271 codegen_->Compare32BitValue(left_low, val_low);
Mark Mendell8659e842016-02-16 10:41:46 -05001272 } else if (right.IsRegisterPair()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001273 Register right_high = right.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001274 Register right_low = right.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001275
1276 __ cmpl(left_high, right_high);
1277 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001278 __ j(X86Condition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001279 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001280 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001281 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001282 __ j(X86Condition(true_high_cond), true_label);
1283 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001284 }
1285 // Must be equal high, so compare the lows.
1286 __ cmpl(left_low, right_low);
Mark Mendell8659e842016-02-16 10:41:46 -05001287 } else {
1288 DCHECK(right.IsDoubleStackSlot());
1289 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
1290 if (if_cond == kCondNE) {
1291 __ j(X86Condition(true_high_cond), true_label);
1292 } else if (if_cond == kCondEQ) {
1293 __ j(X86Condition(false_high_cond), false_label);
1294 } else {
1295 __ j(X86Condition(true_high_cond), true_label);
1296 __ j(X86Condition(false_high_cond), false_label);
1297 }
1298 // Must be equal high, so compare the lows.
1299 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Mark Mendellc4701932015-04-10 13:18:51 -04001300 }
1301 // The last comparison might be unsigned.
1302 __ j(final_condition, true_label);
1303}
1304
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001305void InstructionCodeGeneratorX86::GenerateFPCompare(Location lhs,
1306 Location rhs,
1307 HInstruction* insn,
1308 bool is_double) {
1309 HX86LoadFromConstantTable* const_area = insn->InputAt(1)->AsX86LoadFromConstantTable();
1310 if (is_double) {
1311 if (rhs.IsFpuRegister()) {
1312 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1313 } else if (const_area != nullptr) {
1314 DCHECK(const_area->IsEmittedAtUseSite());
1315 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(),
1316 codegen_->LiteralDoubleAddress(
1317 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
1318 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
1319 } else {
1320 DCHECK(rhs.IsDoubleStackSlot());
1321 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(), Address(ESP, rhs.GetStackIndex()));
1322 }
1323 } else {
1324 if (rhs.IsFpuRegister()) {
1325 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1326 } else if (const_area != nullptr) {
1327 DCHECK(const_area->IsEmittedAtUseSite());
1328 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(),
1329 codegen_->LiteralFloatAddress(
1330 const_area->GetConstant()->AsFloatConstant()->GetValue(),
1331 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
1332 } else {
1333 DCHECK(rhs.IsStackSlot());
1334 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(), Address(ESP, rhs.GetStackIndex()));
1335 }
1336 }
1337}
1338
Mark Mendell152408f2015-12-31 12:28:50 -05001339template<class LabelType>
David Brazdil0debae72015-11-12 18:37:00 +00001340void InstructionCodeGeneratorX86::GenerateCompareTestAndBranch(HCondition* condition,
Mark Mendell152408f2015-12-31 12:28:50 -05001341 LabelType* true_target_in,
1342 LabelType* false_target_in) {
David Brazdil0debae72015-11-12 18:37:00 +00001343 // Generated branching requires both targets to be explicit. If either of the
1344 // targets is nullptr (fallthrough) use and bind `fallthrough_target` instead.
Mark Mendell152408f2015-12-31 12:28:50 -05001345 LabelType fallthrough_target;
1346 LabelType* true_target = true_target_in == nullptr ? &fallthrough_target : true_target_in;
1347 LabelType* false_target = false_target_in == nullptr ? &fallthrough_target : false_target_in;
David Brazdil0debae72015-11-12 18:37:00 +00001348
Mark Mendellc4701932015-04-10 13:18:51 -04001349 LocationSummary* locations = condition->GetLocations();
1350 Location left = locations->InAt(0);
1351 Location right = locations->InAt(1);
1352
Mark Mendellc4701932015-04-10 13:18:51 -04001353 Primitive::Type type = condition->InputAt(0)->GetType();
1354 switch (type) {
1355 case Primitive::kPrimLong:
1356 GenerateLongComparesAndJumps(condition, true_target, false_target);
1357 break;
1358 case Primitive::kPrimFloat:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001359 GenerateFPCompare(left, right, condition, false);
Mark Mendellc4701932015-04-10 13:18:51 -04001360 GenerateFPJumps(condition, true_target, false_target);
1361 break;
1362 case Primitive::kPrimDouble:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001363 GenerateFPCompare(left, right, condition, true);
Mark Mendellc4701932015-04-10 13:18:51 -04001364 GenerateFPJumps(condition, true_target, false_target);
1365 break;
1366 default:
1367 LOG(FATAL) << "Unexpected compare type " << type;
1368 }
1369
David Brazdil0debae72015-11-12 18:37:00 +00001370 if (false_target != &fallthrough_target) {
Mark Mendellc4701932015-04-10 13:18:51 -04001371 __ jmp(false_target);
1372 }
David Brazdil0debae72015-11-12 18:37:00 +00001373
1374 if (fallthrough_target.IsLinked()) {
1375 __ Bind(&fallthrough_target);
1376 }
Mark Mendellc4701932015-04-10 13:18:51 -04001377}
1378
David Brazdil0debae72015-11-12 18:37:00 +00001379static bool AreEflagsSetFrom(HInstruction* cond, HInstruction* branch) {
1380 // Moves may affect the eflags register (move zero uses xorl), so the EFLAGS
1381 // are set only strictly before `branch`. We can't use the eflags on long/FP
1382 // conditions if they are materialized due to the complex branching.
1383 return cond->IsCondition() &&
1384 cond->GetNext() == branch &&
1385 cond->InputAt(0)->GetType() != Primitive::kPrimLong &&
1386 !Primitive::IsFloatingPointType(cond->InputAt(0)->GetType());
1387}
1388
Mark Mendell152408f2015-12-31 12:28:50 -05001389template<class LabelType>
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001390void InstructionCodeGeneratorX86::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00001391 size_t condition_input_index,
Mark Mendell152408f2015-12-31 12:28:50 -05001392 LabelType* true_target,
1393 LabelType* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00001394 HInstruction* cond = instruction->InputAt(condition_input_index);
1395
1396 if (true_target == nullptr && false_target == nullptr) {
1397 // Nothing to do. The code always falls through.
1398 return;
1399 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00001400 // Constant condition, statically compared against "true" (integer value 1).
1401 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00001402 if (true_target != nullptr) {
1403 __ jmp(true_target);
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001404 }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001405 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00001406 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00001407 if (false_target != nullptr) {
1408 __ jmp(false_target);
1409 }
1410 }
1411 return;
1412 }
1413
1414 // The following code generates these patterns:
1415 // (1) true_target == nullptr && false_target != nullptr
1416 // - opposite condition true => branch to false_target
1417 // (2) true_target != nullptr && false_target == nullptr
1418 // - condition true => branch to true_target
1419 // (3) true_target != nullptr && false_target != nullptr
1420 // - condition true => branch to true_target
1421 // - branch to false_target
1422 if (IsBooleanValueOrMaterializedCondition(cond)) {
1423 if (AreEflagsSetFrom(cond, instruction)) {
1424 if (true_target == nullptr) {
1425 __ j(X86Condition(cond->AsCondition()->GetOppositeCondition()), false_target);
1426 } else {
1427 __ j(X86Condition(cond->AsCondition()->GetCondition()), true_target);
1428 }
1429 } else {
1430 // Materialized condition, compare against 0.
1431 Location lhs = instruction->GetLocations()->InAt(condition_input_index);
1432 if (lhs.IsRegister()) {
1433 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
1434 } else {
1435 __ cmpl(Address(ESP, lhs.GetStackIndex()), Immediate(0));
1436 }
1437 if (true_target == nullptr) {
1438 __ j(kEqual, false_target);
1439 } else {
1440 __ j(kNotEqual, true_target);
1441 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001442 }
1443 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001444 // Condition has not been materialized, use its inputs as the comparison and
1445 // its condition as the branch condition.
Mark Mendellb8b97692015-05-22 16:58:19 -04001446 HCondition* condition = cond->AsCondition();
David Brazdil0debae72015-11-12 18:37:00 +00001447
1448 // If this is a long or FP comparison that has been folded into
1449 // the HCondition, generate the comparison directly.
1450 Primitive::Type type = condition->InputAt(0)->GetType();
1451 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
1452 GenerateCompareTestAndBranch(condition, true_target, false_target);
1453 return;
1454 }
1455
1456 Location lhs = condition->GetLocations()->InAt(0);
1457 Location rhs = condition->GetLocations()->InAt(1);
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001458 // LHS is guaranteed to be in a register (see LocationsBuilderX86::HandleCondition).
David Brazdil0debae72015-11-12 18:37:00 +00001459 if (rhs.IsRegister()) {
1460 __ cmpl(lhs.AsRegister<Register>(), rhs.AsRegister<Register>());
1461 } else if (rhs.IsConstant()) {
1462 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
Aart Bika19616e2016-02-01 18:57:58 -08001463 codegen_->Compare32BitValue(lhs.AsRegister<Register>(), constant);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001464 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001465 __ cmpl(lhs.AsRegister<Register>(), Address(ESP, rhs.GetStackIndex()));
1466 }
1467 if (true_target == nullptr) {
1468 __ j(X86Condition(condition->GetOppositeCondition()), false_target);
1469 } else {
Mark Mendellb8b97692015-05-22 16:58:19 -04001470 __ j(X86Condition(condition->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -07001471 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001472 }
David Brazdil0debae72015-11-12 18:37:00 +00001473
1474 // If neither branch falls through (case 3), the conditional branch to `true_target`
1475 // was already emitted (case 2) and we need to emit a jump to `false_target`.
1476 if (true_target != nullptr && false_target != nullptr) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001477 __ jmp(false_target);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001478 }
1479}
1480
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001481void LocationsBuilderX86::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001482 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
1483 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001484 locations->SetInAt(0, Location::Any());
1485 }
1486}
1487
1488void InstructionCodeGeneratorX86::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001489 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
1490 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
1491 Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
1492 nullptr : codegen_->GetLabelOf(true_successor);
1493 Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
1494 nullptr : codegen_->GetLabelOf(false_successor);
1495 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001496}
1497
1498void LocationsBuilderX86::VisitDeoptimize(HDeoptimize* deoptimize) {
1499 LocationSummary* locations = new (GetGraph()->GetArena())
1500 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Vladimir Marko239d6ea2016-09-05 10:44:04 +01001501 locations->SetCustomSlowPathCallerSaves(RegisterSet()); // No caller-save registers.
David Brazdil0debae72015-11-12 18:37:00 +00001502 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001503 locations->SetInAt(0, Location::Any());
1504 }
1505}
1506
1507void InstructionCodeGeneratorX86::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08001508 SlowPathCode* slow_path = deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathX86>(deoptimize);
David Brazdil74eb1b22015-12-14 11:44:01 +00001509 GenerateTestAndBranch<Label>(deoptimize,
1510 /* condition_input_index */ 0,
1511 slow_path->GetEntryLabel(),
1512 /* false_target */ nullptr);
1513}
1514
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001515static bool SelectCanUseCMOV(HSelect* select) {
1516 // There are no conditional move instructions for XMMs.
1517 if (Primitive::IsFloatingPointType(select->GetType())) {
1518 return false;
1519 }
1520
1521 // A FP condition doesn't generate the single CC that we need.
1522 // In 32 bit mode, a long condition doesn't generate a single CC either.
1523 HInstruction* condition = select->GetCondition();
1524 if (condition->IsCondition()) {
1525 Primitive::Type compare_type = condition->InputAt(0)->GetType();
1526 if (compare_type == Primitive::kPrimLong ||
1527 Primitive::IsFloatingPointType(compare_type)) {
1528 return false;
1529 }
1530 }
1531
1532 // We can generate a CMOV for this Select.
1533 return true;
1534}
1535
David Brazdil74eb1b22015-12-14 11:44:01 +00001536void LocationsBuilderX86::VisitSelect(HSelect* select) {
1537 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001538 if (Primitive::IsFloatingPointType(select->GetType())) {
David Brazdil74eb1b22015-12-14 11:44:01 +00001539 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001540 locations->SetInAt(1, Location::Any());
David Brazdil74eb1b22015-12-14 11:44:01 +00001541 } else {
1542 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001543 if (SelectCanUseCMOV(select)) {
1544 if (select->InputAt(1)->IsConstant()) {
1545 // Cmov can't handle a constant value.
1546 locations->SetInAt(1, Location::RequiresRegister());
1547 } else {
1548 locations->SetInAt(1, Location::Any());
1549 }
1550 } else {
1551 locations->SetInAt(1, Location::Any());
1552 }
David Brazdil74eb1b22015-12-14 11:44:01 +00001553 }
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001554 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
1555 locations->SetInAt(2, Location::RequiresRegister());
David Brazdil74eb1b22015-12-14 11:44:01 +00001556 }
1557 locations->SetOut(Location::SameAsFirstInput());
1558}
1559
Roland Levillain0b671c02016-08-19 12:02:34 +01001560void CodeGeneratorX86::GenerateIntCompare(Location lhs, Location rhs) {
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001561 Register lhs_reg = lhs.AsRegister<Register>();
1562 if (rhs.IsConstant()) {
1563 int32_t value = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
Roland Levillain0b671c02016-08-19 12:02:34 +01001564 Compare32BitValue(lhs_reg, value);
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001565 } else if (rhs.IsStackSlot()) {
Roland Levillain0b671c02016-08-19 12:02:34 +01001566 assembler_.cmpl(lhs_reg, Address(ESP, rhs.GetStackIndex()));
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001567 } else {
Roland Levillain0b671c02016-08-19 12:02:34 +01001568 assembler_.cmpl(lhs_reg, rhs.AsRegister<Register>());
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001569 }
1570}
1571
David Brazdil74eb1b22015-12-14 11:44:01 +00001572void InstructionCodeGeneratorX86::VisitSelect(HSelect* select) {
1573 LocationSummary* locations = select->GetLocations();
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001574 DCHECK(locations->InAt(0).Equals(locations->Out()));
1575 if (SelectCanUseCMOV(select)) {
1576 // If both the condition and the source types are integer, we can generate
1577 // a CMOV to implement Select.
1578
1579 HInstruction* select_condition = select->GetCondition();
1580 Condition cond = kNotEqual;
1581
1582 // Figure out how to test the 'condition'.
1583 if (select_condition->IsCondition()) {
1584 HCondition* condition = select_condition->AsCondition();
1585 if (!condition->IsEmittedAtUseSite()) {
1586 // This was a previously materialized condition.
1587 // Can we use the existing condition code?
1588 if (AreEflagsSetFrom(condition, select)) {
1589 // Materialization was the previous instruction. Condition codes are right.
1590 cond = X86Condition(condition->GetCondition());
1591 } else {
1592 // No, we have to recreate the condition code.
1593 Register cond_reg = locations->InAt(2).AsRegister<Register>();
1594 __ testl(cond_reg, cond_reg);
1595 }
1596 } else {
1597 // We can't handle FP or long here.
1598 DCHECK_NE(condition->InputAt(0)->GetType(), Primitive::kPrimLong);
1599 DCHECK(!Primitive::IsFloatingPointType(condition->InputAt(0)->GetType()));
1600 LocationSummary* cond_locations = condition->GetLocations();
Roland Levillain0b671c02016-08-19 12:02:34 +01001601 codegen_->GenerateIntCompare(cond_locations->InAt(0), cond_locations->InAt(1));
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001602 cond = X86Condition(condition->GetCondition());
1603 }
1604 } else {
1605 // Must be a boolean condition, which needs to be compared to 0.
1606 Register cond_reg = locations->InAt(2).AsRegister<Register>();
1607 __ testl(cond_reg, cond_reg);
1608 }
1609
1610 // If the condition is true, overwrite the output, which already contains false.
1611 Location false_loc = locations->InAt(0);
1612 Location true_loc = locations->InAt(1);
1613 if (select->GetType() == Primitive::kPrimLong) {
1614 // 64 bit conditional move.
1615 Register false_high = false_loc.AsRegisterPairHigh<Register>();
1616 Register false_low = false_loc.AsRegisterPairLow<Register>();
1617 if (true_loc.IsRegisterPair()) {
1618 __ cmovl(cond, false_high, true_loc.AsRegisterPairHigh<Register>());
1619 __ cmovl(cond, false_low, true_loc.AsRegisterPairLow<Register>());
1620 } else {
1621 __ cmovl(cond, false_high, Address(ESP, true_loc.GetHighStackIndex(kX86WordSize)));
1622 __ cmovl(cond, false_low, Address(ESP, true_loc.GetStackIndex()));
1623 }
1624 } else {
1625 // 32 bit conditional move.
1626 Register false_reg = false_loc.AsRegister<Register>();
1627 if (true_loc.IsRegister()) {
1628 __ cmovl(cond, false_reg, true_loc.AsRegister<Register>());
1629 } else {
1630 __ cmovl(cond, false_reg, Address(ESP, true_loc.GetStackIndex()));
1631 }
1632 }
1633 } else {
1634 NearLabel false_target;
1635 GenerateTestAndBranch<NearLabel>(
1636 select, /* condition_input_index */ 2, /* true_target */ nullptr, &false_target);
1637 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
1638 __ Bind(&false_target);
1639 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001640}
1641
David Srbecky0cf44932015-12-09 14:09:59 +00001642void LocationsBuilderX86::VisitNativeDebugInfo(HNativeDebugInfo* info) {
1643 new (GetGraph()->GetArena()) LocationSummary(info);
1644}
1645
David Srbeckyd28f4a02016-03-14 17:14:24 +00001646void InstructionCodeGeneratorX86::VisitNativeDebugInfo(HNativeDebugInfo*) {
1647 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00001648}
1649
1650void CodeGeneratorX86::GenerateNop() {
1651 __ nop();
David Srbecky0cf44932015-12-09 14:09:59 +00001652}
1653
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001654void LocationsBuilderX86::HandleCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001655 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +01001656 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Mark Mendellc4701932015-04-10 13:18:51 -04001657 // Handle the long/FP comparisons made in instruction simplification.
1658 switch (cond->InputAt(0)->GetType()) {
1659 case Primitive::kPrimLong: {
1660 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell8659e842016-02-16 10:41:46 -05001661 locations->SetInAt(1, Location::Any());
David Brazdilb3e773e2016-01-26 11:28:37 +00001662 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001663 locations->SetOut(Location::RequiresRegister());
1664 }
1665 break;
1666 }
1667 case Primitive::kPrimFloat:
1668 case Primitive::kPrimDouble: {
1669 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001670 if (cond->InputAt(1)->IsX86LoadFromConstantTable()) {
1671 DCHECK(cond->InputAt(1)->IsEmittedAtUseSite());
1672 } else if (cond->InputAt(1)->IsConstant()) {
1673 locations->SetInAt(1, Location::RequiresFpuRegister());
1674 } else {
1675 locations->SetInAt(1, Location::Any());
1676 }
David Brazdilb3e773e2016-01-26 11:28:37 +00001677 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001678 locations->SetOut(Location::RequiresRegister());
1679 }
1680 break;
1681 }
1682 default:
1683 locations->SetInAt(0, Location::RequiresRegister());
1684 locations->SetInAt(1, Location::Any());
David Brazdilb3e773e2016-01-26 11:28:37 +00001685 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001686 // We need a byte register.
1687 locations->SetOut(Location::RegisterLocation(ECX));
1688 }
1689 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001690 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001691}
1692
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001693void InstructionCodeGeneratorX86::HandleCondition(HCondition* cond) {
David Brazdilb3e773e2016-01-26 11:28:37 +00001694 if (cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001695 return;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001696 }
Mark Mendellc4701932015-04-10 13:18:51 -04001697
1698 LocationSummary* locations = cond->GetLocations();
1699 Location lhs = locations->InAt(0);
1700 Location rhs = locations->InAt(1);
1701 Register reg = locations->Out().AsRegister<Register>();
Mark Mendell152408f2015-12-31 12:28:50 -05001702 NearLabel true_label, false_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001703
1704 switch (cond->InputAt(0)->GetType()) {
1705 default: {
1706 // Integer case.
1707
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01001708 // Clear output register: setb only sets the low byte.
Mark Mendellc4701932015-04-10 13:18:51 -04001709 __ xorl(reg, reg);
Roland Levillain0b671c02016-08-19 12:02:34 +01001710 codegen_->GenerateIntCompare(lhs, rhs);
Aart Bike9f37602015-10-09 11:15:55 -07001711 __ setb(X86Condition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001712 return;
1713 }
1714 case Primitive::kPrimLong:
1715 GenerateLongComparesAndJumps(cond, &true_label, &false_label);
1716 break;
1717 case Primitive::kPrimFloat:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001718 GenerateFPCompare(lhs, rhs, cond, false);
Mark Mendellc4701932015-04-10 13:18:51 -04001719 GenerateFPJumps(cond, &true_label, &false_label);
1720 break;
1721 case Primitive::kPrimDouble:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001722 GenerateFPCompare(lhs, rhs, cond, true);
Mark Mendellc4701932015-04-10 13:18:51 -04001723 GenerateFPJumps(cond, &true_label, &false_label);
1724 break;
1725 }
1726
1727 // Convert the jumps into the result.
Mark Mendell0c9497d2015-08-21 09:30:05 -04001728 NearLabel done_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001729
Roland Levillain4fa13f62015-07-06 18:11:54 +01001730 // False case: result = 0.
Mark Mendellc4701932015-04-10 13:18:51 -04001731 __ Bind(&false_label);
1732 __ xorl(reg, reg);
1733 __ jmp(&done_label);
1734
Roland Levillain4fa13f62015-07-06 18:11:54 +01001735 // True case: result = 1.
Mark Mendellc4701932015-04-10 13:18:51 -04001736 __ Bind(&true_label);
1737 __ movl(reg, Immediate(1));
1738 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07001739}
1740
1741void LocationsBuilderX86::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001742 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001743}
1744
1745void InstructionCodeGeneratorX86::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001746 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001747}
1748
1749void LocationsBuilderX86::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001750 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001751}
1752
1753void InstructionCodeGeneratorX86::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001754 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001755}
1756
1757void LocationsBuilderX86::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001758 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001759}
1760
1761void InstructionCodeGeneratorX86::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001762 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001763}
1764
1765void LocationsBuilderX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001766 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001767}
1768
1769void InstructionCodeGeneratorX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001770 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001771}
1772
1773void LocationsBuilderX86::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001774 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001775}
1776
1777void InstructionCodeGeneratorX86::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001778 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001779}
1780
1781void LocationsBuilderX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001782 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001783}
1784
1785void InstructionCodeGeneratorX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001786 HandleCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001787}
1788
Aart Bike9f37602015-10-09 11:15:55 -07001789void LocationsBuilderX86::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001790 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001791}
1792
1793void InstructionCodeGeneratorX86::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001794 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001795}
1796
1797void LocationsBuilderX86::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001798 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001799}
1800
1801void InstructionCodeGeneratorX86::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001802 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001803}
1804
1805void LocationsBuilderX86::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001806 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001807}
1808
1809void InstructionCodeGeneratorX86::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001810 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001811}
1812
1813void LocationsBuilderX86::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001814 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001815}
1816
1817void InstructionCodeGeneratorX86::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001818 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001819}
1820
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001821void LocationsBuilderX86::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001822 LocationSummary* locations =
1823 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001824 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001825}
1826
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001827void InstructionCodeGeneratorX86::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001828 // Will be generated at use site.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001829}
1830
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001831void LocationsBuilderX86::VisitNullConstant(HNullConstant* constant) {
1832 LocationSummary* locations =
1833 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1834 locations->SetOut(Location::ConstantLocation(constant));
1835}
1836
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001837void InstructionCodeGeneratorX86::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001838 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001839}
1840
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001841void LocationsBuilderX86::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001842 LocationSummary* locations =
1843 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001844 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001845}
1846
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001847void InstructionCodeGeneratorX86::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001848 // Will be generated at use site.
1849}
1850
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001851void LocationsBuilderX86::VisitFloatConstant(HFloatConstant* constant) {
1852 LocationSummary* locations =
1853 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1854 locations->SetOut(Location::ConstantLocation(constant));
1855}
1856
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001857void InstructionCodeGeneratorX86::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001858 // Will be generated at use site.
1859}
1860
1861void LocationsBuilderX86::VisitDoubleConstant(HDoubleConstant* constant) {
1862 LocationSummary* locations =
1863 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1864 locations->SetOut(Location::ConstantLocation(constant));
1865}
1866
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001867void InstructionCodeGeneratorX86::VisitDoubleConstant(HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001868 // Will be generated at use site.
1869}
1870
Calin Juravle27df7582015-04-17 19:12:31 +01001871void LocationsBuilderX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1872 memory_barrier->SetLocations(nullptr);
1873}
1874
1875void InstructionCodeGeneratorX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00001876 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01001877}
1878
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001879void LocationsBuilderX86::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001880 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001881}
1882
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001883void InstructionCodeGeneratorX86::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001884 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001885}
1886
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001887void LocationsBuilderX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001888 LocationSummary* locations =
1889 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001890 switch (ret->InputAt(0)->GetType()) {
1891 case Primitive::kPrimBoolean:
1892 case Primitive::kPrimByte:
1893 case Primitive::kPrimChar:
1894 case Primitive::kPrimShort:
1895 case Primitive::kPrimInt:
1896 case Primitive::kPrimNot:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001897 locations->SetInAt(0, Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001898 break;
1899
1900 case Primitive::kPrimLong:
1901 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001902 0, Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001903 break;
1904
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001905 case Primitive::kPrimFloat:
1906 case Primitive::kPrimDouble:
1907 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001908 0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001909 break;
1910
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001911 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001912 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001913 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001914}
1915
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001916void InstructionCodeGeneratorX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001917 if (kIsDebugBuild) {
1918 switch (ret->InputAt(0)->GetType()) {
1919 case Primitive::kPrimBoolean:
1920 case Primitive::kPrimByte:
1921 case Primitive::kPrimChar:
1922 case Primitive::kPrimShort:
1923 case Primitive::kPrimInt:
1924 case Primitive::kPrimNot:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001925 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<Register>(), EAX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001926 break;
1927
1928 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001929 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairLow<Register>(), EAX);
1930 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairHigh<Register>(), EDX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001931 break;
1932
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001933 case Primitive::kPrimFloat:
1934 case Primitive::kPrimDouble:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001935 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>(), XMM0);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001936 break;
1937
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001938 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001939 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001940 }
1941 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001942 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001943}
1944
Calin Juravle175dc732015-08-25 15:42:32 +01001945void LocationsBuilderX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
1946 // The trampoline uses the same calling convention as dex calling conventions,
1947 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
1948 // the method_idx.
1949 HandleInvoke(invoke);
1950}
1951
1952void InstructionCodeGeneratorX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
1953 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
1954}
1955
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001956void LocationsBuilderX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00001957 // Explicit clinit checks triggered by static invokes must have been pruned by
1958 // art::PrepareForRegisterAllocation.
1959 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001960
Mark Mendellfb8d2792015-03-31 22:16:59 -04001961 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001962 if (intrinsic.TryDispatch(invoke)) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00001963 if (invoke->GetLocations()->CanCall() && invoke->HasPcRelativeDexCache()) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00001964 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00001965 }
Mark Mendell09ed1a32015-03-25 08:30:06 -04001966 return;
1967 }
1968
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001969 HandleInvoke(invoke);
Nicolas Geoffray94015b92015-06-04 18:21:04 +01001970
Vladimir Marko0f7dca42015-11-02 14:36:43 +00001971 // For PC-relative dex cache the invoke has an extra input, the PC-relative address base.
1972 if (invoke->HasPcRelativeDexCache()) {
Vladimir Markob4536b72015-11-24 13:45:23 +00001973 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00001974 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001975}
1976
Mark Mendell09ed1a32015-03-25 08:30:06 -04001977static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86* codegen) {
1978 if (invoke->GetLocations()->Intrinsified()) {
1979 IntrinsicCodeGeneratorX86 intrinsic(codegen);
1980 intrinsic.Dispatch(invoke);
1981 return true;
1982 }
1983 return false;
1984}
1985
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001986void InstructionCodeGeneratorX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00001987 // Explicit clinit checks triggered by static invokes must have been pruned by
1988 // art::PrepareForRegisterAllocation.
1989 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001990
Mark Mendell09ed1a32015-03-25 08:30:06 -04001991 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1992 return;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001993 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001994
Nicolas Geoffray94015b92015-06-04 18:21:04 +01001995 LocationSummary* locations = invoke->GetLocations();
Mark Mendell09ed1a32015-03-25 08:30:06 -04001996 codegen_->GenerateStaticOrDirectCall(
Nicolas Geoffray94015b92015-06-04 18:21:04 +01001997 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Mingyao Yang8693fe12015-04-17 16:51:08 -07001998 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001999}
2000
2001void LocationsBuilderX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00002002 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
2003 if (intrinsic.TryDispatch(invoke)) {
2004 return;
2005 }
2006
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002007 HandleInvoke(invoke);
2008}
2009
2010void LocationsBuilderX86::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002011 InvokeDexCallingConventionVisitorX86 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002012 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002013}
2014
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002015void InstructionCodeGeneratorX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002016 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2017 return;
2018 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002019
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002020 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002021 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01002022 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002023}
2024
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002025void LocationsBuilderX86::VisitInvokeInterface(HInvokeInterface* invoke) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002026 // This call to HandleInvoke allocates a temporary (core) register
2027 // which is also used to transfer the hidden argument from FP to
2028 // core register.
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002029 HandleInvoke(invoke);
2030 // Add the hidden argument.
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002031 invoke->GetLocations()->AddTemp(Location::FpuRegisterLocation(XMM7));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002032}
2033
2034void InstructionCodeGeneratorX86::VisitInvokeInterface(HInvokeInterface* invoke) {
2035 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain0d5a2812015-11-13 10:07:31 +00002036 LocationSummary* locations = invoke->GetLocations();
2037 Register temp = locations->GetTemp(0).AsRegister<Register>();
2038 XmmRegister hidden_reg = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002039 Location receiver = locations->InAt(0);
2040 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2041
Roland Levillain0d5a2812015-11-13 10:07:31 +00002042 // Set the hidden argument. This is safe to do this here, as XMM7
2043 // won't be modified thereafter, before the `call` instruction.
2044 DCHECK_EQ(XMM7, hidden_reg);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002045 __ movl(temp, Immediate(invoke->GetDexMethodIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002046 __ movd(hidden_reg, temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002047
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002048 if (receiver.IsStackSlot()) {
2049 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002050 // /* HeapReference<Class> */ temp = temp->klass_
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002051 __ movl(temp, Address(temp, class_offset));
2052 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002053 // /* HeapReference<Class> */ temp = receiver->klass_
Roland Levillain271ab9c2014-11-27 15:23:57 +00002054 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002055 }
Roland Levillain4d027112015-07-01 15:41:14 +01002056 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00002057 // Instead of simply (possibly) unpoisoning `temp` here, we should
2058 // emit a read barrier for the previous class reference load.
2059 // However this is not required in practice, as this is an
2060 // intermediate/temporary reference and because the current
2061 // concurrent copying collector keeps the from-space memory
2062 // intact/accessible until the end of the marking phase (the
2063 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01002064 __ MaybeUnpoisonHeapReference(temp);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002065 // temp = temp->GetAddressOfIMT()
2066 __ movl(temp,
2067 Address(temp, mirror::Class::ImtPtrOffset(kX86PointerSize).Uint32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002068 // temp = temp->GetImtEntryAt(method_offset);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002069 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00002070 invoke->GetImtIndex(), kX86PointerSize));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002071 __ movl(temp, Address(temp, method_offset));
2072 // call temp->GetEntryPoint();
Roland Levillain0d5a2812015-11-13 10:07:31 +00002073 __ call(Address(temp,
Andreas Gampe542451c2016-07-26 09:02:02 -07002074 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86PointerSize).Int32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002075
2076 DCHECK(!codegen_->IsLeafMethod());
2077 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2078}
2079
Roland Levillain88cb1752014-10-20 16:36:47 +01002080void LocationsBuilderX86::VisitNeg(HNeg* neg) {
2081 LocationSummary* locations =
2082 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2083 switch (neg->GetResultType()) {
2084 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002085 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01002086 locations->SetInAt(0, Location::RequiresRegister());
2087 locations->SetOut(Location::SameAsFirstInput());
2088 break;
2089
Roland Levillain88cb1752014-10-20 16:36:47 +01002090 case Primitive::kPrimFloat:
Roland Levillain5368c212014-11-27 15:03:41 +00002091 locations->SetInAt(0, Location::RequiresFpuRegister());
2092 locations->SetOut(Location::SameAsFirstInput());
2093 locations->AddTemp(Location::RequiresRegister());
2094 locations->AddTemp(Location::RequiresFpuRegister());
2095 break;
2096
Roland Levillain88cb1752014-10-20 16:36:47 +01002097 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002098 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00002099 locations->SetOut(Location::SameAsFirstInput());
2100 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01002101 break;
2102
2103 default:
2104 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2105 }
2106}
2107
2108void InstructionCodeGeneratorX86::VisitNeg(HNeg* neg) {
2109 LocationSummary* locations = neg->GetLocations();
2110 Location out = locations->Out();
2111 Location in = locations->InAt(0);
2112 switch (neg->GetResultType()) {
2113 case Primitive::kPrimInt:
2114 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002115 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002116 __ negl(out.AsRegister<Register>());
Roland Levillain88cb1752014-10-20 16:36:47 +01002117 break;
2118
2119 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002120 DCHECK(in.IsRegisterPair());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002121 DCHECK(in.Equals(out));
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002122 __ negl(out.AsRegisterPairLow<Register>());
2123 // Negation is similar to subtraction from zero. The least
2124 // significant byte triggers a borrow when it is different from
2125 // zero; to take it into account, add 1 to the most significant
2126 // byte if the carry flag (CF) is set to 1 after the first NEGL
2127 // operation.
2128 __ adcl(out.AsRegisterPairHigh<Register>(), Immediate(0));
2129 __ negl(out.AsRegisterPairHigh<Register>());
2130 break;
2131
Roland Levillain5368c212014-11-27 15:03:41 +00002132 case Primitive::kPrimFloat: {
2133 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002134 Register constant = locations->GetTemp(0).AsRegister<Register>();
2135 XmmRegister mask = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002136 // Implement float negation with an exclusive or with value
2137 // 0x80000000 (mask for bit 31, representing the sign of a
2138 // single-precision floating-point number).
2139 __ movl(constant, Immediate(INT32_C(0x80000000)));
2140 __ movd(mask, constant);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002141 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00002142 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002143 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00002144
Roland Levillain5368c212014-11-27 15:03:41 +00002145 case Primitive::kPrimDouble: {
2146 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002147 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002148 // Implement double negation with an exclusive or with value
2149 // 0x8000000000000000 (mask for bit 63, representing the sign of
2150 // a double-precision floating-point number).
2151 __ LoadLongConstant(mask, INT64_C(0x8000000000000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002152 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01002153 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002154 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002155
2156 default:
2157 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2158 }
2159}
2160
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002161void LocationsBuilderX86::VisitX86FPNeg(HX86FPNeg* neg) {
2162 LocationSummary* locations =
2163 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2164 DCHECK(Primitive::IsFloatingPointType(neg->GetType()));
2165 locations->SetInAt(0, Location::RequiresFpuRegister());
2166 locations->SetInAt(1, Location::RequiresRegister());
2167 locations->SetOut(Location::SameAsFirstInput());
2168 locations->AddTemp(Location::RequiresFpuRegister());
2169}
2170
2171void InstructionCodeGeneratorX86::VisitX86FPNeg(HX86FPNeg* neg) {
2172 LocationSummary* locations = neg->GetLocations();
2173 Location out = locations->Out();
2174 DCHECK(locations->InAt(0).Equals(out));
2175
2176 Register constant_area = locations->InAt(1).AsRegister<Register>();
2177 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
2178 if (neg->GetType() == Primitive::kPrimFloat) {
2179 __ movss(mask, codegen_->LiteralInt32Address(INT32_C(0x80000000), constant_area));
2180 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
2181 } else {
2182 __ movsd(mask, codegen_->LiteralInt64Address(INT64_C(0x8000000000000000), constant_area));
2183 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
2184 }
2185}
2186
Roland Levillaindff1f282014-11-05 14:15:05 +00002187void LocationsBuilderX86::VisitTypeConversion(HTypeConversion* conversion) {
Roland Levillaindff1f282014-11-05 14:15:05 +00002188 Primitive::Type result_type = conversion->GetResultType();
2189 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002190 DCHECK_NE(result_type, input_type);
Roland Levillain624279f2014-12-04 11:54:28 +00002191
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002192 // The float-to-long and double-to-long type conversions rely on a
2193 // call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00002194 LocationSummary::CallKind call_kind =
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002195 ((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
2196 && result_type == Primitive::kPrimLong)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01002197 ? LocationSummary::kCallOnMainOnly
Roland Levillain624279f2014-12-04 11:54:28 +00002198 : LocationSummary::kNoCall;
2199 LocationSummary* locations =
2200 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
2201
David Brazdilb2bd1c52015-03-25 11:17:37 +00002202 // The Java language does not allow treating boolean as an integral type but
2203 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00002204
Roland Levillaindff1f282014-11-05 14:15:05 +00002205 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002206 case Primitive::kPrimByte:
2207 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002208 case Primitive::kPrimLong: {
2209 // Type conversion from long to byte is a result of code transformations.
2210 HInstruction* input = conversion->InputAt(0);
2211 Location input_location = input->IsConstant()
2212 ? Location::ConstantLocation(input->AsConstant())
2213 : Location::RegisterPairLocation(EAX, EDX);
2214 locations->SetInAt(0, input_location);
2215 // Make the output overlap to please the register allocator. This greatly simplifies
2216 // the validation of the linear scan implementation
2217 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2218 break;
2219 }
David Brazdil46e2a392015-03-16 17:31:52 +00002220 case Primitive::kPrimBoolean:
2221 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002222 case Primitive::kPrimShort:
2223 case Primitive::kPrimInt:
2224 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002225 // Processing a Dex `int-to-byte' instruction.
Mark Mendell5f874182015-03-04 15:42:45 -05002226 locations->SetInAt(0, Location::ByteRegisterOrConstant(ECX, conversion->InputAt(0)));
2227 // Make the output overlap to please the register allocator. This greatly simplifies
2228 // the validation of the linear scan implementation
2229 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Roland Levillain51d3fc42014-11-13 14:11:42 +00002230 break;
2231
2232 default:
2233 LOG(FATAL) << "Unexpected type conversion from " << input_type
2234 << " to " << result_type;
2235 }
2236 break;
2237
Roland Levillain01a8d712014-11-14 16:27:39 +00002238 case Primitive::kPrimShort:
2239 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002240 case Primitive::kPrimLong:
2241 // Type conversion from long to short is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002242 case Primitive::kPrimBoolean:
2243 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002244 case Primitive::kPrimByte:
2245 case Primitive::kPrimInt:
2246 case Primitive::kPrimChar:
2247 // Processing a Dex `int-to-short' instruction.
2248 locations->SetInAt(0, Location::Any());
2249 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2250 break;
2251
2252 default:
2253 LOG(FATAL) << "Unexpected type conversion from " << input_type
2254 << " to " << result_type;
2255 }
2256 break;
2257
Roland Levillain946e1432014-11-11 17:35:19 +00002258 case Primitive::kPrimInt:
2259 switch (input_type) {
2260 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002261 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002262 locations->SetInAt(0, Location::Any());
2263 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2264 break;
2265
2266 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00002267 // Processing a Dex `float-to-int' instruction.
2268 locations->SetInAt(0, Location::RequiresFpuRegister());
2269 locations->SetOut(Location::RequiresRegister());
2270 locations->AddTemp(Location::RequiresFpuRegister());
2271 break;
2272
Roland Levillain946e1432014-11-11 17:35:19 +00002273 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002274 // Processing a Dex `double-to-int' instruction.
2275 locations->SetInAt(0, Location::RequiresFpuRegister());
2276 locations->SetOut(Location::RequiresRegister());
2277 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00002278 break;
2279
2280 default:
2281 LOG(FATAL) << "Unexpected type conversion from " << input_type
2282 << " to " << result_type;
2283 }
2284 break;
2285
Roland Levillaindff1f282014-11-05 14:15:05 +00002286 case Primitive::kPrimLong:
2287 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002288 case Primitive::kPrimBoolean:
2289 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002290 case Primitive::kPrimByte:
2291 case Primitive::kPrimShort:
2292 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002293 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002294 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002295 locations->SetInAt(0, Location::RegisterLocation(EAX));
2296 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
2297 break;
2298
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002299 case Primitive::kPrimFloat:
Vladimir Marko949c91f2015-01-27 10:48:44 +00002300 case Primitive::kPrimDouble: {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002301 // Processing a Dex `float-to-long' or 'double-to-long' instruction.
Vladimir Marko949c91f2015-01-27 10:48:44 +00002302 InvokeRuntimeCallingConvention calling_convention;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002303 XmmRegister parameter = calling_convention.GetFpuRegisterAt(0);
2304 locations->SetInAt(0, Location::FpuRegisterLocation(parameter));
2305
Vladimir Marko949c91f2015-01-27 10:48:44 +00002306 // The runtime helper puts the result in EAX, EDX.
2307 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Vladimir Marko949c91f2015-01-27 10:48:44 +00002308 }
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002309 break;
Roland Levillaindff1f282014-11-05 14:15:05 +00002310
2311 default:
2312 LOG(FATAL) << "Unexpected type conversion from " << input_type
2313 << " to " << result_type;
2314 }
2315 break;
2316
Roland Levillain981e4542014-11-14 11:47:14 +00002317 case Primitive::kPrimChar:
2318 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002319 case Primitive::kPrimLong:
2320 // Type conversion from long to char is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002321 case Primitive::kPrimBoolean:
2322 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002323 case Primitive::kPrimByte:
2324 case Primitive::kPrimShort:
2325 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002326 // Processing a Dex `int-to-char' instruction.
2327 locations->SetInAt(0, Location::Any());
2328 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2329 break;
2330
2331 default:
2332 LOG(FATAL) << "Unexpected type conversion from " << input_type
2333 << " to " << result_type;
2334 }
2335 break;
2336
Roland Levillaindff1f282014-11-05 14:15:05 +00002337 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002338 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002339 case Primitive::kPrimBoolean:
2340 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002341 case Primitive::kPrimByte:
2342 case Primitive::kPrimShort:
2343 case Primitive::kPrimInt:
2344 case Primitive::kPrimChar:
2345 // Processing a Dex `int-to-float' instruction.
2346 locations->SetInAt(0, Location::RequiresRegister());
2347 locations->SetOut(Location::RequiresFpuRegister());
2348 break;
2349
2350 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002351 // Processing a Dex `long-to-float' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002352 locations->SetInAt(0, Location::Any());
2353 locations->SetOut(Location::Any());
Roland Levillain6d0e4832014-11-27 18:31:21 +00002354 break;
2355
Roland Levillaincff13742014-11-17 14:32:17 +00002356 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002357 // Processing a Dex `double-to-float' instruction.
2358 locations->SetInAt(0, Location::RequiresFpuRegister());
2359 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002360 break;
2361
2362 default:
2363 LOG(FATAL) << "Unexpected type conversion from " << input_type
2364 << " to " << result_type;
2365 };
2366 break;
2367
Roland Levillaindff1f282014-11-05 14:15:05 +00002368 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002369 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002370 case Primitive::kPrimBoolean:
2371 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002372 case Primitive::kPrimByte:
2373 case Primitive::kPrimShort:
2374 case Primitive::kPrimInt:
2375 case Primitive::kPrimChar:
2376 // Processing a Dex `int-to-double' instruction.
2377 locations->SetInAt(0, Location::RequiresRegister());
2378 locations->SetOut(Location::RequiresFpuRegister());
2379 break;
2380
2381 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002382 // Processing a Dex `long-to-double' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002383 locations->SetInAt(0, Location::Any());
2384 locations->SetOut(Location::Any());
Roland Levillain647b9ed2014-11-27 12:06:00 +00002385 break;
2386
Roland Levillaincff13742014-11-17 14:32:17 +00002387 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002388 // Processing a Dex `float-to-double' instruction.
2389 locations->SetInAt(0, Location::RequiresFpuRegister());
2390 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002391 break;
2392
2393 default:
2394 LOG(FATAL) << "Unexpected type conversion from " << input_type
2395 << " to " << result_type;
2396 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002397 break;
2398
2399 default:
2400 LOG(FATAL) << "Unexpected type conversion from " << input_type
2401 << " to " << result_type;
2402 }
2403}
2404
2405void InstructionCodeGeneratorX86::VisitTypeConversion(HTypeConversion* conversion) {
2406 LocationSummary* locations = conversion->GetLocations();
2407 Location out = locations->Out();
2408 Location in = locations->InAt(0);
2409 Primitive::Type result_type = conversion->GetResultType();
2410 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002411 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00002412 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002413 case Primitive::kPrimByte:
2414 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002415 case Primitive::kPrimLong:
2416 // Type conversion from long to byte is a result of code transformations.
2417 if (in.IsRegisterPair()) {
2418 __ movsxb(out.AsRegister<Register>(), in.AsRegisterPairLow<ByteRegister>());
2419 } else {
2420 DCHECK(in.GetConstant()->IsLongConstant());
2421 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2422 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
2423 }
2424 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002425 case Primitive::kPrimBoolean:
2426 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002427 case Primitive::kPrimShort:
2428 case Primitive::kPrimInt:
2429 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002430 // Processing a Dex `int-to-byte' instruction.
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002431 if (in.IsRegister()) {
2432 __ movsxb(out.AsRegister<Register>(), in.AsRegister<ByteRegister>());
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002433 } else {
2434 DCHECK(in.GetConstant()->IsIntConstant());
2435 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
2436 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
2437 }
Roland Levillain51d3fc42014-11-13 14:11:42 +00002438 break;
2439
2440 default:
2441 LOG(FATAL) << "Unexpected type conversion from " << input_type
2442 << " to " << result_type;
2443 }
2444 break;
2445
Roland Levillain01a8d712014-11-14 16:27:39 +00002446 case Primitive::kPrimShort:
2447 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002448 case Primitive::kPrimLong:
2449 // Type conversion from long to short is a result of code transformations.
2450 if (in.IsRegisterPair()) {
2451 __ movsxw(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
2452 } else if (in.IsDoubleStackSlot()) {
2453 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
2454 } else {
2455 DCHECK(in.GetConstant()->IsLongConstant());
2456 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2457 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
2458 }
2459 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002460 case Primitive::kPrimBoolean:
2461 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002462 case Primitive::kPrimByte:
2463 case Primitive::kPrimInt:
2464 case Primitive::kPrimChar:
2465 // Processing a Dex `int-to-short' instruction.
2466 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002467 __ movsxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain01a8d712014-11-14 16:27:39 +00002468 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002469 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain01a8d712014-11-14 16:27:39 +00002470 } else {
2471 DCHECK(in.GetConstant()->IsIntConstant());
2472 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002473 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
Roland Levillain01a8d712014-11-14 16:27:39 +00002474 }
2475 break;
2476
2477 default:
2478 LOG(FATAL) << "Unexpected type conversion from " << input_type
2479 << " to " << result_type;
2480 }
2481 break;
2482
Roland Levillain946e1432014-11-11 17:35:19 +00002483 case Primitive::kPrimInt:
2484 switch (input_type) {
2485 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002486 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002487 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002488 __ movl(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00002489 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002490 __ movl(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain946e1432014-11-11 17:35:19 +00002491 } else {
2492 DCHECK(in.IsConstant());
2493 DCHECK(in.GetConstant()->IsLongConstant());
2494 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002495 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00002496 }
2497 break;
2498
Roland Levillain3f8f9362014-12-02 17:45:01 +00002499 case Primitive::kPrimFloat: {
2500 // Processing a Dex `float-to-int' instruction.
2501 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2502 Register output = out.AsRegister<Register>();
2503 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002504 NearLabel done, nan;
Roland Levillain3f8f9362014-12-02 17:45:01 +00002505
2506 __ movl(output, Immediate(kPrimIntMax));
2507 // temp = int-to-float(output)
2508 __ cvtsi2ss(temp, output);
2509 // if input >= temp goto done
2510 __ comiss(input, temp);
2511 __ j(kAboveEqual, &done);
2512 // if input == NaN goto nan
2513 __ j(kUnordered, &nan);
2514 // output = float-to-int-truncate(input)
2515 __ cvttss2si(output, input);
2516 __ jmp(&done);
2517 __ Bind(&nan);
2518 // output = 0
2519 __ xorl(output, output);
2520 __ Bind(&done);
2521 break;
2522 }
2523
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002524 case Primitive::kPrimDouble: {
2525 // Processing a Dex `double-to-int' instruction.
2526 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2527 Register output = out.AsRegister<Register>();
2528 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002529 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002530
2531 __ movl(output, Immediate(kPrimIntMax));
2532 // temp = int-to-double(output)
2533 __ cvtsi2sd(temp, output);
2534 // if input >= temp goto done
2535 __ comisd(input, temp);
2536 __ j(kAboveEqual, &done);
2537 // if input == NaN goto nan
2538 __ j(kUnordered, &nan);
2539 // output = double-to-int-truncate(input)
2540 __ cvttsd2si(output, input);
2541 __ jmp(&done);
2542 __ Bind(&nan);
2543 // output = 0
2544 __ xorl(output, output);
2545 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00002546 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002547 }
Roland Levillain946e1432014-11-11 17:35:19 +00002548
2549 default:
2550 LOG(FATAL) << "Unexpected type conversion from " << input_type
2551 << " to " << result_type;
2552 }
2553 break;
2554
Roland Levillaindff1f282014-11-05 14:15:05 +00002555 case Primitive::kPrimLong:
2556 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002557 case Primitive::kPrimBoolean:
2558 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002559 case Primitive::kPrimByte:
2560 case Primitive::kPrimShort:
2561 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002562 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002563 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002564 DCHECK_EQ(out.AsRegisterPairLow<Register>(), EAX);
2565 DCHECK_EQ(out.AsRegisterPairHigh<Register>(), EDX);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002566 DCHECK_EQ(in.AsRegister<Register>(), EAX);
Roland Levillaindff1f282014-11-05 14:15:05 +00002567 __ cdq();
2568 break;
2569
2570 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00002571 // Processing a Dex `float-to-long' instruction.
Serban Constantinescuba45db02016-07-12 22:53:02 +01002572 codegen_->InvokeRuntime(kQuickF2l, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00002573 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
Roland Levillain624279f2014-12-04 11:54:28 +00002574 break;
2575
Roland Levillaindff1f282014-11-05 14:15:05 +00002576 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002577 // Processing a Dex `double-to-long' instruction.
Serban Constantinescuba45db02016-07-12 22:53:02 +01002578 codegen_->InvokeRuntime(kQuickD2l, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00002579 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
Roland Levillaindff1f282014-11-05 14:15:05 +00002580 break;
2581
2582 default:
2583 LOG(FATAL) << "Unexpected type conversion from " << input_type
2584 << " to " << result_type;
2585 }
2586 break;
2587
Roland Levillain981e4542014-11-14 11:47:14 +00002588 case Primitive::kPrimChar:
2589 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002590 case Primitive::kPrimLong:
2591 // Type conversion from long to short is a result of code transformations.
2592 if (in.IsRegisterPair()) {
2593 __ movzxw(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
2594 } else if (in.IsDoubleStackSlot()) {
2595 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
2596 } else {
2597 DCHECK(in.GetConstant()->IsLongConstant());
2598 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2599 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
2600 }
2601 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002602 case Primitive::kPrimBoolean:
2603 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002604 case Primitive::kPrimByte:
2605 case Primitive::kPrimShort:
2606 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002607 // Processing a Dex `Process a Dex `int-to-char'' instruction.
2608 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002609 __ movzxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain981e4542014-11-14 11:47:14 +00002610 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002611 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain981e4542014-11-14 11:47:14 +00002612 } else {
2613 DCHECK(in.GetConstant()->IsIntConstant());
2614 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002615 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
Roland Levillain981e4542014-11-14 11:47:14 +00002616 }
2617 break;
2618
2619 default:
2620 LOG(FATAL) << "Unexpected type conversion from " << input_type
2621 << " to " << result_type;
2622 }
2623 break;
2624
Roland Levillaindff1f282014-11-05 14:15:05 +00002625 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002626 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002627 case Primitive::kPrimBoolean:
2628 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002629 case Primitive::kPrimByte:
2630 case Primitive::kPrimShort:
2631 case Primitive::kPrimInt:
2632 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002633 // Processing a Dex `int-to-float' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002634 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002635 break;
2636
Roland Levillain6d0e4832014-11-27 18:31:21 +00002637 case Primitive::kPrimLong: {
2638 // Processing a Dex `long-to-float' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002639 size_t adjustment = 0;
Roland Levillain6d0e4832014-11-27 18:31:21 +00002640
Roland Levillain232ade02015-04-20 15:14:36 +01002641 // Create stack space for the call to
2642 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstps below.
2643 // TODO: enhance register allocator to ask for stack temporaries.
2644 if (!in.IsDoubleStackSlot() || !out.IsStackSlot()) {
2645 adjustment = Primitive::ComponentSize(Primitive::kPrimLong);
2646 __ subl(ESP, Immediate(adjustment));
2647 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002648
Roland Levillain232ade02015-04-20 15:14:36 +01002649 // Load the value to the FP stack, using temporaries if needed.
2650 PushOntoFPStack(in, 0, adjustment, false, true);
2651
2652 if (out.IsStackSlot()) {
2653 __ fstps(Address(ESP, out.GetStackIndex() + adjustment));
2654 } else {
2655 __ fstps(Address(ESP, 0));
2656 Location stack_temp = Location::StackSlot(0);
2657 codegen_->Move32(out, stack_temp);
2658 }
2659
2660 // Remove the temporary stack space we allocated.
2661 if (adjustment != 0) {
2662 __ addl(ESP, Immediate(adjustment));
2663 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002664 break;
2665 }
2666
Roland Levillaincff13742014-11-17 14:32:17 +00002667 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002668 // Processing a Dex `double-to-float' instruction.
2669 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002670 break;
2671
2672 default:
2673 LOG(FATAL) << "Unexpected type conversion from " << input_type
2674 << " to " << result_type;
2675 };
2676 break;
2677
Roland Levillaindff1f282014-11-05 14:15:05 +00002678 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002679 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002680 case Primitive::kPrimBoolean:
2681 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002682 case Primitive::kPrimByte:
2683 case Primitive::kPrimShort:
2684 case Primitive::kPrimInt:
2685 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002686 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002687 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002688 break;
2689
Roland Levillain647b9ed2014-11-27 12:06:00 +00002690 case Primitive::kPrimLong: {
2691 // Processing a Dex `long-to-double' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002692 size_t adjustment = 0;
Roland Levillain647b9ed2014-11-27 12:06:00 +00002693
Roland Levillain232ade02015-04-20 15:14:36 +01002694 // Create stack space for the call to
2695 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstpl below.
2696 // TODO: enhance register allocator to ask for stack temporaries.
2697 if (!in.IsDoubleStackSlot() || !out.IsDoubleStackSlot()) {
2698 adjustment = Primitive::ComponentSize(Primitive::kPrimLong);
2699 __ subl(ESP, Immediate(adjustment));
2700 }
2701
2702 // Load the value to the FP stack, using temporaries if needed.
2703 PushOntoFPStack(in, 0, adjustment, false, true);
2704
2705 if (out.IsDoubleStackSlot()) {
2706 __ fstpl(Address(ESP, out.GetStackIndex() + adjustment));
2707 } else {
2708 __ fstpl(Address(ESP, 0));
2709 Location stack_temp = Location::DoubleStackSlot(0);
2710 codegen_->Move64(out, stack_temp);
2711 }
2712
2713 // Remove the temporary stack space we allocated.
2714 if (adjustment != 0) {
2715 __ addl(ESP, Immediate(adjustment));
2716 }
Roland Levillain647b9ed2014-11-27 12:06:00 +00002717 break;
2718 }
2719
Roland Levillaincff13742014-11-17 14:32:17 +00002720 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002721 // Processing a Dex `float-to-double' instruction.
2722 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002723 break;
2724
2725 default:
2726 LOG(FATAL) << "Unexpected type conversion from " << input_type
2727 << " to " << result_type;
2728 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002729 break;
2730
2731 default:
2732 LOG(FATAL) << "Unexpected type conversion from " << input_type
2733 << " to " << result_type;
2734 }
2735}
2736
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002737void LocationsBuilderX86::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002738 LocationSummary* locations =
2739 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002740 switch (add->GetResultType()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002741 case Primitive::kPrimInt: {
2742 locations->SetInAt(0, Location::RequiresRegister());
2743 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
2744 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2745 break;
2746 }
2747
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002748 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002749 locations->SetInAt(0, Location::RequiresRegister());
2750 locations->SetInAt(1, Location::Any());
2751 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002752 break;
2753 }
2754
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002755 case Primitive::kPrimFloat:
2756 case Primitive::kPrimDouble: {
2757 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002758 if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
2759 DCHECK(add->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00002760 } else if (add->InputAt(1)->IsConstant()) {
2761 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002762 } else {
2763 locations->SetInAt(1, Location::Any());
2764 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002765 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002766 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002767 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002768
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002769 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002770 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
2771 break;
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002772 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002773}
2774
2775void InstructionCodeGeneratorX86::VisitAdd(HAdd* add) {
2776 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002777 Location first = locations->InAt(0);
2778 Location second = locations->InAt(1);
Mark Mendell09b84632015-02-13 17:48:38 -05002779 Location out = locations->Out();
2780
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002781 switch (add->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002782 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002783 if (second.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002784 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2785 __ addl(out.AsRegister<Register>(), second.AsRegister<Register>());
Mark Mendell33bf2452015-05-27 10:08:24 -04002786 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
2787 __ addl(out.AsRegister<Register>(), first.AsRegister<Register>());
Mark Mendell09b84632015-02-13 17:48:38 -05002788 } else {
2789 __ leal(out.AsRegister<Register>(), Address(
2790 first.AsRegister<Register>(), second.AsRegister<Register>(), TIMES_1, 0));
2791 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002792 } else if (second.IsConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002793 int32_t value = second.GetConstant()->AsIntConstant()->GetValue();
2794 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2795 __ addl(out.AsRegister<Register>(), Immediate(value));
2796 } else {
2797 __ leal(out.AsRegister<Register>(), Address(first.AsRegister<Register>(), value));
2798 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002799 } else {
Mark Mendell09b84632015-02-13 17:48:38 -05002800 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002801 __ addl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002802 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002803 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002804 }
2805
2806 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002807 if (second.IsRegisterPair()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002808 __ addl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
2809 __ adcl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002810 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002811 __ addl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
2812 __ adcl(first.AsRegisterPairHigh<Register>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002813 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002814 } else {
2815 DCHECK(second.IsConstant()) << second;
2816 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2817 __ addl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
2818 __ adcl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002819 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002820 break;
2821 }
2822
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002823 case Primitive::kPrimFloat: {
2824 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002825 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04002826 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
2827 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00002828 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04002829 __ addss(first.AsFpuRegister<XmmRegister>(),
2830 codegen_->LiteralFloatAddress(
2831 const_area->GetConstant()->AsFloatConstant()->GetValue(),
2832 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2833 } else {
2834 DCHECK(second.IsStackSlot());
2835 __ addss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002836 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002837 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002838 }
2839
2840 case Primitive::kPrimDouble: {
2841 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002842 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04002843 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
2844 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00002845 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04002846 __ addsd(first.AsFpuRegister<XmmRegister>(),
2847 codegen_->LiteralDoubleAddress(
2848 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
2849 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2850 } else {
2851 DCHECK(second.IsDoubleStackSlot());
2852 __ addsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002853 }
2854 break;
2855 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002856
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002857 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002858 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002859 }
2860}
2861
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002862void LocationsBuilderX86::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002863 LocationSummary* locations =
2864 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002865 switch (sub->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002866 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002867 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002868 locations->SetInAt(0, Location::RequiresRegister());
2869 locations->SetInAt(1, Location::Any());
2870 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002871 break;
2872 }
Calin Juravle11351682014-10-23 15:38:15 +01002873 case Primitive::kPrimFloat:
2874 case Primitive::kPrimDouble: {
2875 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002876 if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
2877 DCHECK(sub->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00002878 } else if (sub->InputAt(1)->IsConstant()) {
2879 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002880 } else {
2881 locations->SetInAt(1, Location::Any());
2882 }
Calin Juravle11351682014-10-23 15:38:15 +01002883 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002884 break;
Calin Juravle11351682014-10-23 15:38:15 +01002885 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002886
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002887 default:
Calin Juravle11351682014-10-23 15:38:15 +01002888 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002889 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002890}
2891
2892void InstructionCodeGeneratorX86::VisitSub(HSub* sub) {
2893 LocationSummary* locations = sub->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002894 Location first = locations->InAt(0);
2895 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01002896 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002897 switch (sub->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002898 case Primitive::kPrimInt: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002899 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002900 __ subl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002901 } else if (second.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002902 __ subl(first.AsRegister<Register>(),
2903 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002904 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002905 __ subl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002906 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002907 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002908 }
2909
2910 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002911 if (second.IsRegisterPair()) {
Calin Juravle11351682014-10-23 15:38:15 +01002912 __ subl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
2913 __ sbbl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002914 } else if (second.IsDoubleStackSlot()) {
Calin Juravle11351682014-10-23 15:38:15 +01002915 __ subl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002916 __ sbbl(first.AsRegisterPairHigh<Register>(),
2917 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002918 } else {
2919 DCHECK(second.IsConstant()) << second;
2920 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2921 __ subl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
2922 __ sbbl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002923 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002924 break;
2925 }
2926
Calin Juravle11351682014-10-23 15:38:15 +01002927 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04002928 if (second.IsFpuRegister()) {
2929 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2930 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
2931 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00002932 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04002933 __ subss(first.AsFpuRegister<XmmRegister>(),
2934 codegen_->LiteralFloatAddress(
2935 const_area->GetConstant()->AsFloatConstant()->GetValue(),
2936 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2937 } else {
2938 DCHECK(second.IsStackSlot());
2939 __ subss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
2940 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002941 break;
Calin Juravle11351682014-10-23 15:38:15 +01002942 }
2943
2944 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04002945 if (second.IsFpuRegister()) {
2946 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2947 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
2948 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00002949 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04002950 __ subsd(first.AsFpuRegister<XmmRegister>(),
2951 codegen_->LiteralDoubleAddress(
2952 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
2953 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2954 } else {
2955 DCHECK(second.IsDoubleStackSlot());
2956 __ subsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
2957 }
Calin Juravle11351682014-10-23 15:38:15 +01002958 break;
2959 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002960
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002961 default:
Calin Juravle11351682014-10-23 15:38:15 +01002962 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002963 }
2964}
2965
Calin Juravle34bacdf2014-10-07 20:23:36 +01002966void LocationsBuilderX86::VisitMul(HMul* mul) {
2967 LocationSummary* locations =
2968 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
2969 switch (mul->GetResultType()) {
2970 case Primitive::kPrimInt:
2971 locations->SetInAt(0, Location::RequiresRegister());
2972 locations->SetInAt(1, Location::Any());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002973 if (mul->InputAt(1)->IsIntConstant()) {
2974 // Can use 3 operand multiply.
2975 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2976 } else {
2977 locations->SetOut(Location::SameAsFirstInput());
2978 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002979 break;
2980 case Primitive::kPrimLong: {
2981 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002982 locations->SetInAt(1, Location::Any());
2983 locations->SetOut(Location::SameAsFirstInput());
2984 // Needed for imul on 32bits with 64bits output.
2985 locations->AddTemp(Location::RegisterLocation(EAX));
2986 locations->AddTemp(Location::RegisterLocation(EDX));
2987 break;
2988 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002989 case Primitive::kPrimFloat:
2990 case Primitive::kPrimDouble: {
2991 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002992 if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
2993 DCHECK(mul->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00002994 } else if (mul->InputAt(1)->IsConstant()) {
2995 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002996 } else {
2997 locations->SetInAt(1, Location::Any());
2998 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002999 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003000 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003001 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003002
3003 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003004 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003005 }
3006}
3007
3008void InstructionCodeGeneratorX86::VisitMul(HMul* mul) {
3009 LocationSummary* locations = mul->GetLocations();
3010 Location first = locations->InAt(0);
3011 Location second = locations->InAt(1);
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003012 Location out = locations->Out();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003013
3014 switch (mul->GetResultType()) {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003015 case Primitive::kPrimInt:
3016 // The constant may have ended up in a register, so test explicitly to avoid
3017 // problems where the output may not be the same as the first operand.
3018 if (mul->InputAt(1)->IsIntConstant()) {
3019 Immediate imm(mul->InputAt(1)->AsIntConstant()->GetValue());
3020 __ imull(out.AsRegister<Register>(), first.AsRegister<Register>(), imm);
3021 } else if (second.IsRegister()) {
3022 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003023 __ imull(first.AsRegister<Register>(), second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003024 } else {
3025 DCHECK(second.IsStackSlot());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003026 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003027 __ imull(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01003028 }
3029 break;
Calin Juravle34bacdf2014-10-07 20:23:36 +01003030
3031 case Primitive::kPrimLong: {
Calin Juravle34bacdf2014-10-07 20:23:36 +01003032 Register in1_hi = first.AsRegisterPairHigh<Register>();
3033 Register in1_lo = first.AsRegisterPairLow<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003034 Register eax = locations->GetTemp(0).AsRegister<Register>();
3035 Register edx = locations->GetTemp(1).AsRegister<Register>();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003036
3037 DCHECK_EQ(EAX, eax);
3038 DCHECK_EQ(EDX, edx);
3039
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003040 // input: in1 - 64 bits, in2 - 64 bits.
Calin Juravle34bacdf2014-10-07 20:23:36 +01003041 // output: in1
3042 // formula: in1.hi : in1.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
3043 // parts: in1.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
3044 // parts: in1.lo = (in1.lo * in2.lo)[31:0]
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003045 if (second.IsConstant()) {
3046 DCHECK(second.GetConstant()->IsLongConstant());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003047
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003048 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3049 int32_t low_value = Low32Bits(value);
3050 int32_t high_value = High32Bits(value);
3051 Immediate low(low_value);
3052 Immediate high(high_value);
3053
3054 __ movl(eax, high);
3055 // eax <- in1.lo * in2.hi
3056 __ imull(eax, in1_lo);
3057 // in1.hi <- in1.hi * in2.lo
3058 __ imull(in1_hi, low);
3059 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3060 __ addl(in1_hi, eax);
3061 // move in2_lo to eax to prepare for double precision
3062 __ movl(eax, low);
3063 // edx:eax <- in1.lo * in2.lo
3064 __ mull(in1_lo);
3065 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3066 __ addl(in1_hi, edx);
3067 // in1.lo <- (in1.lo * in2.lo)[31:0];
3068 __ movl(in1_lo, eax);
3069 } else if (second.IsRegisterPair()) {
3070 Register in2_hi = second.AsRegisterPairHigh<Register>();
3071 Register in2_lo = second.AsRegisterPairLow<Register>();
3072
3073 __ movl(eax, in2_hi);
3074 // eax <- in1.lo * in2.hi
3075 __ imull(eax, in1_lo);
3076 // in1.hi <- in1.hi * in2.lo
3077 __ imull(in1_hi, in2_lo);
3078 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3079 __ addl(in1_hi, eax);
3080 // move in1_lo to eax to prepare for double precision
3081 __ movl(eax, in1_lo);
3082 // edx:eax <- in1.lo * in2.lo
3083 __ mull(in2_lo);
3084 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3085 __ addl(in1_hi, edx);
3086 // in1.lo <- (in1.lo * in2.lo)[31:0];
3087 __ movl(in1_lo, eax);
3088 } else {
3089 DCHECK(second.IsDoubleStackSlot()) << second;
3090 Address in2_hi(ESP, second.GetHighStackIndex(kX86WordSize));
3091 Address in2_lo(ESP, second.GetStackIndex());
3092
3093 __ movl(eax, in2_hi);
3094 // eax <- in1.lo * in2.hi
3095 __ imull(eax, in1_lo);
3096 // in1.hi <- in1.hi * in2.lo
3097 __ imull(in1_hi, in2_lo);
3098 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3099 __ addl(in1_hi, eax);
3100 // move in1_lo to eax to prepare for double precision
3101 __ movl(eax, in1_lo);
3102 // edx:eax <- in1.lo * in2.lo
3103 __ mull(in2_lo);
3104 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3105 __ addl(in1_hi, edx);
3106 // in1.lo <- (in1.lo * in2.lo)[31:0];
3107 __ movl(in1_lo, eax);
3108 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003109
3110 break;
3111 }
3112
Calin Juravleb5bfa962014-10-21 18:02:24 +01003113 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003114 DCHECK(first.Equals(locations->Out()));
3115 if (second.IsFpuRegister()) {
3116 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3117 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3118 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003119 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003120 __ mulss(first.AsFpuRegister<XmmRegister>(),
3121 codegen_->LiteralFloatAddress(
3122 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3123 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3124 } else {
3125 DCHECK(second.IsStackSlot());
3126 __ mulss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3127 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003128 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003129 }
3130
3131 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003132 DCHECK(first.Equals(locations->Out()));
3133 if (second.IsFpuRegister()) {
3134 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3135 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3136 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003137 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003138 __ mulsd(first.AsFpuRegister<XmmRegister>(),
3139 codegen_->LiteralDoubleAddress(
3140 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3141 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3142 } else {
3143 DCHECK(second.IsDoubleStackSlot());
3144 __ mulsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3145 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003146 break;
3147 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003148
3149 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003150 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003151 }
3152}
3153
Roland Levillain232ade02015-04-20 15:14:36 +01003154void InstructionCodeGeneratorX86::PushOntoFPStack(Location source,
3155 uint32_t temp_offset,
3156 uint32_t stack_adjustment,
3157 bool is_fp,
3158 bool is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003159 if (source.IsStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01003160 DCHECK(!is_wide);
3161 if (is_fp) {
3162 __ flds(Address(ESP, source.GetStackIndex() + stack_adjustment));
3163 } else {
3164 __ filds(Address(ESP, source.GetStackIndex() + stack_adjustment));
3165 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003166 } else if (source.IsDoubleStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01003167 DCHECK(is_wide);
3168 if (is_fp) {
3169 __ fldl(Address(ESP, source.GetStackIndex() + stack_adjustment));
3170 } else {
3171 __ fildl(Address(ESP, source.GetStackIndex() + stack_adjustment));
3172 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003173 } else {
3174 // Write the value to the temporary location on the stack and load to FP stack.
Roland Levillain232ade02015-04-20 15:14:36 +01003175 if (!is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003176 Location stack_temp = Location::StackSlot(temp_offset);
3177 codegen_->Move32(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01003178 if (is_fp) {
3179 __ flds(Address(ESP, temp_offset));
3180 } else {
3181 __ filds(Address(ESP, temp_offset));
3182 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003183 } else {
3184 Location stack_temp = Location::DoubleStackSlot(temp_offset);
3185 codegen_->Move64(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01003186 if (is_fp) {
3187 __ fldl(Address(ESP, temp_offset));
3188 } else {
3189 __ fildl(Address(ESP, temp_offset));
3190 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003191 }
3192 }
3193}
3194
3195void InstructionCodeGeneratorX86::GenerateRemFP(HRem *rem) {
3196 Primitive::Type type = rem->GetResultType();
3197 bool is_float = type == Primitive::kPrimFloat;
3198 size_t elem_size = Primitive::ComponentSize(type);
3199 LocationSummary* locations = rem->GetLocations();
3200 Location first = locations->InAt(0);
3201 Location second = locations->InAt(1);
3202 Location out = locations->Out();
3203
3204 // Create stack space for 2 elements.
3205 // TODO: enhance register allocator to ask for stack temporaries.
3206 __ subl(ESP, Immediate(2 * elem_size));
3207
3208 // Load the values to the FP stack in reverse order, using temporaries if needed.
Roland Levillain232ade02015-04-20 15:14:36 +01003209 const bool is_wide = !is_float;
3210 PushOntoFPStack(second, elem_size, 2 * elem_size, /* is_fp */ true, is_wide);
3211 PushOntoFPStack(first, 0, 2 * elem_size, /* is_fp */ true, is_wide);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003212
3213 // Loop doing FPREM until we stabilize.
Mark Mendell0c9497d2015-08-21 09:30:05 -04003214 NearLabel retry;
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003215 __ Bind(&retry);
3216 __ fprem();
3217
3218 // Move FP status to AX.
3219 __ fstsw();
3220
3221 // And see if the argument reduction is complete. This is signaled by the
3222 // C2 FPU flag bit set to 0.
3223 __ andl(EAX, Immediate(kC2ConditionMask));
3224 __ j(kNotEqual, &retry);
3225
3226 // We have settled on the final value. Retrieve it into an XMM register.
3227 // Store FP top of stack to real stack.
3228 if (is_float) {
3229 __ fsts(Address(ESP, 0));
3230 } else {
3231 __ fstl(Address(ESP, 0));
3232 }
3233
3234 // Pop the 2 items from the FP stack.
3235 __ fucompp();
3236
3237 // Load the value from the stack into an XMM register.
3238 DCHECK(out.IsFpuRegister()) << out;
3239 if (is_float) {
3240 __ movss(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
3241 } else {
3242 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
3243 }
3244
3245 // And remove the temporary stack space we allocated.
3246 __ addl(ESP, Immediate(2 * elem_size));
3247}
3248
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003249
3250void InstructionCodeGeneratorX86::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3251 DCHECK(instruction->IsDiv() || instruction->IsRem());
3252
3253 LocationSummary* locations = instruction->GetLocations();
3254 DCHECK(locations->InAt(1).IsConstant());
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003255 DCHECK(locations->InAt(1).GetConstant()->IsIntConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003256
3257 Register out_register = locations->Out().AsRegister<Register>();
3258 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003259 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003260
3261 DCHECK(imm == 1 || imm == -1);
3262
3263 if (instruction->IsRem()) {
3264 __ xorl(out_register, out_register);
3265 } else {
3266 __ movl(out_register, input_register);
3267 if (imm == -1) {
3268 __ negl(out_register);
3269 }
3270 }
3271}
3272
3273
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003274void InstructionCodeGeneratorX86::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003275 LocationSummary* locations = instruction->GetLocations();
3276
3277 Register out_register = locations->Out().AsRegister<Register>();
3278 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003279 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003280 DCHECK(IsPowerOfTwo(AbsOrMin(imm)));
3281 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003282
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003283 Register num = locations->GetTemp(0).AsRegister<Register>();
3284
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003285 __ leal(num, Address(input_register, abs_imm - 1));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003286 __ testl(input_register, input_register);
3287 __ cmovl(kGreaterEqual, num, input_register);
3288 int shift = CTZ(imm);
3289 __ sarl(num, Immediate(shift));
3290
3291 if (imm < 0) {
3292 __ negl(num);
3293 }
3294
3295 __ movl(out_register, num);
3296}
3297
3298void InstructionCodeGeneratorX86::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3299 DCHECK(instruction->IsDiv() || instruction->IsRem());
3300
3301 LocationSummary* locations = instruction->GetLocations();
3302 int imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
3303
3304 Register eax = locations->InAt(0).AsRegister<Register>();
3305 Register out = locations->Out().AsRegister<Register>();
3306 Register num;
3307 Register edx;
3308
3309 if (instruction->IsDiv()) {
3310 edx = locations->GetTemp(0).AsRegister<Register>();
3311 num = locations->GetTemp(1).AsRegister<Register>();
3312 } else {
3313 edx = locations->Out().AsRegister<Register>();
3314 num = locations->GetTemp(0).AsRegister<Register>();
3315 }
3316
3317 DCHECK_EQ(EAX, eax);
3318 DCHECK_EQ(EDX, edx);
3319 if (instruction->IsDiv()) {
3320 DCHECK_EQ(EAX, out);
3321 } else {
3322 DCHECK_EQ(EDX, out);
3323 }
3324
3325 int64_t magic;
3326 int shift;
3327 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
3328
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003329 // Save the numerator.
3330 __ movl(num, eax);
3331
3332 // EAX = magic
3333 __ movl(eax, Immediate(magic));
3334
3335 // EDX:EAX = magic * numerator
3336 __ imull(num);
3337
3338 if (imm > 0 && magic < 0) {
3339 // EDX += num
3340 __ addl(edx, num);
3341 } else if (imm < 0 && magic > 0) {
3342 __ subl(edx, num);
3343 }
3344
3345 // Shift if needed.
3346 if (shift != 0) {
3347 __ sarl(edx, Immediate(shift));
3348 }
3349
3350 // EDX += 1 if EDX < 0
3351 __ movl(eax, edx);
3352 __ shrl(edx, Immediate(31));
3353 __ addl(edx, eax);
3354
3355 if (instruction->IsRem()) {
3356 __ movl(eax, num);
3357 __ imull(edx, Immediate(imm));
3358 __ subl(eax, edx);
3359 __ movl(edx, eax);
3360 } else {
3361 __ movl(eax, edx);
3362 }
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003363}
3364
Calin Juravlebacfec32014-11-14 15:54:36 +00003365void InstructionCodeGeneratorX86::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3366 DCHECK(instruction->IsDiv() || instruction->IsRem());
3367
3368 LocationSummary* locations = instruction->GetLocations();
3369 Location out = locations->Out();
3370 Location first = locations->InAt(0);
3371 Location second = locations->InAt(1);
3372 bool is_div = instruction->IsDiv();
3373
3374 switch (instruction->GetResultType()) {
3375 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003376 DCHECK_EQ(EAX, first.AsRegister<Register>());
3377 DCHECK_EQ(is_div ? EAX : EDX, out.AsRegister<Register>());
Calin Juravlebacfec32014-11-14 15:54:36 +00003378
Vladimir Marko13c86fd2015-11-11 12:37:46 +00003379 if (second.IsConstant()) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003380 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003381
3382 if (imm == 0) {
3383 // Do not generate anything for 0. DivZeroCheck would forbid any generated code.
3384 } else if (imm == 1 || imm == -1) {
3385 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003386 } else if (is_div && IsPowerOfTwo(AbsOrMin(imm))) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003387 DivByPowerOfTwo(instruction->AsDiv());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003388 } else {
3389 DCHECK(imm <= -2 || imm >= 2);
3390 GenerateDivRemWithAnyConstant(instruction);
3391 }
3392 } else {
David Srbecky9cd6d372016-02-09 15:24:47 +00003393 SlowPathCode* slow_path = new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86(
3394 instruction, out.AsRegister<Register>(), is_div);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003395 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00003396
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003397 Register second_reg = second.AsRegister<Register>();
3398 // 0x80000000/-1 triggers an arithmetic exception!
3399 // Dividing by -1 is actually negation and -0x800000000 = 0x80000000 so
3400 // it's safe to just use negl instead of more complex comparisons.
Calin Juravlebacfec32014-11-14 15:54:36 +00003401
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003402 __ cmpl(second_reg, Immediate(-1));
3403 __ j(kEqual, slow_path->GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +00003404
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003405 // edx:eax <- sign-extended of eax
3406 __ cdq();
3407 // eax = quotient, edx = remainder
3408 __ idivl(second_reg);
3409 __ Bind(slow_path->GetExitLabel());
3410 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003411 break;
3412 }
3413
3414 case Primitive::kPrimLong: {
3415 InvokeRuntimeCallingConvention calling_convention;
3416 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
3417 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
3418 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
3419 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
3420 DCHECK_EQ(EAX, out.AsRegisterPairLow<Register>());
3421 DCHECK_EQ(EDX, out.AsRegisterPairHigh<Register>());
3422
3423 if (is_div) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01003424 codegen_->InvokeRuntime(kQuickLdiv, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003425 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003426 } else {
Serban Constantinescuba45db02016-07-12 22:53:02 +01003427 codegen_->InvokeRuntime(kQuickLmod, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003428 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003429 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003430 break;
3431 }
3432
3433 default:
3434 LOG(FATAL) << "Unexpected type for GenerateDivRemIntegral " << instruction->GetResultType();
3435 }
3436}
3437
Calin Juravle7c4954d2014-10-28 16:57:40 +00003438void LocationsBuilderX86::VisitDiv(HDiv* div) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003439 LocationSummary::CallKind call_kind = (div->GetResultType() == Primitive::kPrimLong)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003440 ? LocationSummary::kCallOnMainOnly
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003441 : LocationSummary::kNoCall;
3442 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
3443
Calin Juravle7c4954d2014-10-28 16:57:40 +00003444 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00003445 case Primitive::kPrimInt: {
3446 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003447 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00003448 locations->SetOut(Location::SameAsFirstInput());
3449 // Intel uses edx:eax as the dividend.
3450 locations->AddTemp(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003451 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3452 // which enforces results to be in EAX and EDX, things are simpler if we use EAX also as
3453 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003454 if (div->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003455 locations->AddTemp(Location::RequiresRegister());
3456 }
Calin Juravled0d48522014-11-04 16:40:20 +00003457 break;
3458 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003459 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003460 InvokeRuntimeCallingConvention calling_convention;
3461 locations->SetInAt(0, Location::RegisterPairLocation(
3462 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3463 locations->SetInAt(1, Location::RegisterPairLocation(
3464 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3465 // Runtime helper puts the result in EAX, EDX.
3466 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Calin Juravle7c4954d2014-10-28 16:57:40 +00003467 break;
3468 }
3469 case Primitive::kPrimFloat:
3470 case Primitive::kPrimDouble: {
3471 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003472 if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3473 DCHECK(div->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003474 } else if (div->InputAt(1)->IsConstant()) {
3475 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003476 } else {
3477 locations->SetInAt(1, Location::Any());
3478 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003479 locations->SetOut(Location::SameAsFirstInput());
3480 break;
3481 }
3482
3483 default:
3484 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3485 }
3486}
3487
3488void InstructionCodeGeneratorX86::VisitDiv(HDiv* div) {
3489 LocationSummary* locations = div->GetLocations();
3490 Location first = locations->InAt(0);
3491 Location second = locations->InAt(1);
Calin Juravle7c4954d2014-10-28 16:57:40 +00003492
3493 switch (div->GetResultType()) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003494 case Primitive::kPrimInt:
Calin Juravle7c4954d2014-10-28 16:57:40 +00003495 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003496 GenerateDivRemIntegral(div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00003497 break;
3498 }
3499
3500 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003501 if (second.IsFpuRegister()) {
3502 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3503 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3504 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003505 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003506 __ divss(first.AsFpuRegister<XmmRegister>(),
3507 codegen_->LiteralFloatAddress(
3508 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3509 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3510 } else {
3511 DCHECK(second.IsStackSlot());
3512 __ divss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3513 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003514 break;
3515 }
3516
3517 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003518 if (second.IsFpuRegister()) {
3519 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3520 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3521 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003522 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003523 __ divsd(first.AsFpuRegister<XmmRegister>(),
3524 codegen_->LiteralDoubleAddress(
3525 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3526 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3527 } else {
3528 DCHECK(second.IsDoubleStackSlot());
3529 __ divsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3530 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003531 break;
3532 }
3533
3534 default:
3535 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3536 }
3537}
3538
Calin Juravlebacfec32014-11-14 15:54:36 +00003539void LocationsBuilderX86::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00003540 Primitive::Type type = rem->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003541
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003542 LocationSummary::CallKind call_kind = (rem->GetResultType() == Primitive::kPrimLong)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003543 ? LocationSummary::kCallOnMainOnly
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003544 : LocationSummary::kNoCall;
3545 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
Calin Juravlebacfec32014-11-14 15:54:36 +00003546
Calin Juravled2ec87d2014-12-08 14:24:46 +00003547 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003548 case Primitive::kPrimInt: {
3549 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003550 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00003551 locations->SetOut(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003552 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3553 // which enforces results to be in EAX and EDX, things are simpler if we use EDX also as
3554 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003555 if (rem->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003556 locations->AddTemp(Location::RequiresRegister());
3557 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003558 break;
3559 }
3560 case Primitive::kPrimLong: {
3561 InvokeRuntimeCallingConvention calling_convention;
3562 locations->SetInAt(0, Location::RegisterPairLocation(
3563 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3564 locations->SetInAt(1, Location::RegisterPairLocation(
3565 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3566 // Runtime helper puts the result in EAX, EDX.
3567 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
3568 break;
3569 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003570 case Primitive::kPrimDouble:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003571 case Primitive::kPrimFloat: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003572 locations->SetInAt(0, Location::Any());
3573 locations->SetInAt(1, Location::Any());
3574 locations->SetOut(Location::RequiresFpuRegister());
3575 locations->AddTemp(Location::RegisterLocation(EAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00003576 break;
3577 }
3578
3579 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003580 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003581 }
3582}
3583
3584void InstructionCodeGeneratorX86::VisitRem(HRem* rem) {
3585 Primitive::Type type = rem->GetResultType();
3586 switch (type) {
3587 case Primitive::kPrimInt:
3588 case Primitive::kPrimLong: {
3589 GenerateDivRemIntegral(rem);
3590 break;
3591 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003592 case Primitive::kPrimFloat:
Calin Juravlebacfec32014-11-14 15:54:36 +00003593 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003594 GenerateRemFP(rem);
Calin Juravlebacfec32014-11-14 15:54:36 +00003595 break;
3596 }
3597 default:
3598 LOG(FATAL) << "Unexpected rem type " << type;
3599 }
3600}
3601
Calin Juravled0d48522014-11-04 16:40:20 +00003602void LocationsBuilderX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00003603 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
3604 ? LocationSummary::kCallOnSlowPath
3605 : LocationSummary::kNoCall;
3606 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003607 switch (instruction->GetType()) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003608 case Primitive::kPrimBoolean:
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003609 case Primitive::kPrimByte:
3610 case Primitive::kPrimChar:
3611 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003612 case Primitive::kPrimInt: {
3613 locations->SetInAt(0, Location::Any());
3614 break;
3615 }
3616 case Primitive::kPrimLong: {
3617 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
3618 if (!instruction->IsConstant()) {
3619 locations->AddTemp(Location::RequiresRegister());
3620 }
3621 break;
3622 }
3623 default:
3624 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
3625 }
Calin Juravled0d48522014-11-04 16:40:20 +00003626 if (instruction->HasUses()) {
3627 locations->SetOut(Location::SameAsFirstInput());
3628 }
3629}
3630
3631void InstructionCodeGeneratorX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003632 SlowPathCode* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86(instruction);
Calin Juravled0d48522014-11-04 16:40:20 +00003633 codegen_->AddSlowPath(slow_path);
3634
3635 LocationSummary* locations = instruction->GetLocations();
3636 Location value = locations->InAt(0);
3637
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003638 switch (instruction->GetType()) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003639 case Primitive::kPrimBoolean:
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003640 case Primitive::kPrimByte:
3641 case Primitive::kPrimChar:
3642 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003643 case Primitive::kPrimInt: {
3644 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003645 __ testl(value.AsRegister<Register>(), value.AsRegister<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003646 __ j(kEqual, slow_path->GetEntryLabel());
3647 } else if (value.IsStackSlot()) {
3648 __ cmpl(Address(ESP, value.GetStackIndex()), Immediate(0));
3649 __ j(kEqual, slow_path->GetEntryLabel());
3650 } else {
3651 DCHECK(value.IsConstant()) << value;
3652 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
3653 __ jmp(slow_path->GetEntryLabel());
3654 }
3655 }
3656 break;
Calin Juravled0d48522014-11-04 16:40:20 +00003657 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003658 case Primitive::kPrimLong: {
3659 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003660 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003661 __ movl(temp, value.AsRegisterPairLow<Register>());
3662 __ orl(temp, value.AsRegisterPairHigh<Register>());
3663 __ j(kEqual, slow_path->GetEntryLabel());
3664 } else {
3665 DCHECK(value.IsConstant()) << value;
3666 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
3667 __ jmp(slow_path->GetEntryLabel());
3668 }
3669 }
3670 break;
3671 }
3672 default:
3673 LOG(FATAL) << "Unexpected type for HDivZeroCheck" << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00003674 }
Calin Juravled0d48522014-11-04 16:40:20 +00003675}
3676
Calin Juravle9aec02f2014-11-18 23:06:35 +00003677void LocationsBuilderX86::HandleShift(HBinaryOperation* op) {
3678 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3679
3680 LocationSummary* locations =
3681 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
3682
3683 switch (op->GetResultType()) {
Mark P Mendell73945692015-04-29 14:56:17 +00003684 case Primitive::kPrimInt:
Calin Juravle9aec02f2014-11-18 23:06:35 +00003685 case Primitive::kPrimLong: {
Mark P Mendell73945692015-04-29 14:56:17 +00003686 // Can't have Location::Any() and output SameAsFirstInput()
Calin Juravle9aec02f2014-11-18 23:06:35 +00003687 locations->SetInAt(0, Location::RequiresRegister());
Mark P Mendell73945692015-04-29 14:56:17 +00003688 // The shift count needs to be in CL or a constant.
3689 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, op->InputAt(1)));
Calin Juravle9aec02f2014-11-18 23:06:35 +00003690 locations->SetOut(Location::SameAsFirstInput());
3691 break;
3692 }
3693 default:
3694 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
3695 }
3696}
3697
3698void InstructionCodeGeneratorX86::HandleShift(HBinaryOperation* op) {
3699 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3700
3701 LocationSummary* locations = op->GetLocations();
3702 Location first = locations->InAt(0);
3703 Location second = locations->InAt(1);
3704 DCHECK(first.Equals(locations->Out()));
3705
3706 switch (op->GetResultType()) {
3707 case Primitive::kPrimInt: {
Mark P Mendell73945692015-04-29 14:56:17 +00003708 DCHECK(first.IsRegister());
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003709 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003710 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003711 Register second_reg = second.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003712 DCHECK_EQ(ECX, second_reg);
3713 if (op->IsShl()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003714 __ shll(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003715 } else if (op->IsShr()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003716 __ sarl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003717 } else {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003718 __ shrl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003719 }
3720 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003721 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance;
Mark P Mendell73945692015-04-29 14:56:17 +00003722 if (shift == 0) {
3723 return;
3724 }
3725 Immediate imm(shift);
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003726 if (op->IsShl()) {
3727 __ shll(first_reg, imm);
3728 } else if (op->IsShr()) {
3729 __ sarl(first_reg, imm);
3730 } else {
3731 __ shrl(first_reg, imm);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003732 }
3733 }
3734 break;
3735 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003736 case Primitive::kPrimLong: {
Mark P Mendell73945692015-04-29 14:56:17 +00003737 if (second.IsRegister()) {
3738 Register second_reg = second.AsRegister<Register>();
3739 DCHECK_EQ(ECX, second_reg);
3740 if (op->IsShl()) {
3741 GenerateShlLong(first, second_reg);
3742 } else if (op->IsShr()) {
3743 GenerateShrLong(first, second_reg);
3744 } else {
3745 GenerateUShrLong(first, second_reg);
3746 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003747 } else {
Mark P Mendell73945692015-04-29 14:56:17 +00003748 // Shift by a constant.
Roland Levillain5b5b9312016-03-22 14:57:31 +00003749 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance;
Mark P Mendell73945692015-04-29 14:56:17 +00003750 // Nothing to do if the shift is 0, as the input is already the output.
3751 if (shift != 0) {
3752 if (op->IsShl()) {
3753 GenerateShlLong(first, shift);
3754 } else if (op->IsShr()) {
3755 GenerateShrLong(first, shift);
3756 } else {
3757 GenerateUShrLong(first, shift);
3758 }
3759 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003760 }
3761 break;
3762 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003763 default:
3764 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
3765 }
3766}
3767
Mark P Mendell73945692015-04-29 14:56:17 +00003768void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, int shift) {
3769 Register low = loc.AsRegisterPairLow<Register>();
3770 Register high = loc.AsRegisterPairHigh<Register>();
Mark Mendellba56d062015-05-05 21:34:03 -04003771 if (shift == 1) {
3772 // This is just an addition.
3773 __ addl(low, low);
3774 __ adcl(high, high);
3775 } else if (shift == 32) {
Mark P Mendell73945692015-04-29 14:56:17 +00003776 // Shift by 32 is easy. High gets low, and low gets 0.
3777 codegen_->EmitParallelMoves(
3778 loc.ToLow(),
3779 loc.ToHigh(),
3780 Primitive::kPrimInt,
3781 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
3782 loc.ToLow(),
3783 Primitive::kPrimInt);
3784 } else if (shift > 32) {
3785 // Low part becomes 0. High part is low part << (shift-32).
3786 __ movl(high, low);
3787 __ shll(high, Immediate(shift - 32));
3788 __ xorl(low, low);
3789 } else {
3790 // Between 1 and 31.
3791 __ shld(high, low, Immediate(shift));
3792 __ shll(low, Immediate(shift));
3793 }
3794}
3795
Calin Juravle9aec02f2014-11-18 23:06:35 +00003796void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04003797 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00003798 __ shld(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>(), shifter);
3799 __ shll(loc.AsRegisterPairLow<Register>(), shifter);
3800 __ testl(shifter, Immediate(32));
3801 __ j(kEqual, &done);
3802 __ movl(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>());
3803 __ movl(loc.AsRegisterPairLow<Register>(), Immediate(0));
3804 __ Bind(&done);
3805}
3806
Mark P Mendell73945692015-04-29 14:56:17 +00003807void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, int shift) {
3808 Register low = loc.AsRegisterPairLow<Register>();
3809 Register high = loc.AsRegisterPairHigh<Register>();
3810 if (shift == 32) {
3811 // Need to copy the sign.
3812 DCHECK_NE(low, high);
3813 __ movl(low, high);
3814 __ sarl(high, Immediate(31));
3815 } else if (shift > 32) {
3816 DCHECK_NE(low, high);
3817 // High part becomes sign. Low part is shifted by shift - 32.
3818 __ movl(low, high);
3819 __ sarl(high, Immediate(31));
3820 __ sarl(low, Immediate(shift - 32));
3821 } else {
3822 // Between 1 and 31.
3823 __ shrd(low, high, Immediate(shift));
3824 __ sarl(high, Immediate(shift));
3825 }
3826}
3827
Calin Juravle9aec02f2014-11-18 23:06:35 +00003828void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04003829 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00003830 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
3831 __ sarl(loc.AsRegisterPairHigh<Register>(), shifter);
3832 __ testl(shifter, Immediate(32));
3833 __ j(kEqual, &done);
3834 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
3835 __ sarl(loc.AsRegisterPairHigh<Register>(), Immediate(31));
3836 __ Bind(&done);
3837}
3838
Mark P Mendell73945692015-04-29 14:56:17 +00003839void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, int shift) {
3840 Register low = loc.AsRegisterPairLow<Register>();
3841 Register high = loc.AsRegisterPairHigh<Register>();
3842 if (shift == 32) {
3843 // Shift by 32 is easy. Low gets high, and high gets 0.
3844 codegen_->EmitParallelMoves(
3845 loc.ToHigh(),
3846 loc.ToLow(),
3847 Primitive::kPrimInt,
3848 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
3849 loc.ToHigh(),
3850 Primitive::kPrimInt);
3851 } else if (shift > 32) {
3852 // Low part is high >> (shift - 32). High part becomes 0.
3853 __ movl(low, high);
3854 __ shrl(low, Immediate(shift - 32));
3855 __ xorl(high, high);
3856 } else {
3857 // Between 1 and 31.
3858 __ shrd(low, high, Immediate(shift));
3859 __ shrl(high, Immediate(shift));
3860 }
3861}
3862
Calin Juravle9aec02f2014-11-18 23:06:35 +00003863void InstructionCodeGeneratorX86::GenerateUShrLong(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 __ shrl(loc.AsRegisterPairHigh<Register>(), shifter);
3867 __ testl(shifter, Immediate(32));
3868 __ j(kEqual, &done);
3869 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
3870 __ movl(loc.AsRegisterPairHigh<Register>(), Immediate(0));
3871 __ Bind(&done);
3872}
3873
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003874void LocationsBuilderX86::VisitRor(HRor* ror) {
3875 LocationSummary* locations =
3876 new (GetGraph()->GetArena()) LocationSummary(ror, LocationSummary::kNoCall);
3877
3878 switch (ror->GetResultType()) {
3879 case Primitive::kPrimLong:
3880 // Add the temporary needed.
3881 locations->AddTemp(Location::RequiresRegister());
3882 FALLTHROUGH_INTENDED;
3883 case Primitive::kPrimInt:
3884 locations->SetInAt(0, Location::RequiresRegister());
3885 // The shift count needs to be in CL (unless it is a constant).
3886 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, ror->InputAt(1)));
3887 locations->SetOut(Location::SameAsFirstInput());
3888 break;
3889 default:
3890 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
3891 UNREACHABLE();
3892 }
3893}
3894
3895void InstructionCodeGeneratorX86::VisitRor(HRor* ror) {
3896 LocationSummary* locations = ror->GetLocations();
3897 Location first = locations->InAt(0);
3898 Location second = locations->InAt(1);
3899
3900 if (ror->GetResultType() == Primitive::kPrimInt) {
3901 Register first_reg = first.AsRegister<Register>();
3902 if (second.IsRegister()) {
3903 Register second_reg = second.AsRegister<Register>();
3904 __ rorl(first_reg, second_reg);
3905 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003906 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003907 __ rorl(first_reg, imm);
3908 }
3909 return;
3910 }
3911
3912 DCHECK_EQ(ror->GetResultType(), Primitive::kPrimLong);
3913 Register first_reg_lo = first.AsRegisterPairLow<Register>();
3914 Register first_reg_hi = first.AsRegisterPairHigh<Register>();
3915 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
3916 if (second.IsRegister()) {
3917 Register second_reg = second.AsRegister<Register>();
3918 DCHECK_EQ(second_reg, ECX);
3919 __ movl(temp_reg, first_reg_hi);
3920 __ shrd(first_reg_hi, first_reg_lo, second_reg);
3921 __ shrd(first_reg_lo, temp_reg, second_reg);
3922 __ movl(temp_reg, first_reg_hi);
3923 __ testl(second_reg, Immediate(32));
3924 __ cmovl(kNotEqual, first_reg_hi, first_reg_lo);
3925 __ cmovl(kNotEqual, first_reg_lo, temp_reg);
3926 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003927 int32_t shift_amt = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance;
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003928 if (shift_amt == 0) {
3929 // Already fine.
3930 return;
3931 }
3932 if (shift_amt == 32) {
3933 // Just swap.
3934 __ movl(temp_reg, first_reg_lo);
3935 __ movl(first_reg_lo, first_reg_hi);
3936 __ movl(first_reg_hi, temp_reg);
3937 return;
3938 }
3939
3940 Immediate imm(shift_amt);
3941 // Save the constents of the low value.
3942 __ movl(temp_reg, first_reg_lo);
3943
3944 // Shift right into low, feeding bits from high.
3945 __ shrd(first_reg_lo, first_reg_hi, imm);
3946
3947 // Shift right into high, feeding bits from the original low.
3948 __ shrd(first_reg_hi, temp_reg, imm);
3949
3950 // Swap if needed.
3951 if (shift_amt > 32) {
3952 __ movl(temp_reg, first_reg_lo);
3953 __ movl(first_reg_lo, first_reg_hi);
3954 __ movl(first_reg_hi, temp_reg);
3955 }
3956 }
3957}
3958
Calin Juravle9aec02f2014-11-18 23:06:35 +00003959void LocationsBuilderX86::VisitShl(HShl* shl) {
3960 HandleShift(shl);
3961}
3962
3963void InstructionCodeGeneratorX86::VisitShl(HShl* shl) {
3964 HandleShift(shl);
3965}
3966
3967void LocationsBuilderX86::VisitShr(HShr* shr) {
3968 HandleShift(shr);
3969}
3970
3971void InstructionCodeGeneratorX86::VisitShr(HShr* shr) {
3972 HandleShift(shr);
3973}
3974
3975void LocationsBuilderX86::VisitUShr(HUShr* ushr) {
3976 HandleShift(ushr);
3977}
3978
3979void InstructionCodeGeneratorX86::VisitUShr(HUShr* ushr) {
3980 HandleShift(ushr);
3981}
3982
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003983void LocationsBuilderX86::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003984 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003985 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003986 locations->SetOut(Location::RegisterLocation(EAX));
David Brazdil6de19382016-01-08 17:37:10 +00003987 if (instruction->IsStringAlloc()) {
3988 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
3989 } else {
3990 InvokeRuntimeCallingConvention calling_convention;
3991 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3992 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3993 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003994}
3995
3996void InstructionCodeGeneratorX86::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01003997 // Note: if heap poisoning is enabled, the entry point takes cares
3998 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00003999 if (instruction->IsStringAlloc()) {
4000 // String is allocated through StringFactory. Call NewEmptyString entry point.
4001 Register temp = instruction->GetLocations()->GetTemp(0).AsRegister<Register>();
Andreas Gampe542451c2016-07-26 09:02:02 -07004002 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86PointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00004003 __ fs()->movl(temp, Address::Absolute(QUICK_ENTRY_POINT(pNewEmptyString)));
4004 __ call(Address(temp, code_offset.Int32Value()));
4005 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4006 } else {
Serban Constantinescuba45db02016-07-12 22:53:02 +01004007 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
David Brazdil6de19382016-01-08 17:37:10 +00004008 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
4009 DCHECK(!codegen_->IsLeafMethod());
4010 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004011}
4012
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004013void LocationsBuilderX86::VisitNewArray(HNewArray* instruction) {
4014 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004015 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004016 locations->SetOut(Location::RegisterLocation(EAX));
4017 InvokeRuntimeCallingConvention calling_convention;
4018 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08004019 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004020 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004021}
4022
4023void InstructionCodeGeneratorX86::VisitNewArray(HNewArray* instruction) {
4024 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004025 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
Roland Levillain4d027112015-07-01 15:41:14 +01004026 // Note: if heap poisoning is enabled, the entry point takes cares
4027 // of poisoning the reference.
Serban Constantinescuba45db02016-07-12 22:53:02 +01004028 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00004029 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004030 DCHECK(!codegen_->IsLeafMethod());
4031}
4032
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004033void LocationsBuilderX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004034 LocationSummary* locations =
4035 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01004036 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
4037 if (location.IsStackSlot()) {
4038 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4039 } else if (location.IsDoubleStackSlot()) {
4040 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004041 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01004042 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004043}
4044
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004045void InstructionCodeGeneratorX86::VisitParameterValue(
4046 HParameterValue* instruction ATTRIBUTE_UNUSED) {
4047}
4048
4049void LocationsBuilderX86::VisitCurrentMethod(HCurrentMethod* instruction) {
4050 LocationSummary* locations =
4051 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4052 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
4053}
4054
4055void InstructionCodeGeneratorX86::VisitCurrentMethod(HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004056}
4057
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004058void LocationsBuilderX86::VisitClassTableGet(HClassTableGet* instruction) {
4059 LocationSummary* locations =
4060 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4061 locations->SetInAt(0, Location::RequiresRegister());
4062 locations->SetOut(Location::RequiresRegister());
4063}
4064
4065void InstructionCodeGeneratorX86::VisitClassTableGet(HClassTableGet* instruction) {
4066 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004067 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004068 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004069 instruction->GetIndex(), kX86PointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004070 __ movl(locations->Out().AsRegister<Register>(),
4071 Address(locations->InAt(0).AsRegister<Register>(), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004072 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004073 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00004074 instruction->GetIndex(), kX86PointerSize));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004075 __ movl(locations->Out().AsRegister<Register>(),
4076 Address(locations->InAt(0).AsRegister<Register>(),
4077 mirror::Class::ImtPtrOffset(kX86PointerSize).Uint32Value()));
4078 // temp = temp->GetImtEntryAt(method_offset);
4079 __ movl(locations->Out().AsRegister<Register>(),
4080 Address(locations->Out().AsRegister<Register>(), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004081 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004082}
4083
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004084void LocationsBuilderX86::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004085 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004086 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01004087 locations->SetInAt(0, Location::RequiresRegister());
4088 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004089}
4090
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004091void InstructionCodeGeneratorX86::VisitNot(HNot* not_) {
4092 LocationSummary* locations = not_->GetLocations();
Roland Levillain70566432014-10-24 16:20:17 +01004093 Location in = locations->InAt(0);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01004094 Location out = locations->Out();
Roland Levillain70566432014-10-24 16:20:17 +01004095 DCHECK(in.Equals(out));
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00004096 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004097 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00004098 __ notl(out.AsRegister<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004099 break;
4100
4101 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01004102 __ notl(out.AsRegisterPairLow<Register>());
4103 __ notl(out.AsRegisterPairHigh<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004104 break;
4105
4106 default:
4107 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
4108 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004109}
4110
David Brazdil66d126e2015-04-03 16:02:44 +01004111void LocationsBuilderX86::VisitBooleanNot(HBooleanNot* bool_not) {
4112 LocationSummary* locations =
4113 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
4114 locations->SetInAt(0, Location::RequiresRegister());
4115 locations->SetOut(Location::SameAsFirstInput());
4116}
4117
4118void InstructionCodeGeneratorX86::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01004119 LocationSummary* locations = bool_not->GetLocations();
4120 Location in = locations->InAt(0);
4121 Location out = locations->Out();
4122 DCHECK(in.Equals(out));
4123 __ xorl(out.AsRegister<Register>(), Immediate(1));
4124}
4125
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004126void LocationsBuilderX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004127 LocationSummary* locations =
4128 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00004129 switch (compare->InputAt(0)->GetType()) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00004130 case Primitive::kPrimBoolean:
4131 case Primitive::kPrimByte:
4132 case Primitive::kPrimShort:
4133 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08004134 case Primitive::kPrimInt:
Calin Juravleddb7df22014-11-25 20:56:51 +00004135 case Primitive::kPrimLong: {
4136 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravleddb7df22014-11-25 20:56:51 +00004137 locations->SetInAt(1, Location::Any());
4138 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4139 break;
4140 }
4141 case Primitive::kPrimFloat:
4142 case Primitive::kPrimDouble: {
4143 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004144 if (compare->InputAt(1)->IsX86LoadFromConstantTable()) {
4145 DCHECK(compare->InputAt(1)->IsEmittedAtUseSite());
4146 } else if (compare->InputAt(1)->IsConstant()) {
4147 locations->SetInAt(1, Location::RequiresFpuRegister());
4148 } else {
4149 locations->SetInAt(1, Location::Any());
4150 }
Calin Juravleddb7df22014-11-25 20:56:51 +00004151 locations->SetOut(Location::RequiresRegister());
4152 break;
4153 }
4154 default:
4155 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
4156 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004157}
4158
4159void InstructionCodeGeneratorX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004160 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004161 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00004162 Location left = locations->InAt(0);
4163 Location right = locations->InAt(1);
4164
Mark Mendell0c9497d2015-08-21 09:30:05 -04004165 NearLabel less, greater, done;
Aart Bika19616e2016-02-01 18:57:58 -08004166 Condition less_cond = kLess;
4167
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004168 switch (compare->InputAt(0)->GetType()) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00004169 case Primitive::kPrimBoolean:
4170 case Primitive::kPrimByte:
4171 case Primitive::kPrimShort:
4172 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08004173 case Primitive::kPrimInt: {
Roland Levillain0b671c02016-08-19 12:02:34 +01004174 codegen_->GenerateIntCompare(left, right);
Aart Bika19616e2016-02-01 18:57:58 -08004175 break;
4176 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004177 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004178 Register left_low = left.AsRegisterPairLow<Register>();
4179 Register left_high = left.AsRegisterPairHigh<Register>();
4180 int32_t val_low = 0;
4181 int32_t val_high = 0;
4182 bool right_is_const = false;
4183
4184 if (right.IsConstant()) {
4185 DCHECK(right.GetConstant()->IsLongConstant());
4186 right_is_const = true;
4187 int64_t val = right.GetConstant()->AsLongConstant()->GetValue();
4188 val_low = Low32Bits(val);
4189 val_high = High32Bits(val);
4190 }
4191
Calin Juravleddb7df22014-11-25 20:56:51 +00004192 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004193 __ cmpl(left_high, right.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004194 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004195 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004196 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004197 DCHECK(right_is_const) << right;
Aart Bika19616e2016-02-01 18:57:58 -08004198 codegen_->Compare32BitValue(left_high, val_high);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004199 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004200 __ j(kLess, &less); // Signed compare.
4201 __ j(kGreater, &greater); // Signed compare.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004202 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004203 __ cmpl(left_low, right.AsRegisterPairLow<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004204 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004205 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004206 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004207 DCHECK(right_is_const) << right;
Aart Bika19616e2016-02-01 18:57:58 -08004208 codegen_->Compare32BitValue(left_low, val_low);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004209 }
Aart Bika19616e2016-02-01 18:57:58 -08004210 less_cond = kBelow; // for CF (unsigned).
Calin Juravleddb7df22014-11-25 20:56:51 +00004211 break;
4212 }
4213 case Primitive::kPrimFloat: {
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004214 GenerateFPCompare(left, right, compare, false);
Calin Juravleddb7df22014-11-25 20:56:51 +00004215 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08004216 less_cond = kBelow; // for CF (floats).
Calin Juravleddb7df22014-11-25 20:56:51 +00004217 break;
4218 }
4219 case Primitive::kPrimDouble: {
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004220 GenerateFPCompare(left, right, compare, true);
Calin Juravleddb7df22014-11-25 20:56:51 +00004221 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08004222 less_cond = kBelow; // for CF (floats).
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004223 break;
4224 }
4225 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00004226 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004227 }
Aart Bika19616e2016-02-01 18:57:58 -08004228
Calin Juravleddb7df22014-11-25 20:56:51 +00004229 __ movl(out, Immediate(0));
4230 __ j(kEqual, &done);
Aart Bika19616e2016-02-01 18:57:58 -08004231 __ j(less_cond, &less);
Calin Juravleddb7df22014-11-25 20:56:51 +00004232
4233 __ Bind(&greater);
4234 __ movl(out, Immediate(1));
4235 __ jmp(&done);
4236
4237 __ Bind(&less);
4238 __ movl(out, Immediate(-1));
4239
4240 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004241}
4242
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004243void LocationsBuilderX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004244 LocationSummary* locations =
4245 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko372f10e2016-05-17 16:30:10 +01004246 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01004247 locations->SetInAt(i, Location::Any());
4248 }
4249 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004250}
4251
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004252void InstructionCodeGeneratorX86::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004253 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004254}
4255
Roland Levillain7c1559a2015-12-15 10:55:36 +00004256void CodeGeneratorX86::GenerateMemoryBarrier(MemBarrierKind kind) {
Calin Juravle52c48962014-12-16 17:02:57 +00004257 /*
4258 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
4259 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
4260 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
4261 */
4262 switch (kind) {
4263 case MemBarrierKind::kAnyAny: {
Mark P Mendell17077d82015-12-16 19:15:59 +00004264 MemoryFence();
Calin Juravle52c48962014-12-16 17:02:57 +00004265 break;
4266 }
4267 case MemBarrierKind::kAnyStore:
4268 case MemBarrierKind::kLoadAny:
4269 case MemBarrierKind::kStoreStore: {
4270 // nop
4271 break;
4272 }
Mark Mendell7aa04a12016-01-27 22:39:07 -05004273 case MemBarrierKind::kNTStoreStore:
4274 // Non-Temporal Store/Store needs an explicit fence.
4275 MemoryFence(/* non-temporal */ true);
4276 break;
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004277 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004278}
4279
Vladimir Markodc151b22015-10-15 18:02:30 +01004280HInvokeStaticOrDirect::DispatchInfo CodeGeneratorX86::GetSupportedInvokeStaticOrDirectDispatch(
4281 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
4282 MethodReference target_method ATTRIBUTE_UNUSED) {
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00004283 HInvokeStaticOrDirect::DispatchInfo dispatch_info = desired_dispatch_info;
4284
4285 // We disable pc-relative load when there is an irreducible loop, as the optimization
4286 // is incompatible with it.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004287 // TODO: Create as many X86ComputeBaseMethodAddress instructions
4288 // as needed for methods with irreducible loops.
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00004289 if (GetGraph()->HasIrreducibleLoops() &&
4290 (dispatch_info.method_load_kind ==
4291 HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative)) {
4292 dispatch_info.method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod;
4293 }
4294 switch (dispatch_info.code_ptr_location) {
Vladimir Markodc151b22015-10-15 18:02:30 +01004295 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
4296 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
4297 // For direct code, we actually prefer to call via the code pointer from ArtMethod*.
4298 // (Though the direct CALL ptr16:32 is available for consideration).
4299 return HInvokeStaticOrDirect::DispatchInfo {
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00004300 dispatch_info.method_load_kind,
Vladimir Markodc151b22015-10-15 18:02:30 +01004301 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00004302 dispatch_info.method_load_data,
Vladimir Markodc151b22015-10-15 18:02:30 +01004303 0u
4304 };
4305 default:
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00004306 return dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01004307 }
4308}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004309
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004310Register CodeGeneratorX86::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
4311 Register temp) {
4312 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
Vladimir Markoc53c0792015-11-19 15:48:33 +00004313 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004314 if (!invoke->GetLocations()->Intrinsified()) {
4315 return location.AsRegister<Register>();
4316 }
4317 // For intrinsics we allow any location, so it may be on the stack.
4318 if (!location.IsRegister()) {
4319 __ movl(temp, Address(ESP, location.GetStackIndex()));
4320 return temp;
4321 }
4322 // For register locations, check if the register was saved. If so, get it from the stack.
4323 // Note: There is a chance that the register was saved but not overwritten, so we could
4324 // save one load. However, since this is just an intrinsic slow path we prefer this
4325 // simple and more robust approach rather that trying to determine if that's the case.
4326 SlowPathCode* slow_path = GetCurrentSlowPath();
Serguei Katkov288c7a82016-05-16 11:53:15 +06004327 if (slow_path != nullptr) {
4328 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
4329 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
4330 __ movl(temp, Address(ESP, stack_offset));
4331 return temp;
4332 }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004333 }
4334 return location.AsRegister<Register>();
4335}
4336
Serguei Katkov288c7a82016-05-16 11:53:15 +06004337Location CodeGeneratorX86::GenerateCalleeMethodStaticOrDirectCall(HInvokeStaticOrDirect* invoke,
4338 Location temp) {
Vladimir Marko58155012015-08-19 12:49:41 +00004339 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
4340 switch (invoke->GetMethodLoadKind()) {
4341 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
4342 // temp = thread->string_init_entrypoint
4343 __ fs()->movl(temp.AsRegister<Register>(), Address::Absolute(invoke->GetStringInitOffset()));
4344 break;
4345 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00004346 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004347 break;
4348 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
4349 __ movl(temp.AsRegister<Register>(), Immediate(invoke->GetMethodAddress()));
4350 break;
4351 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004352 __ movl(temp.AsRegister<Register>(), Immediate(/* placeholder */ 0));
Vladimir Marko58155012015-08-19 12:49:41 +00004353 method_patches_.emplace_back(invoke->GetTargetMethod());
4354 __ Bind(&method_patches_.back().label); // Bind the label at the end of the "movl" insn.
4355 break;
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004356 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
4357 Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
4358 temp.AsRegister<Register>());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004359 __ movl(temp.AsRegister<Register>(), Address(base_reg, kDummy32BitOffset));
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004360 // Bind a new fixup label at the end of the "movl" insn.
4361 uint32_t offset = invoke->GetDexCacheArrayOffset();
4362 __ Bind(NewPcRelativeDexCacheArrayPatch(*invoke->GetTargetMethod().dex_file, offset));
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004363 break;
4364 }
Vladimir Marko58155012015-08-19 12:49:41 +00004365 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004366 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004367 Register method_reg;
4368 Register reg = temp.AsRegister<Register>();
4369 if (current_method.IsRegister()) {
4370 method_reg = current_method.AsRegister<Register>();
4371 } else {
David Brazdil58282f42016-01-14 12:45:10 +00004372 DCHECK(invoke->GetLocations()->Intrinsified());
Vladimir Marko58155012015-08-19 12:49:41 +00004373 DCHECK(!current_method.IsValid());
4374 method_reg = reg;
4375 __ movl(reg, Address(ESP, kCurrentMethodStackOffset));
4376 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00004377 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +01004378 __ movl(reg, Address(method_reg,
4379 ArtMethod::DexCacheResolvedMethodsOffset(kX86PointerSize).Int32Value()));
Vladimir Marko40ecb122016-04-06 17:33:41 +01004380 // temp = temp[index_in_cache];
4381 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
4382 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Vladimir Marko58155012015-08-19 12:49:41 +00004383 __ movl(reg, Address(reg, CodeGenerator::GetCachePointerOffset(index_in_cache)));
4384 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +01004385 }
Vladimir Marko58155012015-08-19 12:49:41 +00004386 }
Serguei Katkov288c7a82016-05-16 11:53:15 +06004387 return callee_method;
4388}
4389
4390void CodeGeneratorX86::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
4391 Location callee_method = GenerateCalleeMethodStaticOrDirectCall(invoke, temp);
Vladimir Marko58155012015-08-19 12:49:41 +00004392
4393 switch (invoke->GetCodePtrLocation()) {
4394 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
4395 __ call(GetFrameEntryLabel());
4396 break;
4397 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative: {
4398 relative_call_patches_.emplace_back(invoke->GetTargetMethod());
4399 Label* label = &relative_call_patches_.back().label;
4400 __ call(label); // Bind to the patch label, override at link time.
4401 __ Bind(label); // Bind the label at the end of the "call" insn.
4402 break;
4403 }
4404 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
4405 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
Vladimir Markodc151b22015-10-15 18:02:30 +01004406 // Filtered out by GetSupportedInvokeStaticOrDirectDispatch().
4407 LOG(FATAL) << "Unsupported";
4408 UNREACHABLE();
Vladimir Marko58155012015-08-19 12:49:41 +00004409 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
4410 // (callee_method + offset_of_quick_compiled_code)()
4411 __ call(Address(callee_method.AsRegister<Register>(),
4412 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07004413 kX86PointerSize).Int32Value()));
Vladimir Marko58155012015-08-19 12:49:41 +00004414 break;
Mark Mendell09ed1a32015-03-25 08:30:06 -04004415 }
4416
4417 DCHECK(!IsLeafMethod());
Mark Mendell09ed1a32015-03-25 08:30:06 -04004418}
4419
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004420void CodeGeneratorX86::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_in) {
4421 Register temp = temp_in.AsRegister<Register>();
4422 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
4423 invoke->GetVTableIndex(), kX86PointerSize).Uint32Value();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004424
4425 // Use the calling convention instead of the location of the receiver, as
4426 // intrinsics may have put the receiver in a different register. In the intrinsics
4427 // slow path, the arguments have been moved to the right place, so here we are
4428 // guaranteed that the receiver is the first register of the calling convention.
4429 InvokeDexCallingConvention calling_convention;
4430 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004431 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004432 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004433 __ movl(temp, Address(receiver, class_offset));
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004434 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00004435 // Instead of simply (possibly) unpoisoning `temp` here, we should
4436 // emit a read barrier for the previous class reference load.
4437 // However this is not required in practice, as this is an
4438 // intermediate/temporary reference and because the current
4439 // concurrent copying collector keeps the from-space memory
4440 // intact/accessible until the end of the marking phase (the
4441 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004442 __ MaybeUnpoisonHeapReference(temp);
4443 // temp = temp->GetMethodAt(method_offset);
4444 __ movl(temp, Address(temp, method_offset));
4445 // call temp->GetEntryPoint();
4446 __ call(Address(
Andreas Gampe542451c2016-07-26 09:02:02 -07004447 temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86PointerSize).Int32Value()));
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004448}
4449
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004450void CodeGeneratorX86::RecordSimplePatch() {
4451 if (GetCompilerOptions().GetIncludePatchInformation()) {
4452 simple_patches_.emplace_back();
4453 __ Bind(&simple_patches_.back());
4454 }
4455}
4456
4457void CodeGeneratorX86::RecordStringPatch(HLoadString* load_string) {
4458 string_patches_.emplace_back(load_string->GetDexFile(), load_string->GetStringIndex());
4459 __ Bind(&string_patches_.back().label);
4460}
4461
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004462void CodeGeneratorX86::RecordTypePatch(HLoadClass* load_class) {
4463 type_patches_.emplace_back(load_class->GetDexFile(), load_class->GetTypeIndex());
4464 __ Bind(&type_patches_.back().label);
4465}
4466
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004467Label* CodeGeneratorX86::NewPcRelativeDexCacheArrayPatch(const DexFile& dex_file,
4468 uint32_t element_offset) {
4469 // Add the patch entry and bind its label at the end of the instruction.
4470 pc_relative_dex_cache_patches_.emplace_back(dex_file, element_offset);
4471 return &pc_relative_dex_cache_patches_.back().label;
4472}
4473
Vladimir Marko58155012015-08-19 12:49:41 +00004474void CodeGeneratorX86::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
4475 DCHECK(linker_patches->empty());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004476 size_t size =
4477 method_patches_.size() +
4478 relative_call_patches_.size() +
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004479 pc_relative_dex_cache_patches_.size() +
4480 simple_patches_.size() +
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004481 string_patches_.size() +
4482 type_patches_.size();
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004483 linker_patches->reserve(size);
4484 // The label points to the end of the "movl" insn but the literal offset for method
4485 // patch needs to point to the embedded constant which occupies the last 4 bytes.
4486 constexpr uint32_t kLabelPositionToLiteralOffsetAdjustment = 4u;
Vladimir Marko58155012015-08-19 12:49:41 +00004487 for (const MethodPatchInfo<Label>& info : method_patches_) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004488 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Marko58155012015-08-19 12:49:41 +00004489 linker_patches->push_back(LinkerPatch::MethodPatch(literal_offset,
4490 info.target_method.dex_file,
4491 info.target_method.dex_method_index));
4492 }
4493 for (const MethodPatchInfo<Label>& info : relative_call_patches_) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004494 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Marko58155012015-08-19 12:49:41 +00004495 linker_patches->push_back(LinkerPatch::RelativeCodePatch(literal_offset,
4496 info.target_method.dex_file,
4497 info.target_method.dex_method_index));
4498 }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004499 for (const PcRelativeDexCacheAccessInfo& info : pc_relative_dex_cache_patches_) {
4500 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
4501 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(literal_offset,
4502 &info.target_dex_file,
4503 GetMethodAddressOffset(),
4504 info.element_offset));
4505 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004506 for (const Label& label : simple_patches_) {
4507 uint32_t literal_offset = label.Position() - kLabelPositionToLiteralOffsetAdjustment;
4508 linker_patches->push_back(LinkerPatch::RecordPosition(literal_offset));
4509 }
4510 if (GetCompilerOptions().GetCompilePic()) {
4511 for (const StringPatchInfo<Label>& info : string_patches_) {
4512 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
4513 linker_patches->push_back(LinkerPatch::RelativeStringPatch(literal_offset,
4514 &info.dex_file,
4515 GetMethodAddressOffset(),
4516 info.string_index));
4517 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004518 for (const TypePatchInfo<Label>& info : type_patches_) {
4519 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
4520 linker_patches->push_back(LinkerPatch::RelativeTypePatch(literal_offset,
4521 &info.dex_file,
4522 GetMethodAddressOffset(),
4523 info.type_index));
4524 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004525 } else {
4526 for (const StringPatchInfo<Label>& info : string_patches_) {
4527 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
4528 linker_patches->push_back(LinkerPatch::StringPatch(literal_offset,
4529 &info.dex_file,
4530 info.string_index));
4531 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004532 for (const TypePatchInfo<Label>& info : type_patches_) {
4533 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
4534 linker_patches->push_back(LinkerPatch::TypePatch(literal_offset,
4535 &info.dex_file,
4536 info.type_index));
4537 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004538 }
Vladimir Marko58155012015-08-19 12:49:41 +00004539}
4540
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004541void CodeGeneratorX86::MarkGCCard(Register temp,
4542 Register card,
4543 Register object,
4544 Register value,
4545 bool value_can_be_null) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004546 NearLabel is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004547 if (value_can_be_null) {
4548 __ testl(value, value);
4549 __ j(kEqual, &is_null);
4550 }
Andreas Gampe542451c2016-07-26 09:02:02 -07004551 __ fs()->movl(card, Address::Absolute(Thread::CardTableOffset<kX86PointerSize>().Int32Value()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004552 __ movl(temp, object);
4553 __ shrl(temp, Immediate(gc::accounting::CardTable::kCardShift));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00004554 __ movb(Address(temp, card, TIMES_1, 0),
4555 X86ManagedRegister::FromCpuRegister(card).AsByteRegister());
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004556 if (value_can_be_null) {
4557 __ Bind(&is_null);
4558 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004559}
4560
Calin Juravle52c48962014-12-16 17:02:57 +00004561void LocationsBuilderX86::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
4562 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain0d5a2812015-11-13 10:07:31 +00004563
4564 bool object_field_get_with_read_barrier =
4565 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004566 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00004567 new (GetGraph()->GetArena()) LocationSummary(instruction,
4568 kEmitCompilerReadBarrier ?
4569 LocationSummary::kCallOnSlowPath :
4570 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01004571 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
4572 locations->SetCustomSlowPathCallerSaves(RegisterSet()); // No caller-save registers.
4573 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004574 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004575
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004576 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4577 locations->SetOut(Location::RequiresFpuRegister());
4578 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004579 // The output overlaps in case of long: we don't want the low move
4580 // to overwrite the object's location. Likewise, in the case of
4581 // an object field get with read barriers enabled, we do not want
4582 // the move to overwrite the object's location, as we need it to emit
4583 // the read barrier.
4584 locations->SetOut(
4585 Location::RequiresRegister(),
4586 (object_field_get_with_read_barrier || instruction->GetType() == Primitive::kPrimLong) ?
4587 Location::kOutputOverlap :
4588 Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004589 }
Calin Juravle52c48962014-12-16 17:02:57 +00004590
4591 if (field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong)) {
4592 // Long values can be loaded atomically into an XMM using movsd.
Roland Levillain7c1559a2015-12-15 10:55:36 +00004593 // So we use an XMM register as a temp to achieve atomicity (first
4594 // load the temp into the XMM and then copy the XMM into the
4595 // output, 32 bits at a time).
Calin Juravle52c48962014-12-16 17:02:57 +00004596 locations->AddTemp(Location::RequiresFpuRegister());
4597 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004598}
4599
Calin Juravle52c48962014-12-16 17:02:57 +00004600void InstructionCodeGeneratorX86::HandleFieldGet(HInstruction* instruction,
4601 const FieldInfo& field_info) {
4602 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004603
Calin Juravle52c48962014-12-16 17:02:57 +00004604 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004605 Location base_loc = locations->InAt(0);
4606 Register base = base_loc.AsRegister<Register>();
Calin Juravle52c48962014-12-16 17:02:57 +00004607 Location out = locations->Out();
4608 bool is_volatile = field_info.IsVolatile();
4609 Primitive::Type field_type = field_info.GetFieldType();
4610 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4611
4612 switch (field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004613 case Primitive::kPrimBoolean: {
Calin Juravle52c48962014-12-16 17:02:57 +00004614 __ movzxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004615 break;
4616 }
4617
4618 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00004619 __ movsxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004620 break;
4621 }
4622
4623 case Primitive::kPrimShort: {
Calin Juravle52c48962014-12-16 17:02:57 +00004624 __ movsxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004625 break;
4626 }
4627
4628 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00004629 __ movzxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004630 break;
4631 }
4632
4633 case Primitive::kPrimInt:
Calin Juravle52c48962014-12-16 17:02:57 +00004634 __ movl(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004635 break;
Roland Levillain7c1559a2015-12-15 10:55:36 +00004636
4637 case Primitive::kPrimNot: {
4638 // /* HeapReference<Object> */ out = *(base + offset)
4639 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004640 // Note that a potential implicit null check is handled in this
4641 // CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier call.
4642 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00004643 instruction, out, base, offset, /* needs_null_check */ true);
Roland Levillain7c1559a2015-12-15 10:55:36 +00004644 if (is_volatile) {
4645 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4646 }
4647 } else {
4648 __ movl(out.AsRegister<Register>(), Address(base, offset));
4649 codegen_->MaybeRecordImplicitNullCheck(instruction);
4650 if (is_volatile) {
4651 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4652 }
4653 // If read barriers are enabled, emit read barriers other than
4654 // Baker's using a slow path (and also unpoison the loaded
4655 // reference, if heap poisoning is enabled).
4656 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
4657 }
4658 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004659 }
4660
4661 case Primitive::kPrimLong: {
Calin Juravle52c48962014-12-16 17:02:57 +00004662 if (is_volatile) {
4663 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4664 __ movsd(temp, Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004665 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004666 __ movd(out.AsRegisterPairLow<Register>(), temp);
4667 __ psrlq(temp, Immediate(32));
4668 __ movd(out.AsRegisterPairHigh<Register>(), temp);
4669 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004670 DCHECK_NE(base, out.AsRegisterPairLow<Register>());
Calin Juravle52c48962014-12-16 17:02:57 +00004671 __ movl(out.AsRegisterPairLow<Register>(), Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004672 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004673 __ movl(out.AsRegisterPairHigh<Register>(), Address(base, kX86WordSize + offset));
4674 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004675 break;
4676 }
4677
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004678 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00004679 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004680 break;
4681 }
4682
4683 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00004684 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004685 break;
4686 }
4687
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004688 case Primitive::kPrimVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00004689 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004690 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004691 }
Calin Juravle52c48962014-12-16 17:02:57 +00004692
Roland Levillain7c1559a2015-12-15 10:55:36 +00004693 if (field_type == Primitive::kPrimNot || field_type == Primitive::kPrimLong) {
4694 // Potential implicit null checks, in the case of reference or
4695 // long fields, are handled in the previous switch statement.
4696 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00004697 codegen_->MaybeRecordImplicitNullCheck(instruction);
4698 }
4699
Calin Juravle52c48962014-12-16 17:02:57 +00004700 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004701 if (field_type == Primitive::kPrimNot) {
4702 // Memory barriers, in the case of references, are also handled
4703 // in the previous switch statement.
4704 } else {
4705 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4706 }
Roland Levillain4d027112015-07-01 15:41:14 +01004707 }
Calin Juravle52c48962014-12-16 17:02:57 +00004708}
4709
4710void LocationsBuilderX86::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
4711 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4712
4713 LocationSummary* locations =
4714 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4715 locations->SetInAt(0, Location::RequiresRegister());
4716 bool is_volatile = field_info.IsVolatile();
4717 Primitive::Type field_type = field_info.GetFieldType();
4718 bool is_byte_type = (field_type == Primitive::kPrimBoolean)
4719 || (field_type == Primitive::kPrimByte);
4720
4721 // The register allocator does not support multiple
4722 // inputs that die at entry with one in a specific register.
4723 if (is_byte_type) {
4724 // Ensure the value is in a byte register.
4725 locations->SetInAt(1, Location::RegisterLocation(EAX));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004726 } else if (Primitive::IsFloatingPointType(field_type)) {
Mark Mendell81489372015-11-04 11:30:41 -05004727 if (is_volatile && field_type == Primitive::kPrimDouble) {
4728 // In order to satisfy the semantics of volatile, this must be a single instruction store.
4729 locations->SetInAt(1, Location::RequiresFpuRegister());
4730 } else {
4731 locations->SetInAt(1, Location::FpuRegisterOrConstant(instruction->InputAt(1)));
4732 }
4733 } else if (is_volatile && field_type == Primitive::kPrimLong) {
4734 // In order to satisfy the semantics of volatile, this must be a single instruction store.
Calin Juravle52c48962014-12-16 17:02:57 +00004735 locations->SetInAt(1, Location::RequiresRegister());
Mark Mendell81489372015-11-04 11:30:41 -05004736
Calin Juravle52c48962014-12-16 17:02:57 +00004737 // 64bits value can be atomically written to an address with movsd and an XMM register.
4738 // We need two XMM registers because there's no easier way to (bit) copy a register pair
4739 // into a single XMM register (we copy each pair part into the XMMs and then interleave them).
4740 // NB: We could make the register allocator understand fp_reg <-> core_reg moves but given the
4741 // isolated cases when we need this it isn't worth adding the extra complexity.
4742 locations->AddTemp(Location::RequiresFpuRegister());
4743 locations->AddTemp(Location::RequiresFpuRegister());
Mark Mendell81489372015-11-04 11:30:41 -05004744 } else {
4745 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4746
4747 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
4748 // Temporary registers for the write barrier.
4749 locations->AddTemp(Location::RequiresRegister()); // May be used for reference poisoning too.
4750 // Ensure the card is in a byte register.
4751 locations->AddTemp(Location::RegisterLocation(ECX));
4752 }
Calin Juravle52c48962014-12-16 17:02:57 +00004753 }
4754}
4755
4756void InstructionCodeGeneratorX86::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004757 const FieldInfo& field_info,
4758 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00004759 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4760
4761 LocationSummary* locations = instruction->GetLocations();
4762 Register base = locations->InAt(0).AsRegister<Register>();
4763 Location value = locations->InAt(1);
4764 bool is_volatile = field_info.IsVolatile();
4765 Primitive::Type field_type = field_info.GetFieldType();
4766 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01004767 bool needs_write_barrier =
4768 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00004769
4770 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004771 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
Calin Juravle52c48962014-12-16 17:02:57 +00004772 }
4773
Mark Mendell81489372015-11-04 11:30:41 -05004774 bool maybe_record_implicit_null_check_done = false;
4775
Calin Juravle52c48962014-12-16 17:02:57 +00004776 switch (field_type) {
4777 case Primitive::kPrimBoolean:
4778 case Primitive::kPrimByte: {
4779 __ movb(Address(base, offset), value.AsRegister<ByteRegister>());
4780 break;
4781 }
4782
4783 case Primitive::kPrimShort:
4784 case Primitive::kPrimChar: {
Mark Mendell81489372015-11-04 11:30:41 -05004785 if (value.IsConstant()) {
4786 int16_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4787 __ movw(Address(base, offset), Immediate(v));
4788 } else {
4789 __ movw(Address(base, offset), value.AsRegister<Register>());
4790 }
Calin Juravle52c48962014-12-16 17:02:57 +00004791 break;
4792 }
4793
4794 case Primitive::kPrimInt:
4795 case Primitive::kPrimNot: {
Roland Levillain4d027112015-07-01 15:41:14 +01004796 if (kPoisonHeapReferences && needs_write_barrier) {
4797 // Note that in the case where `value` is a null reference,
4798 // we do not enter this block, as the reference does not
4799 // need poisoning.
4800 DCHECK_EQ(field_type, Primitive::kPrimNot);
4801 Register temp = locations->GetTemp(0).AsRegister<Register>();
4802 __ movl(temp, value.AsRegister<Register>());
4803 __ PoisonHeapReference(temp);
4804 __ movl(Address(base, offset), temp);
Mark Mendell81489372015-11-04 11:30:41 -05004805 } else if (value.IsConstant()) {
4806 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4807 __ movl(Address(base, offset), Immediate(v));
Roland Levillain4d027112015-07-01 15:41:14 +01004808 } else {
Nicolas Geoffray03971632016-03-17 10:44:24 +00004809 DCHECK(value.IsRegister()) << value;
Roland Levillain4d027112015-07-01 15:41:14 +01004810 __ movl(Address(base, offset), value.AsRegister<Register>());
4811 }
Calin Juravle52c48962014-12-16 17:02:57 +00004812 break;
4813 }
4814
4815 case Primitive::kPrimLong: {
4816 if (is_volatile) {
4817 XmmRegister temp1 = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4818 XmmRegister temp2 = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
4819 __ movd(temp1, value.AsRegisterPairLow<Register>());
4820 __ movd(temp2, value.AsRegisterPairHigh<Register>());
4821 __ punpckldq(temp1, temp2);
4822 __ movsd(Address(base, offset), temp1);
Calin Juravle77520bc2015-01-12 18:45:46 +00004823 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell81489372015-11-04 11:30:41 -05004824 } else if (value.IsConstant()) {
4825 int64_t v = CodeGenerator::GetInt64ValueOf(value.GetConstant());
4826 __ movl(Address(base, offset), Immediate(Low32Bits(v)));
4827 codegen_->MaybeRecordImplicitNullCheck(instruction);
4828 __ movl(Address(base, kX86WordSize + offset), Immediate(High32Bits(v)));
Calin Juravle52c48962014-12-16 17:02:57 +00004829 } else {
4830 __ movl(Address(base, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00004831 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004832 __ movl(Address(base, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
4833 }
Mark Mendell81489372015-11-04 11:30:41 -05004834 maybe_record_implicit_null_check_done = true;
Calin Juravle52c48962014-12-16 17:02:57 +00004835 break;
4836 }
4837
4838 case Primitive::kPrimFloat: {
Mark Mendell81489372015-11-04 11:30:41 -05004839 if (value.IsConstant()) {
4840 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4841 __ movl(Address(base, offset), Immediate(v));
4842 } else {
4843 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4844 }
Calin Juravle52c48962014-12-16 17:02:57 +00004845 break;
4846 }
4847
4848 case Primitive::kPrimDouble: {
Mark Mendell81489372015-11-04 11:30:41 -05004849 if (value.IsConstant()) {
4850 int64_t v = CodeGenerator::GetInt64ValueOf(value.GetConstant());
4851 __ movl(Address(base, offset), Immediate(Low32Bits(v)));
4852 codegen_->MaybeRecordImplicitNullCheck(instruction);
4853 __ movl(Address(base, kX86WordSize + offset), Immediate(High32Bits(v)));
4854 maybe_record_implicit_null_check_done = true;
4855 } else {
4856 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4857 }
Calin Juravle52c48962014-12-16 17:02:57 +00004858 break;
4859 }
4860
4861 case Primitive::kPrimVoid:
4862 LOG(FATAL) << "Unreachable type " << field_type;
4863 UNREACHABLE();
4864 }
4865
Mark Mendell81489372015-11-04 11:30:41 -05004866 if (!maybe_record_implicit_null_check_done) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004867 codegen_->MaybeRecordImplicitNullCheck(instruction);
4868 }
4869
Roland Levillain4d027112015-07-01 15:41:14 +01004870 if (needs_write_barrier) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004871 Register temp = locations->GetTemp(0).AsRegister<Register>();
4872 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004873 codegen_->MarkGCCard(temp, card, base, value.AsRegister<Register>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00004874 }
4875
Calin Juravle52c48962014-12-16 17:02:57 +00004876 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004877 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
Calin Juravle52c48962014-12-16 17:02:57 +00004878 }
4879}
4880
4881void LocationsBuilderX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4882 HandleFieldGet(instruction, instruction->GetFieldInfo());
4883}
4884
4885void InstructionCodeGeneratorX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4886 HandleFieldGet(instruction, instruction->GetFieldInfo());
4887}
4888
4889void LocationsBuilderX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
4890 HandleFieldSet(instruction, instruction->GetFieldInfo());
4891}
4892
4893void InstructionCodeGeneratorX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004894 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00004895}
4896
4897void LocationsBuilderX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
4898 HandleFieldSet(instruction, instruction->GetFieldInfo());
4899}
4900
4901void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004902 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00004903}
4904
4905void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4906 HandleFieldGet(instruction, instruction->GetFieldInfo());
4907}
4908
4909void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4910 HandleFieldGet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004911}
4912
Calin Juravlee460d1d2015-09-29 04:52:17 +01004913void LocationsBuilderX86::VisitUnresolvedInstanceFieldGet(
4914 HUnresolvedInstanceFieldGet* instruction) {
4915 FieldAccessCallingConventionX86 calling_convention;
4916 codegen_->CreateUnresolvedFieldLocationSummary(
4917 instruction, instruction->GetFieldType(), calling_convention);
4918}
4919
4920void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldGet(
4921 HUnresolvedInstanceFieldGet* instruction) {
4922 FieldAccessCallingConventionX86 calling_convention;
4923 codegen_->GenerateUnresolvedFieldAccess(instruction,
4924 instruction->GetFieldType(),
4925 instruction->GetFieldIndex(),
4926 instruction->GetDexPc(),
4927 calling_convention);
4928}
4929
4930void LocationsBuilderX86::VisitUnresolvedInstanceFieldSet(
4931 HUnresolvedInstanceFieldSet* instruction) {
4932 FieldAccessCallingConventionX86 calling_convention;
4933 codegen_->CreateUnresolvedFieldLocationSummary(
4934 instruction, instruction->GetFieldType(), calling_convention);
4935}
4936
4937void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldSet(
4938 HUnresolvedInstanceFieldSet* instruction) {
4939 FieldAccessCallingConventionX86 calling_convention;
4940 codegen_->GenerateUnresolvedFieldAccess(instruction,
4941 instruction->GetFieldType(),
4942 instruction->GetFieldIndex(),
4943 instruction->GetDexPc(),
4944 calling_convention);
4945}
4946
4947void LocationsBuilderX86::VisitUnresolvedStaticFieldGet(
4948 HUnresolvedStaticFieldGet* instruction) {
4949 FieldAccessCallingConventionX86 calling_convention;
4950 codegen_->CreateUnresolvedFieldLocationSummary(
4951 instruction, instruction->GetFieldType(), calling_convention);
4952}
4953
4954void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldGet(
4955 HUnresolvedStaticFieldGet* instruction) {
4956 FieldAccessCallingConventionX86 calling_convention;
4957 codegen_->GenerateUnresolvedFieldAccess(instruction,
4958 instruction->GetFieldType(),
4959 instruction->GetFieldIndex(),
4960 instruction->GetDexPc(),
4961 calling_convention);
4962}
4963
4964void LocationsBuilderX86::VisitUnresolvedStaticFieldSet(
4965 HUnresolvedStaticFieldSet* instruction) {
4966 FieldAccessCallingConventionX86 calling_convention;
4967 codegen_->CreateUnresolvedFieldLocationSummary(
4968 instruction, instruction->GetFieldType(), calling_convention);
4969}
4970
4971void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldSet(
4972 HUnresolvedStaticFieldSet* instruction) {
4973 FieldAccessCallingConventionX86 calling_convention;
4974 codegen_->GenerateUnresolvedFieldAccess(instruction,
4975 instruction->GetFieldType(),
4976 instruction->GetFieldIndex(),
4977 instruction->GetDexPc(),
4978 calling_convention);
4979}
4980
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004981void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004982 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4983 ? LocationSummary::kCallOnSlowPath
4984 : LocationSummary::kNoCall;
4985 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
4986 Location loc = codegen_->IsImplicitNullCheckAllowed(instruction)
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004987 ? Location::RequiresRegister()
4988 : Location::Any();
4989 locations->SetInAt(0, loc);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004990 if (instruction->HasUses()) {
4991 locations->SetOut(Location::SameAsFirstInput());
4992 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004993}
4994
Calin Juravle2ae48182016-03-16 14:05:09 +00004995void CodeGeneratorX86::GenerateImplicitNullCheck(HNullCheck* instruction) {
4996 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004997 return;
4998 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004999 LocationSummary* locations = instruction->GetLocations();
5000 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00005001
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005002 __ testl(EAX, Address(obj.AsRegister<Register>(), 0));
Calin Juravle2ae48182016-03-16 14:05:09 +00005003 RecordPcInfo(instruction, instruction->GetDexPc());
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005004}
5005
Calin Juravle2ae48182016-03-16 14:05:09 +00005006void CodeGeneratorX86::GenerateExplicitNullCheck(HNullCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07005007 SlowPathCode* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00005008 AddSlowPath(slow_path);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005009
5010 LocationSummary* locations = instruction->GetLocations();
5011 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005012
5013 if (obj.IsRegister()) {
Mark Mendell42514f62015-03-31 11:34:22 -04005014 __ testl(obj.AsRegister<Register>(), obj.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005015 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005016 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005017 } else {
5018 DCHECK(obj.IsConstant()) << obj;
David Brazdil77a48ae2015-09-15 12:34:04 +00005019 DCHECK(obj.GetConstant()->IsNullConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005020 __ jmp(slow_path->GetEntryLabel());
5021 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005022 }
5023 __ j(kEqual, slow_path->GetEntryLabel());
5024}
5025
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005026void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00005027 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005028}
5029
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005030void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005031 bool object_array_get_with_read_barrier =
5032 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005033 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00005034 new (GetGraph()->GetArena()) LocationSummary(instruction,
5035 object_array_get_with_read_barrier ?
5036 LocationSummary::kCallOnSlowPath :
5037 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01005038 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
5039 locations->SetCustomSlowPathCallerSaves(RegisterSet()); // No caller-save registers.
5040 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005041 locations->SetInAt(0, Location::RequiresRegister());
5042 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005043 if (Primitive::IsFloatingPointType(instruction->GetType())) {
5044 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5045 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005046 // The output overlaps in case of long: we don't want the low move
5047 // to overwrite the array's location. Likewise, in the case of an
5048 // object array get with read barriers enabled, we do not want the
5049 // move to overwrite the array's location, as we need it to emit
5050 // the read barrier.
5051 locations->SetOut(
5052 Location::RequiresRegister(),
5053 (instruction->GetType() == Primitive::kPrimLong || object_array_get_with_read_barrier) ?
5054 Location::kOutputOverlap :
5055 Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005056 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005057}
5058
5059void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
5060 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005061 Location obj_loc = locations->InAt(0);
5062 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005063 Location index = locations->InAt(1);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005064 Location out_loc = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005065 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005066
Calin Juravle77520bc2015-01-12 18:45:46 +00005067 Primitive::Type type = instruction->GetType();
5068 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005069 case Primitive::kPrimBoolean: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005070 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005071 if (index.IsConstant()) {
5072 __ movzxb(out, Address(obj,
5073 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
5074 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005075 __ movzxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005076 }
5077 break;
5078 }
5079
5080 case Primitive::kPrimByte: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005081 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005082 if (index.IsConstant()) {
5083 __ movsxb(out, Address(obj,
5084 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
5085 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005086 __ movsxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005087 }
5088 break;
5089 }
5090
5091 case Primitive::kPrimShort: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005092 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005093 if (index.IsConstant()) {
5094 __ movsxw(out, Address(obj,
5095 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
5096 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005097 __ movsxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005098 }
5099 break;
5100 }
5101
5102 case Primitive::kPrimChar: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005103 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005104 if (index.IsConstant()) {
5105 __ movzxw(out, Address(obj,
5106 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
5107 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005108 __ movzxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005109 }
5110 break;
5111 }
5112
Roland Levillain7c1559a2015-12-15 10:55:36 +00005113 case Primitive::kPrimInt: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005114 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005115 if (index.IsConstant()) {
5116 __ movl(out, Address(obj,
5117 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
5118 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005119 __ movl(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005120 }
5121 break;
5122 }
5123
Roland Levillain7c1559a2015-12-15 10:55:36 +00005124 case Primitive::kPrimNot: {
5125 static_assert(
5126 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
5127 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain7c1559a2015-12-15 10:55:36 +00005128 // /* HeapReference<Object> */ out =
5129 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
5130 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005131 // Note that a potential implicit null check is handled in this
5132 // CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier call.
5133 codegen_->GenerateArrayLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00005134 instruction, out_loc, obj, data_offset, index, /* needs_null_check */ true);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005135 } else {
5136 Register out = out_loc.AsRegister<Register>();
5137 if (index.IsConstant()) {
5138 uint32_t offset =
5139 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
5140 __ movl(out, Address(obj, offset));
5141 codegen_->MaybeRecordImplicitNullCheck(instruction);
5142 // If read barriers are enabled, emit read barriers other than
5143 // Baker's using a slow path (and also unpoison the loaded
5144 // reference, if heap poisoning is enabled).
5145 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
5146 } else {
5147 __ movl(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
5148 codegen_->MaybeRecordImplicitNullCheck(instruction);
5149 // If read barriers are enabled, emit read barriers other than
5150 // Baker's using a slow path (and also unpoison the loaded
5151 // reference, if heap poisoning is enabled).
5152 codegen_->MaybeGenerateReadBarrierSlow(
5153 instruction, out_loc, out_loc, obj_loc, data_offset, index);
5154 }
5155 }
5156 break;
5157 }
5158
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005159 case Primitive::kPrimLong: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005160 DCHECK_NE(obj, out_loc.AsRegisterPairLow<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005161 if (index.IsConstant()) {
5162 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Roland Levillain7c1559a2015-12-15 10:55:36 +00005163 __ movl(out_loc.AsRegisterPairLow<Register>(), Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005164 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005165 __ movl(out_loc.AsRegisterPairHigh<Register>(), Address(obj, offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005166 } else {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005167 __ movl(out_loc.AsRegisterPairLow<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005168 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005169 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005170 __ movl(out_loc.AsRegisterPairHigh<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005171 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005172 }
5173 break;
5174 }
5175
Mark Mendell7c8d0092015-01-26 11:21:33 -05005176 case Primitive::kPrimFloat: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005177 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Mark Mendell7c8d0092015-01-26 11:21:33 -05005178 if (index.IsConstant()) {
5179 __ movss(out, Address(obj,
5180 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
5181 } else {
5182 __ movss(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
5183 }
5184 break;
5185 }
5186
5187 case Primitive::kPrimDouble: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005188 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Mark Mendell7c8d0092015-01-26 11:21:33 -05005189 if (index.IsConstant()) {
5190 __ movsd(out, Address(obj,
5191 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
5192 } else {
5193 __ movsd(out, Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
5194 }
5195 break;
5196 }
5197
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005198 case Primitive::kPrimVoid:
Calin Juravle77520bc2015-01-12 18:45:46 +00005199 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07005200 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005201 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005202
Roland Levillain7c1559a2015-12-15 10:55:36 +00005203 if (type == Primitive::kPrimNot || type == Primitive::kPrimLong) {
5204 // Potential implicit null checks, in the case of reference or
5205 // long arrays, are handled in the previous switch statement.
5206 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00005207 codegen_->MaybeRecordImplicitNullCheck(instruction);
5208 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005209}
5210
5211void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01005212 Primitive::Type value_type = instruction->GetComponentType();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005213
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005214 bool needs_write_barrier =
5215 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Roland Levillain0d5a2812015-11-13 10:07:31 +00005216 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005217
Nicolas Geoffray39468442014-09-02 15:17:15 +01005218 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
5219 instruction,
Vladimir Marko8d49fd72016-08-25 15:20:47 +01005220 may_need_runtime_call_for_type_check ?
Roland Levillain0d5a2812015-11-13 10:07:31 +00005221 LocationSummary::kCallOnSlowPath :
5222 LocationSummary::kNoCall);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005223
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005224 bool is_byte_type = (value_type == Primitive::kPrimBoolean)
5225 || (value_type == Primitive::kPrimByte);
5226 // We need the inputs to be different than the output in case of long operation.
5227 // In case of a byte operation, the register allocator does not support multiple
5228 // inputs that die at entry with one in a specific register.
5229 locations->SetInAt(0, Location::RequiresRegister());
5230 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
5231 if (is_byte_type) {
5232 // Ensure the value is in a byte register.
5233 locations->SetInAt(2, Location::ByteRegisterOrConstant(EAX, instruction->InputAt(2)));
5234 } else if (Primitive::IsFloatingPointType(value_type)) {
Mark Mendell81489372015-11-04 11:30:41 -05005235 locations->SetInAt(2, Location::FpuRegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005236 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005237 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
5238 }
5239 if (needs_write_barrier) {
5240 // Temporary registers for the write barrier.
Roland Levillain16d9f942016-08-25 17:27:56 +01005241 // These registers may be used for Baker read barriers too.
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005242 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
5243 // Ensure the card is in a byte register.
Roland Levillain4f6b0b52015-11-23 19:29:22 +00005244 locations->AddTemp(Location::RegisterLocation(ECX));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005245 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005246}
5247
5248void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
5249 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005250 Location array_loc = locations->InAt(0);
5251 Register array = array_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005252 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005253 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005254 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005255 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
5256 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5257 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005258 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005259 bool needs_write_barrier =
5260 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005261
5262 switch (value_type) {
5263 case Primitive::kPrimBoolean:
5264 case Primitive::kPrimByte: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005265 uint32_t offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
5266 Address address = index.IsConstant()
5267 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + offset)
5268 : Address(array, index.AsRegister<Register>(), TIMES_1, offset);
5269 if (value.IsRegister()) {
5270 __ movb(address, value.AsRegister<ByteRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005271 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005272 __ movb(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005273 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005274 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005275 break;
5276 }
5277
5278 case Primitive::kPrimShort:
5279 case Primitive::kPrimChar: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005280 uint32_t offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
5281 Address address = index.IsConstant()
5282 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + offset)
5283 : Address(array, index.AsRegister<Register>(), TIMES_2, offset);
5284 if (value.IsRegister()) {
5285 __ movw(address, value.AsRegister<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005286 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005287 __ movw(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005288 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005289 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005290 break;
5291 }
5292
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005293 case Primitive::kPrimNot: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005294 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
5295 Address address = index.IsConstant()
5296 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
5297 : Address(array, index.AsRegister<Register>(), TIMES_4, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005298
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005299 if (!value.IsRegister()) {
5300 // Just setting null.
5301 DCHECK(instruction->InputAt(2)->IsNullConstant());
5302 DCHECK(value.IsConstant()) << value;
5303 __ movl(address, Immediate(0));
Calin Juravle77520bc2015-01-12 18:45:46 +00005304 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005305 DCHECK(!needs_write_barrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005306 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005307 break;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005308 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005309
5310 DCHECK(needs_write_barrier);
5311 Register register_value = value.AsRegister<Register>();
Roland Levillain16d9f942016-08-25 17:27:56 +01005312 // We cannot use a NearLabel for `done`, as its range may be too
5313 // short when Baker read barriers are enabled.
5314 Label done;
5315 NearLabel not_null, do_put;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005316 SlowPathCode* slow_path = nullptr;
Roland Levillain16d9f942016-08-25 17:27:56 +01005317 Location temp_loc = locations->GetTemp(0);
5318 Register temp = temp_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005319 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005320 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathX86(instruction);
5321 codegen_->AddSlowPath(slow_path);
5322 if (instruction->GetValueCanBeNull()) {
5323 __ testl(register_value, register_value);
5324 __ j(kNotEqual, &not_null);
5325 __ movl(address, Immediate(0));
5326 codegen_->MaybeRecordImplicitNullCheck(instruction);
5327 __ jmp(&done);
5328 __ Bind(&not_null);
5329 }
5330
Roland Levillain0d5a2812015-11-13 10:07:31 +00005331 if (kEmitCompilerReadBarrier) {
Roland Levillain16d9f942016-08-25 17:27:56 +01005332 if (!kUseBakerReadBarrier) {
5333 // When (non-Baker) read barriers are enabled, the type
5334 // checking instrumentation requires two read barriers
5335 // generated by CodeGeneratorX86::GenerateReadBarrierSlow:
5336 //
5337 // __ movl(temp2, temp);
5338 // // /* HeapReference<Class> */ temp = temp->component_type_
5339 // __ movl(temp, Address(temp, component_offset));
5340 // codegen_->GenerateReadBarrierSlow(
5341 // instruction, temp_loc, temp_loc, temp2_loc, component_offset);
5342 //
5343 // // /* HeapReference<Class> */ temp2 = register_value->klass_
5344 // __ movl(temp2, Address(register_value, class_offset));
5345 // codegen_->GenerateReadBarrierSlow(
5346 // instruction, temp2_loc, temp2_loc, value, class_offset, temp_loc);
5347 //
5348 // __ cmpl(temp, temp2);
5349 //
5350 // However, the second read barrier may trash `temp`, as it
5351 // is a temporary register, and as such would not be saved
5352 // along with live registers before calling the runtime (nor
5353 // restored afterwards). So in this case, we bail out and
5354 // delegate the work to the array set slow path.
5355 //
5356 // TODO: Extend the register allocator to support a new
5357 // "(locally) live temp" location so as to avoid always
5358 // going into the slow path when read barriers are enabled?
5359 //
5360 // There is no such problem with Baker read barriers (see below).
5361 __ jmp(slow_path->GetEntryLabel());
5362 } else {
5363 Location temp2_loc = locations->GetTemp(1);
5364 Register temp2 = temp2_loc.AsRegister<Register>();
5365 // /* HeapReference<Class> */ temp = array->klass_
5366 codegen_->GenerateFieldLoadWithBakerReadBarrier(
5367 instruction, temp_loc, array, class_offset, /* needs_null_check */ true);
5368
5369 // /* HeapReference<Class> */ temp = temp->component_type_
5370 codegen_->GenerateFieldLoadWithBakerReadBarrier(
5371 instruction, temp_loc, temp, component_offset, /* needs_null_check */ false);
5372 // Register `temp` is not trashed by the read barrier
5373 // emitted by GenerateFieldLoadWithBakerReadBarrier below,
5374 // as that method produces a call to a ReadBarrierMarkRegX
5375 // entry point, which saves all potentially live registers,
5376 // including temporaries such a `temp`.
5377 // /* HeapReference<Class> */ temp2 = register_value->klass_
5378 codegen_->GenerateFieldLoadWithBakerReadBarrier(
5379 instruction, temp2_loc, register_value, class_offset, /* needs_null_check */ false);
5380 // If heap poisoning is enabled, `temp` and `temp2` have
5381 // been unpoisoned by the the previous calls to
5382 // CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier.
5383 __ cmpl(temp, temp2);
5384
5385 if (instruction->StaticTypeOfArrayIsObjectArray()) {
5386 __ j(kEqual, &do_put);
5387 // We do not need to emit a read barrier for the
5388 // following heap reference load, as `temp` is only used
5389 // in a comparison with null below, and this reference
5390 // is not kept afterwards. Also, if heap poisoning is
5391 // enabled, there is no need to unpoison that heap
5392 // reference for the same reason (comparison with null).
5393 __ cmpl(Address(temp, super_offset), Immediate(0));
5394 __ j(kNotEqual, slow_path->GetEntryLabel());
5395 __ Bind(&do_put);
5396 } else {
5397 __ j(kNotEqual, slow_path->GetEntryLabel());
5398 }
5399 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005400 } else {
Roland Levillain16d9f942016-08-25 17:27:56 +01005401 // Non read barrier code.
5402
Roland Levillain0d5a2812015-11-13 10:07:31 +00005403 // /* HeapReference<Class> */ temp = array->klass_
5404 __ movl(temp, Address(array, class_offset));
5405 codegen_->MaybeRecordImplicitNullCheck(instruction);
5406 __ MaybeUnpoisonHeapReference(temp);
5407
5408 // /* HeapReference<Class> */ temp = temp->component_type_
5409 __ movl(temp, Address(temp, component_offset));
5410 // If heap poisoning is enabled, no need to unpoison `temp`
5411 // nor the object reference in `register_value->klass`, as
5412 // we are comparing two poisoned references.
5413 __ cmpl(temp, Address(register_value, class_offset));
5414
5415 if (instruction->StaticTypeOfArrayIsObjectArray()) {
5416 __ j(kEqual, &do_put);
5417 // If heap poisoning is enabled, the `temp` reference has
5418 // not been unpoisoned yet; unpoison it now.
5419 __ MaybeUnpoisonHeapReference(temp);
5420
Roland Levillain16d9f942016-08-25 17:27:56 +01005421 // If heap poisoning is enabled, no need to unpoison the
5422 // heap reference loaded below, as it is only used for a
5423 // comparison with null.
5424 __ cmpl(Address(temp, super_offset), Immediate(0));
Roland Levillain0d5a2812015-11-13 10:07:31 +00005425 __ 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) {
Vladimir Marko70e97462016-08-09 11:04:26 +01005651 LocationSummary* locations =
5652 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
5653 locations->SetCustomSlowPathCallerSaves(RegisterSet()); // No caller-save registers.
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005654}
5655
5656void InstructionCodeGeneratorX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005657 HBasicBlock* block = instruction->GetBlock();
5658 if (block->GetLoopInformation() != nullptr) {
5659 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5660 // The back edge will generate the suspend check.
5661 return;
5662 }
5663 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5664 // The goto will generate the suspend check.
5665 return;
5666 }
5667 GenerateSuspendCheck(instruction, nullptr);
5668}
5669
5670void InstructionCodeGeneratorX86::GenerateSuspendCheck(HSuspendCheck* instruction,
5671 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005672 SuspendCheckSlowPathX86* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005673 down_cast<SuspendCheckSlowPathX86*>(instruction->GetSlowPath());
5674 if (slow_path == nullptr) {
5675 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathX86(instruction, successor);
5676 instruction->SetSlowPath(slow_path);
5677 codegen_->AddSlowPath(slow_path);
5678 if (successor != nullptr) {
5679 DCHECK(successor->IsLoopHeader());
5680 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
5681 }
5682 } else {
5683 DCHECK_EQ(slow_path->GetSuccessor(), successor);
5684 }
5685
Andreas Gampe542451c2016-07-26 09:02:02 -07005686 __ fs()->cmpw(Address::Absolute(Thread::ThreadFlagsOffset<kX86PointerSize>().Int32Value()),
Roland Levillain7c1559a2015-12-15 10:55:36 +00005687 Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005688 if (successor == nullptr) {
5689 __ j(kNotEqual, slow_path->GetEntryLabel());
5690 __ Bind(slow_path->GetReturnLabel());
5691 } else {
5692 __ j(kEqual, codegen_->GetLabelOf(successor));
5693 __ jmp(slow_path->GetEntryLabel());
5694 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005695}
5696
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005697X86Assembler* ParallelMoveResolverX86::GetAssembler() const {
5698 return codegen_->GetAssembler();
5699}
5700
Mark Mendell7c8d0092015-01-26 11:21:33 -05005701void ParallelMoveResolverX86::MoveMemoryToMemory32(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005702 ScratchRegisterScope ensure_scratch(
5703 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5704 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5705 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5706 __ movl(temp_reg, Address(ESP, src + stack_offset));
5707 __ movl(Address(ESP, dst + stack_offset), temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005708}
5709
5710void ParallelMoveResolverX86::MoveMemoryToMemory64(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005711 ScratchRegisterScope ensure_scratch(
5712 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5713 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5714 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5715 __ movl(temp_reg, Address(ESP, src + stack_offset));
5716 __ movl(Address(ESP, dst + stack_offset), temp_reg);
5717 __ movl(temp_reg, Address(ESP, src + stack_offset + kX86WordSize));
5718 __ movl(Address(ESP, dst + stack_offset + kX86WordSize), temp_reg);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005719}
5720
5721void ParallelMoveResolverX86::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005722 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005723 Location source = move->GetSource();
5724 Location destination = move->GetDestination();
5725
5726 if (source.IsRegister()) {
5727 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005728 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
David Brazdil74eb1b22015-12-14 11:44:01 +00005729 } else if (destination.IsFpuRegister()) {
5730 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005731 } else {
5732 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00005733 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005734 }
David Brazdil74eb1b22015-12-14 11:44:01 +00005735 } else if (source.IsRegisterPair()) {
5736 size_t elem_size = Primitive::ComponentSize(Primitive::kPrimInt);
5737 // Create stack space for 2 elements.
5738 __ subl(ESP, Immediate(2 * elem_size));
5739 __ movl(Address(ESP, 0), source.AsRegisterPairLow<Register>());
5740 __ movl(Address(ESP, elem_size), source.AsRegisterPairHigh<Register>());
5741 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
5742 // And remove the temporary stack space we allocated.
5743 __ addl(ESP, Immediate(2 * elem_size));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005744 } else if (source.IsFpuRegister()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00005745 if (destination.IsRegister()) {
5746 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
5747 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005748 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
David Brazdil74eb1b22015-12-14 11:44:01 +00005749 } else if (destination.IsRegisterPair()) {
5750 XmmRegister src_reg = source.AsFpuRegister<XmmRegister>();
5751 __ movd(destination.AsRegisterPairLow<Register>(), src_reg);
5752 __ psrlq(src_reg, Immediate(32));
5753 __ movd(destination.AsRegisterPairHigh<Register>(), src_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005754 } else if (destination.IsStackSlot()) {
5755 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
5756 } else {
5757 DCHECK(destination.IsDoubleStackSlot());
5758 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
5759 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005760 } else if (source.IsStackSlot()) {
5761 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005762 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005763 } else if (destination.IsFpuRegister()) {
5764 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005765 } else {
5766 DCHECK(destination.IsStackSlot());
Mark Mendell7c8d0092015-01-26 11:21:33 -05005767 MoveMemoryToMemory32(destination.GetStackIndex(), source.GetStackIndex());
5768 }
5769 } else if (source.IsDoubleStackSlot()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00005770 if (destination.IsRegisterPair()) {
5771 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
5772 __ movl(destination.AsRegisterPairHigh<Register>(),
5773 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
5774 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005775 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
5776 } else {
5777 DCHECK(destination.IsDoubleStackSlot()) << destination;
5778 MoveMemoryToMemory64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005779 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005780 } else if (source.IsConstant()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005781 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00005782 if (constant->IsIntConstant() || constant->IsNullConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05005783 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005784 if (destination.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05005785 if (value == 0) {
5786 __ xorl(destination.AsRegister<Register>(), destination.AsRegister<Register>());
5787 } else {
5788 __ movl(destination.AsRegister<Register>(), Immediate(value));
5789 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005790 } else {
5791 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell09b84632015-02-13 17:48:38 -05005792 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005793 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005794 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005795 float fp_value = constant->AsFloatConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00005796 int32_t value = bit_cast<int32_t, float>(fp_value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005797 Immediate imm(value);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005798 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005799 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
5800 if (value == 0) {
5801 // Easy handling of 0.0.
5802 __ xorps(dest, dest);
5803 } else {
5804 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005805 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5806 Register temp = static_cast<Register>(ensure_scratch.GetRegister());
5807 __ movl(temp, Immediate(value));
5808 __ movd(dest, temp);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005809 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005810 } else {
5811 DCHECK(destination.IsStackSlot()) << destination;
5812 __ movl(Address(ESP, destination.GetStackIndex()), imm);
5813 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005814 } else if (constant->IsLongConstant()) {
5815 int64_t value = constant->AsLongConstant()->GetValue();
5816 int32_t low_value = Low32Bits(value);
5817 int32_t high_value = High32Bits(value);
5818 Immediate low(low_value);
5819 Immediate high(high_value);
5820 if (destination.IsDoubleStackSlot()) {
5821 __ movl(Address(ESP, destination.GetStackIndex()), low);
5822 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
5823 } else {
5824 __ movl(destination.AsRegisterPairLow<Register>(), low);
5825 __ movl(destination.AsRegisterPairHigh<Register>(), high);
5826 }
5827 } else {
5828 DCHECK(constant->IsDoubleConstant());
5829 double dbl_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00005830 int64_t value = bit_cast<int64_t, double>(dbl_value);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005831 int32_t low_value = Low32Bits(value);
5832 int32_t high_value = High32Bits(value);
5833 Immediate low(low_value);
5834 Immediate high(high_value);
5835 if (destination.IsFpuRegister()) {
5836 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
5837 if (value == 0) {
5838 // Easy handling of 0.0.
5839 __ xorpd(dest, dest);
5840 } else {
5841 __ pushl(high);
5842 __ pushl(low);
5843 __ movsd(dest, Address(ESP, 0));
5844 __ addl(ESP, Immediate(8));
5845 }
5846 } else {
5847 DCHECK(destination.IsDoubleStackSlot()) << destination;
5848 __ movl(Address(ESP, destination.GetStackIndex()), low);
5849 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
5850 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005851 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005852 } else {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00005853 LOG(FATAL) << "Unimplemented move: " << destination << " <- " << source;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005854 }
5855}
5856
Mark Mendella5c19ce2015-04-01 12:51:05 -04005857void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005858 Register suggested_scratch = reg == EAX ? EBX : EAX;
5859 ScratchRegisterScope ensure_scratch(
5860 this, reg, suggested_scratch, codegen_->GetNumberOfCoreRegisters());
5861
5862 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5863 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset));
5864 __ movl(Address(ESP, mem + stack_offset), reg);
5865 __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005866}
5867
Mark Mendell7c8d0092015-01-26 11:21:33 -05005868void ParallelMoveResolverX86::Exchange32(XmmRegister reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005869 ScratchRegisterScope ensure_scratch(
5870 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5871
5872 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5873 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5874 __ movl(temp_reg, Address(ESP, mem + stack_offset));
5875 __ movss(Address(ESP, mem + stack_offset), reg);
5876 __ movd(reg, temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005877}
5878
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005879void ParallelMoveResolverX86::Exchange(int mem1, int mem2) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005880 ScratchRegisterScope ensure_scratch1(
5881 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005882
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005883 Register suggested_scratch = ensure_scratch1.GetRegister() == EAX ? EBX : EAX;
5884 ScratchRegisterScope ensure_scratch2(
5885 this, ensure_scratch1.GetRegister(), suggested_scratch, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005886
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005887 int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0;
5888 stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0;
5889 __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset));
5890 __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset));
5891 __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister()));
5892 __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005893}
5894
5895void ParallelMoveResolverX86::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005896 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005897 Location source = move->GetSource();
5898 Location destination = move->GetDestination();
5899
5900 if (source.IsRegister() && destination.IsRegister()) {
Mark Mendell90979812015-07-28 16:41:21 -04005901 // Use XOR swap algorithm to avoid serializing XCHG instruction or using a temporary.
5902 DCHECK_NE(destination.AsRegister<Register>(), source.AsRegister<Register>());
5903 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
5904 __ xorl(source.AsRegister<Register>(), destination.AsRegister<Register>());
5905 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005906 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005907 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005908 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005909 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005910 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
5911 Exchange(destination.GetStackIndex(), source.GetStackIndex());
Mark Mendell7c8d0092015-01-26 11:21:33 -05005912 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
5913 // Use XOR Swap algorithm to avoid a temporary.
5914 DCHECK_NE(source.reg(), destination.reg());
5915 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
5916 __ xorpd(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
5917 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
5918 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
5919 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
5920 } else if (destination.IsFpuRegister() && source.IsStackSlot()) {
5921 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005922 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
5923 // Take advantage of the 16 bytes in the XMM register.
5924 XmmRegister reg = source.AsFpuRegister<XmmRegister>();
5925 Address stack(ESP, destination.GetStackIndex());
5926 // Load the double into the high doubleword.
5927 __ movhpd(reg, stack);
5928
5929 // Store the low double into the destination.
5930 __ movsd(stack, reg);
5931
5932 // Move the high double to the low double.
5933 __ psrldq(reg, Immediate(8));
5934 } else if (destination.IsFpuRegister() && source.IsDoubleStackSlot()) {
5935 // Take advantage of the 16 bytes in the XMM register.
5936 XmmRegister reg = destination.AsFpuRegister<XmmRegister>();
5937 Address stack(ESP, source.GetStackIndex());
5938 // Load the double into the high doubleword.
5939 __ movhpd(reg, stack);
5940
5941 // Store the low double into the destination.
5942 __ movsd(stack, reg);
5943
5944 // Move the high double to the low double.
5945 __ psrldq(reg, Immediate(8));
5946 } else if (destination.IsDoubleStackSlot() && source.IsDoubleStackSlot()) {
5947 Exchange(destination.GetStackIndex(), source.GetStackIndex());
5948 Exchange(destination.GetHighStackIndex(kX86WordSize), source.GetHighStackIndex(kX86WordSize));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005949 } else {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005950 LOG(FATAL) << "Unimplemented: source: " << source << ", destination: " << destination;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005951 }
5952}
5953
5954void ParallelMoveResolverX86::SpillScratch(int reg) {
5955 __ pushl(static_cast<Register>(reg));
5956}
5957
5958void ParallelMoveResolverX86::RestoreScratch(int reg) {
5959 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005960}
5961
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005962HLoadClass::LoadKind CodeGeneratorX86::GetSupportedLoadClassKind(
5963 HLoadClass::LoadKind desired_class_load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005964 switch (desired_class_load_kind) {
5965 case HLoadClass::LoadKind::kReferrersClass:
5966 break;
5967 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
5968 DCHECK(!GetCompilerOptions().GetCompilePic());
5969 break;
5970 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
5971 DCHECK(GetCompilerOptions().GetCompilePic());
5972 FALLTHROUGH_INTENDED;
5973 case HLoadClass::LoadKind::kDexCachePcRelative:
5974 DCHECK(!Runtime::Current()->UseJitCompilation()); // Note: boot image is also non-JIT.
5975 // We disable pc-relative load when there is an irreducible loop, as the optimization
5976 // is incompatible with it.
5977 // TODO: Create as many X86ComputeBaseMethodAddress instructions as needed for methods
5978 // with irreducible loops.
5979 if (GetGraph()->HasIrreducibleLoops()) {
5980 return HLoadClass::LoadKind::kDexCacheViaMethod;
5981 }
5982 break;
5983 case HLoadClass::LoadKind::kBootImageAddress:
5984 break;
5985 case HLoadClass::LoadKind::kDexCacheAddress:
5986 DCHECK(Runtime::Current()->UseJitCompilation());
5987 break;
5988 case HLoadClass::LoadKind::kDexCacheViaMethod:
5989 break;
5990 }
5991 return desired_class_load_kind;
5992}
5993
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005994void LocationsBuilderX86::VisitLoadClass(HLoadClass* cls) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005995 if (cls->NeedsAccessCheck()) {
5996 InvokeRuntimeCallingConvention calling_convention;
5997 CodeGenerator::CreateLoadClassLocationSummary(
5998 cls,
5999 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
6000 Location::RegisterLocation(EAX),
6001 /* code_generator_supports_read_barrier */ true);
6002 return;
6003 }
6004
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006005 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
6006 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006007 ? LocationSummary::kCallOnSlowPath
6008 : LocationSummary::kNoCall;
6009 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006010 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Vladimir Marko70e97462016-08-09 11:04:26 +01006011 locations->SetCustomSlowPathCallerSaves(RegisterSet()); // No caller-save registers.
6012 }
6013
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006014 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
6015 if (load_kind == HLoadClass::LoadKind::kReferrersClass ||
6016 load_kind == HLoadClass::LoadKind::kDexCacheViaMethod ||
6017 load_kind == HLoadClass::LoadKind::kBootImageLinkTimePcRelative ||
6018 load_kind == HLoadClass::LoadKind::kDexCachePcRelative) {
6019 locations->SetInAt(0, Location::RequiresRegister());
6020 }
6021 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006022}
6023
6024void InstructionCodeGeneratorX86::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01006025 LocationSummary* locations = cls->GetLocations();
Calin Juravle98893e12015-10-02 21:05:03 +01006026 if (cls->NeedsAccessCheck()) {
6027 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex());
Serban Constantinescuba45db02016-07-12 22:53:02 +01006028 codegen_->InvokeRuntime(kQuickInitializeTypeAndVerifyAccess, cls, cls->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006029 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Calin Juravle580b6092015-10-06 17:35:58 +01006030 return;
6031 }
6032
Roland Levillain0d5a2812015-11-13 10:07:31 +00006033 Location out_loc = locations->Out();
6034 Register out = out_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006035
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006036 bool generate_null_check = false;
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006037 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006038 switch (cls->GetLoadKind()) {
6039 case HLoadClass::LoadKind::kReferrersClass: {
6040 DCHECK(!cls->CanCallRuntime());
6041 DCHECK(!cls->MustGenerateClinitCheck());
6042 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
6043 Register current_method = locations->InAt(0).AsRegister<Register>();
6044 GenerateGcRootFieldLoad(
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006045 cls,
6046 out_loc,
6047 Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()),
6048 /*fixup_label*/ nullptr,
6049 requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006050 break;
6051 }
6052 case HLoadClass::LoadKind::kBootImageLinkTimeAddress: {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006053 DCHECK(!requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006054 __ movl(out, Immediate(/* placeholder */ 0));
6055 codegen_->RecordTypePatch(cls);
6056 break;
6057 }
6058 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006059 DCHECK(!requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006060 Register method_address = locations->InAt(0).AsRegister<Register>();
6061 __ leal(out, Address(method_address, CodeGeneratorX86::kDummy32BitOffset));
6062 codegen_->RecordTypePatch(cls);
6063 break;
6064 }
6065 case HLoadClass::LoadKind::kBootImageAddress: {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006066 DCHECK(!requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006067 DCHECK_NE(cls->GetAddress(), 0u);
6068 uint32_t address = dchecked_integral_cast<uint32_t>(cls->GetAddress());
6069 __ movl(out, Immediate(address));
6070 codegen_->RecordSimplePatch();
6071 break;
6072 }
6073 case HLoadClass::LoadKind::kDexCacheAddress: {
6074 DCHECK_NE(cls->GetAddress(), 0u);
6075 uint32_t address = dchecked_integral_cast<uint32_t>(cls->GetAddress());
6076 // /* GcRoot<mirror::Class> */ out = *address
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006077 GenerateGcRootFieldLoad(cls,
6078 out_loc,
6079 Address::Absolute(address),
6080 /*fixup_label*/ nullptr,
6081 requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006082 generate_null_check = !cls->IsInDexCache();
6083 break;
6084 }
6085 case HLoadClass::LoadKind::kDexCachePcRelative: {
6086 Register base_reg = locations->InAt(0).AsRegister<Register>();
6087 uint32_t offset = cls->GetDexCacheElementOffset();
6088 Label* fixup_label = codegen_->NewPcRelativeDexCacheArrayPatch(cls->GetDexFile(), offset);
6089 // /* GcRoot<mirror::Class> */ out = *(base + offset) /* PC-relative */
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006090 GenerateGcRootFieldLoad(cls,
6091 out_loc,
6092 Address(base_reg, CodeGeneratorX86::kDummy32BitOffset),
6093 fixup_label,
6094 requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006095 generate_null_check = !cls->IsInDexCache();
6096 break;
6097 }
6098 case HLoadClass::LoadKind::kDexCacheViaMethod: {
6099 // /* GcRoot<mirror::Class>[] */ out =
6100 // current_method.ptr_sized_fields_->dex_cache_resolved_types_
6101 Register current_method = locations->InAt(0).AsRegister<Register>();
6102 __ movl(out, Address(current_method,
6103 ArtMethod::DexCacheResolvedTypesOffset(kX86PointerSize).Int32Value()));
6104 // /* GcRoot<mirror::Class> */ out = out[type_index]
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006105 GenerateGcRootFieldLoad(cls,
6106 out_loc,
6107 Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())),
6108 /*fixup_label*/ nullptr,
6109 requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006110 generate_null_check = !cls->IsInDexCache();
6111 break;
6112 }
6113 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006114
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006115 if (generate_null_check || cls->MustGenerateClinitCheck()) {
6116 DCHECK(cls->CanCallRuntime());
6117 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
6118 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
6119 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006120
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006121 if (generate_null_check) {
6122 __ testl(out, out);
6123 __ j(kEqual, slow_path->GetEntryLabel());
6124 }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006125
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006126 if (cls->MustGenerateClinitCheck()) {
6127 GenerateClassInitializationCheck(slow_path, out);
6128 } else {
6129 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006130 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006131 }
6132}
6133
6134void LocationsBuilderX86::VisitClinitCheck(HClinitCheck* check) {
6135 LocationSummary* locations =
6136 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
6137 locations->SetInAt(0, Location::RequiresRegister());
6138 if (check->HasUses()) {
6139 locations->SetOut(Location::SameAsFirstInput());
6140 }
6141}
6142
6143void InstructionCodeGeneratorX86::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006144 // We assume the class to not be null.
Andreas Gampe85b62f22015-09-09 13:15:38 -07006145 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006146 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006147 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00006148 GenerateClassInitializationCheck(slow_path,
6149 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006150}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006151
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006152void InstructionCodeGeneratorX86::GenerateClassInitializationCheck(
Andreas Gampe85b62f22015-09-09 13:15:38 -07006153 SlowPathCode* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006154 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
6155 Immediate(mirror::Class::kStatusInitialized));
6156 __ j(kLess, slow_path->GetEntryLabel());
6157 __ Bind(slow_path->GetExitLabel());
6158 // No need for memory fence, thanks to the X86 memory model.
6159}
6160
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006161HLoadString::LoadKind CodeGeneratorX86::GetSupportedLoadStringKind(
6162 HLoadString::LoadKind desired_string_load_kind) {
6163 if (kEmitCompilerReadBarrier) {
6164 switch (desired_string_load_kind) {
6165 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
6166 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
6167 case HLoadString::LoadKind::kBootImageAddress:
6168 // TODO: Implement for read barrier.
6169 return HLoadString::LoadKind::kDexCacheViaMethod;
6170 default:
6171 break;
6172 }
6173 }
6174 switch (desired_string_load_kind) {
6175 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
6176 DCHECK(!GetCompilerOptions().GetCompilePic());
6177 break;
6178 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
6179 DCHECK(GetCompilerOptions().GetCompilePic());
6180 FALLTHROUGH_INTENDED;
6181 case HLoadString::LoadKind::kDexCachePcRelative:
Calin Juravleffc87072016-04-20 14:22:09 +01006182 DCHECK(!Runtime::Current()->UseJitCompilation()); // Note: boot image is also non-JIT.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006183 // We disable pc-relative load when there is an irreducible loop, as the optimization
6184 // is incompatible with it.
6185 // TODO: Create as many X86ComputeBaseMethodAddress instructions as needed for methods
6186 // with irreducible loops.
6187 if (GetGraph()->HasIrreducibleLoops()) {
6188 return HLoadString::LoadKind::kDexCacheViaMethod;
6189 }
6190 break;
6191 case HLoadString::LoadKind::kBootImageAddress:
6192 break;
6193 case HLoadString::LoadKind::kDexCacheAddress:
Calin Juravleffc87072016-04-20 14:22:09 +01006194 DCHECK(Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006195 break;
6196 case HLoadString::LoadKind::kDexCacheViaMethod:
6197 break;
6198 }
6199 return desired_string_load_kind;
6200}
6201
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006202void LocationsBuilderX86::VisitLoadString(HLoadString* load) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006203 LocationSummary::CallKind call_kind = (load->NeedsEnvironment() || kEmitCompilerReadBarrier)
Nicolas Geoffray917d0162015-11-24 18:25:35 +00006204 ? LocationSummary::kCallOnSlowPath
6205 : LocationSummary::kNoCall;
6206 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01006207 if (kUseBakerReadBarrier && !load->NeedsEnvironment()) {
6208 locations->SetCustomSlowPathCallerSaves(RegisterSet()); // No caller-save registers.
6209 }
6210
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006211 HLoadString::LoadKind load_kind = load->GetLoadKind();
6212 if (load_kind == HLoadString::LoadKind::kDexCacheViaMethod ||
6213 load_kind == HLoadString::LoadKind::kBootImageLinkTimePcRelative ||
6214 load_kind == HLoadString::LoadKind::kDexCachePcRelative) {
6215 locations->SetInAt(0, Location::RequiresRegister());
6216 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006217 locations->SetOut(Location::RequiresRegister());
6218}
6219
6220void InstructionCodeGeneratorX86::VisitLoadString(HLoadString* load) {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01006221 LocationSummary* locations = load->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006222 Location out_loc = locations->Out();
6223 Register out = out_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006224
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006225 switch (load->GetLoadKind()) {
6226 case HLoadString::LoadKind::kBootImageLinkTimeAddress: {
6227 DCHECK(!kEmitCompilerReadBarrier);
6228 __ movl(out, Immediate(/* placeholder */ 0));
6229 codegen_->RecordStringPatch(load);
6230 return; // No dex cache slow path.
6231 }
6232 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
6233 DCHECK(!kEmitCompilerReadBarrier);
6234 Register method_address = locations->InAt(0).AsRegister<Register>();
6235 __ leal(out, Address(method_address, CodeGeneratorX86::kDummy32BitOffset));
6236 codegen_->RecordStringPatch(load);
6237 return; // No dex cache slow path.
6238 }
6239 case HLoadString::LoadKind::kBootImageAddress: {
6240 DCHECK(!kEmitCompilerReadBarrier);
6241 DCHECK_NE(load->GetAddress(), 0u);
6242 uint32_t address = dchecked_integral_cast<uint32_t>(load->GetAddress());
6243 __ movl(out, Immediate(address));
6244 codegen_->RecordSimplePatch();
6245 return; // No dex cache slow path.
6246 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006247 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07006248 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006249 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006250
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07006251 // TODO: Re-add the compiler code to do string dex cache lookup again.
6252 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86(load);
6253 codegen_->AddSlowPath(slow_path);
6254 __ jmp(slow_path->GetEntryLabel());
6255 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006256}
6257
David Brazdilcb1c0552015-08-04 16:22:25 +01006258static Address GetExceptionTlsAddress() {
Andreas Gampe542451c2016-07-26 09:02:02 -07006259 return Address::Absolute(Thread::ExceptionOffset<kX86PointerSize>().Int32Value());
David Brazdilcb1c0552015-08-04 16:22:25 +01006260}
6261
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006262void LocationsBuilderX86::VisitLoadException(HLoadException* load) {
6263 LocationSummary* locations =
6264 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
6265 locations->SetOut(Location::RequiresRegister());
6266}
6267
6268void InstructionCodeGeneratorX86::VisitLoadException(HLoadException* load) {
David Brazdilcb1c0552015-08-04 16:22:25 +01006269 __ fs()->movl(load->GetLocations()->Out().AsRegister<Register>(), GetExceptionTlsAddress());
6270}
6271
6272void LocationsBuilderX86::VisitClearException(HClearException* clear) {
6273 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
6274}
6275
6276void InstructionCodeGeneratorX86::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
6277 __ fs()->movl(GetExceptionTlsAddress(), Immediate(0));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006278}
6279
6280void LocationsBuilderX86::VisitThrow(HThrow* instruction) {
6281 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006282 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006283 InvokeRuntimeCallingConvention calling_convention;
6284 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6285}
6286
6287void InstructionCodeGeneratorX86::VisitThrow(HThrow* instruction) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01006288 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006289 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006290}
6291
Roland Levillain7c1559a2015-12-15 10:55:36 +00006292static bool TypeCheckNeedsATemporary(TypeCheckKind type_check_kind) {
6293 return kEmitCompilerReadBarrier &&
Vladimir Marko953437b2016-08-24 08:30:46 +00006294 !kUseBakerReadBarrier &&
6295 (type_check_kind == TypeCheckKind::kAbstractClassCheck ||
Roland Levillain7c1559a2015-12-15 10:55:36 +00006296 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
6297 type_check_kind == TypeCheckKind::kArrayObjectCheck);
6298}
6299
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006300void LocationsBuilderX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006301 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain0d5a2812015-11-13 10:07:31 +00006302 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01006303 bool baker_read_barrier_slow_path = false;
Roland Levillain0d5a2812015-11-13 10:07:31 +00006304 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006305 case TypeCheckKind::kExactCheck:
6306 case TypeCheckKind::kAbstractClassCheck:
6307 case TypeCheckKind::kClassHierarchyCheck:
6308 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006309 call_kind =
6310 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Vladimir Marko70e97462016-08-09 11:04:26 +01006311 baker_read_barrier_slow_path = kUseBakerReadBarrier;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006312 break;
6313 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006314 case TypeCheckKind::kUnresolvedCheck:
6315 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006316 call_kind = LocationSummary::kCallOnSlowPath;
6317 break;
6318 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006319
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006320 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01006321 if (baker_read_barrier_slow_path) {
6322 locations->SetCustomSlowPathCallerSaves(RegisterSet()); // No caller-save registers.
6323 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006324 locations->SetInAt(0, Location::RequiresRegister());
6325 locations->SetInAt(1, Location::Any());
6326 // Note that TypeCheckSlowPathX86 uses this "out" register too.
6327 locations->SetOut(Location::RequiresRegister());
6328 // When read barriers are enabled, we need a temporary register for
6329 // some cases.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006330 if (TypeCheckNeedsATemporary(type_check_kind)) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006331 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006332 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006333}
6334
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006335void InstructionCodeGeneratorX86::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006336 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006337 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006338 Location obj_loc = locations->InAt(0);
6339 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006340 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006341 Location out_loc = locations->Out();
6342 Register out = out_loc.AsRegister<Register>();
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006343 Location maybe_temp_loc = TypeCheckNeedsATemporary(type_check_kind) ?
Roland Levillain7c1559a2015-12-15 10:55:36 +00006344 locations->GetTemp(0) :
6345 Location::NoLocation();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006346 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006347 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6348 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6349 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Andreas Gampe85b62f22015-09-09 13:15:38 -07006350 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006351 NearLabel done, zero;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006352
6353 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006354 // Avoid null check if we know obj is not null.
6355 if (instruction->MustDoNullCheck()) {
6356 __ testl(obj, obj);
6357 __ j(kEqual, &zero);
6358 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006359
Roland Levillain0d5a2812015-11-13 10:07:31 +00006360 // /* HeapReference<Class> */ out = obj->klass_
Vladimir Marko953437b2016-08-24 08:30:46 +00006361 GenerateReferenceLoadTwoRegisters(instruction, out_loc, obj_loc, class_offset);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006362
Roland Levillain7c1559a2015-12-15 10:55:36 +00006363 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006364 case TypeCheckKind::kExactCheck: {
6365 if (cls.IsRegister()) {
6366 __ cmpl(out, cls.AsRegister<Register>());
6367 } else {
6368 DCHECK(cls.IsStackSlot()) << cls;
6369 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6370 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006371
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006372 // Classes must be equal for the instanceof to succeed.
6373 __ j(kNotEqual, &zero);
6374 __ movl(out, Immediate(1));
6375 __ jmp(&done);
6376 break;
6377 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006378
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006379 case TypeCheckKind::kAbstractClassCheck: {
6380 // If the class is abstract, we eagerly fetch the super class of the
6381 // object to avoid doing a comparison we know will fail.
6382 NearLabel loop;
6383 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006384 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006385 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006386 __ testl(out, out);
6387 // If `out` is null, we use it for the result, and jump to `done`.
6388 __ j(kEqual, &done);
6389 if (cls.IsRegister()) {
6390 __ cmpl(out, cls.AsRegister<Register>());
6391 } else {
6392 DCHECK(cls.IsStackSlot()) << cls;
6393 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6394 }
6395 __ j(kNotEqual, &loop);
6396 __ movl(out, Immediate(1));
6397 if (zero.IsLinked()) {
6398 __ jmp(&done);
6399 }
6400 break;
6401 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006402
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006403 case TypeCheckKind::kClassHierarchyCheck: {
6404 // Walk over the class hierarchy to find a match.
6405 NearLabel loop, success;
6406 __ Bind(&loop);
6407 if (cls.IsRegister()) {
6408 __ cmpl(out, cls.AsRegister<Register>());
6409 } else {
6410 DCHECK(cls.IsStackSlot()) << cls;
6411 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6412 }
6413 __ j(kEqual, &success);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006414 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006415 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006416 __ testl(out, out);
6417 __ j(kNotEqual, &loop);
6418 // If `out` is null, we use it for the result, and jump to `done`.
6419 __ jmp(&done);
6420 __ Bind(&success);
6421 __ movl(out, Immediate(1));
6422 if (zero.IsLinked()) {
6423 __ jmp(&done);
6424 }
6425 break;
6426 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006427
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006428 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006429 // Do an exact check.
6430 NearLabel exact_check;
6431 if (cls.IsRegister()) {
6432 __ cmpl(out, cls.AsRegister<Register>());
6433 } else {
6434 DCHECK(cls.IsStackSlot()) << cls;
6435 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6436 }
6437 __ j(kEqual, &exact_check);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006438 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006439 // /* HeapReference<Class> */ out = out->component_type_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006440 GenerateReferenceLoadOneRegister(instruction, out_loc, component_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006441 __ testl(out, out);
6442 // If `out` is null, we use it for the result, and jump to `done`.
6443 __ j(kEqual, &done);
6444 __ cmpw(Address(out, primitive_offset), Immediate(Primitive::kPrimNot));
6445 __ j(kNotEqual, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006446 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006447 __ movl(out, Immediate(1));
6448 __ jmp(&done);
6449 break;
6450 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006451
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006452 case TypeCheckKind::kArrayCheck: {
6453 if (cls.IsRegister()) {
6454 __ cmpl(out, cls.AsRegister<Register>());
6455 } else {
6456 DCHECK(cls.IsStackSlot()) << cls;
6457 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6458 }
6459 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain0d5a2812015-11-13 10:07:31 +00006460 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
6461 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006462 codegen_->AddSlowPath(slow_path);
6463 __ j(kNotEqual, slow_path->GetEntryLabel());
6464 __ movl(out, Immediate(1));
6465 if (zero.IsLinked()) {
6466 __ jmp(&done);
6467 }
6468 break;
6469 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006470
Calin Juravle98893e12015-10-02 21:05:03 +01006471 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006472 case TypeCheckKind::kInterfaceCheck: {
6473 // Note that we indeed only call on slow path, but we always go
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006474 // into the slow path for the unresolved and interface check
Roland Levillain0d5a2812015-11-13 10:07:31 +00006475 // cases.
6476 //
6477 // We cannot directly call the InstanceofNonTrivial runtime
6478 // entry point without resorting to a type checking slow path
6479 // here (i.e. by calling InvokeRuntime directly), as it would
6480 // require to assign fixed registers for the inputs of this
6481 // HInstanceOf instruction (following the runtime calling
6482 // convention), which might be cluttered by the potential first
6483 // read barrier emission at the beginning of this method.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006484 //
6485 // TODO: Introduce a new runtime entry point taking the object
6486 // to test (instead of its class) as argument, and let it deal
6487 // with the read barrier issues. This will let us refactor this
6488 // case of the `switch` code as it was previously (with a direct
6489 // call to the runtime not using a type checking slow path).
6490 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006491 DCHECK(locations->OnlyCallsOnSlowPath());
6492 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
6493 /* is_fatal */ false);
6494 codegen_->AddSlowPath(slow_path);
6495 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006496 if (zero.IsLinked()) {
6497 __ jmp(&done);
6498 }
6499 break;
6500 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006501 }
6502
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006503 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006504 __ Bind(&zero);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006505 __ xorl(out, out);
6506 }
6507
6508 if (done.IsLinked()) {
6509 __ Bind(&done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006510 }
6511
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006512 if (slow_path != nullptr) {
6513 __ Bind(slow_path->GetExitLabel());
6514 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006515}
6516
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006517void LocationsBuilderX86::VisitCheckCast(HCheckCast* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006518 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
6519 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006520 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01006521 bool baker_read_barrier_slow_path = false;
Roland Levillain0d5a2812015-11-13 10:07:31 +00006522 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006523 case TypeCheckKind::kExactCheck:
6524 case TypeCheckKind::kAbstractClassCheck:
6525 case TypeCheckKind::kClassHierarchyCheck:
6526 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006527 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
6528 LocationSummary::kCallOnSlowPath :
6529 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Vladimir Marko70e97462016-08-09 11:04:26 +01006530 baker_read_barrier_slow_path = kUseBakerReadBarrier && !throws_into_catch;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006531 break;
6532 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006533 case TypeCheckKind::kUnresolvedCheck:
6534 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006535 call_kind = LocationSummary::kCallOnSlowPath;
6536 break;
6537 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006538 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01006539 if (baker_read_barrier_slow_path) {
6540 locations->SetCustomSlowPathCallerSaves(RegisterSet()); // No caller-save registers.
6541 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006542 locations->SetInAt(0, Location::RequiresRegister());
6543 locations->SetInAt(1, Location::Any());
6544 // Note that TypeCheckSlowPathX86 uses this "temp" register too.
6545 locations->AddTemp(Location::RequiresRegister());
6546 // When read barriers are enabled, we need an additional temporary
6547 // register for some cases.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006548 if (TypeCheckNeedsATemporary(type_check_kind)) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006549 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006550 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006551}
6552
6553void InstructionCodeGeneratorX86::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006554 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006555 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006556 Location obj_loc = locations->InAt(0);
6557 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006558 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006559 Location temp_loc = locations->GetTemp(0);
6560 Register temp = temp_loc.AsRegister<Register>();
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006561 Location maybe_temp2_loc = TypeCheckNeedsATemporary(type_check_kind) ?
Roland Levillain7c1559a2015-12-15 10:55:36 +00006562 locations->GetTemp(1) :
6563 Location::NoLocation();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006564 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
6565 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6566 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6567 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006568
Roland Levillain0d5a2812015-11-13 10:07:31 +00006569 bool is_type_check_slow_path_fatal =
6570 (type_check_kind == TypeCheckKind::kExactCheck ||
6571 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
6572 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
6573 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
6574 !instruction->CanThrowIntoCatchBlock();
6575 SlowPathCode* type_check_slow_path =
6576 new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
6577 is_type_check_slow_path_fatal);
6578 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006579
Roland Levillain0d5a2812015-11-13 10:07:31 +00006580 NearLabel done;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006581 // Avoid null check if we know obj is not null.
6582 if (instruction->MustDoNullCheck()) {
6583 __ testl(obj, obj);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006584 __ j(kEqual, &done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006585 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006586
Roland Levillain0d5a2812015-11-13 10:07:31 +00006587 // /* HeapReference<Class> */ temp = obj->klass_
Vladimir Marko953437b2016-08-24 08:30:46 +00006588 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006589
Roland Levillain0d5a2812015-11-13 10:07:31 +00006590 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006591 case TypeCheckKind::kExactCheck:
6592 case TypeCheckKind::kArrayCheck: {
6593 if (cls.IsRegister()) {
6594 __ cmpl(temp, cls.AsRegister<Register>());
6595 } else {
6596 DCHECK(cls.IsStackSlot()) << cls;
6597 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6598 }
6599 // Jump to slow path for throwing the exception or doing a
6600 // more involved array check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006601 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006602 break;
6603 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006604
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006605 case TypeCheckKind::kAbstractClassCheck: {
6606 // If the class is abstract, we eagerly fetch the super class of the
6607 // object to avoid doing a comparison we know will fail.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006608 NearLabel loop, compare_classes;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006609 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006610 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006611 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006612
6613 // If the class reference currently in `temp` is not null, jump
6614 // to the `compare_classes` label to compare it with the checked
6615 // class.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006616 __ testl(temp, temp);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006617 __ j(kNotEqual, &compare_classes);
6618 // Otherwise, jump to the slow path to throw the exception.
6619 //
6620 // But before, move back the object's class into `temp` before
6621 // going into the slow path, as it has been overwritten in the
6622 // meantime.
6623 // /* HeapReference<Class> */ temp = obj->klass_
Vladimir Marko953437b2016-08-24 08:30:46 +00006624 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006625 __ jmp(type_check_slow_path->GetEntryLabel());
6626
6627 __ Bind(&compare_classes);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006628 if (cls.IsRegister()) {
6629 __ cmpl(temp, cls.AsRegister<Register>());
6630 } else {
6631 DCHECK(cls.IsStackSlot()) << cls;
6632 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6633 }
6634 __ j(kNotEqual, &loop);
6635 break;
6636 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006637
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006638 case TypeCheckKind::kClassHierarchyCheck: {
6639 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006640 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006641 __ Bind(&loop);
6642 if (cls.IsRegister()) {
6643 __ cmpl(temp, cls.AsRegister<Register>());
6644 } else {
6645 DCHECK(cls.IsStackSlot()) << cls;
6646 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6647 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006648 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006649
Roland Levillain0d5a2812015-11-13 10:07:31 +00006650 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006651 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006652
6653 // If the class reference currently in `temp` is not null, jump
6654 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006655 __ testl(temp, temp);
6656 __ j(kNotEqual, &loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006657 // Otherwise, jump to the slow path to throw the exception.
6658 //
6659 // But before, move back the object's class into `temp` before
6660 // going into the slow path, as it has been overwritten in the
6661 // meantime.
6662 // /* HeapReference<Class> */ temp = obj->klass_
Vladimir Marko953437b2016-08-24 08:30:46 +00006663 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006664 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006665 break;
6666 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006667
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006668 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006669 // Do an exact check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006670 NearLabel check_non_primitive_component_type;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006671 if (cls.IsRegister()) {
6672 __ cmpl(temp, cls.AsRegister<Register>());
6673 } else {
6674 DCHECK(cls.IsStackSlot()) << cls;
6675 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6676 }
6677 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006678
6679 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006680 // /* HeapReference<Class> */ temp = temp->component_type_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006681 GenerateReferenceLoadOneRegister(instruction, temp_loc, component_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006682
6683 // If the component type is not null (i.e. the object is indeed
6684 // an array), jump to label `check_non_primitive_component_type`
6685 // to further check that this component type is not a primitive
6686 // type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006687 __ testl(temp, temp);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006688 __ j(kNotEqual, &check_non_primitive_component_type);
6689 // Otherwise, jump to the slow path to throw the exception.
6690 //
6691 // But before, move back the object's class into `temp` before
6692 // going into the slow path, as it has been overwritten in the
6693 // meantime.
6694 // /* HeapReference<Class> */ temp = obj->klass_
Vladimir Marko953437b2016-08-24 08:30:46 +00006695 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006696 __ jmp(type_check_slow_path->GetEntryLabel());
6697
6698 __ Bind(&check_non_primitive_component_type);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006699 __ cmpw(Address(temp, primitive_offset), Immediate(Primitive::kPrimNot));
Roland Levillain0d5a2812015-11-13 10:07:31 +00006700 __ j(kEqual, &done);
6701 // Same comment as above regarding `temp` and the slow path.
6702 // /* HeapReference<Class> */ temp = obj->klass_
Vladimir Marko953437b2016-08-24 08:30:46 +00006703 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006704 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006705 break;
6706 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006707
Calin Juravle98893e12015-10-02 21:05:03 +01006708 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006709 case TypeCheckKind::kInterfaceCheck:
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006710 // We always go into the type check slow path for the unresolved
6711 // and interface check cases.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006712 //
6713 // We cannot directly call the CheckCast runtime entry point
6714 // without resorting to a type checking slow path here (i.e. by
6715 // calling InvokeRuntime directly), as it would require to
6716 // assign fixed registers for the inputs of this HInstanceOf
6717 // instruction (following the runtime calling convention), which
6718 // might be cluttered by the potential first read barrier
6719 // emission at the beginning of this method.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006720 //
6721 // TODO: Introduce a new runtime entry point taking the object
6722 // to test (instead of its class) as argument, and let it deal
6723 // with the read barrier issues. This will let us refactor this
6724 // case of the `switch` code as it was previously (with a direct
6725 // call to the runtime not using a type checking slow path).
6726 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006727 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006728 break;
6729 }
6730 __ Bind(&done);
6731
Roland Levillain0d5a2812015-11-13 10:07:31 +00006732 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006733}
6734
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006735void LocationsBuilderX86::VisitMonitorOperation(HMonitorOperation* instruction) {
6736 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006737 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006738 InvokeRuntimeCallingConvention calling_convention;
6739 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6740}
6741
6742void InstructionCodeGeneratorX86::VisitMonitorOperation(HMonitorOperation* instruction) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01006743 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject
6744 : kQuickUnlockObject,
Alexandre Rames8158f282015-08-07 10:26:17 +01006745 instruction,
Serban Constantinescuba45db02016-07-12 22:53:02 +01006746 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006747 if (instruction->IsEnter()) {
6748 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
6749 } else {
6750 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
6751 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006752}
6753
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006754void LocationsBuilderX86::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
6755void LocationsBuilderX86::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
6756void LocationsBuilderX86::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
6757
6758void LocationsBuilderX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
6759 LocationSummary* locations =
6760 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6761 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
6762 || instruction->GetResultType() == Primitive::kPrimLong);
6763 locations->SetInAt(0, Location::RequiresRegister());
6764 locations->SetInAt(1, Location::Any());
6765 locations->SetOut(Location::SameAsFirstInput());
6766}
6767
6768void InstructionCodeGeneratorX86::VisitAnd(HAnd* instruction) {
6769 HandleBitwiseOperation(instruction);
6770}
6771
6772void InstructionCodeGeneratorX86::VisitOr(HOr* instruction) {
6773 HandleBitwiseOperation(instruction);
6774}
6775
6776void InstructionCodeGeneratorX86::VisitXor(HXor* instruction) {
6777 HandleBitwiseOperation(instruction);
6778}
6779
6780void InstructionCodeGeneratorX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
6781 LocationSummary* locations = instruction->GetLocations();
6782 Location first = locations->InAt(0);
6783 Location second = locations->InAt(1);
6784 DCHECK(first.Equals(locations->Out()));
6785
6786 if (instruction->GetResultType() == Primitive::kPrimInt) {
6787 if (second.IsRegister()) {
6788 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006789 __ andl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006790 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006791 __ orl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006792 } else {
6793 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006794 __ xorl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006795 }
6796 } else if (second.IsConstant()) {
6797 if (instruction->IsAnd()) {
Roland Levillain199f3362014-11-27 17:15:16 +00006798 __ andl(first.AsRegister<Register>(),
6799 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006800 } else if (instruction->IsOr()) {
Roland Levillain199f3362014-11-27 17:15:16 +00006801 __ orl(first.AsRegister<Register>(),
6802 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006803 } else {
6804 DCHECK(instruction->IsXor());
Roland Levillain199f3362014-11-27 17:15:16 +00006805 __ xorl(first.AsRegister<Register>(),
6806 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006807 }
6808 } else {
6809 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006810 __ andl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006811 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006812 __ orl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006813 } else {
6814 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006815 __ xorl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006816 }
6817 }
6818 } else {
6819 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
6820 if (second.IsRegisterPair()) {
6821 if (instruction->IsAnd()) {
6822 __ andl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
6823 __ andl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
6824 } else if (instruction->IsOr()) {
6825 __ orl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
6826 __ orl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
6827 } else {
6828 DCHECK(instruction->IsXor());
6829 __ xorl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
6830 __ xorl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
6831 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006832 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006833 if (instruction->IsAnd()) {
6834 __ andl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
6835 __ andl(first.AsRegisterPairHigh<Register>(),
6836 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
6837 } else if (instruction->IsOr()) {
6838 __ orl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
6839 __ orl(first.AsRegisterPairHigh<Register>(),
6840 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
6841 } else {
6842 DCHECK(instruction->IsXor());
6843 __ xorl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
6844 __ xorl(first.AsRegisterPairHigh<Register>(),
6845 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
6846 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006847 } else {
6848 DCHECK(second.IsConstant()) << second;
6849 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006850 int32_t low_value = Low32Bits(value);
6851 int32_t high_value = High32Bits(value);
6852 Immediate low(low_value);
6853 Immediate high(high_value);
6854 Register first_low = first.AsRegisterPairLow<Register>();
6855 Register first_high = first.AsRegisterPairHigh<Register>();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006856 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006857 if (low_value == 0) {
6858 __ xorl(first_low, first_low);
6859 } else if (low_value != -1) {
6860 __ andl(first_low, low);
6861 }
6862 if (high_value == 0) {
6863 __ xorl(first_high, first_high);
6864 } else if (high_value != -1) {
6865 __ andl(first_high, high);
6866 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006867 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006868 if (low_value != 0) {
6869 __ orl(first_low, low);
6870 }
6871 if (high_value != 0) {
6872 __ orl(first_high, high);
6873 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006874 } else {
6875 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006876 if (low_value != 0) {
6877 __ xorl(first_low, low);
6878 }
6879 if (high_value != 0) {
6880 __ xorl(first_high, high);
6881 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006882 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006883 }
6884 }
6885}
6886
Roland Levillain7c1559a2015-12-15 10:55:36 +00006887void InstructionCodeGeneratorX86::GenerateReferenceLoadOneRegister(HInstruction* instruction,
6888 Location out,
6889 uint32_t offset,
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006890 Location maybe_temp) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006891 Register out_reg = out.AsRegister<Register>();
6892 if (kEmitCompilerReadBarrier) {
6893 if (kUseBakerReadBarrier) {
6894 // Load with fast path based Baker's read barrier.
6895 // /* HeapReference<Object> */ out = *(out + offset)
6896 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00006897 instruction, out, out_reg, offset, /* needs_null_check */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006898 } else {
6899 // Load with slow path based read barrier.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006900 // Save the value of `out` into `maybe_temp` before overwriting it
Roland Levillain7c1559a2015-12-15 10:55:36 +00006901 // in the following move operation, as we will need it for the
6902 // read barrier below.
Vladimir Marko953437b2016-08-24 08:30:46 +00006903 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006904 __ movl(maybe_temp.AsRegister<Register>(), out_reg);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006905 // /* HeapReference<Object> */ out = *(out + offset)
6906 __ movl(out_reg, Address(out_reg, offset));
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006907 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006908 }
6909 } else {
6910 // Plain load with no read barrier.
6911 // /* HeapReference<Object> */ out = *(out + offset)
6912 __ movl(out_reg, Address(out_reg, offset));
6913 __ MaybeUnpoisonHeapReference(out_reg);
6914 }
6915}
6916
6917void InstructionCodeGeneratorX86::GenerateReferenceLoadTwoRegisters(HInstruction* instruction,
6918 Location out,
6919 Location obj,
Vladimir Marko953437b2016-08-24 08:30:46 +00006920 uint32_t offset) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006921 Register out_reg = out.AsRegister<Register>();
6922 Register obj_reg = obj.AsRegister<Register>();
6923 if (kEmitCompilerReadBarrier) {
6924 if (kUseBakerReadBarrier) {
6925 // Load with fast path based Baker's read barrier.
6926 // /* HeapReference<Object> */ out = *(obj + offset)
6927 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00006928 instruction, out, obj_reg, offset, /* needs_null_check */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006929 } else {
6930 // Load with slow path based read barrier.
6931 // /* HeapReference<Object> */ out = *(obj + offset)
6932 __ movl(out_reg, Address(obj_reg, offset));
6933 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
6934 }
6935 } else {
6936 // Plain load with no read barrier.
6937 // /* HeapReference<Object> */ out = *(obj + offset)
6938 __ movl(out_reg, Address(obj_reg, offset));
6939 __ MaybeUnpoisonHeapReference(out_reg);
6940 }
6941}
6942
6943void InstructionCodeGeneratorX86::GenerateGcRootFieldLoad(HInstruction* instruction,
6944 Location root,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006945 const Address& address,
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006946 Label* fixup_label,
6947 bool requires_read_barrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006948 Register root_reg = root.AsRegister<Register>();
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006949 if (requires_read_barrier) {
6950 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006951 if (kUseBakerReadBarrier) {
6952 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
6953 // Baker's read barrier are used:
6954 //
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006955 // root = *address;
Roland Levillain7c1559a2015-12-15 10:55:36 +00006956 // if (Thread::Current()->GetIsGcMarking()) {
6957 // root = ReadBarrier::Mark(root)
6958 // }
6959
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006960 // /* GcRoot<mirror::Object> */ root = *address
6961 __ movl(root_reg, address);
6962 if (fixup_label != nullptr) {
6963 __ Bind(fixup_label);
6964 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00006965 static_assert(
6966 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
6967 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
6968 "have different sizes.");
6969 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
6970 "art::mirror::CompressedReference<mirror::Object> and int32_t "
6971 "have different sizes.");
6972
Vladimir Marko953437b2016-08-24 08:30:46 +00006973 // Slow path marking the GC root `root`.
6974 SlowPathCode* slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathX86(
6975 instruction, root, /* unpoison */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006976 codegen_->AddSlowPath(slow_path);
6977
Andreas Gampe542451c2016-07-26 09:02:02 -07006978 __ fs()->cmpl(Address::Absolute(Thread::IsGcMarkingOffset<kX86PointerSize>().Int32Value()),
Roland Levillain7c1559a2015-12-15 10:55:36 +00006979 Immediate(0));
6980 __ j(kNotEqual, slow_path->GetEntryLabel());
6981 __ Bind(slow_path->GetExitLabel());
6982 } else {
6983 // GC root loaded through a slow path for read barriers other
6984 // than Baker's.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006985 // /* GcRoot<mirror::Object>* */ root = address
6986 __ leal(root_reg, address);
6987 if (fixup_label != nullptr) {
6988 __ Bind(fixup_label);
6989 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00006990 // /* mirror::Object* */ root = root->Read()
6991 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
6992 }
6993 } else {
6994 // Plain GC root load with no read barrier.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006995 // /* GcRoot<mirror::Object> */ root = *address
6996 __ movl(root_reg, address);
6997 if (fixup_label != nullptr) {
6998 __ Bind(fixup_label);
6999 }
Roland Levillaine3f43ac2016-01-19 15:07:47 +00007000 // Note that GC roots are not affected by heap poisoning, thus we
7001 // do not have to unpoison `root_reg` here.
Roland Levillain7c1559a2015-12-15 10:55:36 +00007002 }
7003}
7004
7005void CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
7006 Location ref,
7007 Register obj,
7008 uint32_t offset,
Roland Levillain7c1559a2015-12-15 10:55:36 +00007009 bool needs_null_check) {
7010 DCHECK(kEmitCompilerReadBarrier);
7011 DCHECK(kUseBakerReadBarrier);
7012
7013 // /* HeapReference<Object> */ ref = *(obj + offset)
7014 Address src(obj, offset);
Vladimir Marko953437b2016-08-24 08:30:46 +00007015 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, needs_null_check);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007016}
7017
7018void CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
7019 Location ref,
7020 Register obj,
7021 uint32_t data_offset,
7022 Location index,
Roland Levillain7c1559a2015-12-15 10:55:36 +00007023 bool needs_null_check) {
7024 DCHECK(kEmitCompilerReadBarrier);
7025 DCHECK(kUseBakerReadBarrier);
7026
Roland Levillain3d312422016-06-23 13:53:42 +01007027 static_assert(
7028 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
7029 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain7c1559a2015-12-15 10:55:36 +00007030 // /* HeapReference<Object> */ ref =
7031 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
7032 Address src = index.IsConstant() ?
7033 Address(obj, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset) :
7034 Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset);
Vladimir Marko953437b2016-08-24 08:30:46 +00007035 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, needs_null_check);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007036}
7037
7038void CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
7039 Location ref,
7040 Register obj,
7041 const Address& src,
Roland Levillain7c1559a2015-12-15 10:55:36 +00007042 bool needs_null_check) {
7043 DCHECK(kEmitCompilerReadBarrier);
7044 DCHECK(kUseBakerReadBarrier);
7045
7046 // In slow path based read barriers, the read barrier call is
7047 // inserted after the original load. However, in fast path based
7048 // Baker's read barriers, we need to perform the load of
7049 // mirror::Object::monitor_ *before* the original reference load.
7050 // This load-load ordering is required by the read barrier.
7051 // The fast path/slow path (for Baker's algorithm) should look like:
7052 //
7053 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
7054 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
7055 // HeapReference<Object> ref = *src; // Original reference load.
7056 // bool is_gray = (rb_state == ReadBarrier::gray_ptr_);
7057 // if (is_gray) {
7058 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
7059 // }
7060 //
7061 // Note: the original implementation in ReadBarrier::Barrier is
7062 // slightly more complex as:
7063 // - it implements the load-load fence using a data dependency on
Roland Levillaine3f43ac2016-01-19 15:07:47 +00007064 // the high-bits of rb_state, which are expected to be all zeroes
7065 // (we use CodeGeneratorX86::GenerateMemoryBarrier instead here,
7066 // which is a no-op thanks to the x86 memory model);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007067 // - it performs additional checks that we do not do here for
7068 // performance reasons.
7069
7070 Register ref_reg = ref.AsRegister<Register>();
Roland Levillain7c1559a2015-12-15 10:55:36 +00007071 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
7072
Vladimir Marko953437b2016-08-24 08:30:46 +00007073 // Given the numeric representation, it's enough to check the low bit of the rb_state.
7074 static_assert(ReadBarrier::white_ptr_ == 0, "Expecting white to have value 0");
7075 static_assert(ReadBarrier::gray_ptr_ == 1, "Expecting gray to have value 1");
7076 static_assert(ReadBarrier::black_ptr_ == 2, "Expecting black to have value 2");
7077 constexpr uint32_t gray_byte_position = LockWord::kReadBarrierStateShift / kBitsPerByte;
7078 constexpr uint32_t gray_bit_position = LockWord::kReadBarrierStateShift % kBitsPerByte;
7079 constexpr int32_t test_value = static_cast<int8_t>(1 << gray_bit_position);
7080
7081 // if (rb_state == ReadBarrier::gray_ptr_)
7082 // ref = ReadBarrier::Mark(ref);
7083 // At this point, just do the "if" and make sure that flags are preserved until the branch.
7084 __ testb(Address(obj, monitor_offset + gray_byte_position), Immediate(test_value));
Roland Levillain7c1559a2015-12-15 10:55:36 +00007085 if (needs_null_check) {
7086 MaybeRecordImplicitNullCheck(instruction);
7087 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00007088
7089 // Load fence to prevent load-load reordering.
7090 // Note that this is a no-op, thanks to the x86 memory model.
7091 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
7092
7093 // The actual reference load.
7094 // /* HeapReference<Object> */ ref = *src
Vladimir Marko953437b2016-08-24 08:30:46 +00007095 __ movl(ref_reg, src); // Flags are unaffected.
7096
7097 // Note: Reference unpoisoning modifies the flags, so we need to delay it after the branch.
7098 // Slow path marking the object `ref` when it is gray.
7099 SlowPathCode* slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathX86(
7100 instruction, ref, /* unpoison */ true);
7101 AddSlowPath(slow_path);
7102
7103 // We have done the "if" of the gray bit check above, now branch based on the flags.
7104 __ j(kNotZero, slow_path->GetEntryLabel());
Roland Levillain7c1559a2015-12-15 10:55:36 +00007105
7106 // Object* ref = ref_addr->AsMirrorPtr()
7107 __ MaybeUnpoisonHeapReference(ref_reg);
7108
Roland Levillain7c1559a2015-12-15 10:55:36 +00007109 __ Bind(slow_path->GetExitLabel());
7110}
7111
7112void CodeGeneratorX86::GenerateReadBarrierSlow(HInstruction* instruction,
7113 Location out,
7114 Location ref,
7115 Location obj,
7116 uint32_t offset,
7117 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007118 DCHECK(kEmitCompilerReadBarrier);
7119
Roland Levillain7c1559a2015-12-15 10:55:36 +00007120 // Insert a slow path based read barrier *after* the reference load.
7121 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00007122 // If heap poisoning is enabled, the unpoisoning of the loaded
7123 // reference will be carried out by the runtime within the slow
7124 // path.
7125 //
7126 // Note that `ref` currently does not get unpoisoned (when heap
7127 // poisoning is enabled), which is alright as the `ref` argument is
7128 // not used by the artReadBarrierSlow entry point.
7129 //
7130 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
7131 SlowPathCode* slow_path = new (GetGraph()->GetArena())
7132 ReadBarrierForHeapReferenceSlowPathX86(instruction, out, ref, obj, offset, index);
7133 AddSlowPath(slow_path);
7134
Roland Levillain0d5a2812015-11-13 10:07:31 +00007135 __ jmp(slow_path->GetEntryLabel());
7136 __ Bind(slow_path->GetExitLabel());
7137}
7138
Roland Levillain7c1559a2015-12-15 10:55:36 +00007139void CodeGeneratorX86::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
7140 Location out,
7141 Location ref,
7142 Location obj,
7143 uint32_t offset,
7144 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007145 if (kEmitCompilerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007146 // Baker's read barriers shall be handled by the fast path
7147 // (CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier).
7148 DCHECK(!kUseBakerReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007149 // If heap poisoning is enabled, unpoisoning will be taken care of
7150 // by the runtime within the slow path.
Roland Levillain7c1559a2015-12-15 10:55:36 +00007151 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007152 } else if (kPoisonHeapReferences) {
7153 __ UnpoisonHeapReference(out.AsRegister<Register>());
7154 }
7155}
7156
Roland Levillain7c1559a2015-12-15 10:55:36 +00007157void CodeGeneratorX86::GenerateReadBarrierForRootSlow(HInstruction* instruction,
7158 Location out,
7159 Location root) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007160 DCHECK(kEmitCompilerReadBarrier);
7161
Roland Levillain7c1559a2015-12-15 10:55:36 +00007162 // Insert a slow path based read barrier *after* the GC root load.
7163 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00007164 // Note that GC roots are not affected by heap poisoning, so we do
7165 // not need to do anything special for this here.
7166 SlowPathCode* slow_path =
7167 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathX86(instruction, out, root);
7168 AddSlowPath(slow_path);
7169
Roland Levillain0d5a2812015-11-13 10:07:31 +00007170 __ jmp(slow_path->GetEntryLabel());
7171 __ Bind(slow_path->GetExitLabel());
7172}
7173
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007174void LocationsBuilderX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00007175 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00007176 LOG(FATAL) << "Unreachable";
7177}
7178
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007179void InstructionCodeGeneratorX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00007180 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00007181 LOG(FATAL) << "Unreachable";
7182}
7183
Mark Mendellfe57faa2015-09-18 09:26:15 -04007184// Simple implementation of packed switch - generate cascaded compare/jumps.
7185void LocationsBuilderX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7186 LocationSummary* locations =
7187 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
7188 locations->SetInAt(0, Location::RequiresRegister());
7189}
7190
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007191void InstructionCodeGeneratorX86::GenPackedSwitchWithCompares(Register value_reg,
7192 int32_t lower_bound,
7193 uint32_t num_entries,
7194 HBasicBlock* switch_block,
7195 HBasicBlock* default_block) {
7196 // Figure out the correct compare values and jump conditions.
7197 // Handle the first compare/branch as a special case because it might
7198 // jump to the default case.
7199 DCHECK_GT(num_entries, 2u);
7200 Condition first_condition;
7201 uint32_t index;
7202 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
7203 if (lower_bound != 0) {
7204 first_condition = kLess;
7205 __ cmpl(value_reg, Immediate(lower_bound));
7206 __ j(first_condition, codegen_->GetLabelOf(default_block));
7207 __ j(kEqual, codegen_->GetLabelOf(successors[0]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007208
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007209 index = 1;
7210 } else {
7211 // Handle all the compare/jumps below.
7212 first_condition = kBelow;
7213 index = 0;
7214 }
7215
7216 // Handle the rest of the compare/jumps.
7217 for (; index + 1 < num_entries; index += 2) {
7218 int32_t compare_to_value = lower_bound + index + 1;
7219 __ cmpl(value_reg, Immediate(compare_to_value));
7220 // Jump to successors[index] if value < case_value[index].
7221 __ j(first_condition, codegen_->GetLabelOf(successors[index]));
7222 // Jump to successors[index + 1] if value == case_value[index + 1].
7223 __ j(kEqual, codegen_->GetLabelOf(successors[index + 1]));
7224 }
7225
7226 if (index != num_entries) {
7227 // There are an odd number of entries. Handle the last one.
7228 DCHECK_EQ(index + 1, num_entries);
7229 __ cmpl(value_reg, Immediate(lower_bound + index));
7230 __ j(kEqual, codegen_->GetLabelOf(successors[index]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007231 }
7232
7233 // And the default for any other value.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007234 if (!codegen_->GoesToNextBlock(switch_block, default_block)) {
7235 __ jmp(codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007236 }
7237}
7238
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007239void InstructionCodeGeneratorX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7240 int32_t lower_bound = switch_instr->GetStartValue();
7241 uint32_t num_entries = switch_instr->GetNumEntries();
7242 LocationSummary* locations = switch_instr->GetLocations();
7243 Register value_reg = locations->InAt(0).AsRegister<Register>();
7244
7245 GenPackedSwitchWithCompares(value_reg,
7246 lower_bound,
7247 num_entries,
7248 switch_instr->GetBlock(),
7249 switch_instr->GetDefaultBlock());
7250}
7251
Mark Mendell805b3b52015-09-18 14:10:29 -04007252void LocationsBuilderX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
7253 LocationSummary* locations =
7254 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
7255 locations->SetInAt(0, Location::RequiresRegister());
7256
7257 // Constant area pointer.
7258 locations->SetInAt(1, Location::RequiresRegister());
7259
7260 // And the temporary we need.
7261 locations->AddTemp(Location::RequiresRegister());
7262}
7263
7264void InstructionCodeGeneratorX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
7265 int32_t lower_bound = switch_instr->GetStartValue();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007266 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendell805b3b52015-09-18 14:10:29 -04007267 LocationSummary* locations = switch_instr->GetLocations();
7268 Register value_reg = locations->InAt(0).AsRegister<Register>();
7269 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
7270
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007271 if (num_entries <= kPackedSwitchJumpTableThreshold) {
7272 GenPackedSwitchWithCompares(value_reg,
7273 lower_bound,
7274 num_entries,
7275 switch_instr->GetBlock(),
7276 default_block);
7277 return;
7278 }
7279
Mark Mendell805b3b52015-09-18 14:10:29 -04007280 // Optimizing has a jump area.
7281 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
7282 Register constant_area = locations->InAt(1).AsRegister<Register>();
7283
7284 // Remove the bias, if needed.
7285 if (lower_bound != 0) {
7286 __ leal(temp_reg, Address(value_reg, -lower_bound));
7287 value_reg = temp_reg;
7288 }
7289
7290 // Is the value in range?
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007291 DCHECK_GE(num_entries, 1u);
Mark Mendell805b3b52015-09-18 14:10:29 -04007292 __ cmpl(value_reg, Immediate(num_entries - 1));
7293 __ j(kAbove, codegen_->GetLabelOf(default_block));
7294
7295 // We are in the range of the table.
7296 // Load (target-constant_area) from the jump table, indexing by the value.
7297 __ movl(temp_reg, codegen_->LiteralCaseTable(switch_instr, constant_area, value_reg));
7298
7299 // Compute the actual target address by adding in constant_area.
7300 __ addl(temp_reg, constant_area);
7301
7302 // And jump.
7303 __ jmp(temp_reg);
7304}
7305
Mark Mendell0616ae02015-04-17 12:49:27 -04007306void LocationsBuilderX86::VisitX86ComputeBaseMethodAddress(
7307 HX86ComputeBaseMethodAddress* insn) {
7308 LocationSummary* locations =
7309 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
7310 locations->SetOut(Location::RequiresRegister());
7311}
7312
7313void InstructionCodeGeneratorX86::VisitX86ComputeBaseMethodAddress(
7314 HX86ComputeBaseMethodAddress* insn) {
7315 LocationSummary* locations = insn->GetLocations();
7316 Register reg = locations->Out().AsRegister<Register>();
7317
7318 // Generate call to next instruction.
7319 Label next_instruction;
7320 __ call(&next_instruction);
7321 __ Bind(&next_instruction);
7322
7323 // Remember this offset for later use with constant area.
7324 codegen_->SetMethodAddressOffset(GetAssembler()->CodeSize());
7325
7326 // Grab the return address off the stack.
7327 __ popl(reg);
7328}
7329
7330void LocationsBuilderX86::VisitX86LoadFromConstantTable(
7331 HX86LoadFromConstantTable* insn) {
7332 LocationSummary* locations =
7333 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
7334
7335 locations->SetInAt(0, Location::RequiresRegister());
7336 locations->SetInAt(1, Location::ConstantLocation(insn->GetConstant()));
7337
7338 // If we don't need to be materialized, we only need the inputs to be set.
David Brazdilb3e773e2016-01-26 11:28:37 +00007339 if (insn->IsEmittedAtUseSite()) {
Mark Mendell0616ae02015-04-17 12:49:27 -04007340 return;
7341 }
7342
7343 switch (insn->GetType()) {
7344 case Primitive::kPrimFloat:
7345 case Primitive::kPrimDouble:
7346 locations->SetOut(Location::RequiresFpuRegister());
7347 break;
7348
7349 case Primitive::kPrimInt:
7350 locations->SetOut(Location::RequiresRegister());
7351 break;
7352
7353 default:
7354 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
7355 }
7356}
7357
7358void InstructionCodeGeneratorX86::VisitX86LoadFromConstantTable(HX86LoadFromConstantTable* insn) {
David Brazdilb3e773e2016-01-26 11:28:37 +00007359 if (insn->IsEmittedAtUseSite()) {
Mark Mendell0616ae02015-04-17 12:49:27 -04007360 return;
7361 }
7362
7363 LocationSummary* locations = insn->GetLocations();
7364 Location out = locations->Out();
7365 Register const_area = locations->InAt(0).AsRegister<Register>();
7366 HConstant *value = insn->GetConstant();
7367
7368 switch (insn->GetType()) {
7369 case Primitive::kPrimFloat:
7370 __ movss(out.AsFpuRegister<XmmRegister>(),
7371 codegen_->LiteralFloatAddress(value->AsFloatConstant()->GetValue(), const_area));
7372 break;
7373
7374 case Primitive::kPrimDouble:
7375 __ movsd(out.AsFpuRegister<XmmRegister>(),
7376 codegen_->LiteralDoubleAddress(value->AsDoubleConstant()->GetValue(), const_area));
7377 break;
7378
7379 case Primitive::kPrimInt:
7380 __ movl(out.AsRegister<Register>(),
7381 codegen_->LiteralInt32Address(value->AsIntConstant()->GetValue(), const_area));
7382 break;
7383
7384 default:
7385 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
7386 }
7387}
7388
Mark Mendell0616ae02015-04-17 12:49:27 -04007389/**
7390 * Class to handle late fixup of offsets into constant area.
7391 */
Vladimir Marko5233f932015-09-29 19:01:15 +01007392class RIPFixup : public AssemblerFixup, public ArenaObject<kArenaAllocCodeGenerator> {
Mark Mendell0616ae02015-04-17 12:49:27 -04007393 public:
Mark Mendell805b3b52015-09-18 14:10:29 -04007394 RIPFixup(CodeGeneratorX86& codegen, size_t offset)
7395 : codegen_(&codegen), offset_into_constant_area_(offset) {}
7396
7397 protected:
7398 void SetOffset(size_t offset) { offset_into_constant_area_ = offset; }
7399
7400 CodeGeneratorX86* codegen_;
Mark Mendell0616ae02015-04-17 12:49:27 -04007401
7402 private:
7403 void Process(const MemoryRegion& region, int pos) OVERRIDE {
7404 // Patch the correct offset for the instruction. The place to patch is the
7405 // last 4 bytes of the instruction.
7406 // The value to patch is the distance from the offset in the constant area
7407 // from the address computed by the HX86ComputeBaseMethodAddress instruction.
Mark Mendell805b3b52015-09-18 14:10:29 -04007408 int32_t constant_offset = codegen_->ConstantAreaStart() + offset_into_constant_area_;
7409 int32_t relative_position = constant_offset - codegen_->GetMethodAddressOffset();;
Mark Mendell0616ae02015-04-17 12:49:27 -04007410
7411 // Patch in the right value.
7412 region.StoreUnaligned<int32_t>(pos - 4, relative_position);
7413 }
7414
Mark Mendell0616ae02015-04-17 12:49:27 -04007415 // Location in constant area that the fixup refers to.
Mark Mendell805b3b52015-09-18 14:10:29 -04007416 int32_t offset_into_constant_area_;
Mark Mendell0616ae02015-04-17 12:49:27 -04007417};
7418
Mark Mendell805b3b52015-09-18 14:10:29 -04007419/**
7420 * Class to handle late fixup of offsets to a jump table that will be created in the
7421 * constant area.
7422 */
7423class JumpTableRIPFixup : public RIPFixup {
7424 public:
7425 JumpTableRIPFixup(CodeGeneratorX86& codegen, HX86PackedSwitch* switch_instr)
7426 : RIPFixup(codegen, static_cast<size_t>(-1)), switch_instr_(switch_instr) {}
7427
7428 void CreateJumpTable() {
7429 X86Assembler* assembler = codegen_->GetAssembler();
7430
7431 // Ensure that the reference to the jump table has the correct offset.
7432 const int32_t offset_in_constant_table = assembler->ConstantAreaSize();
7433 SetOffset(offset_in_constant_table);
7434
7435 // The label values in the jump table are computed relative to the
7436 // instruction addressing the constant area.
7437 const int32_t relative_offset = codegen_->GetMethodAddressOffset();
7438
7439 // Populate the jump table with the correct values for the jump table.
7440 int32_t num_entries = switch_instr_->GetNumEntries();
7441 HBasicBlock* block = switch_instr_->GetBlock();
7442 const ArenaVector<HBasicBlock*>& successors = block->GetSuccessors();
7443 // The value that we want is the target offset - the position of the table.
7444 for (int32_t i = 0; i < num_entries; i++) {
7445 HBasicBlock* b = successors[i];
7446 Label* l = codegen_->GetLabelOf(b);
7447 DCHECK(l->IsBound());
7448 int32_t offset_to_block = l->Position() - relative_offset;
7449 assembler->AppendInt32(offset_to_block);
7450 }
7451 }
7452
7453 private:
7454 const HX86PackedSwitch* switch_instr_;
7455};
7456
7457void CodeGeneratorX86::Finalize(CodeAllocator* allocator) {
7458 // Generate the constant area if needed.
7459 X86Assembler* assembler = GetAssembler();
7460 if (!assembler->IsConstantAreaEmpty() || !fixups_to_jump_tables_.empty()) {
7461 // Align to 4 byte boundary to reduce cache misses, as the data is 4 and 8
7462 // byte values.
7463 assembler->Align(4, 0);
7464 constant_area_start_ = assembler->CodeSize();
7465
7466 // Populate any jump tables.
7467 for (auto jump_table : fixups_to_jump_tables_) {
7468 jump_table->CreateJumpTable();
7469 }
7470
7471 // And now add the constant area to the generated code.
7472 assembler->AddConstantArea();
7473 }
7474
7475 // And finish up.
7476 CodeGenerator::Finalize(allocator);
7477}
7478
Mark Mendell0616ae02015-04-17 12:49:27 -04007479Address CodeGeneratorX86::LiteralDoubleAddress(double v, Register reg) {
7480 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddDouble(v));
7481 return Address(reg, kDummy32BitOffset, fixup);
7482}
7483
7484Address CodeGeneratorX86::LiteralFloatAddress(float v, Register reg) {
7485 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddFloat(v));
7486 return Address(reg, kDummy32BitOffset, fixup);
7487}
7488
7489Address CodeGeneratorX86::LiteralInt32Address(int32_t v, Register reg) {
7490 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt32(v));
7491 return Address(reg, kDummy32BitOffset, fixup);
7492}
7493
7494Address CodeGeneratorX86::LiteralInt64Address(int64_t v, Register reg) {
7495 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt64(v));
7496 return Address(reg, kDummy32BitOffset, fixup);
7497}
7498
Aart Bika19616e2016-02-01 18:57:58 -08007499void CodeGeneratorX86::Load32BitValue(Register dest, int32_t value) {
7500 if (value == 0) {
7501 __ xorl(dest, dest);
7502 } else {
7503 __ movl(dest, Immediate(value));
7504 }
7505}
7506
7507void CodeGeneratorX86::Compare32BitValue(Register dest, int32_t value) {
7508 if (value == 0) {
7509 __ testl(dest, dest);
7510 } else {
7511 __ cmpl(dest, Immediate(value));
7512 }
7513}
7514
Mark Mendell805b3b52015-09-18 14:10:29 -04007515Address CodeGeneratorX86::LiteralCaseTable(HX86PackedSwitch* switch_instr,
7516 Register reg,
7517 Register value) {
7518 // Create a fixup to be used to create and address the jump table.
7519 JumpTableRIPFixup* table_fixup =
7520 new (GetGraph()->GetArena()) JumpTableRIPFixup(*this, switch_instr);
7521
7522 // We have to populate the jump tables.
7523 fixups_to_jump_tables_.push_back(table_fixup);
7524
7525 // We want a scaled address, as we are extracting the correct offset from the table.
7526 return Address(reg, value, TIMES_4, kDummy32BitOffset, table_fixup);
7527}
7528
Andreas Gampe85b62f22015-09-09 13:15:38 -07007529// TODO: target as memory.
7530void CodeGeneratorX86::MoveFromReturnRegister(Location target, Primitive::Type type) {
7531 if (!target.IsValid()) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007532 DCHECK_EQ(type, Primitive::kPrimVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07007533 return;
7534 }
7535
7536 DCHECK_NE(type, Primitive::kPrimVoid);
7537
7538 Location return_loc = InvokeDexCallingConventionVisitorX86().GetReturnLocation(type);
7539 if (target.Equals(return_loc)) {
7540 return;
7541 }
7542
7543 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
7544 // with the else branch.
7545 if (type == Primitive::kPrimLong) {
7546 HParallelMove parallel_move(GetGraph()->GetArena());
7547 parallel_move.AddMove(return_loc.ToLow(), target.ToLow(), Primitive::kPrimInt, nullptr);
7548 parallel_move.AddMove(return_loc.ToHigh(), target.ToHigh(), Primitive::kPrimInt, nullptr);
7549 GetMoveResolver()->EmitNativeCode(&parallel_move);
7550 } else {
7551 // Let the parallel move resolver take care of all of this.
7552 HParallelMove parallel_move(GetGraph()->GetArena());
7553 parallel_move.AddMove(return_loc, target, type, nullptr);
7554 GetMoveResolver()->EmitNativeCode(&parallel_move);
7555 }
7556}
7557
Roland Levillain4d027112015-07-01 15:41:14 +01007558#undef __
7559
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00007560} // namespace x86
7561} // namespace art