blob: 1369e5f413700cece583414c1a08bd7dc0253908 [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());
372 SaveLiveRegisters(codegen, instruction_->GetLocations());
Serban Constantinescuba45db02016-07-12 22:53:02 +0100373 x86_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000374 CheckEntrypointTypes<kQuickDeoptimize, void, void>();
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700375 }
376
Alexandre Rames9931f312015-06-19 14:47:01 +0100377 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathX86"; }
378
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700379 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700380 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathX86);
381};
382
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100383class ArraySetSlowPathX86 : public SlowPathCode {
384 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000385 explicit ArraySetSlowPathX86(HInstruction* instruction) : SlowPathCode(instruction) {}
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100386
387 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
388 LocationSummary* locations = instruction_->GetLocations();
389 __ Bind(GetEntryLabel());
390 SaveLiveRegisters(codegen, locations);
391
392 InvokeRuntimeCallingConvention calling_convention;
393 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
394 parallel_move.AddMove(
395 locations->InAt(0),
396 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
397 Primitive::kPrimNot,
398 nullptr);
399 parallel_move.AddMove(
400 locations->InAt(1),
401 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
402 Primitive::kPrimInt,
403 nullptr);
404 parallel_move.AddMove(
405 locations->InAt(2),
406 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
407 Primitive::kPrimNot,
408 nullptr);
409 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
410
411 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Serban Constantinescuba45db02016-07-12 22:53:02 +0100412 x86_codegen->InvokeRuntime(kQuickAputObject, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000413 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100414 RestoreLiveRegisters(codegen, locations);
415 __ jmp(GetExitLabel());
416 }
417
418 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathX86"; }
419
420 private:
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100421 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathX86);
422};
423
Roland Levillain7c1559a2015-12-15 10:55:36 +0000424// Slow path marking an object during a read barrier.
425class ReadBarrierMarkSlowPathX86 : public SlowPathCode {
426 public:
Vladimir Marko953437b2016-08-24 08:30:46 +0000427 ReadBarrierMarkSlowPathX86(HInstruction* instruction, Location obj, bool unpoison)
428 : SlowPathCode(instruction), obj_(obj), unpoison_(unpoison) {
Roland Levillain7c1559a2015-12-15 10:55:36 +0000429 DCHECK(kEmitCompilerReadBarrier);
430 }
431
432 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathX86"; }
433
434 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
435 LocationSummary* locations = instruction_->GetLocations();
Roland Levillain02b75802016-07-13 11:54:35 +0100436 Register reg = obj_.AsRegister<Register>();
Roland Levillain7c1559a2015-12-15 10:55:36 +0000437 DCHECK(locations->CanCall());
Roland Levillain02b75802016-07-13 11:54:35 +0100438 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg));
Roland Levillain7c1559a2015-12-15 10:55:36 +0000439 DCHECK(instruction_->IsInstanceFieldGet() ||
440 instruction_->IsStaticFieldGet() ||
441 instruction_->IsArrayGet() ||
Roland Levillain16d9f942016-08-25 17:27:56 +0100442 instruction_->IsArraySet() ||
Roland Levillain7c1559a2015-12-15 10:55:36 +0000443 instruction_->IsLoadClass() ||
444 instruction_->IsLoadString() ||
445 instruction_->IsInstanceOf() ||
Roland Levillain3d312422016-06-23 13:53:42 +0100446 instruction_->IsCheckCast() ||
Roland Levillain0b671c02016-08-19 12:02:34 +0100447 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()) ||
448 (instruction_->IsInvokeStaticOrDirect() && instruction_->GetLocations()->Intrinsified()))
Roland Levillain7c1559a2015-12-15 10:55:36 +0000449 << "Unexpected instruction in read barrier marking slow path: "
450 << instruction_->DebugName();
451
452 __ Bind(GetEntryLabel());
Vladimir Marko953437b2016-08-24 08:30:46 +0000453 if (unpoison_) {
454 // Object* ref = ref_addr->AsMirrorPtr()
455 __ MaybeUnpoisonHeapReference(reg);
456 }
Roland Levillain4359e612016-07-20 11:32:19 +0100457 // No need to save live registers; it's taken care of by the
458 // entrypoint. Also, there is no need to update the stack mask,
459 // as this runtime call will not trigger a garbage collection.
Roland Levillain7c1559a2015-12-15 10:55:36 +0000460 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Roland Levillain02b75802016-07-13 11:54:35 +0100461 DCHECK_NE(reg, ESP);
462 DCHECK(0 <= reg && reg < kNumberOfCpuRegisters) << reg;
463 // "Compact" slow path, saving two moves.
464 //
465 // Instead of using the standard runtime calling convention (input
466 // and output in EAX):
467 //
468 // EAX <- obj
469 // EAX <- ReadBarrierMark(EAX)
470 // obj <- EAX
471 //
472 // we just use rX (the register holding `obj`) as input and output
473 // of a dedicated entrypoint:
474 //
475 // rX <- ReadBarrierMarkRegX(rX)
476 //
477 int32_t entry_point_offset =
Andreas Gampe542451c2016-07-26 09:02:02 -0700478 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kX86PointerSize>(reg);
Roland Levillaindec8f632016-07-22 17:10:06 +0100479 // This runtime call does not require a stack map.
480 x86_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
Roland Levillain7c1559a2015-12-15 10:55:36 +0000481 __ jmp(GetExitLabel());
482 }
483
484 private:
Roland Levillain7c1559a2015-12-15 10:55:36 +0000485 const Location obj_;
Vladimir Marko953437b2016-08-24 08:30:46 +0000486 const bool unpoison_;
Roland Levillain7c1559a2015-12-15 10:55:36 +0000487
488 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathX86);
489};
490
Roland Levillain0d5a2812015-11-13 10:07:31 +0000491// Slow path generating a read barrier for a heap reference.
492class ReadBarrierForHeapReferenceSlowPathX86 : public SlowPathCode {
493 public:
494 ReadBarrierForHeapReferenceSlowPathX86(HInstruction* instruction,
495 Location out,
496 Location ref,
497 Location obj,
498 uint32_t offset,
499 Location index)
David Srbecky9cd6d372016-02-09 15:24:47 +0000500 : SlowPathCode(instruction),
Roland Levillain0d5a2812015-11-13 10:07:31 +0000501 out_(out),
502 ref_(ref),
503 obj_(obj),
504 offset_(offset),
505 index_(index) {
506 DCHECK(kEmitCompilerReadBarrier);
507 // If `obj` is equal to `out` or `ref`, it means the initial object
508 // has been overwritten by (or after) the heap object reference load
509 // to be instrumented, e.g.:
510 //
511 // __ movl(out, Address(out, offset));
Roland Levillain7c1559a2015-12-15 10:55:36 +0000512 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000513 //
514 // In that case, we have lost the information about the original
515 // object, and the emitted read barrier cannot work properly.
516 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
517 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
518 }
519
520 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
521 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
522 LocationSummary* locations = instruction_->GetLocations();
523 Register reg_out = out_.AsRegister<Register>();
524 DCHECK(locations->CanCall());
525 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
Roland Levillain3d312422016-06-23 13:53:42 +0100526 DCHECK(instruction_->IsInstanceFieldGet() ||
527 instruction_->IsStaticFieldGet() ||
528 instruction_->IsArrayGet() ||
529 instruction_->IsInstanceOf() ||
530 instruction_->IsCheckCast() ||
Roland Levillaindec8f632016-07-22 17:10:06 +0100531 (instruction_->IsInvokeVirtual()) && instruction_->GetLocations()->Intrinsified())
Roland Levillain7c1559a2015-12-15 10:55:36 +0000532 << "Unexpected instruction in read barrier for heap reference slow path: "
533 << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000534
535 __ Bind(GetEntryLabel());
536 SaveLiveRegisters(codegen, locations);
537
538 // We may have to change the index's value, but as `index_` is a
539 // constant member (like other "inputs" of this slow path),
540 // introduce a copy of it, `index`.
541 Location index = index_;
542 if (index_.IsValid()) {
Roland Levillain3d312422016-06-23 13:53:42 +0100543 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
Roland Levillain0d5a2812015-11-13 10:07:31 +0000544 if (instruction_->IsArrayGet()) {
545 // Compute the actual memory offset and store it in `index`.
546 Register index_reg = index_.AsRegister<Register>();
547 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg));
548 if (codegen->IsCoreCalleeSaveRegister(index_reg)) {
549 // We are about to change the value of `index_reg` (see the
550 // calls to art::x86::X86Assembler::shll and
551 // art::x86::X86Assembler::AddImmediate below), but it has
552 // not been saved by the previous call to
553 // art::SlowPathCode::SaveLiveRegisters, as it is a
554 // callee-save register --
555 // art::SlowPathCode::SaveLiveRegisters does not consider
556 // callee-save registers, as it has been designed with the
557 // assumption that callee-save registers are supposed to be
558 // handled by the called function. So, as a callee-save
559 // register, `index_reg` _would_ eventually be saved onto
560 // the stack, but it would be too late: we would have
561 // changed its value earlier. Therefore, we manually save
562 // it here into another freely available register,
563 // `free_reg`, chosen of course among the caller-save
564 // registers (as a callee-save `free_reg` register would
565 // exhibit the same problem).
566 //
567 // Note we could have requested a temporary register from
568 // the register allocator instead; but we prefer not to, as
569 // this is a slow path, and we know we can find a
570 // caller-save register that is available.
571 Register free_reg = FindAvailableCallerSaveRegister(codegen);
572 __ movl(free_reg, index_reg);
573 index_reg = free_reg;
574 index = Location::RegisterLocation(index_reg);
575 } else {
576 // The initial register stored in `index_` has already been
577 // saved in the call to art::SlowPathCode::SaveLiveRegisters
578 // (as it is not a callee-save register), so we can freely
579 // use it.
580 }
581 // Shifting the index value contained in `index_reg` by the scale
582 // factor (2) cannot overflow in practice, as the runtime is
583 // unable to allocate object arrays with a size larger than
584 // 2^26 - 1 (that is, 2^28 - 4 bytes).
585 __ shll(index_reg, Immediate(TIMES_4));
586 static_assert(
587 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
588 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
589 __ AddImmediate(index_reg, Immediate(offset_));
590 } else {
Roland Levillain3d312422016-06-23 13:53:42 +0100591 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
592 // intrinsics, `index_` is not shifted by a scale factor of 2
593 // (as in the case of ArrayGet), as it is actually an offset
594 // to an object field within an object.
595 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000596 DCHECK(instruction_->GetLocations()->Intrinsified());
597 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
598 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
599 << instruction_->AsInvoke()->GetIntrinsic();
600 DCHECK_EQ(offset_, 0U);
601 DCHECK(index_.IsRegisterPair());
602 // UnsafeGet's offset location is a register pair, the low
603 // part contains the correct offset.
604 index = index_.ToLow();
605 }
606 }
607
608 // We're moving two or three locations to locations that could
609 // overlap, so we need a parallel move resolver.
610 InvokeRuntimeCallingConvention calling_convention;
611 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
612 parallel_move.AddMove(ref_,
613 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
614 Primitive::kPrimNot,
615 nullptr);
616 parallel_move.AddMove(obj_,
617 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
618 Primitive::kPrimNot,
619 nullptr);
620 if (index.IsValid()) {
621 parallel_move.AddMove(index,
622 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
623 Primitive::kPrimInt,
624 nullptr);
625 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
626 } else {
627 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
628 __ movl(calling_convention.GetRegisterAt(2), Immediate(offset_));
629 }
Serban Constantinescuba45db02016-07-12 22:53:02 +0100630 x86_codegen->InvokeRuntime(kQuickReadBarrierSlow, instruction_, instruction_->GetDexPc(), this);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000631 CheckEntrypointTypes<
632 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
633 x86_codegen->Move32(out_, Location::RegisterLocation(EAX));
634
635 RestoreLiveRegisters(codegen, locations);
636 __ jmp(GetExitLabel());
637 }
638
639 const char* GetDescription() const OVERRIDE { return "ReadBarrierForHeapReferenceSlowPathX86"; }
640
641 private:
642 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
643 size_t ref = static_cast<int>(ref_.AsRegister<Register>());
644 size_t obj = static_cast<int>(obj_.AsRegister<Register>());
645 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
646 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
647 return static_cast<Register>(i);
648 }
649 }
650 // We shall never fail to find a free caller-save register, as
651 // there are more than two core caller-save registers on x86
652 // (meaning it is possible to find one which is different from
653 // `ref` and `obj`).
654 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
655 LOG(FATAL) << "Could not find a free caller-save register";
656 UNREACHABLE();
657 }
658
Roland Levillain0d5a2812015-11-13 10:07:31 +0000659 const Location out_;
660 const Location ref_;
661 const Location obj_;
662 const uint32_t offset_;
663 // An additional location containing an index to an array.
664 // Only used for HArrayGet and the UnsafeGetObject &
665 // UnsafeGetObjectVolatile intrinsics.
666 const Location index_;
667
668 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathX86);
669};
670
671// Slow path generating a read barrier for a GC root.
672class ReadBarrierForRootSlowPathX86 : public SlowPathCode {
673 public:
674 ReadBarrierForRootSlowPathX86(HInstruction* instruction, Location out, Location root)
David Srbecky9cd6d372016-02-09 15:24:47 +0000675 : SlowPathCode(instruction), out_(out), root_(root) {
Roland Levillain7c1559a2015-12-15 10:55:36 +0000676 DCHECK(kEmitCompilerReadBarrier);
677 }
Roland Levillain0d5a2812015-11-13 10:07:31 +0000678
679 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
680 LocationSummary* locations = instruction_->GetLocations();
681 Register reg_out = out_.AsRegister<Register>();
682 DCHECK(locations->CanCall());
683 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
Roland Levillain7c1559a2015-12-15 10:55:36 +0000684 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
685 << "Unexpected instruction in read barrier for GC root slow path: "
686 << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000687
688 __ Bind(GetEntryLabel());
689 SaveLiveRegisters(codegen, locations);
690
691 InvokeRuntimeCallingConvention calling_convention;
692 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
693 x86_codegen->Move32(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), root_);
Serban Constantinescuba45db02016-07-12 22:53:02 +0100694 x86_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000695 instruction_,
696 instruction_->GetDexPc(),
697 this);
698 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
699 x86_codegen->Move32(out_, Location::RegisterLocation(EAX));
700
701 RestoreLiveRegisters(codegen, locations);
702 __ jmp(GetExitLabel());
703 }
704
705 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathX86"; }
706
707 private:
Roland Levillain0d5a2812015-11-13 10:07:31 +0000708 const Location out_;
709 const Location root_;
710
711 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathX86);
712};
713
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100714#undef __
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100715// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
716#define __ down_cast<X86Assembler*>(GetAssembler())-> // NOLINT
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100717
Aart Bike9f37602015-10-09 11:15:55 -0700718inline Condition X86Condition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -0700719 switch (cond) {
720 case kCondEQ: return kEqual;
721 case kCondNE: return kNotEqual;
722 case kCondLT: return kLess;
723 case kCondLE: return kLessEqual;
724 case kCondGT: return kGreater;
725 case kCondGE: return kGreaterEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700726 case kCondB: return kBelow;
727 case kCondBE: return kBelowEqual;
728 case kCondA: return kAbove;
729 case kCondAE: return kAboveEqual;
Dave Allison20dfc792014-06-16 20:44:29 -0700730 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100731 LOG(FATAL) << "Unreachable";
732 UNREACHABLE();
733}
734
Aart Bike9f37602015-10-09 11:15:55 -0700735// Maps signed condition to unsigned condition and FP condition to x86 name.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100736inline Condition X86UnsignedOrFPCondition(IfCondition cond) {
737 switch (cond) {
738 case kCondEQ: return kEqual;
739 case kCondNE: return kNotEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700740 // Signed to unsigned, and FP to x86 name.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100741 case kCondLT: return kBelow;
742 case kCondLE: return kBelowEqual;
743 case kCondGT: return kAbove;
744 case kCondGE: return kAboveEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700745 // Unsigned remain unchanged.
746 case kCondB: return kBelow;
747 case kCondBE: return kBelowEqual;
748 case kCondA: return kAbove;
749 case kCondAE: return kAboveEqual;
Roland Levillain4fa13f62015-07-06 18:11:54 +0100750 }
751 LOG(FATAL) << "Unreachable";
752 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -0700753}
754
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100755void CodeGeneratorX86::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100756 stream << Register(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100757}
758
759void CodeGeneratorX86::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100760 stream << XmmRegister(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100761}
762
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100763size_t CodeGeneratorX86::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
764 __ movl(Address(ESP, stack_index), static_cast<Register>(reg_id));
765 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100766}
767
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100768size_t CodeGeneratorX86::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
769 __ movl(static_cast<Register>(reg_id), Address(ESP, stack_index));
770 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100771}
772
Mark Mendell7c8d0092015-01-26 11:21:33 -0500773size_t CodeGeneratorX86::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
774 __ movsd(Address(ESP, stack_index), XmmRegister(reg_id));
775 return GetFloatingPointSpillSlotSize();
776}
777
778size_t CodeGeneratorX86::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
779 __ movsd(XmmRegister(reg_id), Address(ESP, stack_index));
780 return GetFloatingPointSpillSlotSize();
781}
782
Calin Juravle175dc732015-08-25 15:42:32 +0100783void CodeGeneratorX86::InvokeRuntime(QuickEntrypointEnum entrypoint,
784 HInstruction* instruction,
785 uint32_t dex_pc,
786 SlowPathCode* slow_path) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +0100787 ValidateInvokeRuntime(instruction, slow_path);
Serban Constantinescuba45db02016-07-12 22:53:02 +0100788 GenerateInvokeRuntime(GetThreadOffset<kX86PointerSize>(entrypoint).Int32Value());
789 if (EntrypointRequiresStackMap(entrypoint)) {
790 RecordPcInfo(instruction, dex_pc, slow_path);
791 }
Alexandre Rames8158f282015-08-07 10:26:17 +0100792}
793
Roland Levillaindec8f632016-07-22 17:10:06 +0100794void CodeGeneratorX86::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
795 HInstruction* instruction,
796 SlowPathCode* slow_path) {
797 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
Serban Constantinescuba45db02016-07-12 22:53:02 +0100798 GenerateInvokeRuntime(entry_point_offset);
799}
800
801void CodeGeneratorX86::GenerateInvokeRuntime(int32_t entry_point_offset) {
Roland Levillaindec8f632016-07-22 17:10:06 +0100802 __ fs()->call(Address::Absolute(entry_point_offset));
803}
804
Mark Mendellfb8d2792015-03-31 22:16:59 -0400805CodeGeneratorX86::CodeGeneratorX86(HGraph* graph,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000806 const X86InstructionSetFeatures& isa_features,
807 const CompilerOptions& compiler_options,
808 OptimizingCompilerStats* stats)
Mark Mendell5f874182015-03-04 15:42:45 -0500809 : CodeGenerator(graph,
810 kNumberOfCpuRegisters,
811 kNumberOfXmmRegisters,
812 kNumberOfRegisterPairs,
813 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
814 arraysize(kCoreCalleeSaves))
815 | (1 << kFakeReturnRegister),
Serban Constantinescuecc43662015-08-13 13:33:12 +0100816 0,
817 compiler_options,
818 stats),
Vladimir Marko225b6462015-09-28 12:17:40 +0100819 block_labels_(nullptr),
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100820 location_builder_(graph, this),
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100821 instruction_visitor_(graph, this),
Mark Mendellfb8d2792015-03-31 22:16:59 -0400822 move_resolver_(graph->GetArena(), this),
Vladimir Marko93205e32016-04-13 11:59:46 +0100823 assembler_(graph->GetArena()),
Vladimir Marko58155012015-08-19 12:49:41 +0000824 isa_features_(isa_features),
Vladimir Marko5233f932015-09-29 19:01:15 +0100825 method_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Mark Mendell805b3b52015-09-18 14:10:29 -0400826 relative_call_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000827 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000828 simple_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
829 string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100830 type_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko93205e32016-04-13 11:59:46 +0100831 constant_area_start_(-1),
832 fixups_to_jump_tables_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
833 method_address_offset_(-1) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000834 // Use a fake return address register to mimic Quick.
835 AddAllocatedRegister(Location::RegisterLocation(kFakeReturnRegister));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100836}
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100837
David Brazdil58282f42016-01-14 12:45:10 +0000838void CodeGeneratorX86::SetupBlockedRegisters() const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100839 // Don't allocate the dalvik style register pair passing.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100840 blocked_register_pairs_[ECX_EDX] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100841
842 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100843 blocked_core_registers_[ESP] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100844
Calin Juravle34bacdf2014-10-07 20:23:36 +0100845 UpdateBlockedPairRegisters();
846}
847
848void CodeGeneratorX86::UpdateBlockedPairRegisters() const {
849 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
850 X86ManagedRegister current =
851 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
852 if (blocked_core_registers_[current.AsRegisterPairLow()]
853 || blocked_core_registers_[current.AsRegisterPairHigh()]) {
854 blocked_register_pairs_[i] = true;
855 }
856 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100857}
858
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100859InstructionCodeGeneratorX86::InstructionCodeGeneratorX86(HGraph* graph, CodeGeneratorX86* codegen)
Aart Bik42249c32016-01-07 15:33:50 -0800860 : InstructionCodeGenerator(graph, codegen),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100861 assembler_(codegen->GetAssembler()),
862 codegen_(codegen) {}
863
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100864static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +0100865 return dwarf::Reg::X86Core(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100866}
867
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000868void CodeGeneratorX86::GenerateFrameEntry() {
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100869 __ cfi().SetCurrentCFAOffset(kX86WordSize); // return address
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000870 __ Bind(&frame_entry_label_);
Roland Levillain199f3362014-11-27 17:15:16 +0000871 bool skip_overflow_check =
872 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000873 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Calin Juravle93edf732015-01-20 20:14:07 +0000874
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000875 if (!skip_overflow_check) {
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100876 __ testl(EAX, Address(ESP, -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86))));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100877 RecordPcInfo(nullptr, 0);
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100878 }
879
Mark Mendell5f874182015-03-04 15:42:45 -0500880 if (HasEmptyFrame()) {
881 return;
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000882 }
Mark Mendell5f874182015-03-04 15:42:45 -0500883
884 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
885 Register reg = kCoreCalleeSaves[i];
886 if (allocated_registers_.ContainsCoreRegister(reg)) {
887 __ pushl(reg);
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100888 __ cfi().AdjustCFAOffset(kX86WordSize);
889 __ cfi().RelOffset(DWARFReg(reg), 0);
Mark Mendell5f874182015-03-04 15:42:45 -0500890 }
891 }
892
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100893 int adjust = GetFrameSize() - FrameEntrySpillSize();
894 __ subl(ESP, Immediate(adjust));
895 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100896 __ movl(Address(ESP, kCurrentMethodStackOffset), kMethodRegisterArgument);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000897}
898
899void CodeGeneratorX86::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +0100900 __ cfi().RememberState();
901 if (!HasEmptyFrame()) {
902 int adjust = GetFrameSize() - FrameEntrySpillSize();
903 __ addl(ESP, Immediate(adjust));
904 __ cfi().AdjustCFAOffset(-adjust);
Mark Mendell5f874182015-03-04 15:42:45 -0500905
David Srbeckyc34dc932015-04-12 09:27:43 +0100906 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
907 Register reg = kCoreCalleeSaves[i];
908 if (allocated_registers_.ContainsCoreRegister(reg)) {
909 __ popl(reg);
910 __ cfi().AdjustCFAOffset(-static_cast<int>(kX86WordSize));
911 __ cfi().Restore(DWARFReg(reg));
912 }
Mark Mendell5f874182015-03-04 15:42:45 -0500913 }
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000914 }
David Srbeckyc34dc932015-04-12 09:27:43 +0100915 __ ret();
916 __ cfi().RestoreState();
917 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000918}
919
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100920void CodeGeneratorX86::Bind(HBasicBlock* block) {
921 __ Bind(GetLabelOf(block));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000922}
923
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100924Location InvokeDexCallingConventionVisitorX86::GetReturnLocation(Primitive::Type type) const {
925 switch (type) {
926 case Primitive::kPrimBoolean:
927 case Primitive::kPrimByte:
928 case Primitive::kPrimChar:
929 case Primitive::kPrimShort:
930 case Primitive::kPrimInt:
931 case Primitive::kPrimNot:
932 return Location::RegisterLocation(EAX);
933
934 case Primitive::kPrimLong:
935 return Location::RegisterPairLocation(EAX, EDX);
936
937 case Primitive::kPrimVoid:
938 return Location::NoLocation();
939
940 case Primitive::kPrimDouble:
941 case Primitive::kPrimFloat:
942 return Location::FpuRegisterLocation(XMM0);
943 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +0100944
945 UNREACHABLE();
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100946}
947
948Location InvokeDexCallingConventionVisitorX86::GetMethodLocation() const {
949 return Location::RegisterLocation(kMethodRegisterArgument);
950}
951
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100952Location InvokeDexCallingConventionVisitorX86::GetNextLocation(Primitive::Type type) {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100953 switch (type) {
954 case Primitive::kPrimBoolean:
955 case Primitive::kPrimByte:
956 case Primitive::kPrimChar:
957 case Primitive::kPrimShort:
958 case Primitive::kPrimInt:
959 case Primitive::kPrimNot: {
960 uint32_t index = gp_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000961 stack_index_++;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100962 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100963 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100964 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000965 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100966 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100967 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100968
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000969 case Primitive::kPrimLong: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100970 uint32_t index = gp_index_;
971 gp_index_ += 2;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000972 stack_index_ += 2;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100973 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100974 X86ManagedRegister pair = X86ManagedRegister::FromRegisterPair(
975 calling_convention.GetRegisterPairAt(index));
976 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100977 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000978 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
979 }
980 }
981
982 case Primitive::kPrimFloat: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100983 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000984 stack_index_++;
985 if (index < calling_convention.GetNumberOfFpuRegisters()) {
986 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
987 } else {
988 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
989 }
990 }
991
992 case Primitive::kPrimDouble: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100993 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000994 stack_index_ += 2;
995 if (index < calling_convention.GetNumberOfFpuRegisters()) {
996 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
997 } else {
998 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100999 }
1000 }
1001
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001002 case Primitive::kPrimVoid:
1003 LOG(FATAL) << "Unexpected parameter type " << type;
1004 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001005 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00001006 return Location::NoLocation();
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001007}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001008
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001009void CodeGeneratorX86::Move32(Location destination, Location source) {
1010 if (source.Equals(destination)) {
1011 return;
1012 }
1013 if (destination.IsRegister()) {
1014 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001015 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001016 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001017 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001018 } else {
1019 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001020 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001021 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001022 } else if (destination.IsFpuRegister()) {
1023 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001024 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001025 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001026 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001027 } else {
1028 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001029 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001030 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001031 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001032 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001033 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001034 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001035 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001036 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Mark Mendell7c8d0092015-01-26 11:21:33 -05001037 } else if (source.IsConstant()) {
1038 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001039 int32_t value = GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05001040 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001041 } else {
1042 DCHECK(source.IsStackSlot());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001043 __ pushl(Address(ESP, source.GetStackIndex()));
1044 __ popl(Address(ESP, destination.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001045 }
1046 }
1047}
1048
1049void CodeGeneratorX86::Move64(Location destination, Location source) {
1050 if (source.Equals(destination)) {
1051 return;
1052 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001053 if (destination.IsRegisterPair()) {
1054 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001055 EmitParallelMoves(
1056 Location::RegisterLocation(source.AsRegisterPairHigh<Register>()),
1057 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001058 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001059 Location::RegisterLocation(source.AsRegisterPairLow<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001060 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()),
1061 Primitive::kPrimInt);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001062 } else if (source.IsFpuRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001063 XmmRegister src_reg = source.AsFpuRegister<XmmRegister>();
1064 __ movd(destination.AsRegisterPairLow<Register>(), src_reg);
1065 __ psrlq(src_reg, Immediate(32));
1066 __ movd(destination.AsRegisterPairHigh<Register>(), src_reg);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001067 } else {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001068 // No conflict possible, so just do the moves.
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001069 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001070 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
1071 __ movl(destination.AsRegisterPairHigh<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001072 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
1073 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001074 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05001075 if (source.IsFpuRegister()) {
1076 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
1077 } else if (source.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001078 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01001079 } else if (source.IsRegisterPair()) {
1080 size_t elem_size = Primitive::ComponentSize(Primitive::kPrimInt);
1081 // Create stack space for 2 elements.
1082 __ subl(ESP, Immediate(2 * elem_size));
1083 __ movl(Address(ESP, 0), source.AsRegisterPairLow<Register>());
1084 __ movl(Address(ESP, elem_size), source.AsRegisterPairHigh<Register>());
1085 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
1086 // And remove the temporary stack space we allocated.
1087 __ addl(ESP, Immediate(2 * elem_size));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001088 } else {
1089 LOG(FATAL) << "Unimplemented";
1090 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001091 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001092 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001093 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001094 // No conflict possible, so just do the moves.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001095 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegisterPairLow<Register>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001096 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001097 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001098 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001099 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001100 } else if (source.IsConstant()) {
1101 HConstant* constant = source.GetConstant();
1102 int64_t value;
1103 if (constant->IsLongConstant()) {
1104 value = constant->AsLongConstant()->GetValue();
1105 } else {
1106 DCHECK(constant->IsDoubleConstant());
Roland Levillainda4d79b2015-03-24 14:36:11 +00001107 value = bit_cast<int64_t, double>(constant->AsDoubleConstant()->GetValue());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001108 }
1109 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(Low32Bits(value)));
1110 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), Immediate(High32Bits(value)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001111 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001112 DCHECK(source.IsDoubleStackSlot()) << source;
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001113 EmitParallelMoves(
1114 Location::StackSlot(source.GetStackIndex()),
1115 Location::StackSlot(destination.GetStackIndex()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001116 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001117 Location::StackSlot(source.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001118 Location::StackSlot(destination.GetHighStackIndex(kX86WordSize)),
1119 Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001120 }
1121 }
1122}
1123
Calin Juravle175dc732015-08-25 15:42:32 +01001124void CodeGeneratorX86::MoveConstant(Location location, int32_t value) {
1125 DCHECK(location.IsRegister());
1126 __ movl(location.AsRegister<Register>(), Immediate(value));
1127}
1128
Calin Juravlee460d1d2015-09-29 04:52:17 +01001129void CodeGeneratorX86::MoveLocation(Location dst, Location src, Primitive::Type dst_type) {
David Brazdil74eb1b22015-12-14 11:44:01 +00001130 HParallelMove move(GetGraph()->GetArena());
1131 if (dst_type == Primitive::kPrimLong && !src.IsConstant() && !src.IsFpuRegister()) {
1132 move.AddMove(src.ToLow(), dst.ToLow(), Primitive::kPrimInt, nullptr);
1133 move.AddMove(src.ToHigh(), dst.ToHigh(), Primitive::kPrimInt, nullptr);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001134 } else {
David Brazdil74eb1b22015-12-14 11:44:01 +00001135 move.AddMove(src, dst, dst_type, nullptr);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001136 }
David Brazdil74eb1b22015-12-14 11:44:01 +00001137 GetMoveResolver()->EmitNativeCode(&move);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001138}
1139
1140void CodeGeneratorX86::AddLocationAsTemp(Location location, LocationSummary* locations) {
1141 if (location.IsRegister()) {
1142 locations->AddTemp(location);
1143 } else if (location.IsRegisterPair()) {
1144 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
1145 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
1146 } else {
1147 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1148 }
1149}
1150
David Brazdilfc6a86a2015-06-26 10:33:45 +00001151void InstructionCodeGeneratorX86::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001152 DCHECK(!successor->IsExitBlock());
1153
1154 HBasicBlock* block = got->GetBlock();
1155 HInstruction* previous = got->GetPrevious();
1156
1157 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +00001158 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001159 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1160 return;
1161 }
1162
1163 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1164 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1165 }
1166 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001167 __ jmp(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001168 }
1169}
1170
David Brazdilfc6a86a2015-06-26 10:33:45 +00001171void LocationsBuilderX86::VisitGoto(HGoto* got) {
1172 got->SetLocations(nullptr);
1173}
1174
1175void InstructionCodeGeneratorX86::VisitGoto(HGoto* got) {
1176 HandleGoto(got, got->GetSuccessor());
1177}
1178
1179void LocationsBuilderX86::VisitTryBoundary(HTryBoundary* try_boundary) {
1180 try_boundary->SetLocations(nullptr);
1181}
1182
1183void InstructionCodeGeneratorX86::VisitTryBoundary(HTryBoundary* try_boundary) {
1184 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
1185 if (!successor->IsExitBlock()) {
1186 HandleGoto(try_boundary, successor);
1187 }
1188}
1189
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001190void LocationsBuilderX86::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001191 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001192}
1193
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001194void InstructionCodeGeneratorX86::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001195}
1196
Mark Mendell152408f2015-12-31 12:28:50 -05001197template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001198void InstructionCodeGeneratorX86::GenerateFPJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001199 LabelType* true_label,
1200 LabelType* false_label) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001201 if (cond->IsFPConditionTrueIfNaN()) {
1202 __ j(kUnordered, true_label);
1203 } else if (cond->IsFPConditionFalseIfNaN()) {
1204 __ j(kUnordered, false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001205 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001206 __ j(X86UnsignedOrFPCondition(cond->GetCondition()), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001207}
1208
Mark Mendell152408f2015-12-31 12:28:50 -05001209template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001210void InstructionCodeGeneratorX86::GenerateLongComparesAndJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001211 LabelType* true_label,
1212 LabelType* false_label) {
Mark Mendellc4701932015-04-10 13:18:51 -04001213 LocationSummary* locations = cond->GetLocations();
1214 Location left = locations->InAt(0);
1215 Location right = locations->InAt(1);
1216 IfCondition if_cond = cond->GetCondition();
1217
Mark Mendellc4701932015-04-10 13:18:51 -04001218 Register left_high = left.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001219 Register left_low = left.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001220 IfCondition true_high_cond = if_cond;
1221 IfCondition false_high_cond = cond->GetOppositeCondition();
Aart Bike9f37602015-10-09 11:15:55 -07001222 Condition final_condition = X86UnsignedOrFPCondition(if_cond); // unsigned on lower part
Mark Mendellc4701932015-04-10 13:18:51 -04001223
1224 // Set the conditions for the test, remembering that == needs to be
1225 // decided using the low words.
1226 switch (if_cond) {
1227 case kCondEQ:
Mark Mendellc4701932015-04-10 13:18:51 -04001228 case kCondNE:
Roland Levillain4fa13f62015-07-06 18:11:54 +01001229 // Nothing to do.
Mark Mendellc4701932015-04-10 13:18:51 -04001230 break;
1231 case kCondLT:
1232 false_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -04001233 break;
1234 case kCondLE:
1235 true_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -04001236 break;
1237 case kCondGT:
1238 false_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -04001239 break;
1240 case kCondGE:
1241 true_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -04001242 break;
Aart Bike9f37602015-10-09 11:15:55 -07001243 case kCondB:
1244 false_high_cond = kCondA;
1245 break;
1246 case kCondBE:
1247 true_high_cond = kCondB;
1248 break;
1249 case kCondA:
1250 false_high_cond = kCondB;
1251 break;
1252 case kCondAE:
1253 true_high_cond = kCondA;
1254 break;
Mark Mendellc4701932015-04-10 13:18:51 -04001255 }
1256
1257 if (right.IsConstant()) {
1258 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
Mark Mendellc4701932015-04-10 13:18:51 -04001259 int32_t val_high = High32Bits(value);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001260 int32_t val_low = Low32Bits(value);
Mark Mendellc4701932015-04-10 13:18:51 -04001261
Aart Bika19616e2016-02-01 18:57:58 -08001262 codegen_->Compare32BitValue(left_high, val_high);
Mark Mendellc4701932015-04-10 13:18:51 -04001263 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001264 __ j(X86Condition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001265 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001266 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001267 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001268 __ j(X86Condition(true_high_cond), true_label);
1269 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001270 }
1271 // Must be equal high, so compare the lows.
Aart Bika19616e2016-02-01 18:57:58 -08001272 codegen_->Compare32BitValue(left_low, val_low);
Mark Mendell8659e842016-02-16 10:41:46 -05001273 } else if (right.IsRegisterPair()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001274 Register right_high = right.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001275 Register right_low = right.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001276
1277 __ cmpl(left_high, right_high);
1278 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001279 __ j(X86Condition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001280 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001281 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001282 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001283 __ j(X86Condition(true_high_cond), true_label);
1284 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001285 }
1286 // Must be equal high, so compare the lows.
1287 __ cmpl(left_low, right_low);
Mark Mendell8659e842016-02-16 10:41:46 -05001288 } else {
1289 DCHECK(right.IsDoubleStackSlot());
1290 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
1291 if (if_cond == kCondNE) {
1292 __ j(X86Condition(true_high_cond), true_label);
1293 } else if (if_cond == kCondEQ) {
1294 __ j(X86Condition(false_high_cond), false_label);
1295 } else {
1296 __ j(X86Condition(true_high_cond), true_label);
1297 __ j(X86Condition(false_high_cond), false_label);
1298 }
1299 // Must be equal high, so compare the lows.
1300 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Mark Mendellc4701932015-04-10 13:18:51 -04001301 }
1302 // The last comparison might be unsigned.
1303 __ j(final_condition, true_label);
1304}
1305
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001306void InstructionCodeGeneratorX86::GenerateFPCompare(Location lhs,
1307 Location rhs,
1308 HInstruction* insn,
1309 bool is_double) {
1310 HX86LoadFromConstantTable* const_area = insn->InputAt(1)->AsX86LoadFromConstantTable();
1311 if (is_double) {
1312 if (rhs.IsFpuRegister()) {
1313 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1314 } else if (const_area != nullptr) {
1315 DCHECK(const_area->IsEmittedAtUseSite());
1316 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(),
1317 codegen_->LiteralDoubleAddress(
1318 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
1319 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
1320 } else {
1321 DCHECK(rhs.IsDoubleStackSlot());
1322 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(), Address(ESP, rhs.GetStackIndex()));
1323 }
1324 } else {
1325 if (rhs.IsFpuRegister()) {
1326 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1327 } else if (const_area != nullptr) {
1328 DCHECK(const_area->IsEmittedAtUseSite());
1329 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(),
1330 codegen_->LiteralFloatAddress(
1331 const_area->GetConstant()->AsFloatConstant()->GetValue(),
1332 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
1333 } else {
1334 DCHECK(rhs.IsStackSlot());
1335 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(), Address(ESP, rhs.GetStackIndex()));
1336 }
1337 }
1338}
1339
Mark Mendell152408f2015-12-31 12:28:50 -05001340template<class LabelType>
David Brazdil0debae72015-11-12 18:37:00 +00001341void InstructionCodeGeneratorX86::GenerateCompareTestAndBranch(HCondition* condition,
Mark Mendell152408f2015-12-31 12:28:50 -05001342 LabelType* true_target_in,
1343 LabelType* false_target_in) {
David Brazdil0debae72015-11-12 18:37:00 +00001344 // Generated branching requires both targets to be explicit. If either of the
1345 // targets is nullptr (fallthrough) use and bind `fallthrough_target` instead.
Mark Mendell152408f2015-12-31 12:28:50 -05001346 LabelType fallthrough_target;
1347 LabelType* true_target = true_target_in == nullptr ? &fallthrough_target : true_target_in;
1348 LabelType* false_target = false_target_in == nullptr ? &fallthrough_target : false_target_in;
David Brazdil0debae72015-11-12 18:37:00 +00001349
Mark Mendellc4701932015-04-10 13:18:51 -04001350 LocationSummary* locations = condition->GetLocations();
1351 Location left = locations->InAt(0);
1352 Location right = locations->InAt(1);
1353
Mark Mendellc4701932015-04-10 13:18:51 -04001354 Primitive::Type type = condition->InputAt(0)->GetType();
1355 switch (type) {
1356 case Primitive::kPrimLong:
1357 GenerateLongComparesAndJumps(condition, true_target, false_target);
1358 break;
1359 case Primitive::kPrimFloat:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001360 GenerateFPCompare(left, right, condition, false);
Mark Mendellc4701932015-04-10 13:18:51 -04001361 GenerateFPJumps(condition, true_target, false_target);
1362 break;
1363 case Primitive::kPrimDouble:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001364 GenerateFPCompare(left, right, condition, true);
Mark Mendellc4701932015-04-10 13:18:51 -04001365 GenerateFPJumps(condition, true_target, false_target);
1366 break;
1367 default:
1368 LOG(FATAL) << "Unexpected compare type " << type;
1369 }
1370
David Brazdil0debae72015-11-12 18:37:00 +00001371 if (false_target != &fallthrough_target) {
Mark Mendellc4701932015-04-10 13:18:51 -04001372 __ jmp(false_target);
1373 }
David Brazdil0debae72015-11-12 18:37:00 +00001374
1375 if (fallthrough_target.IsLinked()) {
1376 __ Bind(&fallthrough_target);
1377 }
Mark Mendellc4701932015-04-10 13:18:51 -04001378}
1379
David Brazdil0debae72015-11-12 18:37:00 +00001380static bool AreEflagsSetFrom(HInstruction* cond, HInstruction* branch) {
1381 // Moves may affect the eflags register (move zero uses xorl), so the EFLAGS
1382 // are set only strictly before `branch`. We can't use the eflags on long/FP
1383 // conditions if they are materialized due to the complex branching.
1384 return cond->IsCondition() &&
1385 cond->GetNext() == branch &&
1386 cond->InputAt(0)->GetType() != Primitive::kPrimLong &&
1387 !Primitive::IsFloatingPointType(cond->InputAt(0)->GetType());
1388}
1389
Mark Mendell152408f2015-12-31 12:28:50 -05001390template<class LabelType>
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001391void InstructionCodeGeneratorX86::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00001392 size_t condition_input_index,
Mark Mendell152408f2015-12-31 12:28:50 -05001393 LabelType* true_target,
1394 LabelType* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00001395 HInstruction* cond = instruction->InputAt(condition_input_index);
1396
1397 if (true_target == nullptr && false_target == nullptr) {
1398 // Nothing to do. The code always falls through.
1399 return;
1400 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00001401 // Constant condition, statically compared against "true" (integer value 1).
1402 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00001403 if (true_target != nullptr) {
1404 __ jmp(true_target);
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001405 }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001406 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00001407 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00001408 if (false_target != nullptr) {
1409 __ jmp(false_target);
1410 }
1411 }
1412 return;
1413 }
1414
1415 // The following code generates these patterns:
1416 // (1) true_target == nullptr && false_target != nullptr
1417 // - opposite condition true => branch to false_target
1418 // (2) true_target != nullptr && false_target == nullptr
1419 // - condition true => branch to true_target
1420 // (3) true_target != nullptr && false_target != nullptr
1421 // - condition true => branch to true_target
1422 // - branch to false_target
1423 if (IsBooleanValueOrMaterializedCondition(cond)) {
1424 if (AreEflagsSetFrom(cond, instruction)) {
1425 if (true_target == nullptr) {
1426 __ j(X86Condition(cond->AsCondition()->GetOppositeCondition()), false_target);
1427 } else {
1428 __ j(X86Condition(cond->AsCondition()->GetCondition()), true_target);
1429 }
1430 } else {
1431 // Materialized condition, compare against 0.
1432 Location lhs = instruction->GetLocations()->InAt(condition_input_index);
1433 if (lhs.IsRegister()) {
1434 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
1435 } else {
1436 __ cmpl(Address(ESP, lhs.GetStackIndex()), Immediate(0));
1437 }
1438 if (true_target == nullptr) {
1439 __ j(kEqual, false_target);
1440 } else {
1441 __ j(kNotEqual, true_target);
1442 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001443 }
1444 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001445 // Condition has not been materialized, use its inputs as the comparison and
1446 // its condition as the branch condition.
Mark Mendellb8b97692015-05-22 16:58:19 -04001447 HCondition* condition = cond->AsCondition();
David Brazdil0debae72015-11-12 18:37:00 +00001448
1449 // If this is a long or FP comparison that has been folded into
1450 // the HCondition, generate the comparison directly.
1451 Primitive::Type type = condition->InputAt(0)->GetType();
1452 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
1453 GenerateCompareTestAndBranch(condition, true_target, false_target);
1454 return;
1455 }
1456
1457 Location lhs = condition->GetLocations()->InAt(0);
1458 Location rhs = condition->GetLocations()->InAt(1);
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001459 // LHS is guaranteed to be in a register (see LocationsBuilderX86::HandleCondition).
David Brazdil0debae72015-11-12 18:37:00 +00001460 if (rhs.IsRegister()) {
1461 __ cmpl(lhs.AsRegister<Register>(), rhs.AsRegister<Register>());
1462 } else if (rhs.IsConstant()) {
1463 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
Aart Bika19616e2016-02-01 18:57:58 -08001464 codegen_->Compare32BitValue(lhs.AsRegister<Register>(), constant);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001465 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001466 __ cmpl(lhs.AsRegister<Register>(), Address(ESP, rhs.GetStackIndex()));
1467 }
1468 if (true_target == nullptr) {
1469 __ j(X86Condition(condition->GetOppositeCondition()), false_target);
1470 } else {
Mark Mendellb8b97692015-05-22 16:58:19 -04001471 __ j(X86Condition(condition->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -07001472 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001473 }
David Brazdil0debae72015-11-12 18:37:00 +00001474
1475 // If neither branch falls through (case 3), the conditional branch to `true_target`
1476 // was already emitted (case 2) and we need to emit a jump to `false_target`.
1477 if (true_target != nullptr && false_target != nullptr) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001478 __ jmp(false_target);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001479 }
1480}
1481
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001482void LocationsBuilderX86::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001483 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
1484 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001485 locations->SetInAt(0, Location::Any());
1486 }
1487}
1488
1489void InstructionCodeGeneratorX86::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001490 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
1491 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
1492 Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
1493 nullptr : codegen_->GetLabelOf(true_successor);
1494 Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
1495 nullptr : codegen_->GetLabelOf(false_successor);
1496 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001497}
1498
1499void LocationsBuilderX86::VisitDeoptimize(HDeoptimize* deoptimize) {
1500 LocationSummary* locations = new (GetGraph()->GetArena())
1501 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
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);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004571 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004572
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004573 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4574 locations->SetOut(Location::RequiresFpuRegister());
4575 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004576 // The output overlaps in case of long: we don't want the low move
4577 // to overwrite the object's location. Likewise, in the case of
4578 // an object field get with read barriers enabled, we do not want
4579 // the move to overwrite the object's location, as we need it to emit
4580 // the read barrier.
4581 locations->SetOut(
4582 Location::RequiresRegister(),
4583 (object_field_get_with_read_barrier || instruction->GetType() == Primitive::kPrimLong) ?
4584 Location::kOutputOverlap :
4585 Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004586 }
Calin Juravle52c48962014-12-16 17:02:57 +00004587
4588 if (field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong)) {
4589 // Long values can be loaded atomically into an XMM using movsd.
Roland Levillain7c1559a2015-12-15 10:55:36 +00004590 // So we use an XMM register as a temp to achieve atomicity (first
4591 // load the temp into the XMM and then copy the XMM into the
4592 // output, 32 bits at a time).
Calin Juravle52c48962014-12-16 17:02:57 +00004593 locations->AddTemp(Location::RequiresFpuRegister());
4594 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004595}
4596
Calin Juravle52c48962014-12-16 17:02:57 +00004597void InstructionCodeGeneratorX86::HandleFieldGet(HInstruction* instruction,
4598 const FieldInfo& field_info) {
4599 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004600
Calin Juravle52c48962014-12-16 17:02:57 +00004601 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004602 Location base_loc = locations->InAt(0);
4603 Register base = base_loc.AsRegister<Register>();
Calin Juravle52c48962014-12-16 17:02:57 +00004604 Location out = locations->Out();
4605 bool is_volatile = field_info.IsVolatile();
4606 Primitive::Type field_type = field_info.GetFieldType();
4607 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4608
4609 switch (field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004610 case Primitive::kPrimBoolean: {
Calin Juravle52c48962014-12-16 17:02:57 +00004611 __ movzxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004612 break;
4613 }
4614
4615 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00004616 __ movsxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004617 break;
4618 }
4619
4620 case Primitive::kPrimShort: {
Calin Juravle52c48962014-12-16 17:02:57 +00004621 __ movsxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004622 break;
4623 }
4624
4625 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00004626 __ movzxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004627 break;
4628 }
4629
4630 case Primitive::kPrimInt:
Calin Juravle52c48962014-12-16 17:02:57 +00004631 __ movl(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004632 break;
Roland Levillain7c1559a2015-12-15 10:55:36 +00004633
4634 case Primitive::kPrimNot: {
4635 // /* HeapReference<Object> */ out = *(base + offset)
4636 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004637 // Note that a potential implicit null check is handled in this
4638 // CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier call.
4639 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00004640 instruction, out, base, offset, /* needs_null_check */ true);
Roland Levillain7c1559a2015-12-15 10:55:36 +00004641 if (is_volatile) {
4642 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4643 }
4644 } else {
4645 __ movl(out.AsRegister<Register>(), Address(base, offset));
4646 codegen_->MaybeRecordImplicitNullCheck(instruction);
4647 if (is_volatile) {
4648 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4649 }
4650 // If read barriers are enabled, emit read barriers other than
4651 // Baker's using a slow path (and also unpoison the loaded
4652 // reference, if heap poisoning is enabled).
4653 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
4654 }
4655 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004656 }
4657
4658 case Primitive::kPrimLong: {
Calin Juravle52c48962014-12-16 17:02:57 +00004659 if (is_volatile) {
4660 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4661 __ movsd(temp, Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004662 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004663 __ movd(out.AsRegisterPairLow<Register>(), temp);
4664 __ psrlq(temp, Immediate(32));
4665 __ movd(out.AsRegisterPairHigh<Register>(), temp);
4666 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004667 DCHECK_NE(base, out.AsRegisterPairLow<Register>());
Calin Juravle52c48962014-12-16 17:02:57 +00004668 __ movl(out.AsRegisterPairLow<Register>(), Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004669 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004670 __ movl(out.AsRegisterPairHigh<Register>(), Address(base, kX86WordSize + offset));
4671 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004672 break;
4673 }
4674
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004675 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00004676 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004677 break;
4678 }
4679
4680 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00004681 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004682 break;
4683 }
4684
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004685 case Primitive::kPrimVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00004686 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004687 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004688 }
Calin Juravle52c48962014-12-16 17:02:57 +00004689
Roland Levillain7c1559a2015-12-15 10:55:36 +00004690 if (field_type == Primitive::kPrimNot || field_type == Primitive::kPrimLong) {
4691 // Potential implicit null checks, in the case of reference or
4692 // long fields, are handled in the previous switch statement.
4693 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00004694 codegen_->MaybeRecordImplicitNullCheck(instruction);
4695 }
4696
Calin Juravle52c48962014-12-16 17:02:57 +00004697 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004698 if (field_type == Primitive::kPrimNot) {
4699 // Memory barriers, in the case of references, are also handled
4700 // in the previous switch statement.
4701 } else {
4702 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4703 }
Roland Levillain4d027112015-07-01 15:41:14 +01004704 }
Calin Juravle52c48962014-12-16 17:02:57 +00004705}
4706
4707void LocationsBuilderX86::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
4708 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4709
4710 LocationSummary* locations =
4711 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4712 locations->SetInAt(0, Location::RequiresRegister());
4713 bool is_volatile = field_info.IsVolatile();
4714 Primitive::Type field_type = field_info.GetFieldType();
4715 bool is_byte_type = (field_type == Primitive::kPrimBoolean)
4716 || (field_type == Primitive::kPrimByte);
4717
4718 // The register allocator does not support multiple
4719 // inputs that die at entry with one in a specific register.
4720 if (is_byte_type) {
4721 // Ensure the value is in a byte register.
4722 locations->SetInAt(1, Location::RegisterLocation(EAX));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004723 } else if (Primitive::IsFloatingPointType(field_type)) {
Mark Mendell81489372015-11-04 11:30:41 -05004724 if (is_volatile && field_type == Primitive::kPrimDouble) {
4725 // In order to satisfy the semantics of volatile, this must be a single instruction store.
4726 locations->SetInAt(1, Location::RequiresFpuRegister());
4727 } else {
4728 locations->SetInAt(1, Location::FpuRegisterOrConstant(instruction->InputAt(1)));
4729 }
4730 } else if (is_volatile && field_type == Primitive::kPrimLong) {
4731 // In order to satisfy the semantics of volatile, this must be a single instruction store.
Calin Juravle52c48962014-12-16 17:02:57 +00004732 locations->SetInAt(1, Location::RequiresRegister());
Mark Mendell81489372015-11-04 11:30:41 -05004733
Calin Juravle52c48962014-12-16 17:02:57 +00004734 // 64bits value can be atomically written to an address with movsd and an XMM register.
4735 // We need two XMM registers because there's no easier way to (bit) copy a register pair
4736 // into a single XMM register (we copy each pair part into the XMMs and then interleave them).
4737 // NB: We could make the register allocator understand fp_reg <-> core_reg moves but given the
4738 // isolated cases when we need this it isn't worth adding the extra complexity.
4739 locations->AddTemp(Location::RequiresFpuRegister());
4740 locations->AddTemp(Location::RequiresFpuRegister());
Mark Mendell81489372015-11-04 11:30:41 -05004741 } else {
4742 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4743
4744 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
4745 // Temporary registers for the write barrier.
4746 locations->AddTemp(Location::RequiresRegister()); // May be used for reference poisoning too.
4747 // Ensure the card is in a byte register.
4748 locations->AddTemp(Location::RegisterLocation(ECX));
4749 }
Calin Juravle52c48962014-12-16 17:02:57 +00004750 }
4751}
4752
4753void InstructionCodeGeneratorX86::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004754 const FieldInfo& field_info,
4755 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00004756 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4757
4758 LocationSummary* locations = instruction->GetLocations();
4759 Register base = locations->InAt(0).AsRegister<Register>();
4760 Location value = locations->InAt(1);
4761 bool is_volatile = field_info.IsVolatile();
4762 Primitive::Type field_type = field_info.GetFieldType();
4763 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01004764 bool needs_write_barrier =
4765 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00004766
4767 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004768 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
Calin Juravle52c48962014-12-16 17:02:57 +00004769 }
4770
Mark Mendell81489372015-11-04 11:30:41 -05004771 bool maybe_record_implicit_null_check_done = false;
4772
Calin Juravle52c48962014-12-16 17:02:57 +00004773 switch (field_type) {
4774 case Primitive::kPrimBoolean:
4775 case Primitive::kPrimByte: {
4776 __ movb(Address(base, offset), value.AsRegister<ByteRegister>());
4777 break;
4778 }
4779
4780 case Primitive::kPrimShort:
4781 case Primitive::kPrimChar: {
Mark Mendell81489372015-11-04 11:30:41 -05004782 if (value.IsConstant()) {
4783 int16_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4784 __ movw(Address(base, offset), Immediate(v));
4785 } else {
4786 __ movw(Address(base, offset), value.AsRegister<Register>());
4787 }
Calin Juravle52c48962014-12-16 17:02:57 +00004788 break;
4789 }
4790
4791 case Primitive::kPrimInt:
4792 case Primitive::kPrimNot: {
Roland Levillain4d027112015-07-01 15:41:14 +01004793 if (kPoisonHeapReferences && needs_write_barrier) {
4794 // Note that in the case where `value` is a null reference,
4795 // we do not enter this block, as the reference does not
4796 // need poisoning.
4797 DCHECK_EQ(field_type, Primitive::kPrimNot);
4798 Register temp = locations->GetTemp(0).AsRegister<Register>();
4799 __ movl(temp, value.AsRegister<Register>());
4800 __ PoisonHeapReference(temp);
4801 __ movl(Address(base, offset), temp);
Mark Mendell81489372015-11-04 11:30:41 -05004802 } else if (value.IsConstant()) {
4803 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4804 __ movl(Address(base, offset), Immediate(v));
Roland Levillain4d027112015-07-01 15:41:14 +01004805 } else {
Nicolas Geoffray03971632016-03-17 10:44:24 +00004806 DCHECK(value.IsRegister()) << value;
Roland Levillain4d027112015-07-01 15:41:14 +01004807 __ movl(Address(base, offset), value.AsRegister<Register>());
4808 }
Calin Juravle52c48962014-12-16 17:02:57 +00004809 break;
4810 }
4811
4812 case Primitive::kPrimLong: {
4813 if (is_volatile) {
4814 XmmRegister temp1 = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4815 XmmRegister temp2 = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
4816 __ movd(temp1, value.AsRegisterPairLow<Register>());
4817 __ movd(temp2, value.AsRegisterPairHigh<Register>());
4818 __ punpckldq(temp1, temp2);
4819 __ movsd(Address(base, offset), temp1);
Calin Juravle77520bc2015-01-12 18:45:46 +00004820 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell81489372015-11-04 11:30:41 -05004821 } else if (value.IsConstant()) {
4822 int64_t v = CodeGenerator::GetInt64ValueOf(value.GetConstant());
4823 __ movl(Address(base, offset), Immediate(Low32Bits(v)));
4824 codegen_->MaybeRecordImplicitNullCheck(instruction);
4825 __ movl(Address(base, kX86WordSize + offset), Immediate(High32Bits(v)));
Calin Juravle52c48962014-12-16 17:02:57 +00004826 } else {
4827 __ movl(Address(base, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00004828 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004829 __ movl(Address(base, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
4830 }
Mark Mendell81489372015-11-04 11:30:41 -05004831 maybe_record_implicit_null_check_done = true;
Calin Juravle52c48962014-12-16 17:02:57 +00004832 break;
4833 }
4834
4835 case Primitive::kPrimFloat: {
Mark Mendell81489372015-11-04 11:30:41 -05004836 if (value.IsConstant()) {
4837 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4838 __ movl(Address(base, offset), Immediate(v));
4839 } else {
4840 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4841 }
Calin Juravle52c48962014-12-16 17:02:57 +00004842 break;
4843 }
4844
4845 case Primitive::kPrimDouble: {
Mark Mendell81489372015-11-04 11:30:41 -05004846 if (value.IsConstant()) {
4847 int64_t v = CodeGenerator::GetInt64ValueOf(value.GetConstant());
4848 __ movl(Address(base, offset), Immediate(Low32Bits(v)));
4849 codegen_->MaybeRecordImplicitNullCheck(instruction);
4850 __ movl(Address(base, kX86WordSize + offset), Immediate(High32Bits(v)));
4851 maybe_record_implicit_null_check_done = true;
4852 } else {
4853 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4854 }
Calin Juravle52c48962014-12-16 17:02:57 +00004855 break;
4856 }
4857
4858 case Primitive::kPrimVoid:
4859 LOG(FATAL) << "Unreachable type " << field_type;
4860 UNREACHABLE();
4861 }
4862
Mark Mendell81489372015-11-04 11:30:41 -05004863 if (!maybe_record_implicit_null_check_done) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004864 codegen_->MaybeRecordImplicitNullCheck(instruction);
4865 }
4866
Roland Levillain4d027112015-07-01 15:41:14 +01004867 if (needs_write_barrier) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004868 Register temp = locations->GetTemp(0).AsRegister<Register>();
4869 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004870 codegen_->MarkGCCard(temp, card, base, value.AsRegister<Register>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00004871 }
4872
Calin Juravle52c48962014-12-16 17:02:57 +00004873 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004874 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
Calin Juravle52c48962014-12-16 17:02:57 +00004875 }
4876}
4877
4878void LocationsBuilderX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4879 HandleFieldGet(instruction, instruction->GetFieldInfo());
4880}
4881
4882void InstructionCodeGeneratorX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4883 HandleFieldGet(instruction, instruction->GetFieldInfo());
4884}
4885
4886void LocationsBuilderX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
4887 HandleFieldSet(instruction, instruction->GetFieldInfo());
4888}
4889
4890void InstructionCodeGeneratorX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004891 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00004892}
4893
4894void LocationsBuilderX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
4895 HandleFieldSet(instruction, instruction->GetFieldInfo());
4896}
4897
4898void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004899 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00004900}
4901
4902void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4903 HandleFieldGet(instruction, instruction->GetFieldInfo());
4904}
4905
4906void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4907 HandleFieldGet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004908}
4909
Calin Juravlee460d1d2015-09-29 04:52:17 +01004910void LocationsBuilderX86::VisitUnresolvedInstanceFieldGet(
4911 HUnresolvedInstanceFieldGet* instruction) {
4912 FieldAccessCallingConventionX86 calling_convention;
4913 codegen_->CreateUnresolvedFieldLocationSummary(
4914 instruction, instruction->GetFieldType(), calling_convention);
4915}
4916
4917void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldGet(
4918 HUnresolvedInstanceFieldGet* instruction) {
4919 FieldAccessCallingConventionX86 calling_convention;
4920 codegen_->GenerateUnresolvedFieldAccess(instruction,
4921 instruction->GetFieldType(),
4922 instruction->GetFieldIndex(),
4923 instruction->GetDexPc(),
4924 calling_convention);
4925}
4926
4927void LocationsBuilderX86::VisitUnresolvedInstanceFieldSet(
4928 HUnresolvedInstanceFieldSet* instruction) {
4929 FieldAccessCallingConventionX86 calling_convention;
4930 codegen_->CreateUnresolvedFieldLocationSummary(
4931 instruction, instruction->GetFieldType(), calling_convention);
4932}
4933
4934void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldSet(
4935 HUnresolvedInstanceFieldSet* instruction) {
4936 FieldAccessCallingConventionX86 calling_convention;
4937 codegen_->GenerateUnresolvedFieldAccess(instruction,
4938 instruction->GetFieldType(),
4939 instruction->GetFieldIndex(),
4940 instruction->GetDexPc(),
4941 calling_convention);
4942}
4943
4944void LocationsBuilderX86::VisitUnresolvedStaticFieldGet(
4945 HUnresolvedStaticFieldGet* instruction) {
4946 FieldAccessCallingConventionX86 calling_convention;
4947 codegen_->CreateUnresolvedFieldLocationSummary(
4948 instruction, instruction->GetFieldType(), calling_convention);
4949}
4950
4951void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldGet(
4952 HUnresolvedStaticFieldGet* instruction) {
4953 FieldAccessCallingConventionX86 calling_convention;
4954 codegen_->GenerateUnresolvedFieldAccess(instruction,
4955 instruction->GetFieldType(),
4956 instruction->GetFieldIndex(),
4957 instruction->GetDexPc(),
4958 calling_convention);
4959}
4960
4961void LocationsBuilderX86::VisitUnresolvedStaticFieldSet(
4962 HUnresolvedStaticFieldSet* instruction) {
4963 FieldAccessCallingConventionX86 calling_convention;
4964 codegen_->CreateUnresolvedFieldLocationSummary(
4965 instruction, instruction->GetFieldType(), calling_convention);
4966}
4967
4968void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldSet(
4969 HUnresolvedStaticFieldSet* instruction) {
4970 FieldAccessCallingConventionX86 calling_convention;
4971 codegen_->GenerateUnresolvedFieldAccess(instruction,
4972 instruction->GetFieldType(),
4973 instruction->GetFieldIndex(),
4974 instruction->GetDexPc(),
4975 calling_convention);
4976}
4977
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004978void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004979 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4980 ? LocationSummary::kCallOnSlowPath
4981 : LocationSummary::kNoCall;
4982 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
4983 Location loc = codegen_->IsImplicitNullCheckAllowed(instruction)
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004984 ? Location::RequiresRegister()
4985 : Location::Any();
4986 locations->SetInAt(0, loc);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004987 if (instruction->HasUses()) {
4988 locations->SetOut(Location::SameAsFirstInput());
4989 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004990}
4991
Calin Juravle2ae48182016-03-16 14:05:09 +00004992void CodeGeneratorX86::GenerateImplicitNullCheck(HNullCheck* instruction) {
4993 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004994 return;
4995 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004996 LocationSummary* locations = instruction->GetLocations();
4997 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00004998
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004999 __ testl(EAX, Address(obj.AsRegister<Register>(), 0));
Calin Juravle2ae48182016-03-16 14:05:09 +00005000 RecordPcInfo(instruction, instruction->GetDexPc());
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005001}
5002
Calin Juravle2ae48182016-03-16 14:05:09 +00005003void CodeGeneratorX86::GenerateExplicitNullCheck(HNullCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07005004 SlowPathCode* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00005005 AddSlowPath(slow_path);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005006
5007 LocationSummary* locations = instruction->GetLocations();
5008 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005009
5010 if (obj.IsRegister()) {
Mark Mendell42514f62015-03-31 11:34:22 -04005011 __ testl(obj.AsRegister<Register>(), obj.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005012 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005013 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005014 } else {
5015 DCHECK(obj.IsConstant()) << obj;
David Brazdil77a48ae2015-09-15 12:34:04 +00005016 DCHECK(obj.GetConstant()->IsNullConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005017 __ jmp(slow_path->GetEntryLabel());
5018 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005019 }
5020 __ j(kEqual, slow_path->GetEntryLabel());
5021}
5022
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005023void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00005024 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005025}
5026
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005027void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005028 bool object_array_get_with_read_barrier =
5029 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005030 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00005031 new (GetGraph()->GetArena()) LocationSummary(instruction,
5032 object_array_get_with_read_barrier ?
5033 LocationSummary::kCallOnSlowPath :
5034 LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005035 locations->SetInAt(0, Location::RequiresRegister());
5036 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005037 if (Primitive::IsFloatingPointType(instruction->GetType())) {
5038 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5039 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005040 // The output overlaps in case of long: we don't want the low move
5041 // to overwrite the array's location. Likewise, in the case of an
5042 // object array get with read barriers enabled, we do not want the
5043 // move to overwrite the array's location, as we need it to emit
5044 // the read barrier.
5045 locations->SetOut(
5046 Location::RequiresRegister(),
5047 (instruction->GetType() == Primitive::kPrimLong || object_array_get_with_read_barrier) ?
5048 Location::kOutputOverlap :
5049 Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005050 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005051}
5052
5053void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
5054 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005055 Location obj_loc = locations->InAt(0);
5056 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005057 Location index = locations->InAt(1);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005058 Location out_loc = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005059 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005060
Calin Juravle77520bc2015-01-12 18:45:46 +00005061 Primitive::Type type = instruction->GetType();
5062 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005063 case Primitive::kPrimBoolean: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005064 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005065 if (index.IsConstant()) {
5066 __ movzxb(out, Address(obj,
5067 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
5068 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005069 __ movzxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005070 }
5071 break;
5072 }
5073
5074 case Primitive::kPrimByte: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005075 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005076 if (index.IsConstant()) {
5077 __ movsxb(out, Address(obj,
5078 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
5079 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005080 __ movsxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005081 }
5082 break;
5083 }
5084
5085 case Primitive::kPrimShort: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005086 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005087 if (index.IsConstant()) {
5088 __ movsxw(out, Address(obj,
5089 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
5090 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005091 __ movsxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005092 }
5093 break;
5094 }
5095
5096 case Primitive::kPrimChar: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005097 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005098 if (index.IsConstant()) {
5099 __ movzxw(out, Address(obj,
5100 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
5101 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005102 __ movzxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005103 }
5104 break;
5105 }
5106
Roland Levillain7c1559a2015-12-15 10:55:36 +00005107 case Primitive::kPrimInt: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005108 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005109 if (index.IsConstant()) {
5110 __ movl(out, Address(obj,
5111 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
5112 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005113 __ movl(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005114 }
5115 break;
5116 }
5117
Roland Levillain7c1559a2015-12-15 10:55:36 +00005118 case Primitive::kPrimNot: {
5119 static_assert(
5120 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
5121 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain7c1559a2015-12-15 10:55:36 +00005122 // /* HeapReference<Object> */ out =
5123 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
5124 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005125 // Note that a potential implicit null check is handled in this
5126 // CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier call.
5127 codegen_->GenerateArrayLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00005128 instruction, out_loc, obj, data_offset, index, /* needs_null_check */ true);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005129 } else {
5130 Register out = out_loc.AsRegister<Register>();
5131 if (index.IsConstant()) {
5132 uint32_t offset =
5133 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
5134 __ movl(out, Address(obj, offset));
5135 codegen_->MaybeRecordImplicitNullCheck(instruction);
5136 // If read barriers are enabled, emit read barriers other than
5137 // Baker's using a slow path (and also unpoison the loaded
5138 // reference, if heap poisoning is enabled).
5139 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
5140 } else {
5141 __ movl(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
5142 codegen_->MaybeRecordImplicitNullCheck(instruction);
5143 // If read barriers are enabled, emit read barriers other than
5144 // Baker's using a slow path (and also unpoison the loaded
5145 // reference, if heap poisoning is enabled).
5146 codegen_->MaybeGenerateReadBarrierSlow(
5147 instruction, out_loc, out_loc, obj_loc, data_offset, index);
5148 }
5149 }
5150 break;
5151 }
5152
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005153 case Primitive::kPrimLong: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005154 DCHECK_NE(obj, out_loc.AsRegisterPairLow<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005155 if (index.IsConstant()) {
5156 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Roland Levillain7c1559a2015-12-15 10:55:36 +00005157 __ movl(out_loc.AsRegisterPairLow<Register>(), Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005158 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005159 __ movl(out_loc.AsRegisterPairHigh<Register>(), Address(obj, offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005160 } else {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005161 __ movl(out_loc.AsRegisterPairLow<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005162 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005163 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005164 __ movl(out_loc.AsRegisterPairHigh<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005165 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005166 }
5167 break;
5168 }
5169
Mark Mendell7c8d0092015-01-26 11:21:33 -05005170 case Primitive::kPrimFloat: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005171 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Mark Mendell7c8d0092015-01-26 11:21:33 -05005172 if (index.IsConstant()) {
5173 __ movss(out, Address(obj,
5174 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
5175 } else {
5176 __ movss(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
5177 }
5178 break;
5179 }
5180
5181 case Primitive::kPrimDouble: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005182 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Mark Mendell7c8d0092015-01-26 11:21:33 -05005183 if (index.IsConstant()) {
5184 __ movsd(out, Address(obj,
5185 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
5186 } else {
5187 __ movsd(out, Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
5188 }
5189 break;
5190 }
5191
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005192 case Primitive::kPrimVoid:
Calin Juravle77520bc2015-01-12 18:45:46 +00005193 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07005194 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005195 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005196
Roland Levillain7c1559a2015-12-15 10:55:36 +00005197 if (type == Primitive::kPrimNot || type == Primitive::kPrimLong) {
5198 // Potential implicit null checks, in the case of reference or
5199 // long arrays, are handled in the previous switch statement.
5200 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00005201 codegen_->MaybeRecordImplicitNullCheck(instruction);
5202 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005203}
5204
5205void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01005206 Primitive::Type value_type = instruction->GetComponentType();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005207
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005208 bool needs_write_barrier =
5209 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Roland Levillain0d5a2812015-11-13 10:07:31 +00005210 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005211
Nicolas Geoffray39468442014-09-02 15:17:15 +01005212 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
5213 instruction,
Vladimir Marko8d49fd72016-08-25 15:20:47 +01005214 may_need_runtime_call_for_type_check ?
Roland Levillain0d5a2812015-11-13 10:07:31 +00005215 LocationSummary::kCallOnSlowPath :
5216 LocationSummary::kNoCall);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005217
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005218 bool is_byte_type = (value_type == Primitive::kPrimBoolean)
5219 || (value_type == Primitive::kPrimByte);
5220 // We need the inputs to be different than the output in case of long operation.
5221 // In case of a byte operation, the register allocator does not support multiple
5222 // inputs that die at entry with one in a specific register.
5223 locations->SetInAt(0, Location::RequiresRegister());
5224 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
5225 if (is_byte_type) {
5226 // Ensure the value is in a byte register.
5227 locations->SetInAt(2, Location::ByteRegisterOrConstant(EAX, instruction->InputAt(2)));
5228 } else if (Primitive::IsFloatingPointType(value_type)) {
Mark Mendell81489372015-11-04 11:30:41 -05005229 locations->SetInAt(2, Location::FpuRegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005230 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005231 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
5232 }
5233 if (needs_write_barrier) {
5234 // Temporary registers for the write barrier.
Roland Levillain16d9f942016-08-25 17:27:56 +01005235 // These registers may be used for Baker read barriers too.
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005236 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
5237 // Ensure the card is in a byte register.
Roland Levillain4f6b0b52015-11-23 19:29:22 +00005238 locations->AddTemp(Location::RegisterLocation(ECX));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005239 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005240}
5241
5242void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
5243 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005244 Location array_loc = locations->InAt(0);
5245 Register array = array_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005246 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005247 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005248 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005249 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
5250 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5251 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005252 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005253 bool needs_write_barrier =
5254 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005255
5256 switch (value_type) {
5257 case Primitive::kPrimBoolean:
5258 case Primitive::kPrimByte: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005259 uint32_t offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
5260 Address address = index.IsConstant()
5261 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + offset)
5262 : Address(array, index.AsRegister<Register>(), TIMES_1, offset);
5263 if (value.IsRegister()) {
5264 __ movb(address, value.AsRegister<ByteRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005265 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005266 __ movb(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005267 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005268 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005269 break;
5270 }
5271
5272 case Primitive::kPrimShort:
5273 case Primitive::kPrimChar: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005274 uint32_t offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
5275 Address address = index.IsConstant()
5276 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + offset)
5277 : Address(array, index.AsRegister<Register>(), TIMES_2, offset);
5278 if (value.IsRegister()) {
5279 __ movw(address, value.AsRegister<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005280 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005281 __ movw(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005282 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005283 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005284 break;
5285 }
5286
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005287 case Primitive::kPrimNot: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005288 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
5289 Address address = index.IsConstant()
5290 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
5291 : Address(array, index.AsRegister<Register>(), TIMES_4, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005292
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005293 if (!value.IsRegister()) {
5294 // Just setting null.
5295 DCHECK(instruction->InputAt(2)->IsNullConstant());
5296 DCHECK(value.IsConstant()) << value;
5297 __ movl(address, Immediate(0));
Calin Juravle77520bc2015-01-12 18:45:46 +00005298 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005299 DCHECK(!needs_write_barrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005300 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005301 break;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005302 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005303
5304 DCHECK(needs_write_barrier);
5305 Register register_value = value.AsRegister<Register>();
Roland Levillain16d9f942016-08-25 17:27:56 +01005306 // We cannot use a NearLabel for `done`, as its range may be too
5307 // short when Baker read barriers are enabled.
5308 Label done;
5309 NearLabel not_null, do_put;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005310 SlowPathCode* slow_path = nullptr;
Roland Levillain16d9f942016-08-25 17:27:56 +01005311 Location temp_loc = locations->GetTemp(0);
5312 Register temp = temp_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005313 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005314 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathX86(instruction);
5315 codegen_->AddSlowPath(slow_path);
5316 if (instruction->GetValueCanBeNull()) {
5317 __ testl(register_value, register_value);
5318 __ j(kNotEqual, &not_null);
5319 __ movl(address, Immediate(0));
5320 codegen_->MaybeRecordImplicitNullCheck(instruction);
5321 __ jmp(&done);
5322 __ Bind(&not_null);
5323 }
5324
Roland Levillain0d5a2812015-11-13 10:07:31 +00005325 if (kEmitCompilerReadBarrier) {
Roland Levillain16d9f942016-08-25 17:27:56 +01005326 if (!kUseBakerReadBarrier) {
5327 // When (non-Baker) read barriers are enabled, the type
5328 // checking instrumentation requires two read barriers
5329 // generated by CodeGeneratorX86::GenerateReadBarrierSlow:
5330 //
5331 // __ movl(temp2, temp);
5332 // // /* HeapReference<Class> */ temp = temp->component_type_
5333 // __ movl(temp, Address(temp, component_offset));
5334 // codegen_->GenerateReadBarrierSlow(
5335 // instruction, temp_loc, temp_loc, temp2_loc, component_offset);
5336 //
5337 // // /* HeapReference<Class> */ temp2 = register_value->klass_
5338 // __ movl(temp2, Address(register_value, class_offset));
5339 // codegen_->GenerateReadBarrierSlow(
5340 // instruction, temp2_loc, temp2_loc, value, class_offset, temp_loc);
5341 //
5342 // __ cmpl(temp, temp2);
5343 //
5344 // However, the second read barrier may trash `temp`, as it
5345 // is a temporary register, and as such would not be saved
5346 // along with live registers before calling the runtime (nor
5347 // restored afterwards). So in this case, we bail out and
5348 // delegate the work to the array set slow path.
5349 //
5350 // TODO: Extend the register allocator to support a new
5351 // "(locally) live temp" location so as to avoid always
5352 // going into the slow path when read barriers are enabled?
5353 //
5354 // There is no such problem with Baker read barriers (see below).
5355 __ jmp(slow_path->GetEntryLabel());
5356 } else {
5357 Location temp2_loc = locations->GetTemp(1);
5358 Register temp2 = temp2_loc.AsRegister<Register>();
5359 // /* HeapReference<Class> */ temp = array->klass_
5360 codegen_->GenerateFieldLoadWithBakerReadBarrier(
5361 instruction, temp_loc, array, class_offset, /* needs_null_check */ true);
5362
5363 // /* HeapReference<Class> */ temp = temp->component_type_
5364 codegen_->GenerateFieldLoadWithBakerReadBarrier(
5365 instruction, temp_loc, temp, component_offset, /* needs_null_check */ false);
5366 // Register `temp` is not trashed by the read barrier
5367 // emitted by GenerateFieldLoadWithBakerReadBarrier below,
5368 // as that method produces a call to a ReadBarrierMarkRegX
5369 // entry point, which saves all potentially live registers,
5370 // including temporaries such a `temp`.
5371 // /* HeapReference<Class> */ temp2 = register_value->klass_
5372 codegen_->GenerateFieldLoadWithBakerReadBarrier(
5373 instruction, temp2_loc, register_value, class_offset, /* needs_null_check */ false);
5374 // If heap poisoning is enabled, `temp` and `temp2` have
5375 // been unpoisoned by the the previous calls to
5376 // CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier.
5377 __ cmpl(temp, temp2);
5378
5379 if (instruction->StaticTypeOfArrayIsObjectArray()) {
5380 __ j(kEqual, &do_put);
5381 // We do not need to emit a read barrier for the
5382 // following heap reference load, as `temp` is only used
5383 // in a comparison with null below, and this reference
5384 // is not kept afterwards. Also, if heap poisoning is
5385 // enabled, there is no need to unpoison that heap
5386 // reference for the same reason (comparison with null).
5387 __ cmpl(Address(temp, super_offset), Immediate(0));
5388 __ j(kNotEqual, slow_path->GetEntryLabel());
5389 __ Bind(&do_put);
5390 } else {
5391 __ j(kNotEqual, slow_path->GetEntryLabel());
5392 }
5393 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005394 } else {
Roland Levillain16d9f942016-08-25 17:27:56 +01005395 // Non read barrier code.
5396
Roland Levillain0d5a2812015-11-13 10:07:31 +00005397 // /* HeapReference<Class> */ temp = array->klass_
5398 __ movl(temp, Address(array, class_offset));
5399 codegen_->MaybeRecordImplicitNullCheck(instruction);
5400 __ MaybeUnpoisonHeapReference(temp);
5401
5402 // /* HeapReference<Class> */ temp = temp->component_type_
5403 __ movl(temp, Address(temp, component_offset));
5404 // If heap poisoning is enabled, no need to unpoison `temp`
5405 // nor the object reference in `register_value->klass`, as
5406 // we are comparing two poisoned references.
5407 __ cmpl(temp, Address(register_value, class_offset));
5408
5409 if (instruction->StaticTypeOfArrayIsObjectArray()) {
5410 __ j(kEqual, &do_put);
5411 // If heap poisoning is enabled, the `temp` reference has
5412 // not been unpoisoned yet; unpoison it now.
5413 __ MaybeUnpoisonHeapReference(temp);
5414
Roland Levillain16d9f942016-08-25 17:27:56 +01005415 // If heap poisoning is enabled, no need to unpoison the
5416 // heap reference loaded below, as it is only used for a
5417 // comparison with null.
5418 __ cmpl(Address(temp, super_offset), Immediate(0));
Roland Levillain0d5a2812015-11-13 10:07:31 +00005419 __ j(kNotEqual, slow_path->GetEntryLabel());
5420 __ Bind(&do_put);
5421 } else {
5422 __ j(kNotEqual, slow_path->GetEntryLabel());
5423 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005424 }
5425 }
5426
5427 if (kPoisonHeapReferences) {
5428 __ movl(temp, register_value);
5429 __ PoisonHeapReference(temp);
5430 __ movl(address, temp);
5431 } else {
5432 __ movl(address, register_value);
5433 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005434 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005435 codegen_->MaybeRecordImplicitNullCheck(instruction);
5436 }
5437
5438 Register card = locations->GetTemp(1).AsRegister<Register>();
5439 codegen_->MarkGCCard(
5440 temp, card, array, value.AsRegister<Register>(), instruction->GetValueCanBeNull());
5441 __ Bind(&done);
5442
5443 if (slow_path != nullptr) {
5444 __ Bind(slow_path->GetExitLabel());
5445 }
5446
5447 break;
5448 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005449
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005450 case Primitive::kPrimInt: {
5451 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
5452 Address address = index.IsConstant()
5453 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
5454 : Address(array, index.AsRegister<Register>(), TIMES_4, offset);
5455 if (value.IsRegister()) {
5456 __ movl(address, value.AsRegister<Register>());
5457 } else {
5458 DCHECK(value.IsConstant()) << value;
5459 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
5460 __ movl(address, Immediate(v));
5461 }
5462 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005463 break;
5464 }
5465
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005466 case Primitive::kPrimLong: {
5467 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005468 if (index.IsConstant()) {
5469 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005470 if (value.IsRegisterPair()) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005471 __ movl(Address(array, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00005472 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005473 __ movl(Address(array, offset + kX86WordSize), value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005474 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005475 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005476 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005477 __ movl(Address(array, offset), Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00005478 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005479 __ movl(Address(array, offset + kX86WordSize), Immediate(High32Bits(val)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005480 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005481 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005482 if (value.IsRegisterPair()) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005483 __ movl(Address(array, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005484 value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00005485 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005486 __ movl(Address(array, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005487 value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005488 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005489 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005490 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005491 __ movl(Address(array, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005492 Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00005493 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005494 __ movl(Address(array, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005495 Immediate(High32Bits(val)));
5496 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005497 }
5498 break;
5499 }
5500
Mark Mendell7c8d0092015-01-26 11:21:33 -05005501 case Primitive::kPrimFloat: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005502 uint32_t offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
5503 Address address = index.IsConstant()
5504 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
5505 : Address(array, index.AsRegister<Register>(), TIMES_4, offset);
Mark Mendell81489372015-11-04 11:30:41 -05005506 if (value.IsFpuRegister()) {
5507 __ movss(address, value.AsFpuRegister<XmmRegister>());
5508 } else {
5509 DCHECK(value.IsConstant());
5510 int32_t v = bit_cast<int32_t, float>(value.GetConstant()->AsFloatConstant()->GetValue());
5511 __ movl(address, Immediate(v));
5512 }
5513 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005514 break;
5515 }
5516
5517 case Primitive::kPrimDouble: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005518 uint32_t offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
5519 Address address = index.IsConstant()
5520 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + offset)
5521 : Address(array, index.AsRegister<Register>(), TIMES_8, offset);
Mark Mendell81489372015-11-04 11:30:41 -05005522 if (value.IsFpuRegister()) {
5523 __ movsd(address, value.AsFpuRegister<XmmRegister>());
5524 } else {
5525 DCHECK(value.IsConstant());
5526 Address address_hi = index.IsConstant() ?
5527 Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) +
5528 offset + kX86WordSize) :
5529 Address(array, index.AsRegister<Register>(), TIMES_8, offset + kX86WordSize);
5530 int64_t v = bit_cast<int64_t, double>(value.GetConstant()->AsDoubleConstant()->GetValue());
5531 __ movl(address, Immediate(Low32Bits(v)));
5532 codegen_->MaybeRecordImplicitNullCheck(instruction);
5533 __ movl(address_hi, Immediate(High32Bits(v)));
5534 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005535 break;
5536 }
5537
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005538 case Primitive::kPrimVoid:
5539 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07005540 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005541 }
5542}
5543
5544void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
5545 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005546 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellee8d9712016-07-12 11:13:15 -04005547 if (!instruction->IsEmittedAtUseSite()) {
5548 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5549 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005550}
5551
5552void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
Mark Mendellee8d9712016-07-12 11:13:15 -04005553 if (instruction->IsEmittedAtUseSite()) {
5554 return;
5555 }
5556
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005557 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01005558 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00005559 Register obj = locations->InAt(0).AsRegister<Register>();
5560 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005561 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005562 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005563}
5564
5565void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00005566 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
5567 ? LocationSummary::kCallOnSlowPath
5568 : LocationSummary::kNoCall;
5569 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Mark Mendellf60c90b2015-03-04 15:12:59 -05005570 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendellee8d9712016-07-12 11:13:15 -04005571 HInstruction* length = instruction->InputAt(1);
5572 if (!length->IsEmittedAtUseSite()) {
5573 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
5574 }
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005575 if (instruction->HasUses()) {
5576 locations->SetOut(Location::SameAsFirstInput());
5577 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005578}
5579
5580void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
5581 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05005582 Location index_loc = locations->InAt(0);
5583 Location length_loc = locations->InAt(1);
Andreas Gampe85b62f22015-09-09 13:15:38 -07005584 SlowPathCode* slow_path =
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01005585 new (GetGraph()->GetArena()) BoundsCheckSlowPathX86(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005586
Mark Mendell99dbd682015-04-22 16:18:52 -04005587 if (length_loc.IsConstant()) {
5588 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
5589 if (index_loc.IsConstant()) {
5590 // BCE will remove the bounds check if we are guarenteed to pass.
5591 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5592 if (index < 0 || index >= length) {
5593 codegen_->AddSlowPath(slow_path);
5594 __ jmp(slow_path->GetEntryLabel());
5595 } else {
5596 // Some optimization after BCE may have generated this, and we should not
5597 // generate a bounds check if it is a valid range.
5598 }
5599 return;
5600 }
5601
5602 // We have to reverse the jump condition because the length is the constant.
5603 Register index_reg = index_loc.AsRegister<Register>();
5604 __ cmpl(index_reg, Immediate(length));
5605 codegen_->AddSlowPath(slow_path);
5606 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005607 } else {
Mark Mendellee8d9712016-07-12 11:13:15 -04005608 HInstruction* array_length = instruction->InputAt(1);
5609 if (array_length->IsEmittedAtUseSite()) {
5610 // Address the length field in the array.
5611 DCHECK(array_length->IsArrayLength());
5612 uint32_t len_offset = CodeGenerator::GetArrayLengthOffset(array_length->AsArrayLength());
5613 Location array_loc = array_length->GetLocations()->InAt(0);
5614 Address array_len(array_loc.AsRegister<Register>(), len_offset);
5615 if (index_loc.IsConstant()) {
5616 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5617 __ cmpl(array_len, Immediate(value));
5618 } else {
5619 __ cmpl(array_len, index_loc.AsRegister<Register>());
5620 }
5621 codegen_->MaybeRecordImplicitNullCheck(array_length);
Mark Mendell99dbd682015-04-22 16:18:52 -04005622 } else {
Mark Mendellee8d9712016-07-12 11:13:15 -04005623 Register length = length_loc.AsRegister<Register>();
5624 if (index_loc.IsConstant()) {
5625 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5626 __ cmpl(length, Immediate(value));
5627 } else {
5628 __ cmpl(length, index_loc.AsRegister<Register>());
5629 }
Mark Mendell99dbd682015-04-22 16:18:52 -04005630 }
5631 codegen_->AddSlowPath(slow_path);
5632 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005633 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005634}
5635
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005636void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005637 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005638}
5639
5640void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005641 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5642}
5643
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005644void LocationsBuilderX86::VisitSuspendCheck(HSuspendCheck* instruction) {
5645 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
5646}
5647
5648void InstructionCodeGeneratorX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005649 HBasicBlock* block = instruction->GetBlock();
5650 if (block->GetLoopInformation() != nullptr) {
5651 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5652 // The back edge will generate the suspend check.
5653 return;
5654 }
5655 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5656 // The goto will generate the suspend check.
5657 return;
5658 }
5659 GenerateSuspendCheck(instruction, nullptr);
5660}
5661
5662void InstructionCodeGeneratorX86::GenerateSuspendCheck(HSuspendCheck* instruction,
5663 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005664 SuspendCheckSlowPathX86* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005665 down_cast<SuspendCheckSlowPathX86*>(instruction->GetSlowPath());
5666 if (slow_path == nullptr) {
5667 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathX86(instruction, successor);
5668 instruction->SetSlowPath(slow_path);
5669 codegen_->AddSlowPath(slow_path);
5670 if (successor != nullptr) {
5671 DCHECK(successor->IsLoopHeader());
5672 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
5673 }
5674 } else {
5675 DCHECK_EQ(slow_path->GetSuccessor(), successor);
5676 }
5677
Andreas Gampe542451c2016-07-26 09:02:02 -07005678 __ fs()->cmpw(Address::Absolute(Thread::ThreadFlagsOffset<kX86PointerSize>().Int32Value()),
Roland Levillain7c1559a2015-12-15 10:55:36 +00005679 Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005680 if (successor == nullptr) {
5681 __ j(kNotEqual, slow_path->GetEntryLabel());
5682 __ Bind(slow_path->GetReturnLabel());
5683 } else {
5684 __ j(kEqual, codegen_->GetLabelOf(successor));
5685 __ jmp(slow_path->GetEntryLabel());
5686 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005687}
5688
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005689X86Assembler* ParallelMoveResolverX86::GetAssembler() const {
5690 return codegen_->GetAssembler();
5691}
5692
Mark Mendell7c8d0092015-01-26 11:21:33 -05005693void ParallelMoveResolverX86::MoveMemoryToMemory32(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005694 ScratchRegisterScope ensure_scratch(
5695 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5696 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5697 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5698 __ movl(temp_reg, Address(ESP, src + stack_offset));
5699 __ movl(Address(ESP, dst + stack_offset), temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005700}
5701
5702void ParallelMoveResolverX86::MoveMemoryToMemory64(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005703 ScratchRegisterScope ensure_scratch(
5704 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5705 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5706 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5707 __ movl(temp_reg, Address(ESP, src + stack_offset));
5708 __ movl(Address(ESP, dst + stack_offset), temp_reg);
5709 __ movl(temp_reg, Address(ESP, src + stack_offset + kX86WordSize));
5710 __ movl(Address(ESP, dst + stack_offset + kX86WordSize), temp_reg);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005711}
5712
5713void ParallelMoveResolverX86::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005714 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005715 Location source = move->GetSource();
5716 Location destination = move->GetDestination();
5717
5718 if (source.IsRegister()) {
5719 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005720 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
David Brazdil74eb1b22015-12-14 11:44:01 +00005721 } else if (destination.IsFpuRegister()) {
5722 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005723 } else {
5724 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00005725 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005726 }
David Brazdil74eb1b22015-12-14 11:44:01 +00005727 } else if (source.IsRegisterPair()) {
5728 size_t elem_size = Primitive::ComponentSize(Primitive::kPrimInt);
5729 // Create stack space for 2 elements.
5730 __ subl(ESP, Immediate(2 * elem_size));
5731 __ movl(Address(ESP, 0), source.AsRegisterPairLow<Register>());
5732 __ movl(Address(ESP, elem_size), source.AsRegisterPairHigh<Register>());
5733 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
5734 // And remove the temporary stack space we allocated.
5735 __ addl(ESP, Immediate(2 * elem_size));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005736 } else if (source.IsFpuRegister()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00005737 if (destination.IsRegister()) {
5738 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
5739 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005740 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
David Brazdil74eb1b22015-12-14 11:44:01 +00005741 } else if (destination.IsRegisterPair()) {
5742 XmmRegister src_reg = source.AsFpuRegister<XmmRegister>();
5743 __ movd(destination.AsRegisterPairLow<Register>(), src_reg);
5744 __ psrlq(src_reg, Immediate(32));
5745 __ movd(destination.AsRegisterPairHigh<Register>(), src_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005746 } else if (destination.IsStackSlot()) {
5747 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
5748 } else {
5749 DCHECK(destination.IsDoubleStackSlot());
5750 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
5751 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005752 } else if (source.IsStackSlot()) {
5753 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005754 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005755 } else if (destination.IsFpuRegister()) {
5756 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005757 } else {
5758 DCHECK(destination.IsStackSlot());
Mark Mendell7c8d0092015-01-26 11:21:33 -05005759 MoveMemoryToMemory32(destination.GetStackIndex(), source.GetStackIndex());
5760 }
5761 } else if (source.IsDoubleStackSlot()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00005762 if (destination.IsRegisterPair()) {
5763 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
5764 __ movl(destination.AsRegisterPairHigh<Register>(),
5765 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
5766 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005767 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
5768 } else {
5769 DCHECK(destination.IsDoubleStackSlot()) << destination;
5770 MoveMemoryToMemory64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005771 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005772 } else if (source.IsConstant()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005773 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00005774 if (constant->IsIntConstant() || constant->IsNullConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05005775 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005776 if (destination.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05005777 if (value == 0) {
5778 __ xorl(destination.AsRegister<Register>(), destination.AsRegister<Register>());
5779 } else {
5780 __ movl(destination.AsRegister<Register>(), Immediate(value));
5781 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005782 } else {
5783 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell09b84632015-02-13 17:48:38 -05005784 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005785 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005786 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005787 float fp_value = constant->AsFloatConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00005788 int32_t value = bit_cast<int32_t, float>(fp_value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005789 Immediate imm(value);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005790 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005791 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
5792 if (value == 0) {
5793 // Easy handling of 0.0.
5794 __ xorps(dest, dest);
5795 } else {
5796 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005797 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5798 Register temp = static_cast<Register>(ensure_scratch.GetRegister());
5799 __ movl(temp, Immediate(value));
5800 __ movd(dest, temp);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005801 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005802 } else {
5803 DCHECK(destination.IsStackSlot()) << destination;
5804 __ movl(Address(ESP, destination.GetStackIndex()), imm);
5805 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005806 } else if (constant->IsLongConstant()) {
5807 int64_t value = constant->AsLongConstant()->GetValue();
5808 int32_t low_value = Low32Bits(value);
5809 int32_t high_value = High32Bits(value);
5810 Immediate low(low_value);
5811 Immediate high(high_value);
5812 if (destination.IsDoubleStackSlot()) {
5813 __ movl(Address(ESP, destination.GetStackIndex()), low);
5814 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
5815 } else {
5816 __ movl(destination.AsRegisterPairLow<Register>(), low);
5817 __ movl(destination.AsRegisterPairHigh<Register>(), high);
5818 }
5819 } else {
5820 DCHECK(constant->IsDoubleConstant());
5821 double dbl_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00005822 int64_t value = bit_cast<int64_t, double>(dbl_value);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005823 int32_t low_value = Low32Bits(value);
5824 int32_t high_value = High32Bits(value);
5825 Immediate low(low_value);
5826 Immediate high(high_value);
5827 if (destination.IsFpuRegister()) {
5828 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
5829 if (value == 0) {
5830 // Easy handling of 0.0.
5831 __ xorpd(dest, dest);
5832 } else {
5833 __ pushl(high);
5834 __ pushl(low);
5835 __ movsd(dest, Address(ESP, 0));
5836 __ addl(ESP, Immediate(8));
5837 }
5838 } else {
5839 DCHECK(destination.IsDoubleStackSlot()) << destination;
5840 __ movl(Address(ESP, destination.GetStackIndex()), low);
5841 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
5842 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005843 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005844 } else {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00005845 LOG(FATAL) << "Unimplemented move: " << destination << " <- " << source;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005846 }
5847}
5848
Mark Mendella5c19ce2015-04-01 12:51:05 -04005849void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005850 Register suggested_scratch = reg == EAX ? EBX : EAX;
5851 ScratchRegisterScope ensure_scratch(
5852 this, reg, suggested_scratch, codegen_->GetNumberOfCoreRegisters());
5853
5854 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5855 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset));
5856 __ movl(Address(ESP, mem + stack_offset), reg);
5857 __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005858}
5859
Mark Mendell7c8d0092015-01-26 11:21:33 -05005860void ParallelMoveResolverX86::Exchange32(XmmRegister reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005861 ScratchRegisterScope ensure_scratch(
5862 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5863
5864 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5865 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5866 __ movl(temp_reg, Address(ESP, mem + stack_offset));
5867 __ movss(Address(ESP, mem + stack_offset), reg);
5868 __ movd(reg, temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005869}
5870
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005871void ParallelMoveResolverX86::Exchange(int mem1, int mem2) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005872 ScratchRegisterScope ensure_scratch1(
5873 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005874
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005875 Register suggested_scratch = ensure_scratch1.GetRegister() == EAX ? EBX : EAX;
5876 ScratchRegisterScope ensure_scratch2(
5877 this, ensure_scratch1.GetRegister(), suggested_scratch, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005878
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005879 int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0;
5880 stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0;
5881 __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset));
5882 __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset));
5883 __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister()));
5884 __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005885}
5886
5887void ParallelMoveResolverX86::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005888 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005889 Location source = move->GetSource();
5890 Location destination = move->GetDestination();
5891
5892 if (source.IsRegister() && destination.IsRegister()) {
Mark Mendell90979812015-07-28 16:41:21 -04005893 // Use XOR swap algorithm to avoid serializing XCHG instruction or using a temporary.
5894 DCHECK_NE(destination.AsRegister<Register>(), source.AsRegister<Register>());
5895 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
5896 __ xorl(source.AsRegister<Register>(), destination.AsRegister<Register>());
5897 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005898 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005899 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005900 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005901 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005902 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
5903 Exchange(destination.GetStackIndex(), source.GetStackIndex());
Mark Mendell7c8d0092015-01-26 11:21:33 -05005904 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
5905 // Use XOR Swap algorithm to avoid a temporary.
5906 DCHECK_NE(source.reg(), destination.reg());
5907 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
5908 __ xorpd(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
5909 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
5910 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
5911 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
5912 } else if (destination.IsFpuRegister() && source.IsStackSlot()) {
5913 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005914 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
5915 // Take advantage of the 16 bytes in the XMM register.
5916 XmmRegister reg = source.AsFpuRegister<XmmRegister>();
5917 Address stack(ESP, destination.GetStackIndex());
5918 // Load the double into the high doubleword.
5919 __ movhpd(reg, stack);
5920
5921 // Store the low double into the destination.
5922 __ movsd(stack, reg);
5923
5924 // Move the high double to the low double.
5925 __ psrldq(reg, Immediate(8));
5926 } else if (destination.IsFpuRegister() && source.IsDoubleStackSlot()) {
5927 // Take advantage of the 16 bytes in the XMM register.
5928 XmmRegister reg = destination.AsFpuRegister<XmmRegister>();
5929 Address stack(ESP, source.GetStackIndex());
5930 // Load the double into the high doubleword.
5931 __ movhpd(reg, stack);
5932
5933 // Store the low double into the destination.
5934 __ movsd(stack, reg);
5935
5936 // Move the high double to the low double.
5937 __ psrldq(reg, Immediate(8));
5938 } else if (destination.IsDoubleStackSlot() && source.IsDoubleStackSlot()) {
5939 Exchange(destination.GetStackIndex(), source.GetStackIndex());
5940 Exchange(destination.GetHighStackIndex(kX86WordSize), source.GetHighStackIndex(kX86WordSize));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005941 } else {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005942 LOG(FATAL) << "Unimplemented: source: " << source << ", destination: " << destination;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005943 }
5944}
5945
5946void ParallelMoveResolverX86::SpillScratch(int reg) {
5947 __ pushl(static_cast<Register>(reg));
5948}
5949
5950void ParallelMoveResolverX86::RestoreScratch(int reg) {
5951 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005952}
5953
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005954HLoadClass::LoadKind CodeGeneratorX86::GetSupportedLoadClassKind(
5955 HLoadClass::LoadKind desired_class_load_kind) {
5956 if (kEmitCompilerReadBarrier) {
5957 switch (desired_class_load_kind) {
5958 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
5959 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
5960 case HLoadClass::LoadKind::kBootImageAddress:
5961 // TODO: Implement for read barrier.
5962 return HLoadClass::LoadKind::kDexCacheViaMethod;
5963 default:
5964 break;
5965 }
5966 }
5967 switch (desired_class_load_kind) {
5968 case HLoadClass::LoadKind::kReferrersClass:
5969 break;
5970 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
5971 DCHECK(!GetCompilerOptions().GetCompilePic());
5972 break;
5973 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
5974 DCHECK(GetCompilerOptions().GetCompilePic());
5975 FALLTHROUGH_INTENDED;
5976 case HLoadClass::LoadKind::kDexCachePcRelative:
5977 DCHECK(!Runtime::Current()->UseJitCompilation()); // Note: boot image is also non-JIT.
5978 // We disable pc-relative load when there is an irreducible loop, as the optimization
5979 // is incompatible with it.
5980 // TODO: Create as many X86ComputeBaseMethodAddress instructions as needed for methods
5981 // with irreducible loops.
5982 if (GetGraph()->HasIrreducibleLoops()) {
5983 return HLoadClass::LoadKind::kDexCacheViaMethod;
5984 }
5985 break;
5986 case HLoadClass::LoadKind::kBootImageAddress:
5987 break;
5988 case HLoadClass::LoadKind::kDexCacheAddress:
5989 DCHECK(Runtime::Current()->UseJitCompilation());
5990 break;
5991 case HLoadClass::LoadKind::kDexCacheViaMethod:
5992 break;
5993 }
5994 return desired_class_load_kind;
5995}
5996
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005997void LocationsBuilderX86::VisitLoadClass(HLoadClass* cls) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005998 if (cls->NeedsAccessCheck()) {
5999 InvokeRuntimeCallingConvention calling_convention;
6000 CodeGenerator::CreateLoadClassLocationSummary(
6001 cls,
6002 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
6003 Location::RegisterLocation(EAX),
6004 /* code_generator_supports_read_barrier */ true);
6005 return;
6006 }
6007
6008 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || kEmitCompilerReadBarrier)
6009 ? LocationSummary::kCallOnSlowPath
6010 : LocationSummary::kNoCall;
6011 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
6012 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
6013 if (load_kind == HLoadClass::LoadKind::kReferrersClass ||
6014 load_kind == HLoadClass::LoadKind::kDexCacheViaMethod ||
6015 load_kind == HLoadClass::LoadKind::kBootImageLinkTimePcRelative ||
6016 load_kind == HLoadClass::LoadKind::kDexCachePcRelative) {
6017 locations->SetInAt(0, Location::RequiresRegister());
6018 }
6019 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006020}
6021
6022void InstructionCodeGeneratorX86::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01006023 LocationSummary* locations = cls->GetLocations();
Calin Juravle98893e12015-10-02 21:05:03 +01006024 if (cls->NeedsAccessCheck()) {
6025 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex());
Serban Constantinescuba45db02016-07-12 22:53:02 +01006026 codegen_->InvokeRuntime(kQuickInitializeTypeAndVerifyAccess, cls, cls->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006027 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Calin Juravle580b6092015-10-06 17:35:58 +01006028 return;
6029 }
6030
Roland Levillain0d5a2812015-11-13 10:07:31 +00006031 Location out_loc = locations->Out();
6032 Register out = out_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006033
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006034 bool generate_null_check = false;
6035 switch (cls->GetLoadKind()) {
6036 case HLoadClass::LoadKind::kReferrersClass: {
6037 DCHECK(!cls->CanCallRuntime());
6038 DCHECK(!cls->MustGenerateClinitCheck());
6039 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
6040 Register current_method = locations->InAt(0).AsRegister<Register>();
6041 GenerateGcRootFieldLoad(
6042 cls, out_loc, Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
6043 break;
6044 }
6045 case HLoadClass::LoadKind::kBootImageLinkTimeAddress: {
6046 DCHECK(!kEmitCompilerReadBarrier);
6047 __ movl(out, Immediate(/* placeholder */ 0));
6048 codegen_->RecordTypePatch(cls);
6049 break;
6050 }
6051 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
6052 DCHECK(!kEmitCompilerReadBarrier);
6053 Register method_address = locations->InAt(0).AsRegister<Register>();
6054 __ leal(out, Address(method_address, CodeGeneratorX86::kDummy32BitOffset));
6055 codegen_->RecordTypePatch(cls);
6056 break;
6057 }
6058 case HLoadClass::LoadKind::kBootImageAddress: {
6059 DCHECK(!kEmitCompilerReadBarrier);
6060 DCHECK_NE(cls->GetAddress(), 0u);
6061 uint32_t address = dchecked_integral_cast<uint32_t>(cls->GetAddress());
6062 __ movl(out, Immediate(address));
6063 codegen_->RecordSimplePatch();
6064 break;
6065 }
6066 case HLoadClass::LoadKind::kDexCacheAddress: {
6067 DCHECK_NE(cls->GetAddress(), 0u);
6068 uint32_t address = dchecked_integral_cast<uint32_t>(cls->GetAddress());
6069 // /* GcRoot<mirror::Class> */ out = *address
6070 GenerateGcRootFieldLoad(cls, out_loc, Address::Absolute(address));
6071 generate_null_check = !cls->IsInDexCache();
6072 break;
6073 }
6074 case HLoadClass::LoadKind::kDexCachePcRelative: {
6075 Register base_reg = locations->InAt(0).AsRegister<Register>();
6076 uint32_t offset = cls->GetDexCacheElementOffset();
6077 Label* fixup_label = codegen_->NewPcRelativeDexCacheArrayPatch(cls->GetDexFile(), offset);
6078 // /* GcRoot<mirror::Class> */ out = *(base + offset) /* PC-relative */
6079 GenerateGcRootFieldLoad(
6080 cls, out_loc, Address(base_reg, CodeGeneratorX86::kDummy32BitOffset), fixup_label);
6081 generate_null_check = !cls->IsInDexCache();
6082 break;
6083 }
6084 case HLoadClass::LoadKind::kDexCacheViaMethod: {
6085 // /* GcRoot<mirror::Class>[] */ out =
6086 // current_method.ptr_sized_fields_->dex_cache_resolved_types_
6087 Register current_method = locations->InAt(0).AsRegister<Register>();
6088 __ movl(out, Address(current_method,
6089 ArtMethod::DexCacheResolvedTypesOffset(kX86PointerSize).Int32Value()));
6090 // /* GcRoot<mirror::Class> */ out = out[type_index]
6091 GenerateGcRootFieldLoad(
6092 cls, out_loc, Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
6093 generate_null_check = !cls->IsInDexCache();
6094 break;
6095 }
6096 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006097
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006098 if (generate_null_check || cls->MustGenerateClinitCheck()) {
6099 DCHECK(cls->CanCallRuntime());
6100 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
6101 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
6102 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006103
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006104 if (generate_null_check) {
6105 __ testl(out, out);
6106 __ j(kEqual, slow_path->GetEntryLabel());
6107 }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006108
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006109 if (cls->MustGenerateClinitCheck()) {
6110 GenerateClassInitializationCheck(slow_path, out);
6111 } else {
6112 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006113 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006114 }
6115}
6116
6117void LocationsBuilderX86::VisitClinitCheck(HClinitCheck* check) {
6118 LocationSummary* locations =
6119 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
6120 locations->SetInAt(0, Location::RequiresRegister());
6121 if (check->HasUses()) {
6122 locations->SetOut(Location::SameAsFirstInput());
6123 }
6124}
6125
6126void InstructionCodeGeneratorX86::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006127 // We assume the class to not be null.
Andreas Gampe85b62f22015-09-09 13:15:38 -07006128 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006129 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006130 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00006131 GenerateClassInitializationCheck(slow_path,
6132 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006133}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006134
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006135void InstructionCodeGeneratorX86::GenerateClassInitializationCheck(
Andreas Gampe85b62f22015-09-09 13:15:38 -07006136 SlowPathCode* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006137 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
6138 Immediate(mirror::Class::kStatusInitialized));
6139 __ j(kLess, slow_path->GetEntryLabel());
6140 __ Bind(slow_path->GetExitLabel());
6141 // No need for memory fence, thanks to the X86 memory model.
6142}
6143
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006144HLoadString::LoadKind CodeGeneratorX86::GetSupportedLoadStringKind(
6145 HLoadString::LoadKind desired_string_load_kind) {
6146 if (kEmitCompilerReadBarrier) {
6147 switch (desired_string_load_kind) {
6148 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
6149 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
6150 case HLoadString::LoadKind::kBootImageAddress:
6151 // TODO: Implement for read barrier.
6152 return HLoadString::LoadKind::kDexCacheViaMethod;
6153 default:
6154 break;
6155 }
6156 }
6157 switch (desired_string_load_kind) {
6158 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
6159 DCHECK(!GetCompilerOptions().GetCompilePic());
6160 break;
6161 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
6162 DCHECK(GetCompilerOptions().GetCompilePic());
6163 FALLTHROUGH_INTENDED;
6164 case HLoadString::LoadKind::kDexCachePcRelative:
Calin Juravleffc87072016-04-20 14:22:09 +01006165 DCHECK(!Runtime::Current()->UseJitCompilation()); // Note: boot image is also non-JIT.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006166 // We disable pc-relative load when there is an irreducible loop, as the optimization
6167 // is incompatible with it.
6168 // TODO: Create as many X86ComputeBaseMethodAddress instructions as needed for methods
6169 // with irreducible loops.
6170 if (GetGraph()->HasIrreducibleLoops()) {
6171 return HLoadString::LoadKind::kDexCacheViaMethod;
6172 }
6173 break;
6174 case HLoadString::LoadKind::kBootImageAddress:
6175 break;
6176 case HLoadString::LoadKind::kDexCacheAddress:
Calin Juravleffc87072016-04-20 14:22:09 +01006177 DCHECK(Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006178 break;
6179 case HLoadString::LoadKind::kDexCacheViaMethod:
6180 break;
6181 }
6182 return desired_string_load_kind;
6183}
6184
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006185void LocationsBuilderX86::VisitLoadString(HLoadString* load) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006186 LocationSummary::CallKind call_kind = (load->NeedsEnvironment() || kEmitCompilerReadBarrier)
Nicolas Geoffray917d0162015-11-24 18:25:35 +00006187 ? LocationSummary::kCallOnSlowPath
6188 : LocationSummary::kNoCall;
6189 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006190 HLoadString::LoadKind load_kind = load->GetLoadKind();
6191 if (load_kind == HLoadString::LoadKind::kDexCacheViaMethod ||
6192 load_kind == HLoadString::LoadKind::kBootImageLinkTimePcRelative ||
6193 load_kind == HLoadString::LoadKind::kDexCachePcRelative) {
6194 locations->SetInAt(0, Location::RequiresRegister());
6195 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006196 locations->SetOut(Location::RequiresRegister());
6197}
6198
6199void InstructionCodeGeneratorX86::VisitLoadString(HLoadString* load) {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01006200 LocationSummary* locations = load->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006201 Location out_loc = locations->Out();
6202 Register out = out_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006203
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006204 switch (load->GetLoadKind()) {
6205 case HLoadString::LoadKind::kBootImageLinkTimeAddress: {
6206 DCHECK(!kEmitCompilerReadBarrier);
6207 __ movl(out, Immediate(/* placeholder */ 0));
6208 codegen_->RecordStringPatch(load);
6209 return; // No dex cache slow path.
6210 }
6211 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
6212 DCHECK(!kEmitCompilerReadBarrier);
6213 Register method_address = locations->InAt(0).AsRegister<Register>();
6214 __ leal(out, Address(method_address, CodeGeneratorX86::kDummy32BitOffset));
6215 codegen_->RecordStringPatch(load);
6216 return; // No dex cache slow path.
6217 }
6218 case HLoadString::LoadKind::kBootImageAddress: {
6219 DCHECK(!kEmitCompilerReadBarrier);
6220 DCHECK_NE(load->GetAddress(), 0u);
6221 uint32_t address = dchecked_integral_cast<uint32_t>(load->GetAddress());
6222 __ movl(out, Immediate(address));
6223 codegen_->RecordSimplePatch();
6224 return; // No dex cache slow path.
6225 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006226 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07006227 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006228 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006229
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07006230 // TODO: Re-add the compiler code to do string dex cache lookup again.
6231 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86(load);
6232 codegen_->AddSlowPath(slow_path);
6233 __ jmp(slow_path->GetEntryLabel());
6234 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006235}
6236
David Brazdilcb1c0552015-08-04 16:22:25 +01006237static Address GetExceptionTlsAddress() {
Andreas Gampe542451c2016-07-26 09:02:02 -07006238 return Address::Absolute(Thread::ExceptionOffset<kX86PointerSize>().Int32Value());
David Brazdilcb1c0552015-08-04 16:22:25 +01006239}
6240
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006241void LocationsBuilderX86::VisitLoadException(HLoadException* load) {
6242 LocationSummary* locations =
6243 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
6244 locations->SetOut(Location::RequiresRegister());
6245}
6246
6247void InstructionCodeGeneratorX86::VisitLoadException(HLoadException* load) {
David Brazdilcb1c0552015-08-04 16:22:25 +01006248 __ fs()->movl(load->GetLocations()->Out().AsRegister<Register>(), GetExceptionTlsAddress());
6249}
6250
6251void LocationsBuilderX86::VisitClearException(HClearException* clear) {
6252 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
6253}
6254
6255void InstructionCodeGeneratorX86::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
6256 __ fs()->movl(GetExceptionTlsAddress(), Immediate(0));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006257}
6258
6259void LocationsBuilderX86::VisitThrow(HThrow* instruction) {
6260 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006261 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006262 InvokeRuntimeCallingConvention calling_convention;
6263 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6264}
6265
6266void InstructionCodeGeneratorX86::VisitThrow(HThrow* instruction) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01006267 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006268 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006269}
6270
Roland Levillain7c1559a2015-12-15 10:55:36 +00006271static bool TypeCheckNeedsATemporary(TypeCheckKind type_check_kind) {
6272 return kEmitCompilerReadBarrier &&
Vladimir Marko953437b2016-08-24 08:30:46 +00006273 !kUseBakerReadBarrier &&
6274 (type_check_kind == TypeCheckKind::kAbstractClassCheck ||
Roland Levillain7c1559a2015-12-15 10:55:36 +00006275 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
6276 type_check_kind == TypeCheckKind::kArrayObjectCheck);
6277}
6278
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006279void LocationsBuilderX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006280 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain0d5a2812015-11-13 10:07:31 +00006281 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
6282 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006283 case TypeCheckKind::kExactCheck:
6284 case TypeCheckKind::kAbstractClassCheck:
6285 case TypeCheckKind::kClassHierarchyCheck:
6286 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006287 call_kind =
6288 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006289 break;
6290 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006291 case TypeCheckKind::kUnresolvedCheck:
6292 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006293 call_kind = LocationSummary::kCallOnSlowPath;
6294 break;
6295 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006296
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006297 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006298 locations->SetInAt(0, Location::RequiresRegister());
6299 locations->SetInAt(1, Location::Any());
6300 // Note that TypeCheckSlowPathX86 uses this "out" register too.
6301 locations->SetOut(Location::RequiresRegister());
6302 // When read barriers are enabled, we need a temporary register for
6303 // some cases.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006304 if (TypeCheckNeedsATemporary(type_check_kind)) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006305 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006306 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006307}
6308
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006309void InstructionCodeGeneratorX86::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006310 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006311 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006312 Location obj_loc = locations->InAt(0);
6313 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006314 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006315 Location out_loc = locations->Out();
6316 Register out = out_loc.AsRegister<Register>();
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006317 Location maybe_temp_loc = TypeCheckNeedsATemporary(type_check_kind) ?
Roland Levillain7c1559a2015-12-15 10:55:36 +00006318 locations->GetTemp(0) :
6319 Location::NoLocation();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006320 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006321 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6322 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6323 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Andreas Gampe85b62f22015-09-09 13:15:38 -07006324 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006325 NearLabel done, zero;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006326
6327 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006328 // Avoid null check if we know obj is not null.
6329 if (instruction->MustDoNullCheck()) {
6330 __ testl(obj, obj);
6331 __ j(kEqual, &zero);
6332 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006333
Roland Levillain0d5a2812015-11-13 10:07:31 +00006334 // /* HeapReference<Class> */ out = obj->klass_
Vladimir Marko953437b2016-08-24 08:30:46 +00006335 GenerateReferenceLoadTwoRegisters(instruction, out_loc, obj_loc, class_offset);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006336
Roland Levillain7c1559a2015-12-15 10:55:36 +00006337 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006338 case TypeCheckKind::kExactCheck: {
6339 if (cls.IsRegister()) {
6340 __ cmpl(out, cls.AsRegister<Register>());
6341 } else {
6342 DCHECK(cls.IsStackSlot()) << cls;
6343 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6344 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006345
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006346 // Classes must be equal for the instanceof to succeed.
6347 __ j(kNotEqual, &zero);
6348 __ movl(out, Immediate(1));
6349 __ jmp(&done);
6350 break;
6351 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006352
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006353 case TypeCheckKind::kAbstractClassCheck: {
6354 // If the class is abstract, we eagerly fetch the super class of the
6355 // object to avoid doing a comparison we know will fail.
6356 NearLabel loop;
6357 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006358 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006359 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006360 __ testl(out, out);
6361 // If `out` is null, we use it for the result, and jump to `done`.
6362 __ j(kEqual, &done);
6363 if (cls.IsRegister()) {
6364 __ cmpl(out, cls.AsRegister<Register>());
6365 } else {
6366 DCHECK(cls.IsStackSlot()) << cls;
6367 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6368 }
6369 __ j(kNotEqual, &loop);
6370 __ movl(out, Immediate(1));
6371 if (zero.IsLinked()) {
6372 __ jmp(&done);
6373 }
6374 break;
6375 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006376
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006377 case TypeCheckKind::kClassHierarchyCheck: {
6378 // Walk over the class hierarchy to find a match.
6379 NearLabel loop, success;
6380 __ Bind(&loop);
6381 if (cls.IsRegister()) {
6382 __ cmpl(out, cls.AsRegister<Register>());
6383 } else {
6384 DCHECK(cls.IsStackSlot()) << cls;
6385 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6386 }
6387 __ j(kEqual, &success);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006388 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006389 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006390 __ testl(out, out);
6391 __ j(kNotEqual, &loop);
6392 // If `out` is null, we use it for the result, and jump to `done`.
6393 __ jmp(&done);
6394 __ Bind(&success);
6395 __ movl(out, Immediate(1));
6396 if (zero.IsLinked()) {
6397 __ jmp(&done);
6398 }
6399 break;
6400 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006401
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006402 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006403 // Do an exact check.
6404 NearLabel exact_check;
6405 if (cls.IsRegister()) {
6406 __ cmpl(out, cls.AsRegister<Register>());
6407 } else {
6408 DCHECK(cls.IsStackSlot()) << cls;
6409 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6410 }
6411 __ j(kEqual, &exact_check);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006412 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006413 // /* HeapReference<Class> */ out = out->component_type_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006414 GenerateReferenceLoadOneRegister(instruction, out_loc, component_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006415 __ testl(out, out);
6416 // If `out` is null, we use it for the result, and jump to `done`.
6417 __ j(kEqual, &done);
6418 __ cmpw(Address(out, primitive_offset), Immediate(Primitive::kPrimNot));
6419 __ j(kNotEqual, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006420 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006421 __ movl(out, Immediate(1));
6422 __ jmp(&done);
6423 break;
6424 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006425
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006426 case TypeCheckKind::kArrayCheck: {
6427 if (cls.IsRegister()) {
6428 __ cmpl(out, cls.AsRegister<Register>());
6429 } else {
6430 DCHECK(cls.IsStackSlot()) << cls;
6431 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6432 }
6433 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain0d5a2812015-11-13 10:07:31 +00006434 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
6435 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006436 codegen_->AddSlowPath(slow_path);
6437 __ j(kNotEqual, slow_path->GetEntryLabel());
6438 __ movl(out, Immediate(1));
6439 if (zero.IsLinked()) {
6440 __ jmp(&done);
6441 }
6442 break;
6443 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006444
Calin Juravle98893e12015-10-02 21:05:03 +01006445 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006446 case TypeCheckKind::kInterfaceCheck: {
6447 // Note that we indeed only call on slow path, but we always go
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006448 // into the slow path for the unresolved and interface check
Roland Levillain0d5a2812015-11-13 10:07:31 +00006449 // cases.
6450 //
6451 // We cannot directly call the InstanceofNonTrivial runtime
6452 // entry point without resorting to a type checking slow path
6453 // here (i.e. by calling InvokeRuntime directly), as it would
6454 // require to assign fixed registers for the inputs of this
6455 // HInstanceOf instruction (following the runtime calling
6456 // convention), which might be cluttered by the potential first
6457 // read barrier emission at the beginning of this method.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006458 //
6459 // TODO: Introduce a new runtime entry point taking the object
6460 // to test (instead of its class) as argument, and let it deal
6461 // with the read barrier issues. This will let us refactor this
6462 // case of the `switch` code as it was previously (with a direct
6463 // call to the runtime not using a type checking slow path).
6464 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006465 DCHECK(locations->OnlyCallsOnSlowPath());
6466 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
6467 /* is_fatal */ false);
6468 codegen_->AddSlowPath(slow_path);
6469 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006470 if (zero.IsLinked()) {
6471 __ jmp(&done);
6472 }
6473 break;
6474 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006475 }
6476
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006477 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006478 __ Bind(&zero);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006479 __ xorl(out, out);
6480 }
6481
6482 if (done.IsLinked()) {
6483 __ Bind(&done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006484 }
6485
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006486 if (slow_path != nullptr) {
6487 __ Bind(slow_path->GetExitLabel());
6488 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006489}
6490
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006491void LocationsBuilderX86::VisitCheckCast(HCheckCast* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006492 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
6493 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006494 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
6495 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006496 case TypeCheckKind::kExactCheck:
6497 case TypeCheckKind::kAbstractClassCheck:
6498 case TypeCheckKind::kClassHierarchyCheck:
6499 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006500 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
6501 LocationSummary::kCallOnSlowPath :
6502 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006503 break;
6504 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006505 case TypeCheckKind::kUnresolvedCheck:
6506 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006507 call_kind = LocationSummary::kCallOnSlowPath;
6508 break;
6509 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006510 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
6511 locations->SetInAt(0, Location::RequiresRegister());
6512 locations->SetInAt(1, Location::Any());
6513 // Note that TypeCheckSlowPathX86 uses this "temp" register too.
6514 locations->AddTemp(Location::RequiresRegister());
6515 // When read barriers are enabled, we need an additional temporary
6516 // register for some cases.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006517 if (TypeCheckNeedsATemporary(type_check_kind)) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006518 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006519 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006520}
6521
6522void InstructionCodeGeneratorX86::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006523 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006524 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006525 Location obj_loc = locations->InAt(0);
6526 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006527 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006528 Location temp_loc = locations->GetTemp(0);
6529 Register temp = temp_loc.AsRegister<Register>();
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006530 Location maybe_temp2_loc = TypeCheckNeedsATemporary(type_check_kind) ?
Roland Levillain7c1559a2015-12-15 10:55:36 +00006531 locations->GetTemp(1) :
6532 Location::NoLocation();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006533 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
6534 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6535 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6536 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006537
Roland Levillain0d5a2812015-11-13 10:07:31 +00006538 bool is_type_check_slow_path_fatal =
6539 (type_check_kind == TypeCheckKind::kExactCheck ||
6540 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
6541 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
6542 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
6543 !instruction->CanThrowIntoCatchBlock();
6544 SlowPathCode* type_check_slow_path =
6545 new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
6546 is_type_check_slow_path_fatal);
6547 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006548
Roland Levillain0d5a2812015-11-13 10:07:31 +00006549 NearLabel done;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006550 // Avoid null check if we know obj is not null.
6551 if (instruction->MustDoNullCheck()) {
6552 __ testl(obj, obj);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006553 __ j(kEqual, &done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006554 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006555
Roland Levillain0d5a2812015-11-13 10:07:31 +00006556 // /* HeapReference<Class> */ temp = obj->klass_
Vladimir Marko953437b2016-08-24 08:30:46 +00006557 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006558
Roland Levillain0d5a2812015-11-13 10:07:31 +00006559 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006560 case TypeCheckKind::kExactCheck:
6561 case TypeCheckKind::kArrayCheck: {
6562 if (cls.IsRegister()) {
6563 __ cmpl(temp, cls.AsRegister<Register>());
6564 } else {
6565 DCHECK(cls.IsStackSlot()) << cls;
6566 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6567 }
6568 // Jump to slow path for throwing the exception or doing a
6569 // more involved array check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006570 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006571 break;
6572 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006573
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006574 case TypeCheckKind::kAbstractClassCheck: {
6575 // If the class is abstract, we eagerly fetch the super class of the
6576 // object to avoid doing a comparison we know will fail.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006577 NearLabel loop, compare_classes;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006578 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006579 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006580 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006581
6582 // If the class reference currently in `temp` is not null, jump
6583 // to the `compare_classes` label to compare it with the checked
6584 // class.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006585 __ testl(temp, temp);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006586 __ j(kNotEqual, &compare_classes);
6587 // Otherwise, jump to the slow path to throw the exception.
6588 //
6589 // But before, move back the object's class into `temp` before
6590 // going into the slow path, as it has been overwritten in the
6591 // meantime.
6592 // /* HeapReference<Class> */ temp = obj->klass_
Vladimir Marko953437b2016-08-24 08:30:46 +00006593 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006594 __ jmp(type_check_slow_path->GetEntryLabel());
6595
6596 __ Bind(&compare_classes);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006597 if (cls.IsRegister()) {
6598 __ cmpl(temp, cls.AsRegister<Register>());
6599 } else {
6600 DCHECK(cls.IsStackSlot()) << cls;
6601 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6602 }
6603 __ j(kNotEqual, &loop);
6604 break;
6605 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006606
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006607 case TypeCheckKind::kClassHierarchyCheck: {
6608 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006609 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006610 __ Bind(&loop);
6611 if (cls.IsRegister()) {
6612 __ cmpl(temp, cls.AsRegister<Register>());
6613 } else {
6614 DCHECK(cls.IsStackSlot()) << cls;
6615 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6616 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006617 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006618
Roland Levillain0d5a2812015-11-13 10:07:31 +00006619 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006620 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006621
6622 // If the class reference currently in `temp` is not null, jump
6623 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006624 __ testl(temp, temp);
6625 __ j(kNotEqual, &loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006626 // Otherwise, jump to the slow path to throw the exception.
6627 //
6628 // But before, move back the object's class into `temp` before
6629 // going into the slow path, as it has been overwritten in the
6630 // meantime.
6631 // /* HeapReference<Class> */ temp = obj->klass_
Vladimir Marko953437b2016-08-24 08:30:46 +00006632 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006633 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006634 break;
6635 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006636
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006637 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006638 // Do an exact check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006639 NearLabel check_non_primitive_component_type;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006640 if (cls.IsRegister()) {
6641 __ cmpl(temp, cls.AsRegister<Register>());
6642 } else {
6643 DCHECK(cls.IsStackSlot()) << cls;
6644 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6645 }
6646 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006647
6648 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006649 // /* HeapReference<Class> */ temp = temp->component_type_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006650 GenerateReferenceLoadOneRegister(instruction, temp_loc, component_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006651
6652 // If the component type is not null (i.e. the object is indeed
6653 // an array), jump to label `check_non_primitive_component_type`
6654 // to further check that this component type is not a primitive
6655 // type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006656 __ testl(temp, temp);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006657 __ j(kNotEqual, &check_non_primitive_component_type);
6658 // Otherwise, jump to the slow path to throw the exception.
6659 //
6660 // But before, move back the object's class into `temp` before
6661 // going into the slow path, as it has been overwritten in the
6662 // meantime.
6663 // /* HeapReference<Class> */ temp = obj->klass_
Vladimir Marko953437b2016-08-24 08:30:46 +00006664 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006665 __ jmp(type_check_slow_path->GetEntryLabel());
6666
6667 __ Bind(&check_non_primitive_component_type);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006668 __ cmpw(Address(temp, primitive_offset), Immediate(Primitive::kPrimNot));
Roland Levillain0d5a2812015-11-13 10:07:31 +00006669 __ j(kEqual, &done);
6670 // Same comment as above regarding `temp` and the slow path.
6671 // /* HeapReference<Class> */ temp = obj->klass_
Vladimir Marko953437b2016-08-24 08:30:46 +00006672 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006673 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006674 break;
6675 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006676
Calin Juravle98893e12015-10-02 21:05:03 +01006677 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006678 case TypeCheckKind::kInterfaceCheck:
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006679 // We always go into the type check slow path for the unresolved
6680 // and interface check cases.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006681 //
6682 // We cannot directly call the CheckCast runtime entry point
6683 // without resorting to a type checking slow path here (i.e. by
6684 // calling InvokeRuntime directly), as it would require to
6685 // assign fixed registers for the inputs of this HInstanceOf
6686 // instruction (following the runtime calling convention), which
6687 // might be cluttered by the potential first read barrier
6688 // emission at the beginning of this method.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006689 //
6690 // TODO: Introduce a new runtime entry point taking the object
6691 // to test (instead of its class) as argument, and let it deal
6692 // with the read barrier issues. This will let us refactor this
6693 // case of the `switch` code as it was previously (with a direct
6694 // call to the runtime not using a type checking slow path).
6695 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006696 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006697 break;
6698 }
6699 __ Bind(&done);
6700
Roland Levillain0d5a2812015-11-13 10:07:31 +00006701 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006702}
6703
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006704void LocationsBuilderX86::VisitMonitorOperation(HMonitorOperation* instruction) {
6705 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006706 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006707 InvokeRuntimeCallingConvention calling_convention;
6708 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6709}
6710
6711void InstructionCodeGeneratorX86::VisitMonitorOperation(HMonitorOperation* instruction) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01006712 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject
6713 : kQuickUnlockObject,
Alexandre Rames8158f282015-08-07 10:26:17 +01006714 instruction,
Serban Constantinescuba45db02016-07-12 22:53:02 +01006715 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006716 if (instruction->IsEnter()) {
6717 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
6718 } else {
6719 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
6720 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006721}
6722
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006723void LocationsBuilderX86::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
6724void LocationsBuilderX86::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
6725void LocationsBuilderX86::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
6726
6727void LocationsBuilderX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
6728 LocationSummary* locations =
6729 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6730 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
6731 || instruction->GetResultType() == Primitive::kPrimLong);
6732 locations->SetInAt(0, Location::RequiresRegister());
6733 locations->SetInAt(1, Location::Any());
6734 locations->SetOut(Location::SameAsFirstInput());
6735}
6736
6737void InstructionCodeGeneratorX86::VisitAnd(HAnd* instruction) {
6738 HandleBitwiseOperation(instruction);
6739}
6740
6741void InstructionCodeGeneratorX86::VisitOr(HOr* instruction) {
6742 HandleBitwiseOperation(instruction);
6743}
6744
6745void InstructionCodeGeneratorX86::VisitXor(HXor* instruction) {
6746 HandleBitwiseOperation(instruction);
6747}
6748
6749void InstructionCodeGeneratorX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
6750 LocationSummary* locations = instruction->GetLocations();
6751 Location first = locations->InAt(0);
6752 Location second = locations->InAt(1);
6753 DCHECK(first.Equals(locations->Out()));
6754
6755 if (instruction->GetResultType() == Primitive::kPrimInt) {
6756 if (second.IsRegister()) {
6757 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006758 __ andl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006759 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006760 __ orl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006761 } else {
6762 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006763 __ xorl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006764 }
6765 } else if (second.IsConstant()) {
6766 if (instruction->IsAnd()) {
Roland Levillain199f3362014-11-27 17:15:16 +00006767 __ andl(first.AsRegister<Register>(),
6768 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006769 } else if (instruction->IsOr()) {
Roland Levillain199f3362014-11-27 17:15:16 +00006770 __ orl(first.AsRegister<Register>(),
6771 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006772 } else {
6773 DCHECK(instruction->IsXor());
Roland Levillain199f3362014-11-27 17:15:16 +00006774 __ xorl(first.AsRegister<Register>(),
6775 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006776 }
6777 } else {
6778 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006779 __ andl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006780 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006781 __ orl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006782 } else {
6783 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006784 __ xorl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006785 }
6786 }
6787 } else {
6788 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
6789 if (second.IsRegisterPair()) {
6790 if (instruction->IsAnd()) {
6791 __ andl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
6792 __ andl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
6793 } else if (instruction->IsOr()) {
6794 __ orl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
6795 __ orl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
6796 } else {
6797 DCHECK(instruction->IsXor());
6798 __ xorl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
6799 __ xorl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
6800 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006801 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006802 if (instruction->IsAnd()) {
6803 __ andl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
6804 __ andl(first.AsRegisterPairHigh<Register>(),
6805 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
6806 } else if (instruction->IsOr()) {
6807 __ orl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
6808 __ orl(first.AsRegisterPairHigh<Register>(),
6809 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
6810 } else {
6811 DCHECK(instruction->IsXor());
6812 __ xorl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
6813 __ xorl(first.AsRegisterPairHigh<Register>(),
6814 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
6815 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006816 } else {
6817 DCHECK(second.IsConstant()) << second;
6818 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006819 int32_t low_value = Low32Bits(value);
6820 int32_t high_value = High32Bits(value);
6821 Immediate low(low_value);
6822 Immediate high(high_value);
6823 Register first_low = first.AsRegisterPairLow<Register>();
6824 Register first_high = first.AsRegisterPairHigh<Register>();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006825 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006826 if (low_value == 0) {
6827 __ xorl(first_low, first_low);
6828 } else if (low_value != -1) {
6829 __ andl(first_low, low);
6830 }
6831 if (high_value == 0) {
6832 __ xorl(first_high, first_high);
6833 } else if (high_value != -1) {
6834 __ andl(first_high, high);
6835 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006836 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006837 if (low_value != 0) {
6838 __ orl(first_low, low);
6839 }
6840 if (high_value != 0) {
6841 __ orl(first_high, high);
6842 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006843 } else {
6844 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006845 if (low_value != 0) {
6846 __ xorl(first_low, low);
6847 }
6848 if (high_value != 0) {
6849 __ xorl(first_high, high);
6850 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006851 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006852 }
6853 }
6854}
6855
Roland Levillain7c1559a2015-12-15 10:55:36 +00006856void InstructionCodeGeneratorX86::GenerateReferenceLoadOneRegister(HInstruction* instruction,
6857 Location out,
6858 uint32_t offset,
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006859 Location maybe_temp) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006860 Register out_reg = out.AsRegister<Register>();
6861 if (kEmitCompilerReadBarrier) {
6862 if (kUseBakerReadBarrier) {
6863 // Load with fast path based Baker's read barrier.
6864 // /* HeapReference<Object> */ out = *(out + offset)
6865 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00006866 instruction, out, out_reg, offset, /* needs_null_check */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006867 } else {
6868 // Load with slow path based read barrier.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006869 // Save the value of `out` into `maybe_temp` before overwriting it
Roland Levillain7c1559a2015-12-15 10:55:36 +00006870 // in the following move operation, as we will need it for the
6871 // read barrier below.
Vladimir Marko953437b2016-08-24 08:30:46 +00006872 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006873 __ movl(maybe_temp.AsRegister<Register>(), out_reg);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006874 // /* HeapReference<Object> */ out = *(out + offset)
6875 __ movl(out_reg, Address(out_reg, offset));
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006876 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006877 }
6878 } else {
6879 // Plain load with no read barrier.
6880 // /* HeapReference<Object> */ out = *(out + offset)
6881 __ movl(out_reg, Address(out_reg, offset));
6882 __ MaybeUnpoisonHeapReference(out_reg);
6883 }
6884}
6885
6886void InstructionCodeGeneratorX86::GenerateReferenceLoadTwoRegisters(HInstruction* instruction,
6887 Location out,
6888 Location obj,
Vladimir Marko953437b2016-08-24 08:30:46 +00006889 uint32_t offset) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006890 Register out_reg = out.AsRegister<Register>();
6891 Register obj_reg = obj.AsRegister<Register>();
6892 if (kEmitCompilerReadBarrier) {
6893 if (kUseBakerReadBarrier) {
6894 // Load with fast path based Baker's read barrier.
6895 // /* HeapReference<Object> */ out = *(obj + offset)
6896 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00006897 instruction, out, obj_reg, offset, /* needs_null_check */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006898 } else {
6899 // Load with slow path based read barrier.
6900 // /* HeapReference<Object> */ out = *(obj + offset)
6901 __ movl(out_reg, Address(obj_reg, offset));
6902 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
6903 }
6904 } else {
6905 // Plain load with no read barrier.
6906 // /* HeapReference<Object> */ out = *(obj + offset)
6907 __ movl(out_reg, Address(obj_reg, offset));
6908 __ MaybeUnpoisonHeapReference(out_reg);
6909 }
6910}
6911
6912void InstructionCodeGeneratorX86::GenerateGcRootFieldLoad(HInstruction* instruction,
6913 Location root,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006914 const Address& address,
6915 Label* fixup_label) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006916 Register root_reg = root.AsRegister<Register>();
6917 if (kEmitCompilerReadBarrier) {
6918 if (kUseBakerReadBarrier) {
6919 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
6920 // Baker's read barrier are used:
6921 //
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006922 // root = *address;
Roland Levillain7c1559a2015-12-15 10:55:36 +00006923 // if (Thread::Current()->GetIsGcMarking()) {
6924 // root = ReadBarrier::Mark(root)
6925 // }
6926
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006927 // /* GcRoot<mirror::Object> */ root = *address
6928 __ movl(root_reg, address);
6929 if (fixup_label != nullptr) {
6930 __ Bind(fixup_label);
6931 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00006932 static_assert(
6933 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
6934 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
6935 "have different sizes.");
6936 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
6937 "art::mirror::CompressedReference<mirror::Object> and int32_t "
6938 "have different sizes.");
6939
Vladimir Marko953437b2016-08-24 08:30:46 +00006940 // Slow path marking the GC root `root`.
6941 SlowPathCode* slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathX86(
6942 instruction, root, /* unpoison */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006943 codegen_->AddSlowPath(slow_path);
6944
Andreas Gampe542451c2016-07-26 09:02:02 -07006945 __ fs()->cmpl(Address::Absolute(Thread::IsGcMarkingOffset<kX86PointerSize>().Int32Value()),
Roland Levillain7c1559a2015-12-15 10:55:36 +00006946 Immediate(0));
6947 __ j(kNotEqual, slow_path->GetEntryLabel());
6948 __ Bind(slow_path->GetExitLabel());
6949 } else {
6950 // GC root loaded through a slow path for read barriers other
6951 // than Baker's.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006952 // /* GcRoot<mirror::Object>* */ root = address
6953 __ leal(root_reg, address);
6954 if (fixup_label != nullptr) {
6955 __ Bind(fixup_label);
6956 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00006957 // /* mirror::Object* */ root = root->Read()
6958 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
6959 }
6960 } else {
6961 // Plain GC root load with no read barrier.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006962 // /* GcRoot<mirror::Object> */ root = *address
6963 __ movl(root_reg, address);
6964 if (fixup_label != nullptr) {
6965 __ Bind(fixup_label);
6966 }
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006967 // Note that GC roots are not affected by heap poisoning, thus we
6968 // do not have to unpoison `root_reg` here.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006969 }
6970}
6971
6972void CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
6973 Location ref,
6974 Register obj,
6975 uint32_t offset,
Roland Levillain7c1559a2015-12-15 10:55:36 +00006976 bool needs_null_check) {
6977 DCHECK(kEmitCompilerReadBarrier);
6978 DCHECK(kUseBakerReadBarrier);
6979
6980 // /* HeapReference<Object> */ ref = *(obj + offset)
6981 Address src(obj, offset);
Vladimir Marko953437b2016-08-24 08:30:46 +00006982 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, needs_null_check);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006983}
6984
6985void CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
6986 Location ref,
6987 Register obj,
6988 uint32_t data_offset,
6989 Location index,
Roland Levillain7c1559a2015-12-15 10:55:36 +00006990 bool needs_null_check) {
6991 DCHECK(kEmitCompilerReadBarrier);
6992 DCHECK(kUseBakerReadBarrier);
6993
Roland Levillain3d312422016-06-23 13:53:42 +01006994 static_assert(
6995 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
6996 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain7c1559a2015-12-15 10:55:36 +00006997 // /* HeapReference<Object> */ ref =
6998 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
6999 Address src = index.IsConstant() ?
7000 Address(obj, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset) :
7001 Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset);
Vladimir Marko953437b2016-08-24 08:30:46 +00007002 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, needs_null_check);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007003}
7004
7005void CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
7006 Location ref,
7007 Register obj,
7008 const Address& src,
Roland Levillain7c1559a2015-12-15 10:55:36 +00007009 bool needs_null_check) {
7010 DCHECK(kEmitCompilerReadBarrier);
7011 DCHECK(kUseBakerReadBarrier);
7012
7013 // In slow path based read barriers, the read barrier call is
7014 // inserted after the original load. However, in fast path based
7015 // Baker's read barriers, we need to perform the load of
7016 // mirror::Object::monitor_ *before* the original reference load.
7017 // This load-load ordering is required by the read barrier.
7018 // The fast path/slow path (for Baker's algorithm) should look like:
7019 //
7020 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
7021 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
7022 // HeapReference<Object> ref = *src; // Original reference load.
7023 // bool is_gray = (rb_state == ReadBarrier::gray_ptr_);
7024 // if (is_gray) {
7025 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
7026 // }
7027 //
7028 // Note: the original implementation in ReadBarrier::Barrier is
7029 // slightly more complex as:
7030 // - it implements the load-load fence using a data dependency on
Roland Levillaine3f43ac2016-01-19 15:07:47 +00007031 // the high-bits of rb_state, which are expected to be all zeroes
7032 // (we use CodeGeneratorX86::GenerateMemoryBarrier instead here,
7033 // which is a no-op thanks to the x86 memory model);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007034 // - it performs additional checks that we do not do here for
7035 // performance reasons.
7036
7037 Register ref_reg = ref.AsRegister<Register>();
Roland Levillain7c1559a2015-12-15 10:55:36 +00007038 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
7039
Vladimir Marko953437b2016-08-24 08:30:46 +00007040 // Given the numeric representation, it's enough to check the low bit of the rb_state.
7041 static_assert(ReadBarrier::white_ptr_ == 0, "Expecting white to have value 0");
7042 static_assert(ReadBarrier::gray_ptr_ == 1, "Expecting gray to have value 1");
7043 static_assert(ReadBarrier::black_ptr_ == 2, "Expecting black to have value 2");
7044 constexpr uint32_t gray_byte_position = LockWord::kReadBarrierStateShift / kBitsPerByte;
7045 constexpr uint32_t gray_bit_position = LockWord::kReadBarrierStateShift % kBitsPerByte;
7046 constexpr int32_t test_value = static_cast<int8_t>(1 << gray_bit_position);
7047
7048 // if (rb_state == ReadBarrier::gray_ptr_)
7049 // ref = ReadBarrier::Mark(ref);
7050 // At this point, just do the "if" and make sure that flags are preserved until the branch.
7051 __ testb(Address(obj, monitor_offset + gray_byte_position), Immediate(test_value));
Roland Levillain7c1559a2015-12-15 10:55:36 +00007052 if (needs_null_check) {
7053 MaybeRecordImplicitNullCheck(instruction);
7054 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00007055
7056 // Load fence to prevent load-load reordering.
7057 // Note that this is a no-op, thanks to the x86 memory model.
7058 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
7059
7060 // The actual reference load.
7061 // /* HeapReference<Object> */ ref = *src
Vladimir Marko953437b2016-08-24 08:30:46 +00007062 __ movl(ref_reg, src); // Flags are unaffected.
7063
7064 // Note: Reference unpoisoning modifies the flags, so we need to delay it after the branch.
7065 // Slow path marking the object `ref` when it is gray.
7066 SlowPathCode* slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathX86(
7067 instruction, ref, /* unpoison */ true);
7068 AddSlowPath(slow_path);
7069
7070 // We have done the "if" of the gray bit check above, now branch based on the flags.
7071 __ j(kNotZero, slow_path->GetEntryLabel());
Roland Levillain7c1559a2015-12-15 10:55:36 +00007072
7073 // Object* ref = ref_addr->AsMirrorPtr()
7074 __ MaybeUnpoisonHeapReference(ref_reg);
7075
Roland Levillain7c1559a2015-12-15 10:55:36 +00007076 __ Bind(slow_path->GetExitLabel());
7077}
7078
7079void CodeGeneratorX86::GenerateReadBarrierSlow(HInstruction* instruction,
7080 Location out,
7081 Location ref,
7082 Location obj,
7083 uint32_t offset,
7084 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007085 DCHECK(kEmitCompilerReadBarrier);
7086
Roland Levillain7c1559a2015-12-15 10:55:36 +00007087 // Insert a slow path based read barrier *after* the reference load.
7088 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00007089 // If heap poisoning is enabled, the unpoisoning of the loaded
7090 // reference will be carried out by the runtime within the slow
7091 // path.
7092 //
7093 // Note that `ref` currently does not get unpoisoned (when heap
7094 // poisoning is enabled), which is alright as the `ref` argument is
7095 // not used by the artReadBarrierSlow entry point.
7096 //
7097 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
7098 SlowPathCode* slow_path = new (GetGraph()->GetArena())
7099 ReadBarrierForHeapReferenceSlowPathX86(instruction, out, ref, obj, offset, index);
7100 AddSlowPath(slow_path);
7101
Roland Levillain0d5a2812015-11-13 10:07:31 +00007102 __ jmp(slow_path->GetEntryLabel());
7103 __ Bind(slow_path->GetExitLabel());
7104}
7105
Roland Levillain7c1559a2015-12-15 10:55:36 +00007106void CodeGeneratorX86::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
7107 Location out,
7108 Location ref,
7109 Location obj,
7110 uint32_t offset,
7111 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007112 if (kEmitCompilerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007113 // Baker's read barriers shall be handled by the fast path
7114 // (CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier).
7115 DCHECK(!kUseBakerReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007116 // If heap poisoning is enabled, unpoisoning will be taken care of
7117 // by the runtime within the slow path.
Roland Levillain7c1559a2015-12-15 10:55:36 +00007118 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007119 } else if (kPoisonHeapReferences) {
7120 __ UnpoisonHeapReference(out.AsRegister<Register>());
7121 }
7122}
7123
Roland Levillain7c1559a2015-12-15 10:55:36 +00007124void CodeGeneratorX86::GenerateReadBarrierForRootSlow(HInstruction* instruction,
7125 Location out,
7126 Location root) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007127 DCHECK(kEmitCompilerReadBarrier);
7128
Roland Levillain7c1559a2015-12-15 10:55:36 +00007129 // Insert a slow path based read barrier *after* the GC root load.
7130 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00007131 // Note that GC roots are not affected by heap poisoning, so we do
7132 // not need to do anything special for this here.
7133 SlowPathCode* slow_path =
7134 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathX86(instruction, out, root);
7135 AddSlowPath(slow_path);
7136
Roland Levillain0d5a2812015-11-13 10:07:31 +00007137 __ jmp(slow_path->GetEntryLabel());
7138 __ Bind(slow_path->GetExitLabel());
7139}
7140
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007141void LocationsBuilderX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00007142 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00007143 LOG(FATAL) << "Unreachable";
7144}
7145
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007146void InstructionCodeGeneratorX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00007147 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00007148 LOG(FATAL) << "Unreachable";
7149}
7150
Mark Mendellfe57faa2015-09-18 09:26:15 -04007151// Simple implementation of packed switch - generate cascaded compare/jumps.
7152void LocationsBuilderX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7153 LocationSummary* locations =
7154 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
7155 locations->SetInAt(0, Location::RequiresRegister());
7156}
7157
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007158void InstructionCodeGeneratorX86::GenPackedSwitchWithCompares(Register value_reg,
7159 int32_t lower_bound,
7160 uint32_t num_entries,
7161 HBasicBlock* switch_block,
7162 HBasicBlock* default_block) {
7163 // Figure out the correct compare values and jump conditions.
7164 // Handle the first compare/branch as a special case because it might
7165 // jump to the default case.
7166 DCHECK_GT(num_entries, 2u);
7167 Condition first_condition;
7168 uint32_t index;
7169 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
7170 if (lower_bound != 0) {
7171 first_condition = kLess;
7172 __ cmpl(value_reg, Immediate(lower_bound));
7173 __ j(first_condition, codegen_->GetLabelOf(default_block));
7174 __ j(kEqual, codegen_->GetLabelOf(successors[0]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007175
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007176 index = 1;
7177 } else {
7178 // Handle all the compare/jumps below.
7179 first_condition = kBelow;
7180 index = 0;
7181 }
7182
7183 // Handle the rest of the compare/jumps.
7184 for (; index + 1 < num_entries; index += 2) {
7185 int32_t compare_to_value = lower_bound + index + 1;
7186 __ cmpl(value_reg, Immediate(compare_to_value));
7187 // Jump to successors[index] if value < case_value[index].
7188 __ j(first_condition, codegen_->GetLabelOf(successors[index]));
7189 // Jump to successors[index + 1] if value == case_value[index + 1].
7190 __ j(kEqual, codegen_->GetLabelOf(successors[index + 1]));
7191 }
7192
7193 if (index != num_entries) {
7194 // There are an odd number of entries. Handle the last one.
7195 DCHECK_EQ(index + 1, num_entries);
7196 __ cmpl(value_reg, Immediate(lower_bound + index));
7197 __ j(kEqual, codegen_->GetLabelOf(successors[index]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007198 }
7199
7200 // And the default for any other value.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007201 if (!codegen_->GoesToNextBlock(switch_block, default_block)) {
7202 __ jmp(codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007203 }
7204}
7205
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007206void InstructionCodeGeneratorX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7207 int32_t lower_bound = switch_instr->GetStartValue();
7208 uint32_t num_entries = switch_instr->GetNumEntries();
7209 LocationSummary* locations = switch_instr->GetLocations();
7210 Register value_reg = locations->InAt(0).AsRegister<Register>();
7211
7212 GenPackedSwitchWithCompares(value_reg,
7213 lower_bound,
7214 num_entries,
7215 switch_instr->GetBlock(),
7216 switch_instr->GetDefaultBlock());
7217}
7218
Mark Mendell805b3b52015-09-18 14:10:29 -04007219void LocationsBuilderX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
7220 LocationSummary* locations =
7221 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
7222 locations->SetInAt(0, Location::RequiresRegister());
7223
7224 // Constant area pointer.
7225 locations->SetInAt(1, Location::RequiresRegister());
7226
7227 // And the temporary we need.
7228 locations->AddTemp(Location::RequiresRegister());
7229}
7230
7231void InstructionCodeGeneratorX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
7232 int32_t lower_bound = switch_instr->GetStartValue();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007233 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendell805b3b52015-09-18 14:10:29 -04007234 LocationSummary* locations = switch_instr->GetLocations();
7235 Register value_reg = locations->InAt(0).AsRegister<Register>();
7236 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
7237
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007238 if (num_entries <= kPackedSwitchJumpTableThreshold) {
7239 GenPackedSwitchWithCompares(value_reg,
7240 lower_bound,
7241 num_entries,
7242 switch_instr->GetBlock(),
7243 default_block);
7244 return;
7245 }
7246
Mark Mendell805b3b52015-09-18 14:10:29 -04007247 // Optimizing has a jump area.
7248 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
7249 Register constant_area = locations->InAt(1).AsRegister<Register>();
7250
7251 // Remove the bias, if needed.
7252 if (lower_bound != 0) {
7253 __ leal(temp_reg, Address(value_reg, -lower_bound));
7254 value_reg = temp_reg;
7255 }
7256
7257 // Is the value in range?
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007258 DCHECK_GE(num_entries, 1u);
Mark Mendell805b3b52015-09-18 14:10:29 -04007259 __ cmpl(value_reg, Immediate(num_entries - 1));
7260 __ j(kAbove, codegen_->GetLabelOf(default_block));
7261
7262 // We are in the range of the table.
7263 // Load (target-constant_area) from the jump table, indexing by the value.
7264 __ movl(temp_reg, codegen_->LiteralCaseTable(switch_instr, constant_area, value_reg));
7265
7266 // Compute the actual target address by adding in constant_area.
7267 __ addl(temp_reg, constant_area);
7268
7269 // And jump.
7270 __ jmp(temp_reg);
7271}
7272
Mark Mendell0616ae02015-04-17 12:49:27 -04007273void LocationsBuilderX86::VisitX86ComputeBaseMethodAddress(
7274 HX86ComputeBaseMethodAddress* insn) {
7275 LocationSummary* locations =
7276 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
7277 locations->SetOut(Location::RequiresRegister());
7278}
7279
7280void InstructionCodeGeneratorX86::VisitX86ComputeBaseMethodAddress(
7281 HX86ComputeBaseMethodAddress* insn) {
7282 LocationSummary* locations = insn->GetLocations();
7283 Register reg = locations->Out().AsRegister<Register>();
7284
7285 // Generate call to next instruction.
7286 Label next_instruction;
7287 __ call(&next_instruction);
7288 __ Bind(&next_instruction);
7289
7290 // Remember this offset for later use with constant area.
7291 codegen_->SetMethodAddressOffset(GetAssembler()->CodeSize());
7292
7293 // Grab the return address off the stack.
7294 __ popl(reg);
7295}
7296
7297void LocationsBuilderX86::VisitX86LoadFromConstantTable(
7298 HX86LoadFromConstantTable* insn) {
7299 LocationSummary* locations =
7300 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
7301
7302 locations->SetInAt(0, Location::RequiresRegister());
7303 locations->SetInAt(1, Location::ConstantLocation(insn->GetConstant()));
7304
7305 // If we don't need to be materialized, we only need the inputs to be set.
David Brazdilb3e773e2016-01-26 11:28:37 +00007306 if (insn->IsEmittedAtUseSite()) {
Mark Mendell0616ae02015-04-17 12:49:27 -04007307 return;
7308 }
7309
7310 switch (insn->GetType()) {
7311 case Primitive::kPrimFloat:
7312 case Primitive::kPrimDouble:
7313 locations->SetOut(Location::RequiresFpuRegister());
7314 break;
7315
7316 case Primitive::kPrimInt:
7317 locations->SetOut(Location::RequiresRegister());
7318 break;
7319
7320 default:
7321 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
7322 }
7323}
7324
7325void InstructionCodeGeneratorX86::VisitX86LoadFromConstantTable(HX86LoadFromConstantTable* insn) {
David Brazdilb3e773e2016-01-26 11:28:37 +00007326 if (insn->IsEmittedAtUseSite()) {
Mark Mendell0616ae02015-04-17 12:49:27 -04007327 return;
7328 }
7329
7330 LocationSummary* locations = insn->GetLocations();
7331 Location out = locations->Out();
7332 Register const_area = locations->InAt(0).AsRegister<Register>();
7333 HConstant *value = insn->GetConstant();
7334
7335 switch (insn->GetType()) {
7336 case Primitive::kPrimFloat:
7337 __ movss(out.AsFpuRegister<XmmRegister>(),
7338 codegen_->LiteralFloatAddress(value->AsFloatConstant()->GetValue(), const_area));
7339 break;
7340
7341 case Primitive::kPrimDouble:
7342 __ movsd(out.AsFpuRegister<XmmRegister>(),
7343 codegen_->LiteralDoubleAddress(value->AsDoubleConstant()->GetValue(), const_area));
7344 break;
7345
7346 case Primitive::kPrimInt:
7347 __ movl(out.AsRegister<Register>(),
7348 codegen_->LiteralInt32Address(value->AsIntConstant()->GetValue(), const_area));
7349 break;
7350
7351 default:
7352 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
7353 }
7354}
7355
Mark Mendell0616ae02015-04-17 12:49:27 -04007356/**
7357 * Class to handle late fixup of offsets into constant area.
7358 */
Vladimir Marko5233f932015-09-29 19:01:15 +01007359class RIPFixup : public AssemblerFixup, public ArenaObject<kArenaAllocCodeGenerator> {
Mark Mendell0616ae02015-04-17 12:49:27 -04007360 public:
Mark Mendell805b3b52015-09-18 14:10:29 -04007361 RIPFixup(CodeGeneratorX86& codegen, size_t offset)
7362 : codegen_(&codegen), offset_into_constant_area_(offset) {}
7363
7364 protected:
7365 void SetOffset(size_t offset) { offset_into_constant_area_ = offset; }
7366
7367 CodeGeneratorX86* codegen_;
Mark Mendell0616ae02015-04-17 12:49:27 -04007368
7369 private:
7370 void Process(const MemoryRegion& region, int pos) OVERRIDE {
7371 // Patch the correct offset for the instruction. The place to patch is the
7372 // last 4 bytes of the instruction.
7373 // The value to patch is the distance from the offset in the constant area
7374 // from the address computed by the HX86ComputeBaseMethodAddress instruction.
Mark Mendell805b3b52015-09-18 14:10:29 -04007375 int32_t constant_offset = codegen_->ConstantAreaStart() + offset_into_constant_area_;
7376 int32_t relative_position = constant_offset - codegen_->GetMethodAddressOffset();;
Mark Mendell0616ae02015-04-17 12:49:27 -04007377
7378 // Patch in the right value.
7379 region.StoreUnaligned<int32_t>(pos - 4, relative_position);
7380 }
7381
Mark Mendell0616ae02015-04-17 12:49:27 -04007382 // Location in constant area that the fixup refers to.
Mark Mendell805b3b52015-09-18 14:10:29 -04007383 int32_t offset_into_constant_area_;
Mark Mendell0616ae02015-04-17 12:49:27 -04007384};
7385
Mark Mendell805b3b52015-09-18 14:10:29 -04007386/**
7387 * Class to handle late fixup of offsets to a jump table that will be created in the
7388 * constant area.
7389 */
7390class JumpTableRIPFixup : public RIPFixup {
7391 public:
7392 JumpTableRIPFixup(CodeGeneratorX86& codegen, HX86PackedSwitch* switch_instr)
7393 : RIPFixup(codegen, static_cast<size_t>(-1)), switch_instr_(switch_instr) {}
7394
7395 void CreateJumpTable() {
7396 X86Assembler* assembler = codegen_->GetAssembler();
7397
7398 // Ensure that the reference to the jump table has the correct offset.
7399 const int32_t offset_in_constant_table = assembler->ConstantAreaSize();
7400 SetOffset(offset_in_constant_table);
7401
7402 // The label values in the jump table are computed relative to the
7403 // instruction addressing the constant area.
7404 const int32_t relative_offset = codegen_->GetMethodAddressOffset();
7405
7406 // Populate the jump table with the correct values for the jump table.
7407 int32_t num_entries = switch_instr_->GetNumEntries();
7408 HBasicBlock* block = switch_instr_->GetBlock();
7409 const ArenaVector<HBasicBlock*>& successors = block->GetSuccessors();
7410 // The value that we want is the target offset - the position of the table.
7411 for (int32_t i = 0; i < num_entries; i++) {
7412 HBasicBlock* b = successors[i];
7413 Label* l = codegen_->GetLabelOf(b);
7414 DCHECK(l->IsBound());
7415 int32_t offset_to_block = l->Position() - relative_offset;
7416 assembler->AppendInt32(offset_to_block);
7417 }
7418 }
7419
7420 private:
7421 const HX86PackedSwitch* switch_instr_;
7422};
7423
7424void CodeGeneratorX86::Finalize(CodeAllocator* allocator) {
7425 // Generate the constant area if needed.
7426 X86Assembler* assembler = GetAssembler();
7427 if (!assembler->IsConstantAreaEmpty() || !fixups_to_jump_tables_.empty()) {
7428 // Align to 4 byte boundary to reduce cache misses, as the data is 4 and 8
7429 // byte values.
7430 assembler->Align(4, 0);
7431 constant_area_start_ = assembler->CodeSize();
7432
7433 // Populate any jump tables.
7434 for (auto jump_table : fixups_to_jump_tables_) {
7435 jump_table->CreateJumpTable();
7436 }
7437
7438 // And now add the constant area to the generated code.
7439 assembler->AddConstantArea();
7440 }
7441
7442 // And finish up.
7443 CodeGenerator::Finalize(allocator);
7444}
7445
Mark Mendell0616ae02015-04-17 12:49:27 -04007446Address CodeGeneratorX86::LiteralDoubleAddress(double v, Register reg) {
7447 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddDouble(v));
7448 return Address(reg, kDummy32BitOffset, fixup);
7449}
7450
7451Address CodeGeneratorX86::LiteralFloatAddress(float v, Register reg) {
7452 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddFloat(v));
7453 return Address(reg, kDummy32BitOffset, fixup);
7454}
7455
7456Address CodeGeneratorX86::LiteralInt32Address(int32_t v, Register reg) {
7457 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt32(v));
7458 return Address(reg, kDummy32BitOffset, fixup);
7459}
7460
7461Address CodeGeneratorX86::LiteralInt64Address(int64_t v, Register reg) {
7462 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt64(v));
7463 return Address(reg, kDummy32BitOffset, fixup);
7464}
7465
Aart Bika19616e2016-02-01 18:57:58 -08007466void CodeGeneratorX86::Load32BitValue(Register dest, int32_t value) {
7467 if (value == 0) {
7468 __ xorl(dest, dest);
7469 } else {
7470 __ movl(dest, Immediate(value));
7471 }
7472}
7473
7474void CodeGeneratorX86::Compare32BitValue(Register dest, int32_t value) {
7475 if (value == 0) {
7476 __ testl(dest, dest);
7477 } else {
7478 __ cmpl(dest, Immediate(value));
7479 }
7480}
7481
Mark Mendell805b3b52015-09-18 14:10:29 -04007482Address CodeGeneratorX86::LiteralCaseTable(HX86PackedSwitch* switch_instr,
7483 Register reg,
7484 Register value) {
7485 // Create a fixup to be used to create and address the jump table.
7486 JumpTableRIPFixup* table_fixup =
7487 new (GetGraph()->GetArena()) JumpTableRIPFixup(*this, switch_instr);
7488
7489 // We have to populate the jump tables.
7490 fixups_to_jump_tables_.push_back(table_fixup);
7491
7492 // We want a scaled address, as we are extracting the correct offset from the table.
7493 return Address(reg, value, TIMES_4, kDummy32BitOffset, table_fixup);
7494}
7495
Andreas Gampe85b62f22015-09-09 13:15:38 -07007496// TODO: target as memory.
7497void CodeGeneratorX86::MoveFromReturnRegister(Location target, Primitive::Type type) {
7498 if (!target.IsValid()) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007499 DCHECK_EQ(type, Primitive::kPrimVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07007500 return;
7501 }
7502
7503 DCHECK_NE(type, Primitive::kPrimVoid);
7504
7505 Location return_loc = InvokeDexCallingConventionVisitorX86().GetReturnLocation(type);
7506 if (target.Equals(return_loc)) {
7507 return;
7508 }
7509
7510 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
7511 // with the else branch.
7512 if (type == Primitive::kPrimLong) {
7513 HParallelMove parallel_move(GetGraph()->GetArena());
7514 parallel_move.AddMove(return_loc.ToLow(), target.ToLow(), Primitive::kPrimInt, nullptr);
7515 parallel_move.AddMove(return_loc.ToHigh(), target.ToHigh(), Primitive::kPrimInt, nullptr);
7516 GetMoveResolver()->EmitNativeCode(&parallel_move);
7517 } else {
7518 // Let the parallel move resolver take care of all of this.
7519 HParallelMove parallel_move(GetGraph()->GetArena());
7520 parallel_move.AddMove(return_loc, target, type, nullptr);
7521 GetMoveResolver()->EmitNativeCode(&parallel_move);
7522 }
7523}
7524
Roland Levillain4d027112015-07-01 15:41:14 +01007525#undef __
7526
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00007527} // namespace x86
7528} // namespace art