blob: 172ce4ab125f8dc1ffc3ffc03fc0ede416b3f1c7 [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 LoadClassSlowPathX86 : public SlowPathCode {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000217 public:
218 LoadClassSlowPathX86(HLoadClass* cls,
219 HInstruction* at,
220 uint32_t dex_pc,
221 bool do_clinit)
David Srbecky9cd6d372016-02-09 15:24:47 +0000222 : SlowPathCode(at), cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000223 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
224 }
225
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000226 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000227 LocationSummary* locations = at_->GetLocations();
228 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
229 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000230 SaveLiveRegisters(codegen, locations);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000231
232 InvokeRuntimeCallingConvention calling_convention;
233 __ movl(calling_convention.GetRegisterAt(0), Immediate(cls_->GetTypeIndex()));
Serban Constantinescuba45db02016-07-12 22:53:02 +0100234 x86_codegen->InvokeRuntime(do_clinit_ ? kQuickInitializeStaticStorage
235 : kQuickInitializeType,
Alexandre Rames8158f282015-08-07 10:26:17 +0100236 at_, dex_pc_, this);
Roland Levillain888d0672015-11-23 18:53:50 +0000237 if (do_clinit_) {
238 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
239 } else {
240 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
241 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000242
243 // Move the class to the desired location.
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000244 Location out = locations->Out();
245 if (out.IsValid()) {
246 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
247 x86_codegen->Move32(out, Location::RegisterLocation(EAX));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000248 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000249
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000250 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000251 __ jmp(GetExitLabel());
252 }
253
Alexandre Rames9931f312015-06-19 14:47:01 +0100254 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathX86"; }
255
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000256 private:
257 // The class this slow path will load.
258 HLoadClass* const cls_;
259
260 // The instruction where this slow path is happening.
261 // (Might be the load class or an initialization check).
262 HInstruction* const at_;
263
264 // The dex PC of `at_`.
265 const uint32_t dex_pc_;
266
267 // Whether to initialize the class.
268 const bool do_clinit_;
269
270 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathX86);
271};
272
Andreas Gampe85b62f22015-09-09 13:15:38 -0700273class TypeCheckSlowPathX86 : public SlowPathCode {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000274 public:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000275 TypeCheckSlowPathX86(HInstruction* instruction, bool is_fatal)
David Srbecky9cd6d372016-02-09 15:24:47 +0000276 : SlowPathCode(instruction), is_fatal_(is_fatal) {}
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000277
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000278 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000279 LocationSummary* locations = instruction_->GetLocations();
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100280 Location object_class = instruction_->IsCheckCast() ? locations->GetTemp(0)
281 : locations->Out();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000282 DCHECK(instruction_->IsCheckCast()
283 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000284
285 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
286 __ Bind(GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000287
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000288 if (!is_fatal_) {
289 SaveLiveRegisters(codegen, locations);
290 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000291
292 // We're moving two locations to locations that could overlap, so we need a parallel
293 // move resolver.
294 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000295 x86_codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100296 locations->InAt(1),
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000297 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100298 Primitive::kPrimNot,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100299 object_class,
Nicolas Geoffray90218252015-04-15 11:56:51 +0100300 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
301 Primitive::kPrimNot);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000302
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000303 if (instruction_->IsInstanceOf()) {
Serban Constantinescuba45db02016-07-12 22:53:02 +0100304 x86_codegen->InvokeRuntime(kQuickInstanceofNonTrivial,
Alexandre Rames8158f282015-08-07 10:26:17 +0100305 instruction_,
306 instruction_->GetDexPc(),
307 this);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000308 CheckEntrypointTypes<
Andreas Gampe67409972016-07-19 22:34:53 -0700309 kQuickInstanceofNonTrivial, size_t, const mirror::Class*, const mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000310 } else {
311 DCHECK(instruction_->IsCheckCast());
Serban Constantinescuba45db02016-07-12 22:53:02 +0100312 x86_codegen->InvokeRuntime(kQuickCheckCast, instruction_, instruction_->GetDexPc(), this);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000313 CheckEntrypointTypes<kQuickCheckCast, void, const mirror::Class*, const mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000314 }
315
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000316 if (!is_fatal_) {
317 if (instruction_->IsInstanceOf()) {
318 x86_codegen->Move32(locations->Out(), Location::RegisterLocation(EAX));
319 }
320 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray75374372015-09-17 17:12:19 +0000321
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000322 __ jmp(GetExitLabel());
323 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000324 }
325
Alexandre Rames9931f312015-06-19 14:47:01 +0100326 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathX86"; }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000327 bool IsFatal() const OVERRIDE { return is_fatal_; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100328
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000329 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000330 const bool is_fatal_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000331
332 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathX86);
333};
334
Andreas Gampe85b62f22015-09-09 13:15:38 -0700335class DeoptimizationSlowPathX86 : public SlowPathCode {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700336 public:
Aart Bik42249c32016-01-07 15:33:50 -0800337 explicit DeoptimizationSlowPathX86(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000338 : SlowPathCode(instruction) {}
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700339
340 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames8158f282015-08-07 10:26:17 +0100341 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700342 __ Bind(GetEntryLabel());
Serban Constantinescuba45db02016-07-12 22:53:02 +0100343 x86_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000344 CheckEntrypointTypes<kQuickDeoptimize, void, void>();
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700345 }
346
Alexandre Rames9931f312015-06-19 14:47:01 +0100347 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathX86"; }
348
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700349 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700350 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathX86);
351};
352
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100353class ArraySetSlowPathX86 : public SlowPathCode {
354 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000355 explicit ArraySetSlowPathX86(HInstruction* instruction) : SlowPathCode(instruction) {}
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100356
357 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
358 LocationSummary* locations = instruction_->GetLocations();
359 __ Bind(GetEntryLabel());
360 SaveLiveRegisters(codegen, locations);
361
362 InvokeRuntimeCallingConvention calling_convention;
363 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
364 parallel_move.AddMove(
365 locations->InAt(0),
366 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
367 Primitive::kPrimNot,
368 nullptr);
369 parallel_move.AddMove(
370 locations->InAt(1),
371 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
372 Primitive::kPrimInt,
373 nullptr);
374 parallel_move.AddMove(
375 locations->InAt(2),
376 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
377 Primitive::kPrimNot,
378 nullptr);
379 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
380
381 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Serban Constantinescuba45db02016-07-12 22:53:02 +0100382 x86_codegen->InvokeRuntime(kQuickAputObject, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000383 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100384 RestoreLiveRegisters(codegen, locations);
385 __ jmp(GetExitLabel());
386 }
387
388 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathX86"; }
389
390 private:
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100391 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathX86);
392};
393
Roland Levillain7c1559a2015-12-15 10:55:36 +0000394// Slow path marking an object during a read barrier.
395class ReadBarrierMarkSlowPathX86 : public SlowPathCode {
396 public:
Vladimir Marko953437b2016-08-24 08:30:46 +0000397 ReadBarrierMarkSlowPathX86(HInstruction* instruction, Location obj, bool unpoison)
398 : SlowPathCode(instruction), obj_(obj), unpoison_(unpoison) {
Roland Levillain7c1559a2015-12-15 10:55:36 +0000399 DCHECK(kEmitCompilerReadBarrier);
400 }
401
402 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathX86"; }
403
404 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
405 LocationSummary* locations = instruction_->GetLocations();
Roland Levillain02b75802016-07-13 11:54:35 +0100406 Register reg = obj_.AsRegister<Register>();
Roland Levillain7c1559a2015-12-15 10:55:36 +0000407 DCHECK(locations->CanCall());
Roland Levillain02b75802016-07-13 11:54:35 +0100408 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg));
Roland Levillain7c1559a2015-12-15 10:55:36 +0000409 DCHECK(instruction_->IsInstanceFieldGet() ||
410 instruction_->IsStaticFieldGet() ||
411 instruction_->IsArrayGet() ||
Roland Levillain16d9f942016-08-25 17:27:56 +0100412 instruction_->IsArraySet() ||
Roland Levillain7c1559a2015-12-15 10:55:36 +0000413 instruction_->IsLoadClass() ||
414 instruction_->IsLoadString() ||
415 instruction_->IsInstanceOf() ||
Roland Levillain3d312422016-06-23 13:53:42 +0100416 instruction_->IsCheckCast() ||
Roland Levillain0b671c02016-08-19 12:02:34 +0100417 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()) ||
418 (instruction_->IsInvokeStaticOrDirect() && instruction_->GetLocations()->Intrinsified()))
Roland Levillain7c1559a2015-12-15 10:55:36 +0000419 << "Unexpected instruction in read barrier marking slow path: "
420 << instruction_->DebugName();
421
422 __ Bind(GetEntryLabel());
Vladimir Marko953437b2016-08-24 08:30:46 +0000423 if (unpoison_) {
424 // Object* ref = ref_addr->AsMirrorPtr()
425 __ MaybeUnpoisonHeapReference(reg);
426 }
Roland Levillain4359e612016-07-20 11:32:19 +0100427 // No need to save live registers; it's taken care of by the
428 // entrypoint. Also, there is no need to update the stack mask,
429 // as this runtime call will not trigger a garbage collection.
Roland Levillain7c1559a2015-12-15 10:55:36 +0000430 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Roland Levillain02b75802016-07-13 11:54:35 +0100431 DCHECK_NE(reg, ESP);
432 DCHECK(0 <= reg && reg < kNumberOfCpuRegisters) << reg;
433 // "Compact" slow path, saving two moves.
434 //
435 // Instead of using the standard runtime calling convention (input
436 // and output in EAX):
437 //
438 // EAX <- obj
439 // EAX <- ReadBarrierMark(EAX)
440 // obj <- EAX
441 //
442 // we just use rX (the register holding `obj`) as input and output
443 // of a dedicated entrypoint:
444 //
445 // rX <- ReadBarrierMarkRegX(rX)
446 //
447 int32_t entry_point_offset =
Andreas Gampe542451c2016-07-26 09:02:02 -0700448 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kX86PointerSize>(reg);
Roland Levillaindec8f632016-07-22 17:10:06 +0100449 // This runtime call does not require a stack map.
450 x86_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
Roland Levillain7c1559a2015-12-15 10:55:36 +0000451 __ jmp(GetExitLabel());
452 }
453
454 private:
Roland Levillain7c1559a2015-12-15 10:55:36 +0000455 const Location obj_;
Vladimir Marko953437b2016-08-24 08:30:46 +0000456 const bool unpoison_;
Roland Levillain7c1559a2015-12-15 10:55:36 +0000457
458 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathX86);
459};
460
Roland Levillain0d5a2812015-11-13 10:07:31 +0000461// Slow path generating a read barrier for a heap reference.
462class ReadBarrierForHeapReferenceSlowPathX86 : public SlowPathCode {
463 public:
464 ReadBarrierForHeapReferenceSlowPathX86(HInstruction* instruction,
465 Location out,
466 Location ref,
467 Location obj,
468 uint32_t offset,
469 Location index)
David Srbecky9cd6d372016-02-09 15:24:47 +0000470 : SlowPathCode(instruction),
Roland Levillain0d5a2812015-11-13 10:07:31 +0000471 out_(out),
472 ref_(ref),
473 obj_(obj),
474 offset_(offset),
475 index_(index) {
476 DCHECK(kEmitCompilerReadBarrier);
477 // If `obj` is equal to `out` or `ref`, it means the initial object
478 // has been overwritten by (or after) the heap object reference load
479 // to be instrumented, e.g.:
480 //
481 // __ movl(out, Address(out, offset));
Roland Levillain7c1559a2015-12-15 10:55:36 +0000482 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000483 //
484 // In that case, we have lost the information about the original
485 // object, and the emitted read barrier cannot work properly.
486 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
487 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
488 }
489
490 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
491 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
492 LocationSummary* locations = instruction_->GetLocations();
493 Register reg_out = out_.AsRegister<Register>();
494 DCHECK(locations->CanCall());
495 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
Roland Levillain3d312422016-06-23 13:53:42 +0100496 DCHECK(instruction_->IsInstanceFieldGet() ||
497 instruction_->IsStaticFieldGet() ||
498 instruction_->IsArrayGet() ||
499 instruction_->IsInstanceOf() ||
500 instruction_->IsCheckCast() ||
Roland Levillaindec8f632016-07-22 17:10:06 +0100501 (instruction_->IsInvokeVirtual()) && instruction_->GetLocations()->Intrinsified())
Roland Levillain7c1559a2015-12-15 10:55:36 +0000502 << "Unexpected instruction in read barrier for heap reference slow path: "
503 << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000504
505 __ Bind(GetEntryLabel());
506 SaveLiveRegisters(codegen, locations);
507
508 // We may have to change the index's value, but as `index_` is a
509 // constant member (like other "inputs" of this slow path),
510 // introduce a copy of it, `index`.
511 Location index = index_;
512 if (index_.IsValid()) {
Roland Levillain3d312422016-06-23 13:53:42 +0100513 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
Roland Levillain0d5a2812015-11-13 10:07:31 +0000514 if (instruction_->IsArrayGet()) {
515 // Compute the actual memory offset and store it in `index`.
516 Register index_reg = index_.AsRegister<Register>();
517 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg));
518 if (codegen->IsCoreCalleeSaveRegister(index_reg)) {
519 // We are about to change the value of `index_reg` (see the
520 // calls to art::x86::X86Assembler::shll and
521 // art::x86::X86Assembler::AddImmediate below), but it has
522 // not been saved by the previous call to
523 // art::SlowPathCode::SaveLiveRegisters, as it is a
524 // callee-save register --
525 // art::SlowPathCode::SaveLiveRegisters does not consider
526 // callee-save registers, as it has been designed with the
527 // assumption that callee-save registers are supposed to be
528 // handled by the called function. So, as a callee-save
529 // register, `index_reg` _would_ eventually be saved onto
530 // the stack, but it would be too late: we would have
531 // changed its value earlier. Therefore, we manually save
532 // it here into another freely available register,
533 // `free_reg`, chosen of course among the caller-save
534 // registers (as a callee-save `free_reg` register would
535 // exhibit the same problem).
536 //
537 // Note we could have requested a temporary register from
538 // the register allocator instead; but we prefer not to, as
539 // this is a slow path, and we know we can find a
540 // caller-save register that is available.
541 Register free_reg = FindAvailableCallerSaveRegister(codegen);
542 __ movl(free_reg, index_reg);
543 index_reg = free_reg;
544 index = Location::RegisterLocation(index_reg);
545 } else {
546 // The initial register stored in `index_` has already been
547 // saved in the call to art::SlowPathCode::SaveLiveRegisters
548 // (as it is not a callee-save register), so we can freely
549 // use it.
550 }
551 // Shifting the index value contained in `index_reg` by the scale
552 // factor (2) cannot overflow in practice, as the runtime is
553 // unable to allocate object arrays with a size larger than
554 // 2^26 - 1 (that is, 2^28 - 4 bytes).
555 __ shll(index_reg, Immediate(TIMES_4));
556 static_assert(
557 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
558 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
559 __ AddImmediate(index_reg, Immediate(offset_));
560 } else {
Roland Levillain3d312422016-06-23 13:53:42 +0100561 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
562 // intrinsics, `index_` is not shifted by a scale factor of 2
563 // (as in the case of ArrayGet), as it is actually an offset
564 // to an object field within an object.
565 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000566 DCHECK(instruction_->GetLocations()->Intrinsified());
567 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
568 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
569 << instruction_->AsInvoke()->GetIntrinsic();
570 DCHECK_EQ(offset_, 0U);
571 DCHECK(index_.IsRegisterPair());
572 // UnsafeGet's offset location is a register pair, the low
573 // part contains the correct offset.
574 index = index_.ToLow();
575 }
576 }
577
578 // We're moving two or three locations to locations that could
579 // overlap, so we need a parallel move resolver.
580 InvokeRuntimeCallingConvention calling_convention;
581 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
582 parallel_move.AddMove(ref_,
583 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
584 Primitive::kPrimNot,
585 nullptr);
586 parallel_move.AddMove(obj_,
587 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
588 Primitive::kPrimNot,
589 nullptr);
590 if (index.IsValid()) {
591 parallel_move.AddMove(index,
592 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
593 Primitive::kPrimInt,
594 nullptr);
595 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
596 } else {
597 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
598 __ movl(calling_convention.GetRegisterAt(2), Immediate(offset_));
599 }
Serban Constantinescuba45db02016-07-12 22:53:02 +0100600 x86_codegen->InvokeRuntime(kQuickReadBarrierSlow, instruction_, instruction_->GetDexPc(), this);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000601 CheckEntrypointTypes<
602 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
603 x86_codegen->Move32(out_, Location::RegisterLocation(EAX));
604
605 RestoreLiveRegisters(codegen, locations);
606 __ jmp(GetExitLabel());
607 }
608
609 const char* GetDescription() const OVERRIDE { return "ReadBarrierForHeapReferenceSlowPathX86"; }
610
611 private:
612 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
613 size_t ref = static_cast<int>(ref_.AsRegister<Register>());
614 size_t obj = static_cast<int>(obj_.AsRegister<Register>());
615 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
616 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
617 return static_cast<Register>(i);
618 }
619 }
620 // We shall never fail to find a free caller-save register, as
621 // there are more than two core caller-save registers on x86
622 // (meaning it is possible to find one which is different from
623 // `ref` and `obj`).
624 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
625 LOG(FATAL) << "Could not find a free caller-save register";
626 UNREACHABLE();
627 }
628
Roland Levillain0d5a2812015-11-13 10:07:31 +0000629 const Location out_;
630 const Location ref_;
631 const Location obj_;
632 const uint32_t offset_;
633 // An additional location containing an index to an array.
634 // Only used for HArrayGet and the UnsafeGetObject &
635 // UnsafeGetObjectVolatile intrinsics.
636 const Location index_;
637
638 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathX86);
639};
640
641// Slow path generating a read barrier for a GC root.
642class ReadBarrierForRootSlowPathX86 : public SlowPathCode {
643 public:
644 ReadBarrierForRootSlowPathX86(HInstruction* instruction, Location out, Location root)
David Srbecky9cd6d372016-02-09 15:24:47 +0000645 : SlowPathCode(instruction), out_(out), root_(root) {
Roland Levillain7c1559a2015-12-15 10:55:36 +0000646 DCHECK(kEmitCompilerReadBarrier);
647 }
Roland Levillain0d5a2812015-11-13 10:07:31 +0000648
649 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
650 LocationSummary* locations = instruction_->GetLocations();
651 Register reg_out = out_.AsRegister<Register>();
652 DCHECK(locations->CanCall());
653 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
Roland Levillain7c1559a2015-12-15 10:55:36 +0000654 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
655 << "Unexpected instruction in read barrier for GC root slow path: "
656 << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000657
658 __ Bind(GetEntryLabel());
659 SaveLiveRegisters(codegen, locations);
660
661 InvokeRuntimeCallingConvention calling_convention;
662 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
663 x86_codegen->Move32(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), root_);
Serban Constantinescuba45db02016-07-12 22:53:02 +0100664 x86_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000665 instruction_,
666 instruction_->GetDexPc(),
667 this);
668 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
669 x86_codegen->Move32(out_, Location::RegisterLocation(EAX));
670
671 RestoreLiveRegisters(codegen, locations);
672 __ jmp(GetExitLabel());
673 }
674
675 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathX86"; }
676
677 private:
Roland Levillain0d5a2812015-11-13 10:07:31 +0000678 const Location out_;
679 const Location root_;
680
681 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathX86);
682};
683
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100684#undef __
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100685// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
686#define __ down_cast<X86Assembler*>(GetAssembler())-> // NOLINT
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100687
Aart Bike9f37602015-10-09 11:15:55 -0700688inline Condition X86Condition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -0700689 switch (cond) {
690 case kCondEQ: return kEqual;
691 case kCondNE: return kNotEqual;
692 case kCondLT: return kLess;
693 case kCondLE: return kLessEqual;
694 case kCondGT: return kGreater;
695 case kCondGE: return kGreaterEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700696 case kCondB: return kBelow;
697 case kCondBE: return kBelowEqual;
698 case kCondA: return kAbove;
699 case kCondAE: return kAboveEqual;
Dave Allison20dfc792014-06-16 20:44:29 -0700700 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100701 LOG(FATAL) << "Unreachable";
702 UNREACHABLE();
703}
704
Aart Bike9f37602015-10-09 11:15:55 -0700705// Maps signed condition to unsigned condition and FP condition to x86 name.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100706inline Condition X86UnsignedOrFPCondition(IfCondition cond) {
707 switch (cond) {
708 case kCondEQ: return kEqual;
709 case kCondNE: return kNotEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700710 // Signed to unsigned, and FP to x86 name.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100711 case kCondLT: return kBelow;
712 case kCondLE: return kBelowEqual;
713 case kCondGT: return kAbove;
714 case kCondGE: return kAboveEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700715 // Unsigned remain unchanged.
716 case kCondB: return kBelow;
717 case kCondBE: return kBelowEqual;
718 case kCondA: return kAbove;
719 case kCondAE: return kAboveEqual;
Roland Levillain4fa13f62015-07-06 18:11:54 +0100720 }
721 LOG(FATAL) << "Unreachable";
722 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -0700723}
724
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100725void CodeGeneratorX86::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100726 stream << Register(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100727}
728
729void CodeGeneratorX86::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100730 stream << XmmRegister(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100731}
732
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100733size_t CodeGeneratorX86::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
734 __ movl(Address(ESP, stack_index), static_cast<Register>(reg_id));
735 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100736}
737
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100738size_t CodeGeneratorX86::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
739 __ movl(static_cast<Register>(reg_id), Address(ESP, stack_index));
740 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100741}
742
Mark Mendell7c8d0092015-01-26 11:21:33 -0500743size_t CodeGeneratorX86::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
744 __ movsd(Address(ESP, stack_index), XmmRegister(reg_id));
745 return GetFloatingPointSpillSlotSize();
746}
747
748size_t CodeGeneratorX86::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
749 __ movsd(XmmRegister(reg_id), Address(ESP, stack_index));
750 return GetFloatingPointSpillSlotSize();
751}
752
Calin Juravle175dc732015-08-25 15:42:32 +0100753void CodeGeneratorX86::InvokeRuntime(QuickEntrypointEnum entrypoint,
754 HInstruction* instruction,
755 uint32_t dex_pc,
756 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +0100757 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Serban Constantinescuba45db02016-07-12 22:53:02 +0100758 GenerateInvokeRuntime(GetThreadOffset<kX86PointerSize>(entrypoint).Int32Value());
759 if (EntrypointRequiresStackMap(entrypoint)) {
760 RecordPcInfo(instruction, dex_pc, slow_path);
761 }
Alexandre Rames8158f282015-08-07 10:26:17 +0100762}
763
Roland Levillaindec8f632016-07-22 17:10:06 +0100764void CodeGeneratorX86::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
765 HInstruction* instruction,
766 SlowPathCode* slow_path) {
767 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
Serban Constantinescuba45db02016-07-12 22:53:02 +0100768 GenerateInvokeRuntime(entry_point_offset);
769}
770
771void CodeGeneratorX86::GenerateInvokeRuntime(int32_t entry_point_offset) {
Roland Levillaindec8f632016-07-22 17:10:06 +0100772 __ fs()->call(Address::Absolute(entry_point_offset));
773}
774
Mark Mendellfb8d2792015-03-31 22:16:59 -0400775CodeGeneratorX86::CodeGeneratorX86(HGraph* graph,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000776 const X86InstructionSetFeatures& isa_features,
777 const CompilerOptions& compiler_options,
778 OptimizingCompilerStats* stats)
Mark Mendell5f874182015-03-04 15:42:45 -0500779 : CodeGenerator(graph,
780 kNumberOfCpuRegisters,
781 kNumberOfXmmRegisters,
782 kNumberOfRegisterPairs,
783 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
784 arraysize(kCoreCalleeSaves))
785 | (1 << kFakeReturnRegister),
Serban Constantinescuecc43662015-08-13 13:33:12 +0100786 0,
787 compiler_options,
788 stats),
Vladimir Marko225b6462015-09-28 12:17:40 +0100789 block_labels_(nullptr),
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100790 location_builder_(graph, this),
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100791 instruction_visitor_(graph, this),
Mark Mendellfb8d2792015-03-31 22:16:59 -0400792 move_resolver_(graph->GetArena(), this),
Vladimir Marko93205e32016-04-13 11:59:46 +0100793 assembler_(graph->GetArena()),
Vladimir Marko58155012015-08-19 12:49:41 +0000794 isa_features_(isa_features),
Vladimir Marko5233f932015-09-29 19:01:15 +0100795 method_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Mark Mendell805b3b52015-09-18 14:10:29 -0400796 relative_call_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000797 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000798 simple_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
799 string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100800 type_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko93205e32016-04-13 11:59:46 +0100801 constant_area_start_(-1),
802 fixups_to_jump_tables_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
803 method_address_offset_(-1) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000804 // Use a fake return address register to mimic Quick.
805 AddAllocatedRegister(Location::RegisterLocation(kFakeReturnRegister));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100806}
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100807
David Brazdil58282f42016-01-14 12:45:10 +0000808void CodeGeneratorX86::SetupBlockedRegisters() const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100809 // Don't allocate the dalvik style register pair passing.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100810 blocked_register_pairs_[ECX_EDX] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100811
812 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100813 blocked_core_registers_[ESP] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100814
Calin Juravle34bacdf2014-10-07 20:23:36 +0100815 UpdateBlockedPairRegisters();
816}
817
818void CodeGeneratorX86::UpdateBlockedPairRegisters() const {
819 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
820 X86ManagedRegister current =
821 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
822 if (blocked_core_registers_[current.AsRegisterPairLow()]
823 || blocked_core_registers_[current.AsRegisterPairHigh()]) {
824 blocked_register_pairs_[i] = true;
825 }
826 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100827}
828
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100829InstructionCodeGeneratorX86::InstructionCodeGeneratorX86(HGraph* graph, CodeGeneratorX86* codegen)
Aart Bik42249c32016-01-07 15:33:50 -0800830 : InstructionCodeGenerator(graph, codegen),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100831 assembler_(codegen->GetAssembler()),
832 codegen_(codegen) {}
833
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100834static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +0100835 return dwarf::Reg::X86Core(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100836}
837
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000838void CodeGeneratorX86::GenerateFrameEntry() {
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100839 __ cfi().SetCurrentCFAOffset(kX86WordSize); // return address
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000840 __ Bind(&frame_entry_label_);
Roland Levillain199f3362014-11-27 17:15:16 +0000841 bool skip_overflow_check =
842 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000843 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Calin Juravle93edf732015-01-20 20:14:07 +0000844
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000845 if (!skip_overflow_check) {
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100846 __ testl(EAX, Address(ESP, -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86))));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100847 RecordPcInfo(nullptr, 0);
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100848 }
849
Mark Mendell5f874182015-03-04 15:42:45 -0500850 if (HasEmptyFrame()) {
851 return;
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000852 }
Mark Mendell5f874182015-03-04 15:42:45 -0500853
854 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
855 Register reg = kCoreCalleeSaves[i];
856 if (allocated_registers_.ContainsCoreRegister(reg)) {
857 __ pushl(reg);
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100858 __ cfi().AdjustCFAOffset(kX86WordSize);
859 __ cfi().RelOffset(DWARFReg(reg), 0);
Mark Mendell5f874182015-03-04 15:42:45 -0500860 }
861 }
862
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100863 int adjust = GetFrameSize() - FrameEntrySpillSize();
864 __ subl(ESP, Immediate(adjust));
865 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100866 __ movl(Address(ESP, kCurrentMethodStackOffset), kMethodRegisterArgument);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000867}
868
869void CodeGeneratorX86::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +0100870 __ cfi().RememberState();
871 if (!HasEmptyFrame()) {
872 int adjust = GetFrameSize() - FrameEntrySpillSize();
873 __ addl(ESP, Immediate(adjust));
874 __ cfi().AdjustCFAOffset(-adjust);
Mark Mendell5f874182015-03-04 15:42:45 -0500875
David Srbeckyc34dc932015-04-12 09:27:43 +0100876 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
877 Register reg = kCoreCalleeSaves[i];
878 if (allocated_registers_.ContainsCoreRegister(reg)) {
879 __ popl(reg);
880 __ cfi().AdjustCFAOffset(-static_cast<int>(kX86WordSize));
881 __ cfi().Restore(DWARFReg(reg));
882 }
Mark Mendell5f874182015-03-04 15:42:45 -0500883 }
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000884 }
David Srbeckyc34dc932015-04-12 09:27:43 +0100885 __ ret();
886 __ cfi().RestoreState();
887 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000888}
889
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100890void CodeGeneratorX86::Bind(HBasicBlock* block) {
891 __ Bind(GetLabelOf(block));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000892}
893
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100894Location InvokeDexCallingConventionVisitorX86::GetReturnLocation(Primitive::Type type) const {
895 switch (type) {
896 case Primitive::kPrimBoolean:
897 case Primitive::kPrimByte:
898 case Primitive::kPrimChar:
899 case Primitive::kPrimShort:
900 case Primitive::kPrimInt:
901 case Primitive::kPrimNot:
902 return Location::RegisterLocation(EAX);
903
904 case Primitive::kPrimLong:
905 return Location::RegisterPairLocation(EAX, EDX);
906
907 case Primitive::kPrimVoid:
908 return Location::NoLocation();
909
910 case Primitive::kPrimDouble:
911 case Primitive::kPrimFloat:
912 return Location::FpuRegisterLocation(XMM0);
913 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +0100914
915 UNREACHABLE();
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100916}
917
918Location InvokeDexCallingConventionVisitorX86::GetMethodLocation() const {
919 return Location::RegisterLocation(kMethodRegisterArgument);
920}
921
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100922Location InvokeDexCallingConventionVisitorX86::GetNextLocation(Primitive::Type type) {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100923 switch (type) {
924 case Primitive::kPrimBoolean:
925 case Primitive::kPrimByte:
926 case Primitive::kPrimChar:
927 case Primitive::kPrimShort:
928 case Primitive::kPrimInt:
929 case Primitive::kPrimNot: {
930 uint32_t index = gp_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000931 stack_index_++;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100932 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100933 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100934 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000935 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100936 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100937 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100938
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000939 case Primitive::kPrimLong: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100940 uint32_t index = gp_index_;
941 gp_index_ += 2;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000942 stack_index_ += 2;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100943 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100944 X86ManagedRegister pair = X86ManagedRegister::FromRegisterPair(
945 calling_convention.GetRegisterPairAt(index));
946 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100947 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000948 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
949 }
950 }
951
952 case Primitive::kPrimFloat: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100953 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000954 stack_index_++;
955 if (index < calling_convention.GetNumberOfFpuRegisters()) {
956 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
957 } else {
958 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
959 }
960 }
961
962 case Primitive::kPrimDouble: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100963 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000964 stack_index_ += 2;
965 if (index < calling_convention.GetNumberOfFpuRegisters()) {
966 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
967 } else {
968 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100969 }
970 }
971
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100972 case Primitive::kPrimVoid:
973 LOG(FATAL) << "Unexpected parameter type " << type;
974 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100975 }
Roland Levillain0d5a2812015-11-13 10:07:31 +0000976 return Location::NoLocation();
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100977}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100978
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100979void CodeGeneratorX86::Move32(Location destination, Location source) {
980 if (source.Equals(destination)) {
981 return;
982 }
983 if (destination.IsRegister()) {
984 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000985 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100986 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000987 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100988 } else {
989 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000990 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100991 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100992 } else if (destination.IsFpuRegister()) {
993 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000994 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100995 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000996 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100997 } else {
998 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000999 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001000 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001001 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001002 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001003 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001004 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001005 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001006 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Mark Mendell7c8d0092015-01-26 11:21:33 -05001007 } else if (source.IsConstant()) {
1008 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001009 int32_t value = GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05001010 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001011 } else {
1012 DCHECK(source.IsStackSlot());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001013 __ pushl(Address(ESP, source.GetStackIndex()));
1014 __ popl(Address(ESP, destination.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001015 }
1016 }
1017}
1018
1019void CodeGeneratorX86::Move64(Location destination, Location source) {
1020 if (source.Equals(destination)) {
1021 return;
1022 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001023 if (destination.IsRegisterPair()) {
1024 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001025 EmitParallelMoves(
1026 Location::RegisterLocation(source.AsRegisterPairHigh<Register>()),
1027 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001028 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001029 Location::RegisterLocation(source.AsRegisterPairLow<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001030 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()),
1031 Primitive::kPrimInt);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001032 } else if (source.IsFpuRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001033 XmmRegister src_reg = source.AsFpuRegister<XmmRegister>();
1034 __ movd(destination.AsRegisterPairLow<Register>(), src_reg);
1035 __ psrlq(src_reg, Immediate(32));
1036 __ movd(destination.AsRegisterPairHigh<Register>(), src_reg);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001037 } else {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001038 // No conflict possible, so just do the moves.
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001039 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001040 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
1041 __ movl(destination.AsRegisterPairHigh<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001042 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
1043 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001044 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05001045 if (source.IsFpuRegister()) {
1046 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
1047 } else if (source.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001048 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01001049 } else if (source.IsRegisterPair()) {
1050 size_t elem_size = Primitive::ComponentSize(Primitive::kPrimInt);
1051 // Create stack space for 2 elements.
1052 __ subl(ESP, Immediate(2 * elem_size));
1053 __ movl(Address(ESP, 0), source.AsRegisterPairLow<Register>());
1054 __ movl(Address(ESP, elem_size), source.AsRegisterPairHigh<Register>());
1055 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
1056 // And remove the temporary stack space we allocated.
1057 __ addl(ESP, Immediate(2 * elem_size));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001058 } else {
1059 LOG(FATAL) << "Unimplemented";
1060 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001061 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001062 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001063 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001064 // No conflict possible, so just do the moves.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001065 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegisterPairLow<Register>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001066 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001067 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001068 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001069 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001070 } else if (source.IsConstant()) {
1071 HConstant* constant = source.GetConstant();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001072 DCHECK(constant->IsLongConstant() || constant->IsDoubleConstant());
1073 int64_t value = GetInt64ValueOf(constant);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001074 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(Low32Bits(value)));
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001075 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
1076 Immediate(High32Bits(value)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001077 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001078 DCHECK(source.IsDoubleStackSlot()) << source;
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001079 EmitParallelMoves(
1080 Location::StackSlot(source.GetStackIndex()),
1081 Location::StackSlot(destination.GetStackIndex()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001082 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001083 Location::StackSlot(source.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001084 Location::StackSlot(destination.GetHighStackIndex(kX86WordSize)),
1085 Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001086 }
1087 }
1088}
1089
Calin Juravle175dc732015-08-25 15:42:32 +01001090void CodeGeneratorX86::MoveConstant(Location location, int32_t value) {
1091 DCHECK(location.IsRegister());
1092 __ movl(location.AsRegister<Register>(), Immediate(value));
1093}
1094
Calin Juravlee460d1d2015-09-29 04:52:17 +01001095void CodeGeneratorX86::MoveLocation(Location dst, Location src, Primitive::Type dst_type) {
David Brazdil74eb1b22015-12-14 11:44:01 +00001096 HParallelMove move(GetGraph()->GetArena());
1097 if (dst_type == Primitive::kPrimLong && !src.IsConstant() && !src.IsFpuRegister()) {
1098 move.AddMove(src.ToLow(), dst.ToLow(), Primitive::kPrimInt, nullptr);
1099 move.AddMove(src.ToHigh(), dst.ToHigh(), Primitive::kPrimInt, nullptr);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001100 } else {
David Brazdil74eb1b22015-12-14 11:44:01 +00001101 move.AddMove(src, dst, dst_type, nullptr);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001102 }
David Brazdil74eb1b22015-12-14 11:44:01 +00001103 GetMoveResolver()->EmitNativeCode(&move);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001104}
1105
1106void CodeGeneratorX86::AddLocationAsTemp(Location location, LocationSummary* locations) {
1107 if (location.IsRegister()) {
1108 locations->AddTemp(location);
1109 } else if (location.IsRegisterPair()) {
1110 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
1111 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
1112 } else {
1113 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1114 }
1115}
1116
David Brazdilfc6a86a2015-06-26 10:33:45 +00001117void InstructionCodeGeneratorX86::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001118 DCHECK(!successor->IsExitBlock());
1119
1120 HBasicBlock* block = got->GetBlock();
1121 HInstruction* previous = got->GetPrevious();
1122
1123 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +00001124 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001125 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1126 return;
1127 }
1128
1129 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1130 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1131 }
1132 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001133 __ jmp(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001134 }
1135}
1136
David Brazdilfc6a86a2015-06-26 10:33:45 +00001137void LocationsBuilderX86::VisitGoto(HGoto* got) {
1138 got->SetLocations(nullptr);
1139}
1140
1141void InstructionCodeGeneratorX86::VisitGoto(HGoto* got) {
1142 HandleGoto(got, got->GetSuccessor());
1143}
1144
1145void LocationsBuilderX86::VisitTryBoundary(HTryBoundary* try_boundary) {
1146 try_boundary->SetLocations(nullptr);
1147}
1148
1149void InstructionCodeGeneratorX86::VisitTryBoundary(HTryBoundary* try_boundary) {
1150 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
1151 if (!successor->IsExitBlock()) {
1152 HandleGoto(try_boundary, successor);
1153 }
1154}
1155
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001156void LocationsBuilderX86::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001157 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001158}
1159
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001160void InstructionCodeGeneratorX86::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001161}
1162
Mark Mendell152408f2015-12-31 12:28:50 -05001163template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001164void InstructionCodeGeneratorX86::GenerateFPJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001165 LabelType* true_label,
1166 LabelType* false_label) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001167 if (cond->IsFPConditionTrueIfNaN()) {
1168 __ j(kUnordered, true_label);
1169 } else if (cond->IsFPConditionFalseIfNaN()) {
1170 __ j(kUnordered, false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001171 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001172 __ j(X86UnsignedOrFPCondition(cond->GetCondition()), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001173}
1174
Mark Mendell152408f2015-12-31 12:28:50 -05001175template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001176void InstructionCodeGeneratorX86::GenerateLongComparesAndJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001177 LabelType* true_label,
1178 LabelType* false_label) {
Mark Mendellc4701932015-04-10 13:18:51 -04001179 LocationSummary* locations = cond->GetLocations();
1180 Location left = locations->InAt(0);
1181 Location right = locations->InAt(1);
1182 IfCondition if_cond = cond->GetCondition();
1183
Mark Mendellc4701932015-04-10 13:18:51 -04001184 Register left_high = left.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001185 Register left_low = left.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001186 IfCondition true_high_cond = if_cond;
1187 IfCondition false_high_cond = cond->GetOppositeCondition();
Aart Bike9f37602015-10-09 11:15:55 -07001188 Condition final_condition = X86UnsignedOrFPCondition(if_cond); // unsigned on lower part
Mark Mendellc4701932015-04-10 13:18:51 -04001189
1190 // Set the conditions for the test, remembering that == needs to be
1191 // decided using the low words.
1192 switch (if_cond) {
1193 case kCondEQ:
Mark Mendellc4701932015-04-10 13:18:51 -04001194 case kCondNE:
Roland Levillain4fa13f62015-07-06 18:11:54 +01001195 // Nothing to do.
Mark Mendellc4701932015-04-10 13:18:51 -04001196 break;
1197 case kCondLT:
1198 false_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -04001199 break;
1200 case kCondLE:
1201 true_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -04001202 break;
1203 case kCondGT:
1204 false_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -04001205 break;
1206 case kCondGE:
1207 true_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -04001208 break;
Aart Bike9f37602015-10-09 11:15:55 -07001209 case kCondB:
1210 false_high_cond = kCondA;
1211 break;
1212 case kCondBE:
1213 true_high_cond = kCondB;
1214 break;
1215 case kCondA:
1216 false_high_cond = kCondB;
1217 break;
1218 case kCondAE:
1219 true_high_cond = kCondA;
1220 break;
Mark Mendellc4701932015-04-10 13:18:51 -04001221 }
1222
1223 if (right.IsConstant()) {
1224 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
Mark Mendellc4701932015-04-10 13:18:51 -04001225 int32_t val_high = High32Bits(value);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001226 int32_t val_low = Low32Bits(value);
Mark Mendellc4701932015-04-10 13:18:51 -04001227
Aart Bika19616e2016-02-01 18:57:58 -08001228 codegen_->Compare32BitValue(left_high, val_high);
Mark Mendellc4701932015-04-10 13:18:51 -04001229 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001230 __ j(X86Condition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001231 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001232 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001233 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001234 __ j(X86Condition(true_high_cond), true_label);
1235 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001236 }
1237 // Must be equal high, so compare the lows.
Aart Bika19616e2016-02-01 18:57:58 -08001238 codegen_->Compare32BitValue(left_low, val_low);
Mark Mendell8659e842016-02-16 10:41:46 -05001239 } else if (right.IsRegisterPair()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001240 Register right_high = right.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001241 Register right_low = right.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001242
1243 __ cmpl(left_high, right_high);
1244 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001245 __ j(X86Condition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001246 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001247 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001248 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001249 __ j(X86Condition(true_high_cond), true_label);
1250 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001251 }
1252 // Must be equal high, so compare the lows.
1253 __ cmpl(left_low, right_low);
Mark Mendell8659e842016-02-16 10:41:46 -05001254 } else {
1255 DCHECK(right.IsDoubleStackSlot());
1256 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
1257 if (if_cond == kCondNE) {
1258 __ j(X86Condition(true_high_cond), true_label);
1259 } else if (if_cond == kCondEQ) {
1260 __ j(X86Condition(false_high_cond), false_label);
1261 } else {
1262 __ j(X86Condition(true_high_cond), true_label);
1263 __ j(X86Condition(false_high_cond), false_label);
1264 }
1265 // Must be equal high, so compare the lows.
1266 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Mark Mendellc4701932015-04-10 13:18:51 -04001267 }
1268 // The last comparison might be unsigned.
1269 __ j(final_condition, true_label);
1270}
1271
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001272void InstructionCodeGeneratorX86::GenerateFPCompare(Location lhs,
1273 Location rhs,
1274 HInstruction* insn,
1275 bool is_double) {
1276 HX86LoadFromConstantTable* const_area = insn->InputAt(1)->AsX86LoadFromConstantTable();
1277 if (is_double) {
1278 if (rhs.IsFpuRegister()) {
1279 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1280 } else if (const_area != nullptr) {
1281 DCHECK(const_area->IsEmittedAtUseSite());
1282 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(),
1283 codegen_->LiteralDoubleAddress(
1284 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
1285 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
1286 } else {
1287 DCHECK(rhs.IsDoubleStackSlot());
1288 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(), Address(ESP, rhs.GetStackIndex()));
1289 }
1290 } else {
1291 if (rhs.IsFpuRegister()) {
1292 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1293 } else if (const_area != nullptr) {
1294 DCHECK(const_area->IsEmittedAtUseSite());
1295 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(),
1296 codegen_->LiteralFloatAddress(
1297 const_area->GetConstant()->AsFloatConstant()->GetValue(),
1298 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
1299 } else {
1300 DCHECK(rhs.IsStackSlot());
1301 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(), Address(ESP, rhs.GetStackIndex()));
1302 }
1303 }
1304}
1305
Mark Mendell152408f2015-12-31 12:28:50 -05001306template<class LabelType>
David Brazdil0debae72015-11-12 18:37:00 +00001307void InstructionCodeGeneratorX86::GenerateCompareTestAndBranch(HCondition* condition,
Mark Mendell152408f2015-12-31 12:28:50 -05001308 LabelType* true_target_in,
1309 LabelType* false_target_in) {
David Brazdil0debae72015-11-12 18:37:00 +00001310 // Generated branching requires both targets to be explicit. If either of the
1311 // targets is nullptr (fallthrough) use and bind `fallthrough_target` instead.
Mark Mendell152408f2015-12-31 12:28:50 -05001312 LabelType fallthrough_target;
1313 LabelType* true_target = true_target_in == nullptr ? &fallthrough_target : true_target_in;
1314 LabelType* false_target = false_target_in == nullptr ? &fallthrough_target : false_target_in;
David Brazdil0debae72015-11-12 18:37:00 +00001315
Mark Mendellc4701932015-04-10 13:18:51 -04001316 LocationSummary* locations = condition->GetLocations();
1317 Location left = locations->InAt(0);
1318 Location right = locations->InAt(1);
1319
Mark Mendellc4701932015-04-10 13:18:51 -04001320 Primitive::Type type = condition->InputAt(0)->GetType();
1321 switch (type) {
1322 case Primitive::kPrimLong:
1323 GenerateLongComparesAndJumps(condition, true_target, false_target);
1324 break;
1325 case Primitive::kPrimFloat:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001326 GenerateFPCompare(left, right, condition, false);
Mark Mendellc4701932015-04-10 13:18:51 -04001327 GenerateFPJumps(condition, true_target, false_target);
1328 break;
1329 case Primitive::kPrimDouble:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001330 GenerateFPCompare(left, right, condition, true);
Mark Mendellc4701932015-04-10 13:18:51 -04001331 GenerateFPJumps(condition, true_target, false_target);
1332 break;
1333 default:
1334 LOG(FATAL) << "Unexpected compare type " << type;
1335 }
1336
David Brazdil0debae72015-11-12 18:37:00 +00001337 if (false_target != &fallthrough_target) {
Mark Mendellc4701932015-04-10 13:18:51 -04001338 __ jmp(false_target);
1339 }
David Brazdil0debae72015-11-12 18:37:00 +00001340
1341 if (fallthrough_target.IsLinked()) {
1342 __ Bind(&fallthrough_target);
1343 }
Mark Mendellc4701932015-04-10 13:18:51 -04001344}
1345
David Brazdil0debae72015-11-12 18:37:00 +00001346static bool AreEflagsSetFrom(HInstruction* cond, HInstruction* branch) {
1347 // Moves may affect the eflags register (move zero uses xorl), so the EFLAGS
1348 // are set only strictly before `branch`. We can't use the eflags on long/FP
1349 // conditions if they are materialized due to the complex branching.
1350 return cond->IsCondition() &&
1351 cond->GetNext() == branch &&
1352 cond->InputAt(0)->GetType() != Primitive::kPrimLong &&
1353 !Primitive::IsFloatingPointType(cond->InputAt(0)->GetType());
1354}
1355
Mark Mendell152408f2015-12-31 12:28:50 -05001356template<class LabelType>
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001357void InstructionCodeGeneratorX86::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00001358 size_t condition_input_index,
Mark Mendell152408f2015-12-31 12:28:50 -05001359 LabelType* true_target,
1360 LabelType* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00001361 HInstruction* cond = instruction->InputAt(condition_input_index);
1362
1363 if (true_target == nullptr && false_target == nullptr) {
1364 // Nothing to do. The code always falls through.
1365 return;
1366 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00001367 // Constant condition, statically compared against "true" (integer value 1).
1368 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00001369 if (true_target != nullptr) {
1370 __ jmp(true_target);
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001371 }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001372 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00001373 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00001374 if (false_target != nullptr) {
1375 __ jmp(false_target);
1376 }
1377 }
1378 return;
1379 }
1380
1381 // The following code generates these patterns:
1382 // (1) true_target == nullptr && false_target != nullptr
1383 // - opposite condition true => branch to false_target
1384 // (2) true_target != nullptr && false_target == nullptr
1385 // - condition true => branch to true_target
1386 // (3) true_target != nullptr && false_target != nullptr
1387 // - condition true => branch to true_target
1388 // - branch to false_target
1389 if (IsBooleanValueOrMaterializedCondition(cond)) {
1390 if (AreEflagsSetFrom(cond, instruction)) {
1391 if (true_target == nullptr) {
1392 __ j(X86Condition(cond->AsCondition()->GetOppositeCondition()), false_target);
1393 } else {
1394 __ j(X86Condition(cond->AsCondition()->GetCondition()), true_target);
1395 }
1396 } else {
1397 // Materialized condition, compare against 0.
1398 Location lhs = instruction->GetLocations()->InAt(condition_input_index);
1399 if (lhs.IsRegister()) {
1400 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
1401 } else {
1402 __ cmpl(Address(ESP, lhs.GetStackIndex()), Immediate(0));
1403 }
1404 if (true_target == nullptr) {
1405 __ j(kEqual, false_target);
1406 } else {
1407 __ j(kNotEqual, true_target);
1408 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001409 }
1410 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001411 // Condition has not been materialized, use its inputs as the comparison and
1412 // its condition as the branch condition.
Mark Mendellb8b97692015-05-22 16:58:19 -04001413 HCondition* condition = cond->AsCondition();
David Brazdil0debae72015-11-12 18:37:00 +00001414
1415 // If this is a long or FP comparison that has been folded into
1416 // the HCondition, generate the comparison directly.
1417 Primitive::Type type = condition->InputAt(0)->GetType();
1418 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
1419 GenerateCompareTestAndBranch(condition, true_target, false_target);
1420 return;
1421 }
1422
1423 Location lhs = condition->GetLocations()->InAt(0);
1424 Location rhs = condition->GetLocations()->InAt(1);
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001425 // LHS is guaranteed to be in a register (see LocationsBuilderX86::HandleCondition).
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001426 codegen_->GenerateIntCompare(lhs, rhs);
David Brazdil0debae72015-11-12 18:37:00 +00001427 if (true_target == nullptr) {
1428 __ j(X86Condition(condition->GetOppositeCondition()), false_target);
1429 } else {
Mark Mendellb8b97692015-05-22 16:58:19 -04001430 __ j(X86Condition(condition->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -07001431 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001432 }
David Brazdil0debae72015-11-12 18:37:00 +00001433
1434 // If neither branch falls through (case 3), the conditional branch to `true_target`
1435 // was already emitted (case 2) and we need to emit a jump to `false_target`.
1436 if (true_target != nullptr && false_target != nullptr) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001437 __ jmp(false_target);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001438 }
1439}
1440
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001441void LocationsBuilderX86::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001442 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
1443 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001444 locations->SetInAt(0, Location::Any());
1445 }
1446}
1447
1448void InstructionCodeGeneratorX86::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001449 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
1450 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
1451 Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
1452 nullptr : codegen_->GetLabelOf(true_successor);
1453 Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
1454 nullptr : codegen_->GetLabelOf(false_successor);
1455 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001456}
1457
1458void LocationsBuilderX86::VisitDeoptimize(HDeoptimize* deoptimize) {
1459 LocationSummary* locations = new (GetGraph()->GetArena())
1460 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Vladimir Marko239d6ea2016-09-05 10:44:04 +01001461 locations->SetCustomSlowPathCallerSaves(RegisterSet()); // No caller-save registers.
David Brazdil0debae72015-11-12 18:37:00 +00001462 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001463 locations->SetInAt(0, Location::Any());
1464 }
1465}
1466
1467void InstructionCodeGeneratorX86::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08001468 SlowPathCode* slow_path = deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathX86>(deoptimize);
David Brazdil74eb1b22015-12-14 11:44:01 +00001469 GenerateTestAndBranch<Label>(deoptimize,
1470 /* condition_input_index */ 0,
1471 slow_path->GetEntryLabel(),
1472 /* false_target */ nullptr);
1473}
1474
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001475static bool SelectCanUseCMOV(HSelect* select) {
1476 // There are no conditional move instructions for XMMs.
1477 if (Primitive::IsFloatingPointType(select->GetType())) {
1478 return false;
1479 }
1480
1481 // A FP condition doesn't generate the single CC that we need.
1482 // In 32 bit mode, a long condition doesn't generate a single CC either.
1483 HInstruction* condition = select->GetCondition();
1484 if (condition->IsCondition()) {
1485 Primitive::Type compare_type = condition->InputAt(0)->GetType();
1486 if (compare_type == Primitive::kPrimLong ||
1487 Primitive::IsFloatingPointType(compare_type)) {
1488 return false;
1489 }
1490 }
1491
1492 // We can generate a CMOV for this Select.
1493 return true;
1494}
1495
David Brazdil74eb1b22015-12-14 11:44:01 +00001496void LocationsBuilderX86::VisitSelect(HSelect* select) {
1497 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001498 if (Primitive::IsFloatingPointType(select->GetType())) {
David Brazdil74eb1b22015-12-14 11:44:01 +00001499 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001500 locations->SetInAt(1, Location::Any());
David Brazdil74eb1b22015-12-14 11:44:01 +00001501 } else {
1502 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001503 if (SelectCanUseCMOV(select)) {
1504 if (select->InputAt(1)->IsConstant()) {
1505 // Cmov can't handle a constant value.
1506 locations->SetInAt(1, Location::RequiresRegister());
1507 } else {
1508 locations->SetInAt(1, Location::Any());
1509 }
1510 } else {
1511 locations->SetInAt(1, Location::Any());
1512 }
David Brazdil74eb1b22015-12-14 11:44:01 +00001513 }
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001514 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
1515 locations->SetInAt(2, Location::RequiresRegister());
David Brazdil74eb1b22015-12-14 11:44:01 +00001516 }
1517 locations->SetOut(Location::SameAsFirstInput());
1518}
1519
1520void InstructionCodeGeneratorX86::VisitSelect(HSelect* select) {
1521 LocationSummary* locations = select->GetLocations();
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001522 DCHECK(locations->InAt(0).Equals(locations->Out()));
1523 if (SelectCanUseCMOV(select)) {
1524 // If both the condition and the source types are integer, we can generate
1525 // a CMOV to implement Select.
1526
1527 HInstruction* select_condition = select->GetCondition();
1528 Condition cond = kNotEqual;
1529
1530 // Figure out how to test the 'condition'.
1531 if (select_condition->IsCondition()) {
1532 HCondition* condition = select_condition->AsCondition();
1533 if (!condition->IsEmittedAtUseSite()) {
1534 // This was a previously materialized condition.
1535 // Can we use the existing condition code?
1536 if (AreEflagsSetFrom(condition, select)) {
1537 // Materialization was the previous instruction. Condition codes are right.
1538 cond = X86Condition(condition->GetCondition());
1539 } else {
1540 // No, we have to recreate the condition code.
1541 Register cond_reg = locations->InAt(2).AsRegister<Register>();
1542 __ testl(cond_reg, cond_reg);
1543 }
1544 } else {
1545 // We can't handle FP or long here.
1546 DCHECK_NE(condition->InputAt(0)->GetType(), Primitive::kPrimLong);
1547 DCHECK(!Primitive::IsFloatingPointType(condition->InputAt(0)->GetType()));
1548 LocationSummary* cond_locations = condition->GetLocations();
Roland Levillain0b671c02016-08-19 12:02:34 +01001549 codegen_->GenerateIntCompare(cond_locations->InAt(0), cond_locations->InAt(1));
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001550 cond = X86Condition(condition->GetCondition());
1551 }
1552 } else {
1553 // Must be a boolean condition, which needs to be compared to 0.
1554 Register cond_reg = locations->InAt(2).AsRegister<Register>();
1555 __ testl(cond_reg, cond_reg);
1556 }
1557
1558 // If the condition is true, overwrite the output, which already contains false.
1559 Location false_loc = locations->InAt(0);
1560 Location true_loc = locations->InAt(1);
1561 if (select->GetType() == Primitive::kPrimLong) {
1562 // 64 bit conditional move.
1563 Register false_high = false_loc.AsRegisterPairHigh<Register>();
1564 Register false_low = false_loc.AsRegisterPairLow<Register>();
1565 if (true_loc.IsRegisterPair()) {
1566 __ cmovl(cond, false_high, true_loc.AsRegisterPairHigh<Register>());
1567 __ cmovl(cond, false_low, true_loc.AsRegisterPairLow<Register>());
1568 } else {
1569 __ cmovl(cond, false_high, Address(ESP, true_loc.GetHighStackIndex(kX86WordSize)));
1570 __ cmovl(cond, false_low, Address(ESP, true_loc.GetStackIndex()));
1571 }
1572 } else {
1573 // 32 bit conditional move.
1574 Register false_reg = false_loc.AsRegister<Register>();
1575 if (true_loc.IsRegister()) {
1576 __ cmovl(cond, false_reg, true_loc.AsRegister<Register>());
1577 } else {
1578 __ cmovl(cond, false_reg, Address(ESP, true_loc.GetStackIndex()));
1579 }
1580 }
1581 } else {
1582 NearLabel false_target;
1583 GenerateTestAndBranch<NearLabel>(
1584 select, /* condition_input_index */ 2, /* true_target */ nullptr, &false_target);
1585 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
1586 __ Bind(&false_target);
1587 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001588}
1589
David Srbecky0cf44932015-12-09 14:09:59 +00001590void LocationsBuilderX86::VisitNativeDebugInfo(HNativeDebugInfo* info) {
1591 new (GetGraph()->GetArena()) LocationSummary(info);
1592}
1593
David Srbeckyd28f4a02016-03-14 17:14:24 +00001594void InstructionCodeGeneratorX86::VisitNativeDebugInfo(HNativeDebugInfo*) {
1595 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00001596}
1597
1598void CodeGeneratorX86::GenerateNop() {
1599 __ nop();
David Srbecky0cf44932015-12-09 14:09:59 +00001600}
1601
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001602void LocationsBuilderX86::HandleCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001603 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +01001604 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Mark Mendellc4701932015-04-10 13:18:51 -04001605 // Handle the long/FP comparisons made in instruction simplification.
1606 switch (cond->InputAt(0)->GetType()) {
1607 case Primitive::kPrimLong: {
1608 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell8659e842016-02-16 10:41:46 -05001609 locations->SetInAt(1, Location::Any());
David Brazdilb3e773e2016-01-26 11:28:37 +00001610 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001611 locations->SetOut(Location::RequiresRegister());
1612 }
1613 break;
1614 }
1615 case Primitive::kPrimFloat:
1616 case Primitive::kPrimDouble: {
1617 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001618 if (cond->InputAt(1)->IsX86LoadFromConstantTable()) {
1619 DCHECK(cond->InputAt(1)->IsEmittedAtUseSite());
1620 } else if (cond->InputAt(1)->IsConstant()) {
1621 locations->SetInAt(1, Location::RequiresFpuRegister());
1622 } else {
1623 locations->SetInAt(1, Location::Any());
1624 }
David Brazdilb3e773e2016-01-26 11:28:37 +00001625 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001626 locations->SetOut(Location::RequiresRegister());
1627 }
1628 break;
1629 }
1630 default:
1631 locations->SetInAt(0, Location::RequiresRegister());
1632 locations->SetInAt(1, Location::Any());
David Brazdilb3e773e2016-01-26 11:28:37 +00001633 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001634 // We need a byte register.
1635 locations->SetOut(Location::RegisterLocation(ECX));
1636 }
1637 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001638 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001639}
1640
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001641void InstructionCodeGeneratorX86::HandleCondition(HCondition* cond) {
David Brazdilb3e773e2016-01-26 11:28:37 +00001642 if (cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001643 return;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001644 }
Mark Mendellc4701932015-04-10 13:18:51 -04001645
1646 LocationSummary* locations = cond->GetLocations();
1647 Location lhs = locations->InAt(0);
1648 Location rhs = locations->InAt(1);
1649 Register reg = locations->Out().AsRegister<Register>();
Mark Mendell152408f2015-12-31 12:28:50 -05001650 NearLabel true_label, false_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001651
1652 switch (cond->InputAt(0)->GetType()) {
1653 default: {
1654 // Integer case.
1655
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01001656 // Clear output register: setb only sets the low byte.
Mark Mendellc4701932015-04-10 13:18:51 -04001657 __ xorl(reg, reg);
Roland Levillain0b671c02016-08-19 12:02:34 +01001658 codegen_->GenerateIntCompare(lhs, rhs);
Aart Bike9f37602015-10-09 11:15:55 -07001659 __ setb(X86Condition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001660 return;
1661 }
1662 case Primitive::kPrimLong:
1663 GenerateLongComparesAndJumps(cond, &true_label, &false_label);
1664 break;
1665 case Primitive::kPrimFloat:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001666 GenerateFPCompare(lhs, rhs, cond, false);
Mark Mendellc4701932015-04-10 13:18:51 -04001667 GenerateFPJumps(cond, &true_label, &false_label);
1668 break;
1669 case Primitive::kPrimDouble:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001670 GenerateFPCompare(lhs, rhs, cond, true);
Mark Mendellc4701932015-04-10 13:18:51 -04001671 GenerateFPJumps(cond, &true_label, &false_label);
1672 break;
1673 }
1674
1675 // Convert the jumps into the result.
Mark Mendell0c9497d2015-08-21 09:30:05 -04001676 NearLabel done_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001677
Roland Levillain4fa13f62015-07-06 18:11:54 +01001678 // False case: result = 0.
Mark Mendellc4701932015-04-10 13:18:51 -04001679 __ Bind(&false_label);
1680 __ xorl(reg, reg);
1681 __ jmp(&done_label);
1682
Roland Levillain4fa13f62015-07-06 18:11:54 +01001683 // True case: result = 1.
Mark Mendellc4701932015-04-10 13:18:51 -04001684 __ Bind(&true_label);
1685 __ movl(reg, Immediate(1));
1686 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07001687}
1688
1689void LocationsBuilderX86::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001690 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001691}
1692
1693void InstructionCodeGeneratorX86::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001694 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001695}
1696
1697void LocationsBuilderX86::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001698 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001699}
1700
1701void InstructionCodeGeneratorX86::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001702 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001703}
1704
1705void LocationsBuilderX86::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001706 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001707}
1708
1709void InstructionCodeGeneratorX86::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001710 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001711}
1712
1713void LocationsBuilderX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001714 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001715}
1716
1717void InstructionCodeGeneratorX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001718 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001719}
1720
1721void LocationsBuilderX86::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001722 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001723}
1724
1725void InstructionCodeGeneratorX86::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001726 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001727}
1728
1729void LocationsBuilderX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001730 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001731}
1732
1733void InstructionCodeGeneratorX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001734 HandleCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001735}
1736
Aart Bike9f37602015-10-09 11:15:55 -07001737void LocationsBuilderX86::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001738 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001739}
1740
1741void InstructionCodeGeneratorX86::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001742 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001743}
1744
1745void LocationsBuilderX86::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001746 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001747}
1748
1749void InstructionCodeGeneratorX86::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001750 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001751}
1752
1753void LocationsBuilderX86::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001754 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001755}
1756
1757void InstructionCodeGeneratorX86::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001758 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001759}
1760
1761void LocationsBuilderX86::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001762 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001763}
1764
1765void InstructionCodeGeneratorX86::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001766 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001767}
1768
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001769void LocationsBuilderX86::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001770 LocationSummary* locations =
1771 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001772 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001773}
1774
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001775void InstructionCodeGeneratorX86::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001776 // Will be generated at use site.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001777}
1778
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001779void LocationsBuilderX86::VisitNullConstant(HNullConstant* constant) {
1780 LocationSummary* locations =
1781 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1782 locations->SetOut(Location::ConstantLocation(constant));
1783}
1784
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001785void InstructionCodeGeneratorX86::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001786 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001787}
1788
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001789void LocationsBuilderX86::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001790 LocationSummary* locations =
1791 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001792 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001793}
1794
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001795void InstructionCodeGeneratorX86::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001796 // Will be generated at use site.
1797}
1798
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001799void LocationsBuilderX86::VisitFloatConstant(HFloatConstant* constant) {
1800 LocationSummary* locations =
1801 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1802 locations->SetOut(Location::ConstantLocation(constant));
1803}
1804
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001805void InstructionCodeGeneratorX86::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001806 // Will be generated at use site.
1807}
1808
1809void LocationsBuilderX86::VisitDoubleConstant(HDoubleConstant* constant) {
1810 LocationSummary* locations =
1811 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1812 locations->SetOut(Location::ConstantLocation(constant));
1813}
1814
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001815void InstructionCodeGeneratorX86::VisitDoubleConstant(HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001816 // Will be generated at use site.
1817}
1818
Calin Juravle27df7582015-04-17 19:12:31 +01001819void LocationsBuilderX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1820 memory_barrier->SetLocations(nullptr);
1821}
1822
1823void InstructionCodeGeneratorX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00001824 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01001825}
1826
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001827void LocationsBuilderX86::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001828 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001829}
1830
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001831void InstructionCodeGeneratorX86::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001832 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001833}
1834
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001835void LocationsBuilderX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001836 LocationSummary* locations =
1837 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001838 switch (ret->InputAt(0)->GetType()) {
1839 case Primitive::kPrimBoolean:
1840 case Primitive::kPrimByte:
1841 case Primitive::kPrimChar:
1842 case Primitive::kPrimShort:
1843 case Primitive::kPrimInt:
1844 case Primitive::kPrimNot:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001845 locations->SetInAt(0, Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001846 break;
1847
1848 case Primitive::kPrimLong:
1849 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001850 0, Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001851 break;
1852
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001853 case Primitive::kPrimFloat:
1854 case Primitive::kPrimDouble:
1855 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001856 0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001857 break;
1858
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001859 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001860 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001861 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001862}
1863
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001864void InstructionCodeGeneratorX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001865 if (kIsDebugBuild) {
1866 switch (ret->InputAt(0)->GetType()) {
1867 case Primitive::kPrimBoolean:
1868 case Primitive::kPrimByte:
1869 case Primitive::kPrimChar:
1870 case Primitive::kPrimShort:
1871 case Primitive::kPrimInt:
1872 case Primitive::kPrimNot:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001873 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<Register>(), EAX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001874 break;
1875
1876 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001877 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairLow<Register>(), EAX);
1878 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairHigh<Register>(), EDX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001879 break;
1880
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001881 case Primitive::kPrimFloat:
1882 case Primitive::kPrimDouble:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001883 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>(), XMM0);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001884 break;
1885
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001886 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001887 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001888 }
1889 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001890 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001891}
1892
Calin Juravle175dc732015-08-25 15:42:32 +01001893void LocationsBuilderX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
1894 // The trampoline uses the same calling convention as dex calling conventions,
1895 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
1896 // the method_idx.
1897 HandleInvoke(invoke);
1898}
1899
1900void InstructionCodeGeneratorX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
1901 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
1902}
1903
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001904void LocationsBuilderX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00001905 // Explicit clinit checks triggered by static invokes must have been pruned by
1906 // art::PrepareForRegisterAllocation.
1907 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001908
Mark Mendellfb8d2792015-03-31 22:16:59 -04001909 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001910 if (intrinsic.TryDispatch(invoke)) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00001911 if (invoke->GetLocations()->CanCall() && invoke->HasPcRelativeDexCache()) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00001912 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00001913 }
Mark Mendell09ed1a32015-03-25 08:30:06 -04001914 return;
1915 }
1916
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001917 HandleInvoke(invoke);
Nicolas Geoffray94015b92015-06-04 18:21:04 +01001918
Vladimir Marko0f7dca42015-11-02 14:36:43 +00001919 // For PC-relative dex cache the invoke has an extra input, the PC-relative address base.
1920 if (invoke->HasPcRelativeDexCache()) {
Vladimir Markob4536b72015-11-24 13:45:23 +00001921 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00001922 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001923}
1924
Mark Mendell09ed1a32015-03-25 08:30:06 -04001925static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86* codegen) {
1926 if (invoke->GetLocations()->Intrinsified()) {
1927 IntrinsicCodeGeneratorX86 intrinsic(codegen);
1928 intrinsic.Dispatch(invoke);
1929 return true;
1930 }
1931 return false;
1932}
1933
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001934void InstructionCodeGeneratorX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00001935 // Explicit clinit checks triggered by static invokes must have been pruned by
1936 // art::PrepareForRegisterAllocation.
1937 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001938
Mark Mendell09ed1a32015-03-25 08:30:06 -04001939 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1940 return;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001941 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001942
Nicolas Geoffray94015b92015-06-04 18:21:04 +01001943 LocationSummary* locations = invoke->GetLocations();
Mark Mendell09ed1a32015-03-25 08:30:06 -04001944 codegen_->GenerateStaticOrDirectCall(
Nicolas Geoffray94015b92015-06-04 18:21:04 +01001945 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Mingyao Yang8693fe12015-04-17 16:51:08 -07001946 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001947}
1948
1949void LocationsBuilderX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00001950 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
1951 if (intrinsic.TryDispatch(invoke)) {
1952 return;
1953 }
1954
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001955 HandleInvoke(invoke);
1956}
1957
1958void LocationsBuilderX86::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001959 InvokeDexCallingConventionVisitorX86 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001960 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001961}
1962
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001963void InstructionCodeGeneratorX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampebfb5ba92015-09-01 15:45:02 +00001964 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1965 return;
1966 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001967
Andreas Gampebfb5ba92015-09-01 15:45:02 +00001968 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001969 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001970 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001971}
1972
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001973void LocationsBuilderX86::VisitInvokeInterface(HInvokeInterface* invoke) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00001974 // This call to HandleInvoke allocates a temporary (core) register
1975 // which is also used to transfer the hidden argument from FP to
1976 // core register.
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001977 HandleInvoke(invoke);
1978 // Add the hidden argument.
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001979 invoke->GetLocations()->AddTemp(Location::FpuRegisterLocation(XMM7));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001980}
1981
1982void InstructionCodeGeneratorX86::VisitInvokeInterface(HInvokeInterface* invoke) {
1983 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain0d5a2812015-11-13 10:07:31 +00001984 LocationSummary* locations = invoke->GetLocations();
1985 Register temp = locations->GetTemp(0).AsRegister<Register>();
1986 XmmRegister hidden_reg = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001987 Location receiver = locations->InAt(0);
1988 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1989
Roland Levillain0d5a2812015-11-13 10:07:31 +00001990 // Set the hidden argument. This is safe to do this here, as XMM7
1991 // won't be modified thereafter, before the `call` instruction.
1992 DCHECK_EQ(XMM7, hidden_reg);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001993 __ movl(temp, Immediate(invoke->GetDexMethodIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00001994 __ movd(hidden_reg, temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001995
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001996 if (receiver.IsStackSlot()) {
1997 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00001998 // /* HeapReference<Class> */ temp = temp->klass_
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001999 __ movl(temp, Address(temp, class_offset));
2000 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002001 // /* HeapReference<Class> */ temp = receiver->klass_
Roland Levillain271ab9c2014-11-27 15:23:57 +00002002 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002003 }
Roland Levillain4d027112015-07-01 15:41:14 +01002004 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00002005 // Instead of simply (possibly) unpoisoning `temp` here, we should
2006 // emit a read barrier for the previous class reference load.
2007 // However this is not required in practice, as this is an
2008 // intermediate/temporary reference and because the current
2009 // concurrent copying collector keeps the from-space memory
2010 // intact/accessible until the end of the marking phase (the
2011 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01002012 __ MaybeUnpoisonHeapReference(temp);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002013 // temp = temp->GetAddressOfIMT()
2014 __ movl(temp,
2015 Address(temp, mirror::Class::ImtPtrOffset(kX86PointerSize).Uint32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002016 // temp = temp->GetImtEntryAt(method_offset);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002017 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00002018 invoke->GetImtIndex(), kX86PointerSize));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002019 __ movl(temp, Address(temp, method_offset));
2020 // call temp->GetEntryPoint();
Roland Levillain0d5a2812015-11-13 10:07:31 +00002021 __ call(Address(temp,
Andreas Gampe542451c2016-07-26 09:02:02 -07002022 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86PointerSize).Int32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002023
2024 DCHECK(!codegen_->IsLeafMethod());
2025 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2026}
2027
Roland Levillain88cb1752014-10-20 16:36:47 +01002028void LocationsBuilderX86::VisitNeg(HNeg* neg) {
2029 LocationSummary* locations =
2030 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2031 switch (neg->GetResultType()) {
2032 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002033 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01002034 locations->SetInAt(0, Location::RequiresRegister());
2035 locations->SetOut(Location::SameAsFirstInput());
2036 break;
2037
Roland Levillain88cb1752014-10-20 16:36:47 +01002038 case Primitive::kPrimFloat:
Roland Levillain5368c212014-11-27 15:03:41 +00002039 locations->SetInAt(0, Location::RequiresFpuRegister());
2040 locations->SetOut(Location::SameAsFirstInput());
2041 locations->AddTemp(Location::RequiresRegister());
2042 locations->AddTemp(Location::RequiresFpuRegister());
2043 break;
2044
Roland Levillain88cb1752014-10-20 16:36:47 +01002045 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002046 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00002047 locations->SetOut(Location::SameAsFirstInput());
2048 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01002049 break;
2050
2051 default:
2052 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2053 }
2054}
2055
2056void InstructionCodeGeneratorX86::VisitNeg(HNeg* neg) {
2057 LocationSummary* locations = neg->GetLocations();
2058 Location out = locations->Out();
2059 Location in = locations->InAt(0);
2060 switch (neg->GetResultType()) {
2061 case Primitive::kPrimInt:
2062 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002063 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002064 __ negl(out.AsRegister<Register>());
Roland Levillain88cb1752014-10-20 16:36:47 +01002065 break;
2066
2067 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002068 DCHECK(in.IsRegisterPair());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002069 DCHECK(in.Equals(out));
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002070 __ negl(out.AsRegisterPairLow<Register>());
2071 // Negation is similar to subtraction from zero. The least
2072 // significant byte triggers a borrow when it is different from
2073 // zero; to take it into account, add 1 to the most significant
2074 // byte if the carry flag (CF) is set to 1 after the first NEGL
2075 // operation.
2076 __ adcl(out.AsRegisterPairHigh<Register>(), Immediate(0));
2077 __ negl(out.AsRegisterPairHigh<Register>());
2078 break;
2079
Roland Levillain5368c212014-11-27 15:03:41 +00002080 case Primitive::kPrimFloat: {
2081 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002082 Register constant = locations->GetTemp(0).AsRegister<Register>();
2083 XmmRegister mask = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002084 // Implement float negation with an exclusive or with value
2085 // 0x80000000 (mask for bit 31, representing the sign of a
2086 // single-precision floating-point number).
2087 __ movl(constant, Immediate(INT32_C(0x80000000)));
2088 __ movd(mask, constant);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002089 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00002090 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002091 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00002092
Roland Levillain5368c212014-11-27 15:03:41 +00002093 case Primitive::kPrimDouble: {
2094 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002095 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002096 // Implement double negation with an exclusive or with value
2097 // 0x8000000000000000 (mask for bit 63, representing the sign of
2098 // a double-precision floating-point number).
2099 __ LoadLongConstant(mask, INT64_C(0x8000000000000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002100 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01002101 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002102 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002103
2104 default:
2105 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2106 }
2107}
2108
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002109void LocationsBuilderX86::VisitX86FPNeg(HX86FPNeg* neg) {
2110 LocationSummary* locations =
2111 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2112 DCHECK(Primitive::IsFloatingPointType(neg->GetType()));
2113 locations->SetInAt(0, Location::RequiresFpuRegister());
2114 locations->SetInAt(1, Location::RequiresRegister());
2115 locations->SetOut(Location::SameAsFirstInput());
2116 locations->AddTemp(Location::RequiresFpuRegister());
2117}
2118
2119void InstructionCodeGeneratorX86::VisitX86FPNeg(HX86FPNeg* neg) {
2120 LocationSummary* locations = neg->GetLocations();
2121 Location out = locations->Out();
2122 DCHECK(locations->InAt(0).Equals(out));
2123
2124 Register constant_area = locations->InAt(1).AsRegister<Register>();
2125 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
2126 if (neg->GetType() == Primitive::kPrimFloat) {
2127 __ movss(mask, codegen_->LiteralInt32Address(INT32_C(0x80000000), constant_area));
2128 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
2129 } else {
2130 __ movsd(mask, codegen_->LiteralInt64Address(INT64_C(0x8000000000000000), constant_area));
2131 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
2132 }
2133}
2134
Roland Levillaindff1f282014-11-05 14:15:05 +00002135void LocationsBuilderX86::VisitTypeConversion(HTypeConversion* conversion) {
Roland Levillaindff1f282014-11-05 14:15:05 +00002136 Primitive::Type result_type = conversion->GetResultType();
2137 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002138 DCHECK_NE(result_type, input_type);
Roland Levillain624279f2014-12-04 11:54:28 +00002139
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002140 // The float-to-long and double-to-long type conversions rely on a
2141 // call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00002142 LocationSummary::CallKind call_kind =
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002143 ((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
2144 && result_type == Primitive::kPrimLong)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01002145 ? LocationSummary::kCallOnMainOnly
Roland Levillain624279f2014-12-04 11:54:28 +00002146 : LocationSummary::kNoCall;
2147 LocationSummary* locations =
2148 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
2149
David Brazdilb2bd1c52015-03-25 11:17:37 +00002150 // The Java language does not allow treating boolean as an integral type but
2151 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00002152
Roland Levillaindff1f282014-11-05 14:15:05 +00002153 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002154 case Primitive::kPrimByte:
2155 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002156 case Primitive::kPrimLong: {
2157 // Type conversion from long to byte is a result of code transformations.
2158 HInstruction* input = conversion->InputAt(0);
2159 Location input_location = input->IsConstant()
2160 ? Location::ConstantLocation(input->AsConstant())
2161 : Location::RegisterPairLocation(EAX, EDX);
2162 locations->SetInAt(0, input_location);
2163 // Make the output overlap to please the register allocator. This greatly simplifies
2164 // the validation of the linear scan implementation
2165 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2166 break;
2167 }
David Brazdil46e2a392015-03-16 17:31:52 +00002168 case Primitive::kPrimBoolean:
2169 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002170 case Primitive::kPrimShort:
2171 case Primitive::kPrimInt:
2172 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002173 // Processing a Dex `int-to-byte' instruction.
Mark Mendell5f874182015-03-04 15:42:45 -05002174 locations->SetInAt(0, Location::ByteRegisterOrConstant(ECX, conversion->InputAt(0)));
2175 // Make the output overlap to please the register allocator. This greatly simplifies
2176 // the validation of the linear scan implementation
2177 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Roland Levillain51d3fc42014-11-13 14:11:42 +00002178 break;
2179
2180 default:
2181 LOG(FATAL) << "Unexpected type conversion from " << input_type
2182 << " to " << result_type;
2183 }
2184 break;
2185
Roland Levillain01a8d712014-11-14 16:27:39 +00002186 case Primitive::kPrimShort:
2187 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002188 case Primitive::kPrimLong:
2189 // Type conversion from long to short is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002190 case Primitive::kPrimBoolean:
2191 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002192 case Primitive::kPrimByte:
2193 case Primitive::kPrimInt:
2194 case Primitive::kPrimChar:
2195 // Processing a Dex `int-to-short' instruction.
2196 locations->SetInAt(0, Location::Any());
2197 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2198 break;
2199
2200 default:
2201 LOG(FATAL) << "Unexpected type conversion from " << input_type
2202 << " to " << result_type;
2203 }
2204 break;
2205
Roland Levillain946e1432014-11-11 17:35:19 +00002206 case Primitive::kPrimInt:
2207 switch (input_type) {
2208 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002209 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002210 locations->SetInAt(0, Location::Any());
2211 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2212 break;
2213
2214 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00002215 // Processing a Dex `float-to-int' instruction.
2216 locations->SetInAt(0, Location::RequiresFpuRegister());
2217 locations->SetOut(Location::RequiresRegister());
2218 locations->AddTemp(Location::RequiresFpuRegister());
2219 break;
2220
Roland Levillain946e1432014-11-11 17:35:19 +00002221 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002222 // Processing a Dex `double-to-int' instruction.
2223 locations->SetInAt(0, Location::RequiresFpuRegister());
2224 locations->SetOut(Location::RequiresRegister());
2225 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00002226 break;
2227
2228 default:
2229 LOG(FATAL) << "Unexpected type conversion from " << input_type
2230 << " to " << result_type;
2231 }
2232 break;
2233
Roland Levillaindff1f282014-11-05 14:15:05 +00002234 case Primitive::kPrimLong:
2235 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002236 case Primitive::kPrimBoolean:
2237 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002238 case Primitive::kPrimByte:
2239 case Primitive::kPrimShort:
2240 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002241 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002242 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002243 locations->SetInAt(0, Location::RegisterLocation(EAX));
2244 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
2245 break;
2246
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002247 case Primitive::kPrimFloat:
Vladimir Marko949c91f2015-01-27 10:48:44 +00002248 case Primitive::kPrimDouble: {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002249 // Processing a Dex `float-to-long' or 'double-to-long' instruction.
Vladimir Marko949c91f2015-01-27 10:48:44 +00002250 InvokeRuntimeCallingConvention calling_convention;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002251 XmmRegister parameter = calling_convention.GetFpuRegisterAt(0);
2252 locations->SetInAt(0, Location::FpuRegisterLocation(parameter));
2253
Vladimir Marko949c91f2015-01-27 10:48:44 +00002254 // The runtime helper puts the result in EAX, EDX.
2255 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Vladimir Marko949c91f2015-01-27 10:48:44 +00002256 }
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002257 break;
Roland Levillaindff1f282014-11-05 14:15:05 +00002258
2259 default:
2260 LOG(FATAL) << "Unexpected type conversion from " << input_type
2261 << " to " << result_type;
2262 }
2263 break;
2264
Roland Levillain981e4542014-11-14 11:47:14 +00002265 case Primitive::kPrimChar:
2266 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002267 case Primitive::kPrimLong:
2268 // Type conversion from long to char is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002269 case Primitive::kPrimBoolean:
2270 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002271 case Primitive::kPrimByte:
2272 case Primitive::kPrimShort:
2273 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002274 // Processing a Dex `int-to-char' instruction.
2275 locations->SetInAt(0, Location::Any());
2276 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2277 break;
2278
2279 default:
2280 LOG(FATAL) << "Unexpected type conversion from " << input_type
2281 << " to " << result_type;
2282 }
2283 break;
2284
Roland Levillaindff1f282014-11-05 14:15:05 +00002285 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002286 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002287 case Primitive::kPrimBoolean:
2288 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002289 case Primitive::kPrimByte:
2290 case Primitive::kPrimShort:
2291 case Primitive::kPrimInt:
2292 case Primitive::kPrimChar:
2293 // Processing a Dex `int-to-float' instruction.
2294 locations->SetInAt(0, Location::RequiresRegister());
2295 locations->SetOut(Location::RequiresFpuRegister());
2296 break;
2297
2298 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002299 // Processing a Dex `long-to-float' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002300 locations->SetInAt(0, Location::Any());
2301 locations->SetOut(Location::Any());
Roland Levillain6d0e4832014-11-27 18:31:21 +00002302 break;
2303
Roland Levillaincff13742014-11-17 14:32:17 +00002304 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002305 // Processing a Dex `double-to-float' instruction.
2306 locations->SetInAt(0, Location::RequiresFpuRegister());
2307 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002308 break;
2309
2310 default:
2311 LOG(FATAL) << "Unexpected type conversion from " << input_type
2312 << " to " << result_type;
2313 };
2314 break;
2315
Roland Levillaindff1f282014-11-05 14:15:05 +00002316 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002317 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002318 case Primitive::kPrimBoolean:
2319 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002320 case Primitive::kPrimByte:
2321 case Primitive::kPrimShort:
2322 case Primitive::kPrimInt:
2323 case Primitive::kPrimChar:
2324 // Processing a Dex `int-to-double' instruction.
2325 locations->SetInAt(0, Location::RequiresRegister());
2326 locations->SetOut(Location::RequiresFpuRegister());
2327 break;
2328
2329 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002330 // Processing a Dex `long-to-double' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002331 locations->SetInAt(0, Location::Any());
2332 locations->SetOut(Location::Any());
Roland Levillain647b9ed2014-11-27 12:06:00 +00002333 break;
2334
Roland Levillaincff13742014-11-17 14:32:17 +00002335 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002336 // Processing a Dex `float-to-double' instruction.
2337 locations->SetInAt(0, Location::RequiresFpuRegister());
2338 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002339 break;
2340
2341 default:
2342 LOG(FATAL) << "Unexpected type conversion from " << input_type
2343 << " to " << result_type;
2344 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002345 break;
2346
2347 default:
2348 LOG(FATAL) << "Unexpected type conversion from " << input_type
2349 << " to " << result_type;
2350 }
2351}
2352
2353void InstructionCodeGeneratorX86::VisitTypeConversion(HTypeConversion* conversion) {
2354 LocationSummary* locations = conversion->GetLocations();
2355 Location out = locations->Out();
2356 Location in = locations->InAt(0);
2357 Primitive::Type result_type = conversion->GetResultType();
2358 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002359 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00002360 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002361 case Primitive::kPrimByte:
2362 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002363 case Primitive::kPrimLong:
2364 // Type conversion from long to byte is a result of code transformations.
2365 if (in.IsRegisterPair()) {
2366 __ movsxb(out.AsRegister<Register>(), in.AsRegisterPairLow<ByteRegister>());
2367 } else {
2368 DCHECK(in.GetConstant()->IsLongConstant());
2369 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2370 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
2371 }
2372 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002373 case Primitive::kPrimBoolean:
2374 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002375 case Primitive::kPrimShort:
2376 case Primitive::kPrimInt:
2377 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002378 // Processing a Dex `int-to-byte' instruction.
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002379 if (in.IsRegister()) {
2380 __ movsxb(out.AsRegister<Register>(), in.AsRegister<ByteRegister>());
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002381 } else {
2382 DCHECK(in.GetConstant()->IsIntConstant());
2383 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
2384 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
2385 }
Roland Levillain51d3fc42014-11-13 14:11:42 +00002386 break;
2387
2388 default:
2389 LOG(FATAL) << "Unexpected type conversion from " << input_type
2390 << " to " << result_type;
2391 }
2392 break;
2393
Roland Levillain01a8d712014-11-14 16:27:39 +00002394 case Primitive::kPrimShort:
2395 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002396 case Primitive::kPrimLong:
2397 // Type conversion from long to short is a result of code transformations.
2398 if (in.IsRegisterPair()) {
2399 __ movsxw(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
2400 } else if (in.IsDoubleStackSlot()) {
2401 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
2402 } else {
2403 DCHECK(in.GetConstant()->IsLongConstant());
2404 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2405 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
2406 }
2407 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002408 case Primitive::kPrimBoolean:
2409 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002410 case Primitive::kPrimByte:
2411 case Primitive::kPrimInt:
2412 case Primitive::kPrimChar:
2413 // Processing a Dex `int-to-short' instruction.
2414 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002415 __ movsxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain01a8d712014-11-14 16:27:39 +00002416 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002417 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain01a8d712014-11-14 16:27:39 +00002418 } else {
2419 DCHECK(in.GetConstant()->IsIntConstant());
2420 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002421 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
Roland Levillain01a8d712014-11-14 16:27:39 +00002422 }
2423 break;
2424
2425 default:
2426 LOG(FATAL) << "Unexpected type conversion from " << input_type
2427 << " to " << result_type;
2428 }
2429 break;
2430
Roland Levillain946e1432014-11-11 17:35:19 +00002431 case Primitive::kPrimInt:
2432 switch (input_type) {
2433 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002434 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002435 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002436 __ movl(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00002437 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002438 __ movl(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain946e1432014-11-11 17:35:19 +00002439 } else {
2440 DCHECK(in.IsConstant());
2441 DCHECK(in.GetConstant()->IsLongConstant());
2442 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002443 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00002444 }
2445 break;
2446
Roland Levillain3f8f9362014-12-02 17:45:01 +00002447 case Primitive::kPrimFloat: {
2448 // Processing a Dex `float-to-int' instruction.
2449 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2450 Register output = out.AsRegister<Register>();
2451 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002452 NearLabel done, nan;
Roland Levillain3f8f9362014-12-02 17:45:01 +00002453
2454 __ movl(output, Immediate(kPrimIntMax));
2455 // temp = int-to-float(output)
2456 __ cvtsi2ss(temp, output);
2457 // if input >= temp goto done
2458 __ comiss(input, temp);
2459 __ j(kAboveEqual, &done);
2460 // if input == NaN goto nan
2461 __ j(kUnordered, &nan);
2462 // output = float-to-int-truncate(input)
2463 __ cvttss2si(output, input);
2464 __ jmp(&done);
2465 __ Bind(&nan);
2466 // output = 0
2467 __ xorl(output, output);
2468 __ Bind(&done);
2469 break;
2470 }
2471
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002472 case Primitive::kPrimDouble: {
2473 // Processing a Dex `double-to-int' instruction.
2474 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2475 Register output = out.AsRegister<Register>();
2476 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002477 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002478
2479 __ movl(output, Immediate(kPrimIntMax));
2480 // temp = int-to-double(output)
2481 __ cvtsi2sd(temp, output);
2482 // if input >= temp goto done
2483 __ comisd(input, temp);
2484 __ j(kAboveEqual, &done);
2485 // if input == NaN goto nan
2486 __ j(kUnordered, &nan);
2487 // output = double-to-int-truncate(input)
2488 __ cvttsd2si(output, input);
2489 __ jmp(&done);
2490 __ Bind(&nan);
2491 // output = 0
2492 __ xorl(output, output);
2493 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00002494 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002495 }
Roland Levillain946e1432014-11-11 17:35:19 +00002496
2497 default:
2498 LOG(FATAL) << "Unexpected type conversion from " << input_type
2499 << " to " << result_type;
2500 }
2501 break;
2502
Roland Levillaindff1f282014-11-05 14:15:05 +00002503 case Primitive::kPrimLong:
2504 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002505 case Primitive::kPrimBoolean:
2506 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002507 case Primitive::kPrimByte:
2508 case Primitive::kPrimShort:
2509 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002510 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002511 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002512 DCHECK_EQ(out.AsRegisterPairLow<Register>(), EAX);
2513 DCHECK_EQ(out.AsRegisterPairHigh<Register>(), EDX);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002514 DCHECK_EQ(in.AsRegister<Register>(), EAX);
Roland Levillaindff1f282014-11-05 14:15:05 +00002515 __ cdq();
2516 break;
2517
2518 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00002519 // Processing a Dex `float-to-long' instruction.
Serban Constantinescuba45db02016-07-12 22:53:02 +01002520 codegen_->InvokeRuntime(kQuickF2l, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00002521 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
Roland Levillain624279f2014-12-04 11:54:28 +00002522 break;
2523
Roland Levillaindff1f282014-11-05 14:15:05 +00002524 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002525 // Processing a Dex `double-to-long' instruction.
Serban Constantinescuba45db02016-07-12 22:53:02 +01002526 codegen_->InvokeRuntime(kQuickD2l, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00002527 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
Roland Levillaindff1f282014-11-05 14:15:05 +00002528 break;
2529
2530 default:
2531 LOG(FATAL) << "Unexpected type conversion from " << input_type
2532 << " to " << result_type;
2533 }
2534 break;
2535
Roland Levillain981e4542014-11-14 11:47:14 +00002536 case Primitive::kPrimChar:
2537 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002538 case Primitive::kPrimLong:
2539 // Type conversion from long to short is a result of code transformations.
2540 if (in.IsRegisterPair()) {
2541 __ movzxw(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
2542 } else if (in.IsDoubleStackSlot()) {
2543 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
2544 } else {
2545 DCHECK(in.GetConstant()->IsLongConstant());
2546 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2547 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
2548 }
2549 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002550 case Primitive::kPrimBoolean:
2551 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002552 case Primitive::kPrimByte:
2553 case Primitive::kPrimShort:
2554 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002555 // Processing a Dex `Process a Dex `int-to-char'' instruction.
2556 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002557 __ movzxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain981e4542014-11-14 11:47:14 +00002558 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002559 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain981e4542014-11-14 11:47:14 +00002560 } else {
2561 DCHECK(in.GetConstant()->IsIntConstant());
2562 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002563 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
Roland Levillain981e4542014-11-14 11:47:14 +00002564 }
2565 break;
2566
2567 default:
2568 LOG(FATAL) << "Unexpected type conversion from " << input_type
2569 << " to " << result_type;
2570 }
2571 break;
2572
Roland Levillaindff1f282014-11-05 14:15:05 +00002573 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002574 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002575 case Primitive::kPrimBoolean:
2576 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002577 case Primitive::kPrimByte:
2578 case Primitive::kPrimShort:
2579 case Primitive::kPrimInt:
2580 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002581 // Processing a Dex `int-to-float' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002582 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002583 break;
2584
Roland Levillain6d0e4832014-11-27 18:31:21 +00002585 case Primitive::kPrimLong: {
2586 // Processing a Dex `long-to-float' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002587 size_t adjustment = 0;
Roland Levillain6d0e4832014-11-27 18:31:21 +00002588
Roland Levillain232ade02015-04-20 15:14:36 +01002589 // Create stack space for the call to
2590 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstps below.
2591 // TODO: enhance register allocator to ask for stack temporaries.
2592 if (!in.IsDoubleStackSlot() || !out.IsStackSlot()) {
2593 adjustment = Primitive::ComponentSize(Primitive::kPrimLong);
2594 __ subl(ESP, Immediate(adjustment));
2595 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002596
Roland Levillain232ade02015-04-20 15:14:36 +01002597 // Load the value to the FP stack, using temporaries if needed.
2598 PushOntoFPStack(in, 0, adjustment, false, true);
2599
2600 if (out.IsStackSlot()) {
2601 __ fstps(Address(ESP, out.GetStackIndex() + adjustment));
2602 } else {
2603 __ fstps(Address(ESP, 0));
2604 Location stack_temp = Location::StackSlot(0);
2605 codegen_->Move32(out, stack_temp);
2606 }
2607
2608 // Remove the temporary stack space we allocated.
2609 if (adjustment != 0) {
2610 __ addl(ESP, Immediate(adjustment));
2611 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002612 break;
2613 }
2614
Roland Levillaincff13742014-11-17 14:32:17 +00002615 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002616 // Processing a Dex `double-to-float' instruction.
2617 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002618 break;
2619
2620 default:
2621 LOG(FATAL) << "Unexpected type conversion from " << input_type
2622 << " to " << result_type;
2623 };
2624 break;
2625
Roland Levillaindff1f282014-11-05 14:15:05 +00002626 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002627 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002628 case Primitive::kPrimBoolean:
2629 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002630 case Primitive::kPrimByte:
2631 case Primitive::kPrimShort:
2632 case Primitive::kPrimInt:
2633 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002634 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002635 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002636 break;
2637
Roland Levillain647b9ed2014-11-27 12:06:00 +00002638 case Primitive::kPrimLong: {
2639 // Processing a Dex `long-to-double' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002640 size_t adjustment = 0;
Roland Levillain647b9ed2014-11-27 12:06:00 +00002641
Roland Levillain232ade02015-04-20 15:14:36 +01002642 // Create stack space for the call to
2643 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstpl below.
2644 // TODO: enhance register allocator to ask for stack temporaries.
2645 if (!in.IsDoubleStackSlot() || !out.IsDoubleStackSlot()) {
2646 adjustment = Primitive::ComponentSize(Primitive::kPrimLong);
2647 __ subl(ESP, Immediate(adjustment));
2648 }
2649
2650 // Load the value to the FP stack, using temporaries if needed.
2651 PushOntoFPStack(in, 0, adjustment, false, true);
2652
2653 if (out.IsDoubleStackSlot()) {
2654 __ fstpl(Address(ESP, out.GetStackIndex() + adjustment));
2655 } else {
2656 __ fstpl(Address(ESP, 0));
2657 Location stack_temp = Location::DoubleStackSlot(0);
2658 codegen_->Move64(out, stack_temp);
2659 }
2660
2661 // Remove the temporary stack space we allocated.
2662 if (adjustment != 0) {
2663 __ addl(ESP, Immediate(adjustment));
2664 }
Roland Levillain647b9ed2014-11-27 12:06:00 +00002665 break;
2666 }
2667
Roland Levillaincff13742014-11-17 14:32:17 +00002668 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002669 // Processing a Dex `float-to-double' instruction.
2670 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002671 break;
2672
2673 default:
2674 LOG(FATAL) << "Unexpected type conversion from " << input_type
2675 << " to " << result_type;
2676 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002677 break;
2678
2679 default:
2680 LOG(FATAL) << "Unexpected type conversion from " << input_type
2681 << " to " << result_type;
2682 }
2683}
2684
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002685void LocationsBuilderX86::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002686 LocationSummary* locations =
2687 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002688 switch (add->GetResultType()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002689 case Primitive::kPrimInt: {
2690 locations->SetInAt(0, Location::RequiresRegister());
2691 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
2692 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2693 break;
2694 }
2695
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002696 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002697 locations->SetInAt(0, Location::RequiresRegister());
2698 locations->SetInAt(1, Location::Any());
2699 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002700 break;
2701 }
2702
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002703 case Primitive::kPrimFloat:
2704 case Primitive::kPrimDouble: {
2705 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002706 if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
2707 DCHECK(add->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00002708 } else if (add->InputAt(1)->IsConstant()) {
2709 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002710 } else {
2711 locations->SetInAt(1, Location::Any());
2712 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002713 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002714 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002715 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002716
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002717 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002718 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
2719 break;
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002720 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002721}
2722
2723void InstructionCodeGeneratorX86::VisitAdd(HAdd* add) {
2724 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002725 Location first = locations->InAt(0);
2726 Location second = locations->InAt(1);
Mark Mendell09b84632015-02-13 17:48:38 -05002727 Location out = locations->Out();
2728
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002729 switch (add->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002730 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002731 if (second.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002732 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2733 __ addl(out.AsRegister<Register>(), second.AsRegister<Register>());
Mark Mendell33bf2452015-05-27 10:08:24 -04002734 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
2735 __ addl(out.AsRegister<Register>(), first.AsRegister<Register>());
Mark Mendell09b84632015-02-13 17:48:38 -05002736 } else {
2737 __ leal(out.AsRegister<Register>(), Address(
2738 first.AsRegister<Register>(), second.AsRegister<Register>(), TIMES_1, 0));
2739 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002740 } else if (second.IsConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002741 int32_t value = second.GetConstant()->AsIntConstant()->GetValue();
2742 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2743 __ addl(out.AsRegister<Register>(), Immediate(value));
2744 } else {
2745 __ leal(out.AsRegister<Register>(), Address(first.AsRegister<Register>(), value));
2746 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002747 } else {
Mark Mendell09b84632015-02-13 17:48:38 -05002748 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002749 __ addl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002750 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002751 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002752 }
2753
2754 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002755 if (second.IsRegisterPair()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002756 __ addl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
2757 __ adcl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002758 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002759 __ addl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
2760 __ adcl(first.AsRegisterPairHigh<Register>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002761 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002762 } else {
2763 DCHECK(second.IsConstant()) << second;
2764 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2765 __ addl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
2766 __ adcl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002767 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002768 break;
2769 }
2770
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002771 case Primitive::kPrimFloat: {
2772 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002773 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04002774 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
2775 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00002776 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04002777 __ addss(first.AsFpuRegister<XmmRegister>(),
2778 codegen_->LiteralFloatAddress(
2779 const_area->GetConstant()->AsFloatConstant()->GetValue(),
2780 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2781 } else {
2782 DCHECK(second.IsStackSlot());
2783 __ addss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002784 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002785 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002786 }
2787
2788 case Primitive::kPrimDouble: {
2789 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002790 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04002791 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
2792 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00002793 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04002794 __ addsd(first.AsFpuRegister<XmmRegister>(),
2795 codegen_->LiteralDoubleAddress(
2796 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
2797 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2798 } else {
2799 DCHECK(second.IsDoubleStackSlot());
2800 __ addsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002801 }
2802 break;
2803 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002804
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002805 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002806 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002807 }
2808}
2809
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002810void LocationsBuilderX86::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002811 LocationSummary* locations =
2812 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002813 switch (sub->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002814 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002815 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002816 locations->SetInAt(0, Location::RequiresRegister());
2817 locations->SetInAt(1, Location::Any());
2818 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002819 break;
2820 }
Calin Juravle11351682014-10-23 15:38:15 +01002821 case Primitive::kPrimFloat:
2822 case Primitive::kPrimDouble: {
2823 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002824 if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
2825 DCHECK(sub->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00002826 } else if (sub->InputAt(1)->IsConstant()) {
2827 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002828 } else {
2829 locations->SetInAt(1, Location::Any());
2830 }
Calin Juravle11351682014-10-23 15:38:15 +01002831 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002832 break;
Calin Juravle11351682014-10-23 15:38:15 +01002833 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002834
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002835 default:
Calin Juravle11351682014-10-23 15:38:15 +01002836 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002837 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002838}
2839
2840void InstructionCodeGeneratorX86::VisitSub(HSub* sub) {
2841 LocationSummary* locations = sub->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002842 Location first = locations->InAt(0);
2843 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01002844 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002845 switch (sub->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002846 case Primitive::kPrimInt: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002847 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002848 __ subl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002849 } else if (second.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002850 __ subl(first.AsRegister<Register>(),
2851 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002852 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002853 __ subl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002854 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002855 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002856 }
2857
2858 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002859 if (second.IsRegisterPair()) {
Calin Juravle11351682014-10-23 15:38:15 +01002860 __ subl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
2861 __ sbbl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002862 } else if (second.IsDoubleStackSlot()) {
Calin Juravle11351682014-10-23 15:38:15 +01002863 __ subl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002864 __ sbbl(first.AsRegisterPairHigh<Register>(),
2865 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002866 } else {
2867 DCHECK(second.IsConstant()) << second;
2868 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2869 __ subl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
2870 __ sbbl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002871 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002872 break;
2873 }
2874
Calin Juravle11351682014-10-23 15:38:15 +01002875 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04002876 if (second.IsFpuRegister()) {
2877 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2878 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
2879 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00002880 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04002881 __ subss(first.AsFpuRegister<XmmRegister>(),
2882 codegen_->LiteralFloatAddress(
2883 const_area->GetConstant()->AsFloatConstant()->GetValue(),
2884 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2885 } else {
2886 DCHECK(second.IsStackSlot());
2887 __ subss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
2888 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002889 break;
Calin Juravle11351682014-10-23 15:38:15 +01002890 }
2891
2892 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04002893 if (second.IsFpuRegister()) {
2894 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2895 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
2896 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00002897 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04002898 __ subsd(first.AsFpuRegister<XmmRegister>(),
2899 codegen_->LiteralDoubleAddress(
2900 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
2901 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2902 } else {
2903 DCHECK(second.IsDoubleStackSlot());
2904 __ subsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
2905 }
Calin Juravle11351682014-10-23 15:38:15 +01002906 break;
2907 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002908
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002909 default:
Calin Juravle11351682014-10-23 15:38:15 +01002910 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002911 }
2912}
2913
Calin Juravle34bacdf2014-10-07 20:23:36 +01002914void LocationsBuilderX86::VisitMul(HMul* mul) {
2915 LocationSummary* locations =
2916 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
2917 switch (mul->GetResultType()) {
2918 case Primitive::kPrimInt:
2919 locations->SetInAt(0, Location::RequiresRegister());
2920 locations->SetInAt(1, Location::Any());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002921 if (mul->InputAt(1)->IsIntConstant()) {
2922 // Can use 3 operand multiply.
2923 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2924 } else {
2925 locations->SetOut(Location::SameAsFirstInput());
2926 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002927 break;
2928 case Primitive::kPrimLong: {
2929 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002930 locations->SetInAt(1, Location::Any());
2931 locations->SetOut(Location::SameAsFirstInput());
2932 // Needed for imul on 32bits with 64bits output.
2933 locations->AddTemp(Location::RegisterLocation(EAX));
2934 locations->AddTemp(Location::RegisterLocation(EDX));
2935 break;
2936 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002937 case Primitive::kPrimFloat:
2938 case Primitive::kPrimDouble: {
2939 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002940 if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
2941 DCHECK(mul->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00002942 } else if (mul->InputAt(1)->IsConstant()) {
2943 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002944 } else {
2945 locations->SetInAt(1, Location::Any());
2946 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002947 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002948 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002949 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002950
2951 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002952 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002953 }
2954}
2955
2956void InstructionCodeGeneratorX86::VisitMul(HMul* mul) {
2957 LocationSummary* locations = mul->GetLocations();
2958 Location first = locations->InAt(0);
2959 Location second = locations->InAt(1);
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002960 Location out = locations->Out();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002961
2962 switch (mul->GetResultType()) {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002963 case Primitive::kPrimInt:
2964 // The constant may have ended up in a register, so test explicitly to avoid
2965 // problems where the output may not be the same as the first operand.
2966 if (mul->InputAt(1)->IsIntConstant()) {
2967 Immediate imm(mul->InputAt(1)->AsIntConstant()->GetValue());
2968 __ imull(out.AsRegister<Register>(), first.AsRegister<Register>(), imm);
2969 } else if (second.IsRegister()) {
2970 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002971 __ imull(first.AsRegister<Register>(), second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002972 } else {
2973 DCHECK(second.IsStackSlot());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002974 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002975 __ imull(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01002976 }
2977 break;
Calin Juravle34bacdf2014-10-07 20:23:36 +01002978
2979 case Primitive::kPrimLong: {
Calin Juravle34bacdf2014-10-07 20:23:36 +01002980 Register in1_hi = first.AsRegisterPairHigh<Register>();
2981 Register in1_lo = first.AsRegisterPairLow<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002982 Register eax = locations->GetTemp(0).AsRegister<Register>();
2983 Register edx = locations->GetTemp(1).AsRegister<Register>();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002984
2985 DCHECK_EQ(EAX, eax);
2986 DCHECK_EQ(EDX, edx);
2987
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002988 // input: in1 - 64 bits, in2 - 64 bits.
Calin Juravle34bacdf2014-10-07 20:23:36 +01002989 // output: in1
2990 // formula: in1.hi : in1.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
2991 // parts: in1.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
2992 // parts: in1.lo = (in1.lo * in2.lo)[31:0]
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002993 if (second.IsConstant()) {
2994 DCHECK(second.GetConstant()->IsLongConstant());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002995
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002996 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2997 int32_t low_value = Low32Bits(value);
2998 int32_t high_value = High32Bits(value);
2999 Immediate low(low_value);
3000 Immediate high(high_value);
3001
3002 __ movl(eax, high);
3003 // eax <- in1.lo * in2.hi
3004 __ imull(eax, in1_lo);
3005 // in1.hi <- in1.hi * in2.lo
3006 __ imull(in1_hi, low);
3007 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3008 __ addl(in1_hi, eax);
3009 // move in2_lo to eax to prepare for double precision
3010 __ movl(eax, low);
3011 // edx:eax <- in1.lo * in2.lo
3012 __ mull(in1_lo);
3013 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3014 __ addl(in1_hi, edx);
3015 // in1.lo <- (in1.lo * in2.lo)[31:0];
3016 __ movl(in1_lo, eax);
3017 } else if (second.IsRegisterPair()) {
3018 Register in2_hi = second.AsRegisterPairHigh<Register>();
3019 Register in2_lo = second.AsRegisterPairLow<Register>();
3020
3021 __ movl(eax, in2_hi);
3022 // eax <- in1.lo * in2.hi
3023 __ imull(eax, in1_lo);
3024 // in1.hi <- in1.hi * in2.lo
3025 __ imull(in1_hi, in2_lo);
3026 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3027 __ addl(in1_hi, eax);
3028 // move in1_lo to eax to prepare for double precision
3029 __ movl(eax, in1_lo);
3030 // edx:eax <- in1.lo * in2.lo
3031 __ mull(in2_lo);
3032 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3033 __ addl(in1_hi, edx);
3034 // in1.lo <- (in1.lo * in2.lo)[31:0];
3035 __ movl(in1_lo, eax);
3036 } else {
3037 DCHECK(second.IsDoubleStackSlot()) << second;
3038 Address in2_hi(ESP, second.GetHighStackIndex(kX86WordSize));
3039 Address in2_lo(ESP, second.GetStackIndex());
3040
3041 __ movl(eax, in2_hi);
3042 // eax <- in1.lo * in2.hi
3043 __ imull(eax, in1_lo);
3044 // in1.hi <- in1.hi * in2.lo
3045 __ imull(in1_hi, in2_lo);
3046 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3047 __ addl(in1_hi, eax);
3048 // move in1_lo to eax to prepare for double precision
3049 __ movl(eax, in1_lo);
3050 // edx:eax <- in1.lo * in2.lo
3051 __ mull(in2_lo);
3052 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3053 __ addl(in1_hi, edx);
3054 // in1.lo <- (in1.lo * in2.lo)[31:0];
3055 __ movl(in1_lo, eax);
3056 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003057
3058 break;
3059 }
3060
Calin Juravleb5bfa962014-10-21 18:02:24 +01003061 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003062 DCHECK(first.Equals(locations->Out()));
3063 if (second.IsFpuRegister()) {
3064 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3065 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3066 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003067 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003068 __ mulss(first.AsFpuRegister<XmmRegister>(),
3069 codegen_->LiteralFloatAddress(
3070 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3071 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3072 } else {
3073 DCHECK(second.IsStackSlot());
3074 __ mulss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3075 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003076 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003077 }
3078
3079 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003080 DCHECK(first.Equals(locations->Out()));
3081 if (second.IsFpuRegister()) {
3082 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3083 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3084 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003085 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003086 __ mulsd(first.AsFpuRegister<XmmRegister>(),
3087 codegen_->LiteralDoubleAddress(
3088 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3089 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3090 } else {
3091 DCHECK(second.IsDoubleStackSlot());
3092 __ mulsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3093 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003094 break;
3095 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003096
3097 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003098 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003099 }
3100}
3101
Roland Levillain232ade02015-04-20 15:14:36 +01003102void InstructionCodeGeneratorX86::PushOntoFPStack(Location source,
3103 uint32_t temp_offset,
3104 uint32_t stack_adjustment,
3105 bool is_fp,
3106 bool is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003107 if (source.IsStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01003108 DCHECK(!is_wide);
3109 if (is_fp) {
3110 __ flds(Address(ESP, source.GetStackIndex() + stack_adjustment));
3111 } else {
3112 __ filds(Address(ESP, source.GetStackIndex() + stack_adjustment));
3113 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003114 } else if (source.IsDoubleStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01003115 DCHECK(is_wide);
3116 if (is_fp) {
3117 __ fldl(Address(ESP, source.GetStackIndex() + stack_adjustment));
3118 } else {
3119 __ fildl(Address(ESP, source.GetStackIndex() + stack_adjustment));
3120 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003121 } else {
3122 // Write the value to the temporary location on the stack and load to FP stack.
Roland Levillain232ade02015-04-20 15:14:36 +01003123 if (!is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003124 Location stack_temp = Location::StackSlot(temp_offset);
3125 codegen_->Move32(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01003126 if (is_fp) {
3127 __ flds(Address(ESP, temp_offset));
3128 } else {
3129 __ filds(Address(ESP, temp_offset));
3130 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003131 } else {
3132 Location stack_temp = Location::DoubleStackSlot(temp_offset);
3133 codegen_->Move64(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01003134 if (is_fp) {
3135 __ fldl(Address(ESP, temp_offset));
3136 } else {
3137 __ fildl(Address(ESP, temp_offset));
3138 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003139 }
3140 }
3141}
3142
3143void InstructionCodeGeneratorX86::GenerateRemFP(HRem *rem) {
3144 Primitive::Type type = rem->GetResultType();
3145 bool is_float = type == Primitive::kPrimFloat;
3146 size_t elem_size = Primitive::ComponentSize(type);
3147 LocationSummary* locations = rem->GetLocations();
3148 Location first = locations->InAt(0);
3149 Location second = locations->InAt(1);
3150 Location out = locations->Out();
3151
3152 // Create stack space for 2 elements.
3153 // TODO: enhance register allocator to ask for stack temporaries.
3154 __ subl(ESP, Immediate(2 * elem_size));
3155
3156 // Load the values to the FP stack in reverse order, using temporaries if needed.
Roland Levillain232ade02015-04-20 15:14:36 +01003157 const bool is_wide = !is_float;
3158 PushOntoFPStack(second, elem_size, 2 * elem_size, /* is_fp */ true, is_wide);
3159 PushOntoFPStack(first, 0, 2 * elem_size, /* is_fp */ true, is_wide);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003160
3161 // Loop doing FPREM until we stabilize.
Mark Mendell0c9497d2015-08-21 09:30:05 -04003162 NearLabel retry;
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003163 __ Bind(&retry);
3164 __ fprem();
3165
3166 // Move FP status to AX.
3167 __ fstsw();
3168
3169 // And see if the argument reduction is complete. This is signaled by the
3170 // C2 FPU flag bit set to 0.
3171 __ andl(EAX, Immediate(kC2ConditionMask));
3172 __ j(kNotEqual, &retry);
3173
3174 // We have settled on the final value. Retrieve it into an XMM register.
3175 // Store FP top of stack to real stack.
3176 if (is_float) {
3177 __ fsts(Address(ESP, 0));
3178 } else {
3179 __ fstl(Address(ESP, 0));
3180 }
3181
3182 // Pop the 2 items from the FP stack.
3183 __ fucompp();
3184
3185 // Load the value from the stack into an XMM register.
3186 DCHECK(out.IsFpuRegister()) << out;
3187 if (is_float) {
3188 __ movss(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
3189 } else {
3190 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
3191 }
3192
3193 // And remove the temporary stack space we allocated.
3194 __ addl(ESP, Immediate(2 * elem_size));
3195}
3196
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003197
3198void InstructionCodeGeneratorX86::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3199 DCHECK(instruction->IsDiv() || instruction->IsRem());
3200
3201 LocationSummary* locations = instruction->GetLocations();
3202 DCHECK(locations->InAt(1).IsConstant());
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003203 DCHECK(locations->InAt(1).GetConstant()->IsIntConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003204
3205 Register out_register = locations->Out().AsRegister<Register>();
3206 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003207 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003208
3209 DCHECK(imm == 1 || imm == -1);
3210
3211 if (instruction->IsRem()) {
3212 __ xorl(out_register, out_register);
3213 } else {
3214 __ movl(out_register, input_register);
3215 if (imm == -1) {
3216 __ negl(out_register);
3217 }
3218 }
3219}
3220
3221
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003222void InstructionCodeGeneratorX86::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003223 LocationSummary* locations = instruction->GetLocations();
3224
3225 Register out_register = locations->Out().AsRegister<Register>();
3226 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003227 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003228 DCHECK(IsPowerOfTwo(AbsOrMin(imm)));
3229 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003230
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003231 Register num = locations->GetTemp(0).AsRegister<Register>();
3232
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003233 __ leal(num, Address(input_register, abs_imm - 1));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003234 __ testl(input_register, input_register);
3235 __ cmovl(kGreaterEqual, num, input_register);
3236 int shift = CTZ(imm);
3237 __ sarl(num, Immediate(shift));
3238
3239 if (imm < 0) {
3240 __ negl(num);
3241 }
3242
3243 __ movl(out_register, num);
3244}
3245
3246void InstructionCodeGeneratorX86::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3247 DCHECK(instruction->IsDiv() || instruction->IsRem());
3248
3249 LocationSummary* locations = instruction->GetLocations();
3250 int imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
3251
3252 Register eax = locations->InAt(0).AsRegister<Register>();
3253 Register out = locations->Out().AsRegister<Register>();
3254 Register num;
3255 Register edx;
3256
3257 if (instruction->IsDiv()) {
3258 edx = locations->GetTemp(0).AsRegister<Register>();
3259 num = locations->GetTemp(1).AsRegister<Register>();
3260 } else {
3261 edx = locations->Out().AsRegister<Register>();
3262 num = locations->GetTemp(0).AsRegister<Register>();
3263 }
3264
3265 DCHECK_EQ(EAX, eax);
3266 DCHECK_EQ(EDX, edx);
3267 if (instruction->IsDiv()) {
3268 DCHECK_EQ(EAX, out);
3269 } else {
3270 DCHECK_EQ(EDX, out);
3271 }
3272
3273 int64_t magic;
3274 int shift;
3275 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
3276
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003277 // Save the numerator.
3278 __ movl(num, eax);
3279
3280 // EAX = magic
3281 __ movl(eax, Immediate(magic));
3282
3283 // EDX:EAX = magic * numerator
3284 __ imull(num);
3285
3286 if (imm > 0 && magic < 0) {
3287 // EDX += num
3288 __ addl(edx, num);
3289 } else if (imm < 0 && magic > 0) {
3290 __ subl(edx, num);
3291 }
3292
3293 // Shift if needed.
3294 if (shift != 0) {
3295 __ sarl(edx, Immediate(shift));
3296 }
3297
3298 // EDX += 1 if EDX < 0
3299 __ movl(eax, edx);
3300 __ shrl(edx, Immediate(31));
3301 __ addl(edx, eax);
3302
3303 if (instruction->IsRem()) {
3304 __ movl(eax, num);
3305 __ imull(edx, Immediate(imm));
3306 __ subl(eax, edx);
3307 __ movl(edx, eax);
3308 } else {
3309 __ movl(eax, edx);
3310 }
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003311}
3312
Calin Juravlebacfec32014-11-14 15:54:36 +00003313void InstructionCodeGeneratorX86::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3314 DCHECK(instruction->IsDiv() || instruction->IsRem());
3315
3316 LocationSummary* locations = instruction->GetLocations();
3317 Location out = locations->Out();
3318 Location first = locations->InAt(0);
3319 Location second = locations->InAt(1);
3320 bool is_div = instruction->IsDiv();
3321
3322 switch (instruction->GetResultType()) {
3323 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003324 DCHECK_EQ(EAX, first.AsRegister<Register>());
3325 DCHECK_EQ(is_div ? EAX : EDX, out.AsRegister<Register>());
Calin Juravlebacfec32014-11-14 15:54:36 +00003326
Vladimir Marko13c86fd2015-11-11 12:37:46 +00003327 if (second.IsConstant()) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003328 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003329
3330 if (imm == 0) {
3331 // Do not generate anything for 0. DivZeroCheck would forbid any generated code.
3332 } else if (imm == 1 || imm == -1) {
3333 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003334 } else if (is_div && IsPowerOfTwo(AbsOrMin(imm))) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003335 DivByPowerOfTwo(instruction->AsDiv());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003336 } else {
3337 DCHECK(imm <= -2 || imm >= 2);
3338 GenerateDivRemWithAnyConstant(instruction);
3339 }
3340 } else {
David Srbecky9cd6d372016-02-09 15:24:47 +00003341 SlowPathCode* slow_path = new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86(
3342 instruction, out.AsRegister<Register>(), is_div);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003343 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00003344
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003345 Register second_reg = second.AsRegister<Register>();
3346 // 0x80000000/-1 triggers an arithmetic exception!
3347 // Dividing by -1 is actually negation and -0x800000000 = 0x80000000 so
3348 // it's safe to just use negl instead of more complex comparisons.
Calin Juravlebacfec32014-11-14 15:54:36 +00003349
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003350 __ cmpl(second_reg, Immediate(-1));
3351 __ j(kEqual, slow_path->GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +00003352
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003353 // edx:eax <- sign-extended of eax
3354 __ cdq();
3355 // eax = quotient, edx = remainder
3356 __ idivl(second_reg);
3357 __ Bind(slow_path->GetExitLabel());
3358 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003359 break;
3360 }
3361
3362 case Primitive::kPrimLong: {
3363 InvokeRuntimeCallingConvention calling_convention;
3364 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
3365 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
3366 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
3367 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
3368 DCHECK_EQ(EAX, out.AsRegisterPairLow<Register>());
3369 DCHECK_EQ(EDX, out.AsRegisterPairHigh<Register>());
3370
3371 if (is_div) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01003372 codegen_->InvokeRuntime(kQuickLdiv, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003373 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003374 } else {
Serban Constantinescuba45db02016-07-12 22:53:02 +01003375 codegen_->InvokeRuntime(kQuickLmod, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003376 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003377 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003378 break;
3379 }
3380
3381 default:
3382 LOG(FATAL) << "Unexpected type for GenerateDivRemIntegral " << instruction->GetResultType();
3383 }
3384}
3385
Calin Juravle7c4954d2014-10-28 16:57:40 +00003386void LocationsBuilderX86::VisitDiv(HDiv* div) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003387 LocationSummary::CallKind call_kind = (div->GetResultType() == Primitive::kPrimLong)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003388 ? LocationSummary::kCallOnMainOnly
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003389 : LocationSummary::kNoCall;
3390 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
3391
Calin Juravle7c4954d2014-10-28 16:57:40 +00003392 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00003393 case Primitive::kPrimInt: {
3394 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003395 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00003396 locations->SetOut(Location::SameAsFirstInput());
3397 // Intel uses edx:eax as the dividend.
3398 locations->AddTemp(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003399 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3400 // which enforces results to be in EAX and EDX, things are simpler if we use EAX also as
3401 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003402 if (div->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003403 locations->AddTemp(Location::RequiresRegister());
3404 }
Calin Juravled0d48522014-11-04 16:40:20 +00003405 break;
3406 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003407 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003408 InvokeRuntimeCallingConvention calling_convention;
3409 locations->SetInAt(0, Location::RegisterPairLocation(
3410 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3411 locations->SetInAt(1, Location::RegisterPairLocation(
3412 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3413 // Runtime helper puts the result in EAX, EDX.
3414 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Calin Juravle7c4954d2014-10-28 16:57:40 +00003415 break;
3416 }
3417 case Primitive::kPrimFloat:
3418 case Primitive::kPrimDouble: {
3419 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003420 if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3421 DCHECK(div->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003422 } else if (div->InputAt(1)->IsConstant()) {
3423 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003424 } else {
3425 locations->SetInAt(1, Location::Any());
3426 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003427 locations->SetOut(Location::SameAsFirstInput());
3428 break;
3429 }
3430
3431 default:
3432 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3433 }
3434}
3435
3436void InstructionCodeGeneratorX86::VisitDiv(HDiv* div) {
3437 LocationSummary* locations = div->GetLocations();
3438 Location first = locations->InAt(0);
3439 Location second = locations->InAt(1);
Calin Juravle7c4954d2014-10-28 16:57:40 +00003440
3441 switch (div->GetResultType()) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003442 case Primitive::kPrimInt:
Calin Juravle7c4954d2014-10-28 16:57:40 +00003443 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003444 GenerateDivRemIntegral(div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00003445 break;
3446 }
3447
3448 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003449 if (second.IsFpuRegister()) {
3450 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3451 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3452 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003453 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003454 __ divss(first.AsFpuRegister<XmmRegister>(),
3455 codegen_->LiteralFloatAddress(
3456 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3457 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3458 } else {
3459 DCHECK(second.IsStackSlot());
3460 __ divss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3461 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003462 break;
3463 }
3464
3465 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003466 if (second.IsFpuRegister()) {
3467 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3468 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3469 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003470 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003471 __ divsd(first.AsFpuRegister<XmmRegister>(),
3472 codegen_->LiteralDoubleAddress(
3473 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3474 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3475 } else {
3476 DCHECK(second.IsDoubleStackSlot());
3477 __ divsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3478 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003479 break;
3480 }
3481
3482 default:
3483 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3484 }
3485}
3486
Calin Juravlebacfec32014-11-14 15:54:36 +00003487void LocationsBuilderX86::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00003488 Primitive::Type type = rem->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003489
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003490 LocationSummary::CallKind call_kind = (rem->GetResultType() == Primitive::kPrimLong)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003491 ? LocationSummary::kCallOnMainOnly
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003492 : LocationSummary::kNoCall;
3493 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
Calin Juravlebacfec32014-11-14 15:54:36 +00003494
Calin Juravled2ec87d2014-12-08 14:24:46 +00003495 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003496 case Primitive::kPrimInt: {
3497 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003498 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00003499 locations->SetOut(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003500 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3501 // which enforces results to be in EAX and EDX, things are simpler if we use EDX also as
3502 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003503 if (rem->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003504 locations->AddTemp(Location::RequiresRegister());
3505 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003506 break;
3507 }
3508 case Primitive::kPrimLong: {
3509 InvokeRuntimeCallingConvention calling_convention;
3510 locations->SetInAt(0, Location::RegisterPairLocation(
3511 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3512 locations->SetInAt(1, Location::RegisterPairLocation(
3513 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3514 // Runtime helper puts the result in EAX, EDX.
3515 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
3516 break;
3517 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003518 case Primitive::kPrimDouble:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003519 case Primitive::kPrimFloat: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003520 locations->SetInAt(0, Location::Any());
3521 locations->SetInAt(1, Location::Any());
3522 locations->SetOut(Location::RequiresFpuRegister());
3523 locations->AddTemp(Location::RegisterLocation(EAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00003524 break;
3525 }
3526
3527 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003528 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003529 }
3530}
3531
3532void InstructionCodeGeneratorX86::VisitRem(HRem* rem) {
3533 Primitive::Type type = rem->GetResultType();
3534 switch (type) {
3535 case Primitive::kPrimInt:
3536 case Primitive::kPrimLong: {
3537 GenerateDivRemIntegral(rem);
3538 break;
3539 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003540 case Primitive::kPrimFloat:
Calin Juravlebacfec32014-11-14 15:54:36 +00003541 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003542 GenerateRemFP(rem);
Calin Juravlebacfec32014-11-14 15:54:36 +00003543 break;
3544 }
3545 default:
3546 LOG(FATAL) << "Unexpected rem type " << type;
3547 }
3548}
3549
Calin Juravled0d48522014-11-04 16:40:20 +00003550void LocationsBuilderX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00003551 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
3552 ? LocationSummary::kCallOnSlowPath
3553 : LocationSummary::kNoCall;
3554 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003555 switch (instruction->GetType()) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003556 case Primitive::kPrimBoolean:
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003557 case Primitive::kPrimByte:
3558 case Primitive::kPrimChar:
3559 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003560 case Primitive::kPrimInt: {
3561 locations->SetInAt(0, Location::Any());
3562 break;
3563 }
3564 case Primitive::kPrimLong: {
3565 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
3566 if (!instruction->IsConstant()) {
3567 locations->AddTemp(Location::RequiresRegister());
3568 }
3569 break;
3570 }
3571 default:
3572 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
3573 }
Calin Juravled0d48522014-11-04 16:40:20 +00003574 if (instruction->HasUses()) {
3575 locations->SetOut(Location::SameAsFirstInput());
3576 }
3577}
3578
3579void InstructionCodeGeneratorX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003580 SlowPathCode* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86(instruction);
Calin Juravled0d48522014-11-04 16:40:20 +00003581 codegen_->AddSlowPath(slow_path);
3582
3583 LocationSummary* locations = instruction->GetLocations();
3584 Location value = locations->InAt(0);
3585
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003586 switch (instruction->GetType()) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003587 case Primitive::kPrimBoolean:
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003588 case Primitive::kPrimByte:
3589 case Primitive::kPrimChar:
3590 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003591 case Primitive::kPrimInt: {
3592 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003593 __ testl(value.AsRegister<Register>(), value.AsRegister<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003594 __ j(kEqual, slow_path->GetEntryLabel());
3595 } else if (value.IsStackSlot()) {
3596 __ cmpl(Address(ESP, value.GetStackIndex()), Immediate(0));
3597 __ j(kEqual, slow_path->GetEntryLabel());
3598 } else {
3599 DCHECK(value.IsConstant()) << value;
3600 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01003601 __ jmp(slow_path->GetEntryLabel());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003602 }
3603 }
3604 break;
Calin Juravled0d48522014-11-04 16:40:20 +00003605 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003606 case Primitive::kPrimLong: {
3607 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003608 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003609 __ movl(temp, value.AsRegisterPairLow<Register>());
3610 __ orl(temp, value.AsRegisterPairHigh<Register>());
3611 __ j(kEqual, slow_path->GetEntryLabel());
3612 } else {
3613 DCHECK(value.IsConstant()) << value;
3614 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
3615 __ jmp(slow_path->GetEntryLabel());
3616 }
3617 }
3618 break;
3619 }
3620 default:
3621 LOG(FATAL) << "Unexpected type for HDivZeroCheck" << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00003622 }
Calin Juravled0d48522014-11-04 16:40:20 +00003623}
3624
Calin Juravle9aec02f2014-11-18 23:06:35 +00003625void LocationsBuilderX86::HandleShift(HBinaryOperation* op) {
3626 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3627
3628 LocationSummary* locations =
3629 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
3630
3631 switch (op->GetResultType()) {
Mark P Mendell73945692015-04-29 14:56:17 +00003632 case Primitive::kPrimInt:
Calin Juravle9aec02f2014-11-18 23:06:35 +00003633 case Primitive::kPrimLong: {
Mark P Mendell73945692015-04-29 14:56:17 +00003634 // Can't have Location::Any() and output SameAsFirstInput()
Calin Juravle9aec02f2014-11-18 23:06:35 +00003635 locations->SetInAt(0, Location::RequiresRegister());
Mark P Mendell73945692015-04-29 14:56:17 +00003636 // The shift count needs to be in CL or a constant.
3637 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, op->InputAt(1)));
Calin Juravle9aec02f2014-11-18 23:06:35 +00003638 locations->SetOut(Location::SameAsFirstInput());
3639 break;
3640 }
3641 default:
3642 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
3643 }
3644}
3645
3646void InstructionCodeGeneratorX86::HandleShift(HBinaryOperation* op) {
3647 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3648
3649 LocationSummary* locations = op->GetLocations();
3650 Location first = locations->InAt(0);
3651 Location second = locations->InAt(1);
3652 DCHECK(first.Equals(locations->Out()));
3653
3654 switch (op->GetResultType()) {
3655 case Primitive::kPrimInt: {
Mark P Mendell73945692015-04-29 14:56:17 +00003656 DCHECK(first.IsRegister());
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003657 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003658 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003659 Register second_reg = second.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003660 DCHECK_EQ(ECX, second_reg);
3661 if (op->IsShl()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003662 __ shll(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003663 } else if (op->IsShr()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003664 __ sarl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003665 } else {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003666 __ shrl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003667 }
3668 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003669 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance;
Mark P Mendell73945692015-04-29 14:56:17 +00003670 if (shift == 0) {
3671 return;
3672 }
3673 Immediate imm(shift);
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003674 if (op->IsShl()) {
3675 __ shll(first_reg, imm);
3676 } else if (op->IsShr()) {
3677 __ sarl(first_reg, imm);
3678 } else {
3679 __ shrl(first_reg, imm);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003680 }
3681 }
3682 break;
3683 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003684 case Primitive::kPrimLong: {
Mark P Mendell73945692015-04-29 14:56:17 +00003685 if (second.IsRegister()) {
3686 Register second_reg = second.AsRegister<Register>();
3687 DCHECK_EQ(ECX, second_reg);
3688 if (op->IsShl()) {
3689 GenerateShlLong(first, second_reg);
3690 } else if (op->IsShr()) {
3691 GenerateShrLong(first, second_reg);
3692 } else {
3693 GenerateUShrLong(first, second_reg);
3694 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003695 } else {
Mark P Mendell73945692015-04-29 14:56:17 +00003696 // Shift by a constant.
Roland Levillain5b5b9312016-03-22 14:57:31 +00003697 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance;
Mark P Mendell73945692015-04-29 14:56:17 +00003698 // Nothing to do if the shift is 0, as the input is already the output.
3699 if (shift != 0) {
3700 if (op->IsShl()) {
3701 GenerateShlLong(first, shift);
3702 } else if (op->IsShr()) {
3703 GenerateShrLong(first, shift);
3704 } else {
3705 GenerateUShrLong(first, shift);
3706 }
3707 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003708 }
3709 break;
3710 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003711 default:
3712 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
3713 }
3714}
3715
Mark P Mendell73945692015-04-29 14:56:17 +00003716void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, int shift) {
3717 Register low = loc.AsRegisterPairLow<Register>();
3718 Register high = loc.AsRegisterPairHigh<Register>();
Mark Mendellba56d062015-05-05 21:34:03 -04003719 if (shift == 1) {
3720 // This is just an addition.
3721 __ addl(low, low);
3722 __ adcl(high, high);
3723 } else if (shift == 32) {
Mark P Mendell73945692015-04-29 14:56:17 +00003724 // Shift by 32 is easy. High gets low, and low gets 0.
3725 codegen_->EmitParallelMoves(
3726 loc.ToLow(),
3727 loc.ToHigh(),
3728 Primitive::kPrimInt,
3729 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
3730 loc.ToLow(),
3731 Primitive::kPrimInt);
3732 } else if (shift > 32) {
3733 // Low part becomes 0. High part is low part << (shift-32).
3734 __ movl(high, low);
3735 __ shll(high, Immediate(shift - 32));
3736 __ xorl(low, low);
3737 } else {
3738 // Between 1 and 31.
3739 __ shld(high, low, Immediate(shift));
3740 __ shll(low, Immediate(shift));
3741 }
3742}
3743
Calin Juravle9aec02f2014-11-18 23:06:35 +00003744void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04003745 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00003746 __ shld(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>(), shifter);
3747 __ shll(loc.AsRegisterPairLow<Register>(), shifter);
3748 __ testl(shifter, Immediate(32));
3749 __ j(kEqual, &done);
3750 __ movl(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>());
3751 __ movl(loc.AsRegisterPairLow<Register>(), Immediate(0));
3752 __ Bind(&done);
3753}
3754
Mark P Mendell73945692015-04-29 14:56:17 +00003755void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, int shift) {
3756 Register low = loc.AsRegisterPairLow<Register>();
3757 Register high = loc.AsRegisterPairHigh<Register>();
3758 if (shift == 32) {
3759 // Need to copy the sign.
3760 DCHECK_NE(low, high);
3761 __ movl(low, high);
3762 __ sarl(high, Immediate(31));
3763 } else if (shift > 32) {
3764 DCHECK_NE(low, high);
3765 // High part becomes sign. Low part is shifted by shift - 32.
3766 __ movl(low, high);
3767 __ sarl(high, Immediate(31));
3768 __ sarl(low, Immediate(shift - 32));
3769 } else {
3770 // Between 1 and 31.
3771 __ shrd(low, high, Immediate(shift));
3772 __ sarl(high, Immediate(shift));
3773 }
3774}
3775
Calin Juravle9aec02f2014-11-18 23:06:35 +00003776void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04003777 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00003778 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
3779 __ sarl(loc.AsRegisterPairHigh<Register>(), shifter);
3780 __ testl(shifter, Immediate(32));
3781 __ j(kEqual, &done);
3782 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
3783 __ sarl(loc.AsRegisterPairHigh<Register>(), Immediate(31));
3784 __ Bind(&done);
3785}
3786
Mark P Mendell73945692015-04-29 14:56:17 +00003787void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, int shift) {
3788 Register low = loc.AsRegisterPairLow<Register>();
3789 Register high = loc.AsRegisterPairHigh<Register>();
3790 if (shift == 32) {
3791 // Shift by 32 is easy. Low gets high, and high gets 0.
3792 codegen_->EmitParallelMoves(
3793 loc.ToHigh(),
3794 loc.ToLow(),
3795 Primitive::kPrimInt,
3796 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
3797 loc.ToHigh(),
3798 Primitive::kPrimInt);
3799 } else if (shift > 32) {
3800 // Low part is high >> (shift - 32). High part becomes 0.
3801 __ movl(low, high);
3802 __ shrl(low, Immediate(shift - 32));
3803 __ xorl(high, high);
3804 } else {
3805 // Between 1 and 31.
3806 __ shrd(low, high, Immediate(shift));
3807 __ shrl(high, Immediate(shift));
3808 }
3809}
3810
Calin Juravle9aec02f2014-11-18 23:06:35 +00003811void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04003812 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00003813 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
3814 __ shrl(loc.AsRegisterPairHigh<Register>(), shifter);
3815 __ testl(shifter, Immediate(32));
3816 __ j(kEqual, &done);
3817 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
3818 __ movl(loc.AsRegisterPairHigh<Register>(), Immediate(0));
3819 __ Bind(&done);
3820}
3821
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003822void LocationsBuilderX86::VisitRor(HRor* ror) {
3823 LocationSummary* locations =
3824 new (GetGraph()->GetArena()) LocationSummary(ror, LocationSummary::kNoCall);
3825
3826 switch (ror->GetResultType()) {
3827 case Primitive::kPrimLong:
3828 // Add the temporary needed.
3829 locations->AddTemp(Location::RequiresRegister());
3830 FALLTHROUGH_INTENDED;
3831 case Primitive::kPrimInt:
3832 locations->SetInAt(0, Location::RequiresRegister());
3833 // The shift count needs to be in CL (unless it is a constant).
3834 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, ror->InputAt(1)));
3835 locations->SetOut(Location::SameAsFirstInput());
3836 break;
3837 default:
3838 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
3839 UNREACHABLE();
3840 }
3841}
3842
3843void InstructionCodeGeneratorX86::VisitRor(HRor* ror) {
3844 LocationSummary* locations = ror->GetLocations();
3845 Location first = locations->InAt(0);
3846 Location second = locations->InAt(1);
3847
3848 if (ror->GetResultType() == Primitive::kPrimInt) {
3849 Register first_reg = first.AsRegister<Register>();
3850 if (second.IsRegister()) {
3851 Register second_reg = second.AsRegister<Register>();
3852 __ rorl(first_reg, second_reg);
3853 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003854 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003855 __ rorl(first_reg, imm);
3856 }
3857 return;
3858 }
3859
3860 DCHECK_EQ(ror->GetResultType(), Primitive::kPrimLong);
3861 Register first_reg_lo = first.AsRegisterPairLow<Register>();
3862 Register first_reg_hi = first.AsRegisterPairHigh<Register>();
3863 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
3864 if (second.IsRegister()) {
3865 Register second_reg = second.AsRegister<Register>();
3866 DCHECK_EQ(second_reg, ECX);
3867 __ movl(temp_reg, first_reg_hi);
3868 __ shrd(first_reg_hi, first_reg_lo, second_reg);
3869 __ shrd(first_reg_lo, temp_reg, second_reg);
3870 __ movl(temp_reg, first_reg_hi);
3871 __ testl(second_reg, Immediate(32));
3872 __ cmovl(kNotEqual, first_reg_hi, first_reg_lo);
3873 __ cmovl(kNotEqual, first_reg_lo, temp_reg);
3874 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003875 int32_t shift_amt = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance;
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003876 if (shift_amt == 0) {
3877 // Already fine.
3878 return;
3879 }
3880 if (shift_amt == 32) {
3881 // Just swap.
3882 __ movl(temp_reg, first_reg_lo);
3883 __ movl(first_reg_lo, first_reg_hi);
3884 __ movl(first_reg_hi, temp_reg);
3885 return;
3886 }
3887
3888 Immediate imm(shift_amt);
3889 // Save the constents of the low value.
3890 __ movl(temp_reg, first_reg_lo);
3891
3892 // Shift right into low, feeding bits from high.
3893 __ shrd(first_reg_lo, first_reg_hi, imm);
3894
3895 // Shift right into high, feeding bits from the original low.
3896 __ shrd(first_reg_hi, temp_reg, imm);
3897
3898 // Swap if needed.
3899 if (shift_amt > 32) {
3900 __ movl(temp_reg, first_reg_lo);
3901 __ movl(first_reg_lo, first_reg_hi);
3902 __ movl(first_reg_hi, temp_reg);
3903 }
3904 }
3905}
3906
Calin Juravle9aec02f2014-11-18 23:06:35 +00003907void LocationsBuilderX86::VisitShl(HShl* shl) {
3908 HandleShift(shl);
3909}
3910
3911void InstructionCodeGeneratorX86::VisitShl(HShl* shl) {
3912 HandleShift(shl);
3913}
3914
3915void LocationsBuilderX86::VisitShr(HShr* shr) {
3916 HandleShift(shr);
3917}
3918
3919void InstructionCodeGeneratorX86::VisitShr(HShr* shr) {
3920 HandleShift(shr);
3921}
3922
3923void LocationsBuilderX86::VisitUShr(HUShr* ushr) {
3924 HandleShift(ushr);
3925}
3926
3927void InstructionCodeGeneratorX86::VisitUShr(HUShr* ushr) {
3928 HandleShift(ushr);
3929}
3930
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003931void LocationsBuilderX86::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003932 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003933 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003934 locations->SetOut(Location::RegisterLocation(EAX));
David Brazdil6de19382016-01-08 17:37:10 +00003935 if (instruction->IsStringAlloc()) {
3936 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
3937 } else {
3938 InvokeRuntimeCallingConvention calling_convention;
3939 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3940 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3941 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003942}
3943
3944void InstructionCodeGeneratorX86::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01003945 // Note: if heap poisoning is enabled, the entry point takes cares
3946 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00003947 if (instruction->IsStringAlloc()) {
3948 // String is allocated through StringFactory. Call NewEmptyString entry point.
3949 Register temp = instruction->GetLocations()->GetTemp(0).AsRegister<Register>();
Andreas Gampe542451c2016-07-26 09:02:02 -07003950 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86PointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00003951 __ fs()->movl(temp, Address::Absolute(QUICK_ENTRY_POINT(pNewEmptyString)));
3952 __ call(Address(temp, code_offset.Int32Value()));
3953 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3954 } else {
Serban Constantinescuba45db02016-07-12 22:53:02 +01003955 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
David Brazdil6de19382016-01-08 17:37:10 +00003956 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
3957 DCHECK(!codegen_->IsLeafMethod());
3958 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003959}
3960
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003961void LocationsBuilderX86::VisitNewArray(HNewArray* instruction) {
3962 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003963 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003964 locations->SetOut(Location::RegisterLocation(EAX));
3965 InvokeRuntimeCallingConvention calling_convention;
3966 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003967 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003968 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003969}
3970
3971void InstructionCodeGeneratorX86::VisitNewArray(HNewArray* instruction) {
3972 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003973 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
Roland Levillain4d027112015-07-01 15:41:14 +01003974 // Note: if heap poisoning is enabled, the entry point takes cares
3975 // of poisoning the reference.
Serban Constantinescuba45db02016-07-12 22:53:02 +01003976 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003977 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003978 DCHECK(!codegen_->IsLeafMethod());
3979}
3980
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003981void LocationsBuilderX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003982 LocationSummary* locations =
3983 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01003984 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
3985 if (location.IsStackSlot()) {
3986 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3987 } else if (location.IsDoubleStackSlot()) {
3988 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003989 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01003990 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003991}
3992
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003993void InstructionCodeGeneratorX86::VisitParameterValue(
3994 HParameterValue* instruction ATTRIBUTE_UNUSED) {
3995}
3996
3997void LocationsBuilderX86::VisitCurrentMethod(HCurrentMethod* instruction) {
3998 LocationSummary* locations =
3999 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4000 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
4001}
4002
4003void InstructionCodeGeneratorX86::VisitCurrentMethod(HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004004}
4005
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004006void LocationsBuilderX86::VisitClassTableGet(HClassTableGet* instruction) {
4007 LocationSummary* locations =
4008 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4009 locations->SetInAt(0, Location::RequiresRegister());
4010 locations->SetOut(Location::RequiresRegister());
4011}
4012
4013void InstructionCodeGeneratorX86::VisitClassTableGet(HClassTableGet* instruction) {
4014 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004015 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004016 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004017 instruction->GetIndex(), kX86PointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004018 __ movl(locations->Out().AsRegister<Register>(),
4019 Address(locations->InAt(0).AsRegister<Register>(), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004020 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004021 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00004022 instruction->GetIndex(), kX86PointerSize));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004023 __ movl(locations->Out().AsRegister<Register>(),
4024 Address(locations->InAt(0).AsRegister<Register>(),
4025 mirror::Class::ImtPtrOffset(kX86PointerSize).Uint32Value()));
4026 // temp = temp->GetImtEntryAt(method_offset);
4027 __ movl(locations->Out().AsRegister<Register>(),
4028 Address(locations->Out().AsRegister<Register>(), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004029 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004030}
4031
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004032void LocationsBuilderX86::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004033 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004034 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01004035 locations->SetInAt(0, Location::RequiresRegister());
4036 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004037}
4038
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004039void InstructionCodeGeneratorX86::VisitNot(HNot* not_) {
4040 LocationSummary* locations = not_->GetLocations();
Roland Levillain70566432014-10-24 16:20:17 +01004041 Location in = locations->InAt(0);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01004042 Location out = locations->Out();
Roland Levillain70566432014-10-24 16:20:17 +01004043 DCHECK(in.Equals(out));
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00004044 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004045 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00004046 __ notl(out.AsRegister<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004047 break;
4048
4049 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01004050 __ notl(out.AsRegisterPairLow<Register>());
4051 __ notl(out.AsRegisterPairHigh<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004052 break;
4053
4054 default:
4055 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
4056 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004057}
4058
David Brazdil66d126e2015-04-03 16:02:44 +01004059void LocationsBuilderX86::VisitBooleanNot(HBooleanNot* bool_not) {
4060 LocationSummary* locations =
4061 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
4062 locations->SetInAt(0, Location::RequiresRegister());
4063 locations->SetOut(Location::SameAsFirstInput());
4064}
4065
4066void InstructionCodeGeneratorX86::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01004067 LocationSummary* locations = bool_not->GetLocations();
4068 Location in = locations->InAt(0);
4069 Location out = locations->Out();
4070 DCHECK(in.Equals(out));
4071 __ xorl(out.AsRegister<Register>(), Immediate(1));
4072}
4073
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004074void LocationsBuilderX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004075 LocationSummary* locations =
4076 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00004077 switch (compare->InputAt(0)->GetType()) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00004078 case Primitive::kPrimBoolean:
4079 case Primitive::kPrimByte:
4080 case Primitive::kPrimShort:
4081 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08004082 case Primitive::kPrimInt:
Calin Juravleddb7df22014-11-25 20:56:51 +00004083 case Primitive::kPrimLong: {
4084 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravleddb7df22014-11-25 20:56:51 +00004085 locations->SetInAt(1, Location::Any());
4086 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4087 break;
4088 }
4089 case Primitive::kPrimFloat:
4090 case Primitive::kPrimDouble: {
4091 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004092 if (compare->InputAt(1)->IsX86LoadFromConstantTable()) {
4093 DCHECK(compare->InputAt(1)->IsEmittedAtUseSite());
4094 } else if (compare->InputAt(1)->IsConstant()) {
4095 locations->SetInAt(1, Location::RequiresFpuRegister());
4096 } else {
4097 locations->SetInAt(1, Location::Any());
4098 }
Calin Juravleddb7df22014-11-25 20:56:51 +00004099 locations->SetOut(Location::RequiresRegister());
4100 break;
4101 }
4102 default:
4103 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
4104 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004105}
4106
4107void InstructionCodeGeneratorX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004108 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004109 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00004110 Location left = locations->InAt(0);
4111 Location right = locations->InAt(1);
4112
Mark Mendell0c9497d2015-08-21 09:30:05 -04004113 NearLabel less, greater, done;
Aart Bika19616e2016-02-01 18:57:58 -08004114 Condition less_cond = kLess;
4115
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004116 switch (compare->InputAt(0)->GetType()) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00004117 case Primitive::kPrimBoolean:
4118 case Primitive::kPrimByte:
4119 case Primitive::kPrimShort:
4120 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08004121 case Primitive::kPrimInt: {
Roland Levillain0b671c02016-08-19 12:02:34 +01004122 codegen_->GenerateIntCompare(left, right);
Aart Bika19616e2016-02-01 18:57:58 -08004123 break;
4124 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004125 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004126 Register left_low = left.AsRegisterPairLow<Register>();
4127 Register left_high = left.AsRegisterPairHigh<Register>();
4128 int32_t val_low = 0;
4129 int32_t val_high = 0;
4130 bool right_is_const = false;
4131
4132 if (right.IsConstant()) {
4133 DCHECK(right.GetConstant()->IsLongConstant());
4134 right_is_const = true;
4135 int64_t val = right.GetConstant()->AsLongConstant()->GetValue();
4136 val_low = Low32Bits(val);
4137 val_high = High32Bits(val);
4138 }
4139
Calin Juravleddb7df22014-11-25 20:56:51 +00004140 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004141 __ cmpl(left_high, right.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004142 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004143 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004144 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004145 DCHECK(right_is_const) << right;
Aart Bika19616e2016-02-01 18:57:58 -08004146 codegen_->Compare32BitValue(left_high, val_high);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004147 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004148 __ j(kLess, &less); // Signed compare.
4149 __ j(kGreater, &greater); // Signed compare.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004150 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004151 __ cmpl(left_low, right.AsRegisterPairLow<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004152 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004153 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004154 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004155 DCHECK(right_is_const) << right;
Aart Bika19616e2016-02-01 18:57:58 -08004156 codegen_->Compare32BitValue(left_low, val_low);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004157 }
Aart Bika19616e2016-02-01 18:57:58 -08004158 less_cond = kBelow; // for CF (unsigned).
Calin Juravleddb7df22014-11-25 20:56:51 +00004159 break;
4160 }
4161 case Primitive::kPrimFloat: {
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004162 GenerateFPCompare(left, right, compare, false);
Calin Juravleddb7df22014-11-25 20:56:51 +00004163 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08004164 less_cond = kBelow; // for CF (floats).
Calin Juravleddb7df22014-11-25 20:56:51 +00004165 break;
4166 }
4167 case Primitive::kPrimDouble: {
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004168 GenerateFPCompare(left, right, compare, true);
Calin Juravleddb7df22014-11-25 20:56:51 +00004169 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08004170 less_cond = kBelow; // for CF (floats).
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004171 break;
4172 }
4173 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00004174 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004175 }
Aart Bika19616e2016-02-01 18:57:58 -08004176
Calin Juravleddb7df22014-11-25 20:56:51 +00004177 __ movl(out, Immediate(0));
4178 __ j(kEqual, &done);
Aart Bika19616e2016-02-01 18:57:58 -08004179 __ j(less_cond, &less);
Calin Juravleddb7df22014-11-25 20:56:51 +00004180
4181 __ Bind(&greater);
4182 __ movl(out, Immediate(1));
4183 __ jmp(&done);
4184
4185 __ Bind(&less);
4186 __ movl(out, Immediate(-1));
4187
4188 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004189}
4190
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004191void LocationsBuilderX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004192 LocationSummary* locations =
4193 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko372f10e2016-05-17 16:30:10 +01004194 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01004195 locations->SetInAt(i, Location::Any());
4196 }
4197 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004198}
4199
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004200void InstructionCodeGeneratorX86::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004201 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004202}
4203
Roland Levillain7c1559a2015-12-15 10:55:36 +00004204void CodeGeneratorX86::GenerateMemoryBarrier(MemBarrierKind kind) {
Calin Juravle52c48962014-12-16 17:02:57 +00004205 /*
4206 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
4207 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
4208 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
4209 */
4210 switch (kind) {
4211 case MemBarrierKind::kAnyAny: {
Mark P Mendell17077d82015-12-16 19:15:59 +00004212 MemoryFence();
Calin Juravle52c48962014-12-16 17:02:57 +00004213 break;
4214 }
4215 case MemBarrierKind::kAnyStore:
4216 case MemBarrierKind::kLoadAny:
4217 case MemBarrierKind::kStoreStore: {
4218 // nop
4219 break;
4220 }
Mark Mendell7aa04a12016-01-27 22:39:07 -05004221 case MemBarrierKind::kNTStoreStore:
4222 // Non-Temporal Store/Store needs an explicit fence.
4223 MemoryFence(/* non-temporal */ true);
4224 break;
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004225 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004226}
4227
Vladimir Markodc151b22015-10-15 18:02:30 +01004228HInvokeStaticOrDirect::DispatchInfo CodeGeneratorX86::GetSupportedInvokeStaticOrDirectDispatch(
4229 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
4230 MethodReference target_method ATTRIBUTE_UNUSED) {
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00004231 HInvokeStaticOrDirect::DispatchInfo dispatch_info = desired_dispatch_info;
4232
4233 // We disable pc-relative load when there is an irreducible loop, as the optimization
4234 // is incompatible with it.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004235 // TODO: Create as many X86ComputeBaseMethodAddress instructions
4236 // as needed for methods with irreducible loops.
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00004237 if (GetGraph()->HasIrreducibleLoops() &&
4238 (dispatch_info.method_load_kind ==
4239 HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative)) {
4240 dispatch_info.method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod;
4241 }
4242 switch (dispatch_info.code_ptr_location) {
Vladimir Markodc151b22015-10-15 18:02:30 +01004243 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
4244 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
4245 // For direct code, we actually prefer to call via the code pointer from ArtMethod*.
4246 // (Though the direct CALL ptr16:32 is available for consideration).
4247 return HInvokeStaticOrDirect::DispatchInfo {
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00004248 dispatch_info.method_load_kind,
Vladimir Markodc151b22015-10-15 18:02:30 +01004249 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00004250 dispatch_info.method_load_data,
Vladimir Markodc151b22015-10-15 18:02:30 +01004251 0u
4252 };
4253 default:
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00004254 return dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01004255 }
4256}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004257
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004258Register CodeGeneratorX86::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
4259 Register temp) {
4260 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
Vladimir Markoc53c0792015-11-19 15:48:33 +00004261 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004262 if (!invoke->GetLocations()->Intrinsified()) {
4263 return location.AsRegister<Register>();
4264 }
4265 // For intrinsics we allow any location, so it may be on the stack.
4266 if (!location.IsRegister()) {
4267 __ movl(temp, Address(ESP, location.GetStackIndex()));
4268 return temp;
4269 }
4270 // For register locations, check if the register was saved. If so, get it from the stack.
4271 // Note: There is a chance that the register was saved but not overwritten, so we could
4272 // save one load. However, since this is just an intrinsic slow path we prefer this
4273 // simple and more robust approach rather that trying to determine if that's the case.
4274 SlowPathCode* slow_path = GetCurrentSlowPath();
Serguei Katkov288c7a82016-05-16 11:53:15 +06004275 if (slow_path != nullptr) {
4276 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
4277 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
4278 __ movl(temp, Address(ESP, stack_offset));
4279 return temp;
4280 }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004281 }
4282 return location.AsRegister<Register>();
4283}
4284
Serguei Katkov288c7a82016-05-16 11:53:15 +06004285Location CodeGeneratorX86::GenerateCalleeMethodStaticOrDirectCall(HInvokeStaticOrDirect* invoke,
4286 Location temp) {
Vladimir Marko58155012015-08-19 12:49:41 +00004287 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
4288 switch (invoke->GetMethodLoadKind()) {
4289 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
4290 // temp = thread->string_init_entrypoint
4291 __ fs()->movl(temp.AsRegister<Register>(), Address::Absolute(invoke->GetStringInitOffset()));
4292 break;
4293 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00004294 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004295 break;
4296 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
4297 __ movl(temp.AsRegister<Register>(), Immediate(invoke->GetMethodAddress()));
4298 break;
4299 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004300 __ movl(temp.AsRegister<Register>(), Immediate(/* placeholder */ 0));
Vladimir Marko58155012015-08-19 12:49:41 +00004301 method_patches_.emplace_back(invoke->GetTargetMethod());
4302 __ Bind(&method_patches_.back().label); // Bind the label at the end of the "movl" insn.
4303 break;
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004304 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
4305 Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
4306 temp.AsRegister<Register>());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004307 __ movl(temp.AsRegister<Register>(), Address(base_reg, kDummy32BitOffset));
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004308 // Bind a new fixup label at the end of the "movl" insn.
4309 uint32_t offset = invoke->GetDexCacheArrayOffset();
4310 __ Bind(NewPcRelativeDexCacheArrayPatch(*invoke->GetTargetMethod().dex_file, offset));
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004311 break;
4312 }
Vladimir Marko58155012015-08-19 12:49:41 +00004313 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004314 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004315 Register method_reg;
4316 Register reg = temp.AsRegister<Register>();
4317 if (current_method.IsRegister()) {
4318 method_reg = current_method.AsRegister<Register>();
4319 } else {
David Brazdil58282f42016-01-14 12:45:10 +00004320 DCHECK(invoke->GetLocations()->Intrinsified());
Vladimir Marko58155012015-08-19 12:49:41 +00004321 DCHECK(!current_method.IsValid());
4322 method_reg = reg;
4323 __ movl(reg, Address(ESP, kCurrentMethodStackOffset));
4324 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00004325 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +01004326 __ movl(reg, Address(method_reg,
4327 ArtMethod::DexCacheResolvedMethodsOffset(kX86PointerSize).Int32Value()));
Vladimir Marko40ecb122016-04-06 17:33:41 +01004328 // temp = temp[index_in_cache];
4329 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
4330 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Vladimir Marko58155012015-08-19 12:49:41 +00004331 __ movl(reg, Address(reg, CodeGenerator::GetCachePointerOffset(index_in_cache)));
4332 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +01004333 }
Vladimir Marko58155012015-08-19 12:49:41 +00004334 }
Serguei Katkov288c7a82016-05-16 11:53:15 +06004335 return callee_method;
4336}
4337
4338void CodeGeneratorX86::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
4339 Location callee_method = GenerateCalleeMethodStaticOrDirectCall(invoke, temp);
Vladimir Marko58155012015-08-19 12:49:41 +00004340
4341 switch (invoke->GetCodePtrLocation()) {
4342 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
4343 __ call(GetFrameEntryLabel());
4344 break;
4345 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative: {
4346 relative_call_patches_.emplace_back(invoke->GetTargetMethod());
4347 Label* label = &relative_call_patches_.back().label;
4348 __ call(label); // Bind to the patch label, override at link time.
4349 __ Bind(label); // Bind the label at the end of the "call" insn.
4350 break;
4351 }
4352 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
4353 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
Vladimir Markodc151b22015-10-15 18:02:30 +01004354 // Filtered out by GetSupportedInvokeStaticOrDirectDispatch().
4355 LOG(FATAL) << "Unsupported";
4356 UNREACHABLE();
Vladimir Marko58155012015-08-19 12:49:41 +00004357 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
4358 // (callee_method + offset_of_quick_compiled_code)()
4359 __ call(Address(callee_method.AsRegister<Register>(),
4360 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07004361 kX86PointerSize).Int32Value()));
Vladimir Marko58155012015-08-19 12:49:41 +00004362 break;
Mark Mendell09ed1a32015-03-25 08:30:06 -04004363 }
4364
4365 DCHECK(!IsLeafMethod());
Mark Mendell09ed1a32015-03-25 08:30:06 -04004366}
4367
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004368void CodeGeneratorX86::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_in) {
4369 Register temp = temp_in.AsRegister<Register>();
4370 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
4371 invoke->GetVTableIndex(), kX86PointerSize).Uint32Value();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004372
4373 // Use the calling convention instead of the location of the receiver, as
4374 // intrinsics may have put the receiver in a different register. In the intrinsics
4375 // slow path, the arguments have been moved to the right place, so here we are
4376 // guaranteed that the receiver is the first register of the calling convention.
4377 InvokeDexCallingConvention calling_convention;
4378 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004379 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004380 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004381 __ movl(temp, Address(receiver, class_offset));
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004382 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00004383 // Instead of simply (possibly) unpoisoning `temp` here, we should
4384 // emit a read barrier for the previous class reference load.
4385 // However this is not required in practice, as this is an
4386 // intermediate/temporary reference and because the current
4387 // concurrent copying collector keeps the from-space memory
4388 // intact/accessible until the end of the marking phase (the
4389 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004390 __ MaybeUnpoisonHeapReference(temp);
4391 // temp = temp->GetMethodAt(method_offset);
4392 __ movl(temp, Address(temp, method_offset));
4393 // call temp->GetEntryPoint();
4394 __ call(Address(
Andreas Gampe542451c2016-07-26 09:02:02 -07004395 temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86PointerSize).Int32Value()));
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004396}
4397
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004398void CodeGeneratorX86::RecordSimplePatch() {
4399 if (GetCompilerOptions().GetIncludePatchInformation()) {
4400 simple_patches_.emplace_back();
4401 __ Bind(&simple_patches_.back());
4402 }
4403}
4404
4405void CodeGeneratorX86::RecordStringPatch(HLoadString* load_string) {
4406 string_patches_.emplace_back(load_string->GetDexFile(), load_string->GetStringIndex());
4407 __ Bind(&string_patches_.back().label);
4408}
4409
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004410void CodeGeneratorX86::RecordTypePatch(HLoadClass* load_class) {
4411 type_patches_.emplace_back(load_class->GetDexFile(), load_class->GetTypeIndex());
4412 __ Bind(&type_patches_.back().label);
4413}
4414
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004415Label* CodeGeneratorX86::NewPcRelativeDexCacheArrayPatch(const DexFile& dex_file,
4416 uint32_t element_offset) {
4417 // Add the patch entry and bind its label at the end of the instruction.
4418 pc_relative_dex_cache_patches_.emplace_back(dex_file, element_offset);
4419 return &pc_relative_dex_cache_patches_.back().label;
4420}
4421
Vladimir Marko58155012015-08-19 12:49:41 +00004422void CodeGeneratorX86::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
4423 DCHECK(linker_patches->empty());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004424 size_t size =
4425 method_patches_.size() +
4426 relative_call_patches_.size() +
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004427 pc_relative_dex_cache_patches_.size() +
4428 simple_patches_.size() +
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004429 string_patches_.size() +
4430 type_patches_.size();
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004431 linker_patches->reserve(size);
4432 // The label points to the end of the "movl" insn but the literal offset for method
4433 // patch needs to point to the embedded constant which occupies the last 4 bytes.
4434 constexpr uint32_t kLabelPositionToLiteralOffsetAdjustment = 4u;
Vladimir Marko58155012015-08-19 12:49:41 +00004435 for (const MethodPatchInfo<Label>& info : method_patches_) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004436 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Marko58155012015-08-19 12:49:41 +00004437 linker_patches->push_back(LinkerPatch::MethodPatch(literal_offset,
4438 info.target_method.dex_file,
4439 info.target_method.dex_method_index));
4440 }
4441 for (const MethodPatchInfo<Label>& info : relative_call_patches_) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004442 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Marko58155012015-08-19 12:49:41 +00004443 linker_patches->push_back(LinkerPatch::RelativeCodePatch(literal_offset,
4444 info.target_method.dex_file,
4445 info.target_method.dex_method_index));
4446 }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004447 for (const PcRelativeDexCacheAccessInfo& info : pc_relative_dex_cache_patches_) {
4448 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
4449 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(literal_offset,
4450 &info.target_dex_file,
4451 GetMethodAddressOffset(),
4452 info.element_offset));
4453 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004454 for (const Label& label : simple_patches_) {
4455 uint32_t literal_offset = label.Position() - kLabelPositionToLiteralOffsetAdjustment;
4456 linker_patches->push_back(LinkerPatch::RecordPosition(literal_offset));
4457 }
4458 if (GetCompilerOptions().GetCompilePic()) {
4459 for (const StringPatchInfo<Label>& info : string_patches_) {
4460 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
4461 linker_patches->push_back(LinkerPatch::RelativeStringPatch(literal_offset,
4462 &info.dex_file,
4463 GetMethodAddressOffset(),
4464 info.string_index));
4465 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004466 for (const TypePatchInfo<Label>& info : type_patches_) {
4467 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
4468 linker_patches->push_back(LinkerPatch::RelativeTypePatch(literal_offset,
4469 &info.dex_file,
4470 GetMethodAddressOffset(),
4471 info.type_index));
4472 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004473 } else {
4474 for (const StringPatchInfo<Label>& info : string_patches_) {
4475 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
4476 linker_patches->push_back(LinkerPatch::StringPatch(literal_offset,
4477 &info.dex_file,
4478 info.string_index));
4479 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004480 for (const TypePatchInfo<Label>& info : type_patches_) {
4481 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
4482 linker_patches->push_back(LinkerPatch::TypePatch(literal_offset,
4483 &info.dex_file,
4484 info.type_index));
4485 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004486 }
Vladimir Marko58155012015-08-19 12:49:41 +00004487}
4488
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004489void CodeGeneratorX86::MarkGCCard(Register temp,
4490 Register card,
4491 Register object,
4492 Register value,
4493 bool value_can_be_null) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004494 NearLabel is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004495 if (value_can_be_null) {
4496 __ testl(value, value);
4497 __ j(kEqual, &is_null);
4498 }
Andreas Gampe542451c2016-07-26 09:02:02 -07004499 __ fs()->movl(card, Address::Absolute(Thread::CardTableOffset<kX86PointerSize>().Int32Value()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004500 __ movl(temp, object);
4501 __ shrl(temp, Immediate(gc::accounting::CardTable::kCardShift));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00004502 __ movb(Address(temp, card, TIMES_1, 0),
4503 X86ManagedRegister::FromCpuRegister(card).AsByteRegister());
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004504 if (value_can_be_null) {
4505 __ Bind(&is_null);
4506 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004507}
4508
Calin Juravle52c48962014-12-16 17:02:57 +00004509void LocationsBuilderX86::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
4510 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain0d5a2812015-11-13 10:07:31 +00004511
4512 bool object_field_get_with_read_barrier =
4513 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004514 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00004515 new (GetGraph()->GetArena()) LocationSummary(instruction,
4516 kEmitCompilerReadBarrier ?
4517 LocationSummary::kCallOnSlowPath :
4518 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01004519 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
4520 locations->SetCustomSlowPathCallerSaves(RegisterSet()); // No caller-save registers.
4521 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004522 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004523
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004524 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4525 locations->SetOut(Location::RequiresFpuRegister());
4526 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004527 // The output overlaps in case of long: we don't want the low move
4528 // to overwrite the object's location. Likewise, in the case of
4529 // an object field get with read barriers enabled, we do not want
4530 // the move to overwrite the object's location, as we need it to emit
4531 // the read barrier.
4532 locations->SetOut(
4533 Location::RequiresRegister(),
4534 (object_field_get_with_read_barrier || instruction->GetType() == Primitive::kPrimLong) ?
4535 Location::kOutputOverlap :
4536 Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004537 }
Calin Juravle52c48962014-12-16 17:02:57 +00004538
4539 if (field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong)) {
4540 // Long values can be loaded atomically into an XMM using movsd.
Roland Levillain7c1559a2015-12-15 10:55:36 +00004541 // So we use an XMM register as a temp to achieve atomicity (first
4542 // load the temp into the XMM and then copy the XMM into the
4543 // output, 32 bits at a time).
Calin Juravle52c48962014-12-16 17:02:57 +00004544 locations->AddTemp(Location::RequiresFpuRegister());
4545 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004546}
4547
Calin Juravle52c48962014-12-16 17:02:57 +00004548void InstructionCodeGeneratorX86::HandleFieldGet(HInstruction* instruction,
4549 const FieldInfo& field_info) {
4550 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004551
Calin Juravle52c48962014-12-16 17:02:57 +00004552 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004553 Location base_loc = locations->InAt(0);
4554 Register base = base_loc.AsRegister<Register>();
Calin Juravle52c48962014-12-16 17:02:57 +00004555 Location out = locations->Out();
4556 bool is_volatile = field_info.IsVolatile();
4557 Primitive::Type field_type = field_info.GetFieldType();
4558 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4559
4560 switch (field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004561 case Primitive::kPrimBoolean: {
Calin Juravle52c48962014-12-16 17:02:57 +00004562 __ movzxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004563 break;
4564 }
4565
4566 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00004567 __ movsxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004568 break;
4569 }
4570
4571 case Primitive::kPrimShort: {
Calin Juravle52c48962014-12-16 17:02:57 +00004572 __ movsxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004573 break;
4574 }
4575
4576 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00004577 __ movzxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004578 break;
4579 }
4580
4581 case Primitive::kPrimInt:
Calin Juravle52c48962014-12-16 17:02:57 +00004582 __ movl(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004583 break;
Roland Levillain7c1559a2015-12-15 10:55:36 +00004584
4585 case Primitive::kPrimNot: {
4586 // /* HeapReference<Object> */ out = *(base + offset)
4587 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004588 // Note that a potential implicit null check is handled in this
4589 // CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier call.
4590 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00004591 instruction, out, base, offset, /* needs_null_check */ true);
Roland Levillain7c1559a2015-12-15 10:55:36 +00004592 if (is_volatile) {
4593 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4594 }
4595 } else {
4596 __ movl(out.AsRegister<Register>(), Address(base, offset));
4597 codegen_->MaybeRecordImplicitNullCheck(instruction);
4598 if (is_volatile) {
4599 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4600 }
4601 // If read barriers are enabled, emit read barriers other than
4602 // Baker's using a slow path (and also unpoison the loaded
4603 // reference, if heap poisoning is enabled).
4604 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
4605 }
4606 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004607 }
4608
4609 case Primitive::kPrimLong: {
Calin Juravle52c48962014-12-16 17:02:57 +00004610 if (is_volatile) {
4611 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4612 __ movsd(temp, Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004613 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004614 __ movd(out.AsRegisterPairLow<Register>(), temp);
4615 __ psrlq(temp, Immediate(32));
4616 __ movd(out.AsRegisterPairHigh<Register>(), temp);
4617 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004618 DCHECK_NE(base, out.AsRegisterPairLow<Register>());
Calin Juravle52c48962014-12-16 17:02:57 +00004619 __ movl(out.AsRegisterPairLow<Register>(), Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004620 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004621 __ movl(out.AsRegisterPairHigh<Register>(), Address(base, kX86WordSize + offset));
4622 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004623 break;
4624 }
4625
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004626 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00004627 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004628 break;
4629 }
4630
4631 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00004632 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004633 break;
4634 }
4635
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004636 case Primitive::kPrimVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00004637 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004638 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004639 }
Calin Juravle52c48962014-12-16 17:02:57 +00004640
Roland Levillain7c1559a2015-12-15 10:55:36 +00004641 if (field_type == Primitive::kPrimNot || field_type == Primitive::kPrimLong) {
4642 // Potential implicit null checks, in the case of reference or
4643 // long fields, are handled in the previous switch statement.
4644 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00004645 codegen_->MaybeRecordImplicitNullCheck(instruction);
4646 }
4647
Calin Juravle52c48962014-12-16 17:02:57 +00004648 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004649 if (field_type == Primitive::kPrimNot) {
4650 // Memory barriers, in the case of references, are also handled
4651 // in the previous switch statement.
4652 } else {
4653 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4654 }
Roland Levillain4d027112015-07-01 15:41:14 +01004655 }
Calin Juravle52c48962014-12-16 17:02:57 +00004656}
4657
4658void LocationsBuilderX86::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
4659 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4660
4661 LocationSummary* locations =
4662 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4663 locations->SetInAt(0, Location::RequiresRegister());
4664 bool is_volatile = field_info.IsVolatile();
4665 Primitive::Type field_type = field_info.GetFieldType();
4666 bool is_byte_type = (field_type == Primitive::kPrimBoolean)
4667 || (field_type == Primitive::kPrimByte);
4668
4669 // The register allocator does not support multiple
4670 // inputs that die at entry with one in a specific register.
4671 if (is_byte_type) {
4672 // Ensure the value is in a byte register.
4673 locations->SetInAt(1, Location::RegisterLocation(EAX));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004674 } else if (Primitive::IsFloatingPointType(field_type)) {
Mark Mendell81489372015-11-04 11:30:41 -05004675 if (is_volatile && field_type == Primitive::kPrimDouble) {
4676 // In order to satisfy the semantics of volatile, this must be a single instruction store.
4677 locations->SetInAt(1, Location::RequiresFpuRegister());
4678 } else {
4679 locations->SetInAt(1, Location::FpuRegisterOrConstant(instruction->InputAt(1)));
4680 }
4681 } else if (is_volatile && field_type == Primitive::kPrimLong) {
4682 // In order to satisfy the semantics of volatile, this must be a single instruction store.
Calin Juravle52c48962014-12-16 17:02:57 +00004683 locations->SetInAt(1, Location::RequiresRegister());
Mark Mendell81489372015-11-04 11:30:41 -05004684
Calin Juravle52c48962014-12-16 17:02:57 +00004685 // 64bits value can be atomically written to an address with movsd and an XMM register.
4686 // We need two XMM registers because there's no easier way to (bit) copy a register pair
4687 // into a single XMM register (we copy each pair part into the XMMs and then interleave them).
4688 // NB: We could make the register allocator understand fp_reg <-> core_reg moves but given the
4689 // isolated cases when we need this it isn't worth adding the extra complexity.
4690 locations->AddTemp(Location::RequiresFpuRegister());
4691 locations->AddTemp(Location::RequiresFpuRegister());
Mark Mendell81489372015-11-04 11:30:41 -05004692 } else {
4693 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4694
4695 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
4696 // Temporary registers for the write barrier.
4697 locations->AddTemp(Location::RequiresRegister()); // May be used for reference poisoning too.
4698 // Ensure the card is in a byte register.
4699 locations->AddTemp(Location::RegisterLocation(ECX));
4700 }
Calin Juravle52c48962014-12-16 17:02:57 +00004701 }
4702}
4703
4704void InstructionCodeGeneratorX86::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004705 const FieldInfo& field_info,
4706 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00004707 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4708
4709 LocationSummary* locations = instruction->GetLocations();
4710 Register base = locations->InAt(0).AsRegister<Register>();
4711 Location value = locations->InAt(1);
4712 bool is_volatile = field_info.IsVolatile();
4713 Primitive::Type field_type = field_info.GetFieldType();
4714 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01004715 bool needs_write_barrier =
4716 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00004717
4718 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004719 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
Calin Juravle52c48962014-12-16 17:02:57 +00004720 }
4721
Mark Mendell81489372015-11-04 11:30:41 -05004722 bool maybe_record_implicit_null_check_done = false;
4723
Calin Juravle52c48962014-12-16 17:02:57 +00004724 switch (field_type) {
4725 case Primitive::kPrimBoolean:
4726 case Primitive::kPrimByte: {
4727 __ movb(Address(base, offset), value.AsRegister<ByteRegister>());
4728 break;
4729 }
4730
4731 case Primitive::kPrimShort:
4732 case Primitive::kPrimChar: {
Mark Mendell81489372015-11-04 11:30:41 -05004733 if (value.IsConstant()) {
4734 int16_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4735 __ movw(Address(base, offset), Immediate(v));
4736 } else {
4737 __ movw(Address(base, offset), value.AsRegister<Register>());
4738 }
Calin Juravle52c48962014-12-16 17:02:57 +00004739 break;
4740 }
4741
4742 case Primitive::kPrimInt:
4743 case Primitive::kPrimNot: {
Roland Levillain4d027112015-07-01 15:41:14 +01004744 if (kPoisonHeapReferences && needs_write_barrier) {
4745 // Note that in the case where `value` is a null reference,
4746 // we do not enter this block, as the reference does not
4747 // need poisoning.
4748 DCHECK_EQ(field_type, Primitive::kPrimNot);
4749 Register temp = locations->GetTemp(0).AsRegister<Register>();
4750 __ movl(temp, value.AsRegister<Register>());
4751 __ PoisonHeapReference(temp);
4752 __ movl(Address(base, offset), temp);
Mark Mendell81489372015-11-04 11:30:41 -05004753 } else if (value.IsConstant()) {
4754 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4755 __ movl(Address(base, offset), Immediate(v));
Roland Levillain4d027112015-07-01 15:41:14 +01004756 } else {
Nicolas Geoffray03971632016-03-17 10:44:24 +00004757 DCHECK(value.IsRegister()) << value;
Roland Levillain4d027112015-07-01 15:41:14 +01004758 __ movl(Address(base, offset), value.AsRegister<Register>());
4759 }
Calin Juravle52c48962014-12-16 17:02:57 +00004760 break;
4761 }
4762
4763 case Primitive::kPrimLong: {
4764 if (is_volatile) {
4765 XmmRegister temp1 = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4766 XmmRegister temp2 = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
4767 __ movd(temp1, value.AsRegisterPairLow<Register>());
4768 __ movd(temp2, value.AsRegisterPairHigh<Register>());
4769 __ punpckldq(temp1, temp2);
4770 __ movsd(Address(base, offset), temp1);
Calin Juravle77520bc2015-01-12 18:45:46 +00004771 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell81489372015-11-04 11:30:41 -05004772 } else if (value.IsConstant()) {
4773 int64_t v = CodeGenerator::GetInt64ValueOf(value.GetConstant());
4774 __ movl(Address(base, offset), Immediate(Low32Bits(v)));
4775 codegen_->MaybeRecordImplicitNullCheck(instruction);
4776 __ movl(Address(base, kX86WordSize + offset), Immediate(High32Bits(v)));
Calin Juravle52c48962014-12-16 17:02:57 +00004777 } else {
4778 __ movl(Address(base, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00004779 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004780 __ movl(Address(base, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
4781 }
Mark Mendell81489372015-11-04 11:30:41 -05004782 maybe_record_implicit_null_check_done = true;
Calin Juravle52c48962014-12-16 17:02:57 +00004783 break;
4784 }
4785
4786 case Primitive::kPrimFloat: {
Mark Mendell81489372015-11-04 11:30:41 -05004787 if (value.IsConstant()) {
4788 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4789 __ movl(Address(base, offset), Immediate(v));
4790 } else {
4791 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4792 }
Calin Juravle52c48962014-12-16 17:02:57 +00004793 break;
4794 }
4795
4796 case Primitive::kPrimDouble: {
Mark Mendell81489372015-11-04 11:30:41 -05004797 if (value.IsConstant()) {
4798 int64_t v = CodeGenerator::GetInt64ValueOf(value.GetConstant());
4799 __ movl(Address(base, offset), Immediate(Low32Bits(v)));
4800 codegen_->MaybeRecordImplicitNullCheck(instruction);
4801 __ movl(Address(base, kX86WordSize + offset), Immediate(High32Bits(v)));
4802 maybe_record_implicit_null_check_done = true;
4803 } else {
4804 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4805 }
Calin Juravle52c48962014-12-16 17:02:57 +00004806 break;
4807 }
4808
4809 case Primitive::kPrimVoid:
4810 LOG(FATAL) << "Unreachable type " << field_type;
4811 UNREACHABLE();
4812 }
4813
Mark Mendell81489372015-11-04 11:30:41 -05004814 if (!maybe_record_implicit_null_check_done) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004815 codegen_->MaybeRecordImplicitNullCheck(instruction);
4816 }
4817
Roland Levillain4d027112015-07-01 15:41:14 +01004818 if (needs_write_barrier) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004819 Register temp = locations->GetTemp(0).AsRegister<Register>();
4820 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004821 codegen_->MarkGCCard(temp, card, base, value.AsRegister<Register>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00004822 }
4823
Calin Juravle52c48962014-12-16 17:02:57 +00004824 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004825 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
Calin Juravle52c48962014-12-16 17:02:57 +00004826 }
4827}
4828
4829void LocationsBuilderX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4830 HandleFieldGet(instruction, instruction->GetFieldInfo());
4831}
4832
4833void InstructionCodeGeneratorX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4834 HandleFieldGet(instruction, instruction->GetFieldInfo());
4835}
4836
4837void LocationsBuilderX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
4838 HandleFieldSet(instruction, instruction->GetFieldInfo());
4839}
4840
4841void InstructionCodeGeneratorX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004842 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00004843}
4844
4845void LocationsBuilderX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
4846 HandleFieldSet(instruction, instruction->GetFieldInfo());
4847}
4848
4849void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004850 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00004851}
4852
4853void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4854 HandleFieldGet(instruction, instruction->GetFieldInfo());
4855}
4856
4857void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4858 HandleFieldGet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004859}
4860
Calin Juravlee460d1d2015-09-29 04:52:17 +01004861void LocationsBuilderX86::VisitUnresolvedInstanceFieldGet(
4862 HUnresolvedInstanceFieldGet* instruction) {
4863 FieldAccessCallingConventionX86 calling_convention;
4864 codegen_->CreateUnresolvedFieldLocationSummary(
4865 instruction, instruction->GetFieldType(), calling_convention);
4866}
4867
4868void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldGet(
4869 HUnresolvedInstanceFieldGet* instruction) {
4870 FieldAccessCallingConventionX86 calling_convention;
4871 codegen_->GenerateUnresolvedFieldAccess(instruction,
4872 instruction->GetFieldType(),
4873 instruction->GetFieldIndex(),
4874 instruction->GetDexPc(),
4875 calling_convention);
4876}
4877
4878void LocationsBuilderX86::VisitUnresolvedInstanceFieldSet(
4879 HUnresolvedInstanceFieldSet* instruction) {
4880 FieldAccessCallingConventionX86 calling_convention;
4881 codegen_->CreateUnresolvedFieldLocationSummary(
4882 instruction, instruction->GetFieldType(), calling_convention);
4883}
4884
4885void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldSet(
4886 HUnresolvedInstanceFieldSet* instruction) {
4887 FieldAccessCallingConventionX86 calling_convention;
4888 codegen_->GenerateUnresolvedFieldAccess(instruction,
4889 instruction->GetFieldType(),
4890 instruction->GetFieldIndex(),
4891 instruction->GetDexPc(),
4892 calling_convention);
4893}
4894
4895void LocationsBuilderX86::VisitUnresolvedStaticFieldGet(
4896 HUnresolvedStaticFieldGet* instruction) {
4897 FieldAccessCallingConventionX86 calling_convention;
4898 codegen_->CreateUnresolvedFieldLocationSummary(
4899 instruction, instruction->GetFieldType(), calling_convention);
4900}
4901
4902void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldGet(
4903 HUnresolvedStaticFieldGet* instruction) {
4904 FieldAccessCallingConventionX86 calling_convention;
4905 codegen_->GenerateUnresolvedFieldAccess(instruction,
4906 instruction->GetFieldType(),
4907 instruction->GetFieldIndex(),
4908 instruction->GetDexPc(),
4909 calling_convention);
4910}
4911
4912void LocationsBuilderX86::VisitUnresolvedStaticFieldSet(
4913 HUnresolvedStaticFieldSet* instruction) {
4914 FieldAccessCallingConventionX86 calling_convention;
4915 codegen_->CreateUnresolvedFieldLocationSummary(
4916 instruction, instruction->GetFieldType(), calling_convention);
4917}
4918
4919void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldSet(
4920 HUnresolvedStaticFieldSet* instruction) {
4921 FieldAccessCallingConventionX86 calling_convention;
4922 codegen_->GenerateUnresolvedFieldAccess(instruction,
4923 instruction->GetFieldType(),
4924 instruction->GetFieldIndex(),
4925 instruction->GetDexPc(),
4926 calling_convention);
4927}
4928
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004929void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko3b7537b2016-09-13 11:56:01 +00004930 LocationSummary* locations = codegen_->CreateNullCheckLocations(instruction);
4931 if (!codegen_->GetCompilerOptions().GetImplicitNullChecks()) {
4932 // Explicit null checks can use any location.
4933 locations->SetInAt(0, Location::Any());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004934 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004935}
4936
Calin Juravle2ae48182016-03-16 14:05:09 +00004937void CodeGeneratorX86::GenerateImplicitNullCheck(HNullCheck* instruction) {
4938 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004939 return;
4940 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004941 LocationSummary* locations = instruction->GetLocations();
4942 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00004943
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004944 __ testl(EAX, Address(obj.AsRegister<Register>(), 0));
Calin Juravle2ae48182016-03-16 14:05:09 +00004945 RecordPcInfo(instruction, instruction->GetDexPc());
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004946}
4947
Calin Juravle2ae48182016-03-16 14:05:09 +00004948void CodeGeneratorX86::GenerateExplicitNullCheck(HNullCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07004949 SlowPathCode* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00004950 AddSlowPath(slow_path);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004951
4952 LocationSummary* locations = instruction->GetLocations();
4953 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004954
4955 if (obj.IsRegister()) {
Mark Mendell42514f62015-03-31 11:34:22 -04004956 __ testl(obj.AsRegister<Register>(), obj.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004957 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004958 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004959 } else {
4960 DCHECK(obj.IsConstant()) << obj;
David Brazdil77a48ae2015-09-15 12:34:04 +00004961 DCHECK(obj.GetConstant()->IsNullConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004962 __ jmp(slow_path->GetEntryLabel());
4963 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004964 }
4965 __ j(kEqual, slow_path->GetEntryLabel());
4966}
4967
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004968void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00004969 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004970}
4971
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004972void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004973 bool object_array_get_with_read_barrier =
4974 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004975 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00004976 new (GetGraph()->GetArena()) LocationSummary(instruction,
4977 object_array_get_with_read_barrier ?
4978 LocationSummary::kCallOnSlowPath :
4979 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01004980 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
4981 locations->SetCustomSlowPathCallerSaves(RegisterSet()); // No caller-save registers.
4982 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004983 locations->SetInAt(0, Location::RequiresRegister());
4984 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004985 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4986 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4987 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004988 // The output overlaps in case of long: we don't want the low move
4989 // to overwrite the array's location. Likewise, in the case of an
4990 // object array get with read barriers enabled, we do not want the
4991 // move to overwrite the array's location, as we need it to emit
4992 // the read barrier.
4993 locations->SetOut(
4994 Location::RequiresRegister(),
4995 (instruction->GetType() == Primitive::kPrimLong || object_array_get_with_read_barrier) ?
4996 Location::kOutputOverlap :
4997 Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004998 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004999}
5000
5001void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
5002 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005003 Location obj_loc = locations->InAt(0);
5004 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005005 Location index = locations->InAt(1);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005006 Location out_loc = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005007 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005008
Calin Juravle77520bc2015-01-12 18:45:46 +00005009 Primitive::Type type = instruction->GetType();
5010 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005011 case Primitive::kPrimBoolean: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005012 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005013 __ movzxb(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005014 break;
5015 }
5016
5017 case Primitive::kPrimByte: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005018 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005019 __ movsxb(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005020 break;
5021 }
5022
5023 case Primitive::kPrimShort: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005024 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005025 __ movsxw(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005026 break;
5027 }
5028
5029 case Primitive::kPrimChar: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005030 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005031 __ movzxw(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005032 break;
5033 }
5034
Roland Levillain7c1559a2015-12-15 10:55:36 +00005035 case Primitive::kPrimInt: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005036 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005037 __ movl(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005038 break;
5039 }
5040
Roland Levillain7c1559a2015-12-15 10:55:36 +00005041 case Primitive::kPrimNot: {
5042 static_assert(
5043 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
5044 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain7c1559a2015-12-15 10:55:36 +00005045 // /* HeapReference<Object> */ out =
5046 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
5047 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005048 // Note that a potential implicit null check is handled in this
5049 // CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier call.
5050 codegen_->GenerateArrayLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00005051 instruction, out_loc, obj, data_offset, index, /* needs_null_check */ true);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005052 } else {
5053 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005054 __ movl(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset));
5055 codegen_->MaybeRecordImplicitNullCheck(instruction);
5056 // If read barriers are enabled, emit read barriers other than
5057 // Baker's using a slow path (and also unpoison the loaded
5058 // reference, if heap poisoning is enabled).
Roland Levillain7c1559a2015-12-15 10:55:36 +00005059 if (index.IsConstant()) {
5060 uint32_t offset =
5061 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Roland Levillain7c1559a2015-12-15 10:55:36 +00005062 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
5063 } else {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005064 codegen_->MaybeGenerateReadBarrierSlow(
5065 instruction, out_loc, out_loc, obj_loc, data_offset, index);
5066 }
5067 }
5068 break;
5069 }
5070
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005071 case Primitive::kPrimLong: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005072 DCHECK_NE(obj, out_loc.AsRegisterPairLow<Register>());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005073 __ movl(out_loc.AsRegisterPairLow<Register>(),
5074 CodeGeneratorX86::ArrayAddress(obj, index, TIMES_8, data_offset));
5075 codegen_->MaybeRecordImplicitNullCheck(instruction);
5076 __ movl(out_loc.AsRegisterPairHigh<Register>(),
5077 CodeGeneratorX86::ArrayAddress(obj, index, TIMES_8, data_offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005078 break;
5079 }
5080
Mark Mendell7c8d0092015-01-26 11:21:33 -05005081 case Primitive::kPrimFloat: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005082 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005083 __ movss(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005084 break;
5085 }
5086
5087 case Primitive::kPrimDouble: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005088 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005089 __ movsd(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_8, data_offset));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005090 break;
5091 }
5092
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005093 case Primitive::kPrimVoid:
Calin Juravle77520bc2015-01-12 18:45:46 +00005094 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07005095 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005096 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005097
Roland Levillain7c1559a2015-12-15 10:55:36 +00005098 if (type == Primitive::kPrimNot || type == Primitive::kPrimLong) {
5099 // Potential implicit null checks, in the case of reference or
5100 // long arrays, are handled in the previous switch statement.
5101 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00005102 codegen_->MaybeRecordImplicitNullCheck(instruction);
5103 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005104}
5105
5106void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01005107 Primitive::Type value_type = instruction->GetComponentType();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005108
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005109 bool needs_write_barrier =
5110 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Roland Levillain0d5a2812015-11-13 10:07:31 +00005111 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005112
Nicolas Geoffray39468442014-09-02 15:17:15 +01005113 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
5114 instruction,
Vladimir Marko8d49fd72016-08-25 15:20:47 +01005115 may_need_runtime_call_for_type_check ?
Roland Levillain0d5a2812015-11-13 10:07:31 +00005116 LocationSummary::kCallOnSlowPath :
5117 LocationSummary::kNoCall);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005118
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005119 bool is_byte_type = (value_type == Primitive::kPrimBoolean)
5120 || (value_type == Primitive::kPrimByte);
5121 // We need the inputs to be different than the output in case of long operation.
5122 // In case of a byte operation, the register allocator does not support multiple
5123 // inputs that die at entry with one in a specific register.
5124 locations->SetInAt(0, Location::RequiresRegister());
5125 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
5126 if (is_byte_type) {
5127 // Ensure the value is in a byte register.
5128 locations->SetInAt(2, Location::ByteRegisterOrConstant(EAX, instruction->InputAt(2)));
5129 } else if (Primitive::IsFloatingPointType(value_type)) {
Mark Mendell81489372015-11-04 11:30:41 -05005130 locations->SetInAt(2, Location::FpuRegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005131 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005132 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
5133 }
5134 if (needs_write_barrier) {
5135 // Temporary registers for the write barrier.
5136 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
5137 // Ensure the card is in a byte register.
Roland Levillain4f6b0b52015-11-23 19:29:22 +00005138 locations->AddTemp(Location::RegisterLocation(ECX));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005139 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005140}
5141
5142void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
5143 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005144 Location array_loc = locations->InAt(0);
5145 Register array = array_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005146 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005147 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005148 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005149 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
5150 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5151 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005152 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005153 bool needs_write_barrier =
5154 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005155
5156 switch (value_type) {
5157 case Primitive::kPrimBoolean:
5158 case Primitive::kPrimByte: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005159 uint32_t offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005160 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_1, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005161 if (value.IsRegister()) {
5162 __ movb(address, value.AsRegister<ByteRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005163 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005164 __ movb(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005165 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005166 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005167 break;
5168 }
5169
5170 case Primitive::kPrimShort:
5171 case Primitive::kPrimChar: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005172 uint32_t offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005173 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_2, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005174 if (value.IsRegister()) {
5175 __ movw(address, value.AsRegister<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005176 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005177 __ movw(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005178 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005179 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005180 break;
5181 }
5182
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005183 case Primitive::kPrimNot: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005184 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005185 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_4, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005186
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005187 if (!value.IsRegister()) {
5188 // Just setting null.
5189 DCHECK(instruction->InputAt(2)->IsNullConstant());
5190 DCHECK(value.IsConstant()) << value;
5191 __ movl(address, Immediate(0));
Calin Juravle77520bc2015-01-12 18:45:46 +00005192 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005193 DCHECK(!needs_write_barrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005194 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005195 break;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005196 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005197
5198 DCHECK(needs_write_barrier);
5199 Register register_value = value.AsRegister<Register>();
Roland Levillain16d9f942016-08-25 17:27:56 +01005200 // We cannot use a NearLabel for `done`, as its range may be too
5201 // short when Baker read barriers are enabled.
5202 Label done;
5203 NearLabel not_null, do_put;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005204 SlowPathCode* slow_path = nullptr;
Roland Levillain16d9f942016-08-25 17:27:56 +01005205 Location temp_loc = locations->GetTemp(0);
5206 Register temp = temp_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005207 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005208 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathX86(instruction);
5209 codegen_->AddSlowPath(slow_path);
5210 if (instruction->GetValueCanBeNull()) {
5211 __ testl(register_value, register_value);
5212 __ j(kNotEqual, &not_null);
5213 __ movl(address, Immediate(0));
5214 codegen_->MaybeRecordImplicitNullCheck(instruction);
5215 __ jmp(&done);
5216 __ Bind(&not_null);
5217 }
5218
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005219 // Note that when Baker read barriers are enabled, the type
5220 // checks are performed without read barriers. This is fine,
5221 // even in the case where a class object is in the from-space
5222 // after the flip, as a comparison involving such a type would
5223 // not produce a false positive; it may of course produce a
5224 // false negative, in which case we would take the ArraySet
5225 // slow path.
Roland Levillain16d9f942016-08-25 17:27:56 +01005226
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005227 // /* HeapReference<Class> */ temp = array->klass_
5228 __ movl(temp, Address(array, class_offset));
5229 codegen_->MaybeRecordImplicitNullCheck(instruction);
5230 __ MaybeUnpoisonHeapReference(temp);
Roland Levillain16d9f942016-08-25 17:27:56 +01005231
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005232 // /* HeapReference<Class> */ temp = temp->component_type_
5233 __ movl(temp, Address(temp, component_offset));
5234 // If heap poisoning is enabled, no need to unpoison `temp`
5235 // nor the object reference in `register_value->klass`, as
5236 // we are comparing two poisoned references.
5237 __ cmpl(temp, Address(register_value, class_offset));
Roland Levillain16d9f942016-08-25 17:27:56 +01005238
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005239 if (instruction->StaticTypeOfArrayIsObjectArray()) {
5240 __ j(kEqual, &do_put);
5241 // If heap poisoning is enabled, the `temp` reference has
5242 // not been unpoisoned yet; unpoison it now.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005243 __ MaybeUnpoisonHeapReference(temp);
5244
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005245 // If heap poisoning is enabled, no need to unpoison the
5246 // heap reference loaded below, as it is only used for a
5247 // comparison with null.
5248 __ cmpl(Address(temp, super_offset), Immediate(0));
5249 __ j(kNotEqual, slow_path->GetEntryLabel());
5250 __ Bind(&do_put);
5251 } else {
5252 __ j(kNotEqual, slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005253 }
5254 }
5255
5256 if (kPoisonHeapReferences) {
5257 __ movl(temp, register_value);
5258 __ PoisonHeapReference(temp);
5259 __ movl(address, temp);
5260 } else {
5261 __ movl(address, register_value);
5262 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005263 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005264 codegen_->MaybeRecordImplicitNullCheck(instruction);
5265 }
5266
5267 Register card = locations->GetTemp(1).AsRegister<Register>();
5268 codegen_->MarkGCCard(
5269 temp, card, array, value.AsRegister<Register>(), instruction->GetValueCanBeNull());
5270 __ Bind(&done);
5271
5272 if (slow_path != nullptr) {
5273 __ Bind(slow_path->GetExitLabel());
5274 }
5275
5276 break;
5277 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005278
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005279 case Primitive::kPrimInt: {
5280 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005281 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_4, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005282 if (value.IsRegister()) {
5283 __ movl(address, value.AsRegister<Register>());
5284 } else {
5285 DCHECK(value.IsConstant()) << value;
5286 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
5287 __ movl(address, Immediate(v));
5288 }
5289 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005290 break;
5291 }
5292
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005293 case Primitive::kPrimLong: {
5294 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005295 if (value.IsRegisterPair()) {
5296 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset),
5297 value.AsRegisterPairLow<Register>());
5298 codegen_->MaybeRecordImplicitNullCheck(instruction);
5299 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset + kX86WordSize),
5300 value.AsRegisterPairHigh<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005301 } else {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005302 DCHECK(value.IsConstant());
5303 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
5304 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset),
5305 Immediate(Low32Bits(val)));
5306 codegen_->MaybeRecordImplicitNullCheck(instruction);
5307 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset + kX86WordSize),
5308 Immediate(High32Bits(val)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005309 }
5310 break;
5311 }
5312
Mark Mendell7c8d0092015-01-26 11:21:33 -05005313 case Primitive::kPrimFloat: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005314 uint32_t offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005315 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_4, offset);
Mark Mendell81489372015-11-04 11:30:41 -05005316 if (value.IsFpuRegister()) {
5317 __ movss(address, value.AsFpuRegister<XmmRegister>());
5318 } else {
5319 DCHECK(value.IsConstant());
5320 int32_t v = bit_cast<int32_t, float>(value.GetConstant()->AsFloatConstant()->GetValue());
5321 __ movl(address, Immediate(v));
5322 }
5323 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005324 break;
5325 }
5326
5327 case Primitive::kPrimDouble: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005328 uint32_t offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005329 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, offset);
Mark Mendell81489372015-11-04 11:30:41 -05005330 if (value.IsFpuRegister()) {
5331 __ movsd(address, value.AsFpuRegister<XmmRegister>());
5332 } else {
5333 DCHECK(value.IsConstant());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005334 Address address_hi =
5335 CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, offset + kX86WordSize);
Mark Mendell81489372015-11-04 11:30:41 -05005336 int64_t v = bit_cast<int64_t, double>(value.GetConstant()->AsDoubleConstant()->GetValue());
5337 __ movl(address, Immediate(Low32Bits(v)));
5338 codegen_->MaybeRecordImplicitNullCheck(instruction);
5339 __ movl(address_hi, Immediate(High32Bits(v)));
5340 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005341 break;
5342 }
5343
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005344 case Primitive::kPrimVoid:
5345 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07005346 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005347 }
5348}
5349
5350void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
5351 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005352 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellee8d9712016-07-12 11:13:15 -04005353 if (!instruction->IsEmittedAtUseSite()) {
5354 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5355 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005356}
5357
5358void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
Mark Mendellee8d9712016-07-12 11:13:15 -04005359 if (instruction->IsEmittedAtUseSite()) {
5360 return;
5361 }
5362
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005363 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01005364 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00005365 Register obj = locations->InAt(0).AsRegister<Register>();
5366 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005367 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005368 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005369}
5370
5371void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00005372 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
5373 ? LocationSummary::kCallOnSlowPath
5374 : LocationSummary::kNoCall;
5375 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Mark Mendellf60c90b2015-03-04 15:12:59 -05005376 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendellee8d9712016-07-12 11:13:15 -04005377 HInstruction* length = instruction->InputAt(1);
5378 if (!length->IsEmittedAtUseSite()) {
5379 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
5380 }
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005381 if (instruction->HasUses()) {
5382 locations->SetOut(Location::SameAsFirstInput());
5383 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005384}
5385
5386void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
5387 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05005388 Location index_loc = locations->InAt(0);
5389 Location length_loc = locations->InAt(1);
Andreas Gampe85b62f22015-09-09 13:15:38 -07005390 SlowPathCode* slow_path =
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01005391 new (GetGraph()->GetArena()) BoundsCheckSlowPathX86(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005392
Mark Mendell99dbd682015-04-22 16:18:52 -04005393 if (length_loc.IsConstant()) {
5394 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
5395 if (index_loc.IsConstant()) {
5396 // BCE will remove the bounds check if we are guarenteed to pass.
5397 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5398 if (index < 0 || index >= length) {
5399 codegen_->AddSlowPath(slow_path);
5400 __ jmp(slow_path->GetEntryLabel());
5401 } else {
5402 // Some optimization after BCE may have generated this, and we should not
5403 // generate a bounds check if it is a valid range.
5404 }
5405 return;
5406 }
5407
5408 // We have to reverse the jump condition because the length is the constant.
5409 Register index_reg = index_loc.AsRegister<Register>();
5410 __ cmpl(index_reg, Immediate(length));
5411 codegen_->AddSlowPath(slow_path);
5412 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005413 } else {
Mark Mendellee8d9712016-07-12 11:13:15 -04005414 HInstruction* array_length = instruction->InputAt(1);
5415 if (array_length->IsEmittedAtUseSite()) {
5416 // Address the length field in the array.
5417 DCHECK(array_length->IsArrayLength());
5418 uint32_t len_offset = CodeGenerator::GetArrayLengthOffset(array_length->AsArrayLength());
5419 Location array_loc = array_length->GetLocations()->InAt(0);
5420 Address array_len(array_loc.AsRegister<Register>(), len_offset);
5421 if (index_loc.IsConstant()) {
5422 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5423 __ cmpl(array_len, Immediate(value));
5424 } else {
5425 __ cmpl(array_len, index_loc.AsRegister<Register>());
5426 }
5427 codegen_->MaybeRecordImplicitNullCheck(array_length);
Mark Mendell99dbd682015-04-22 16:18:52 -04005428 } else {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005429 codegen_->GenerateIntCompare(length_loc, index_loc);
Mark Mendell99dbd682015-04-22 16:18:52 -04005430 }
5431 codegen_->AddSlowPath(slow_path);
5432 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005433 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005434}
5435
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005436void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005437 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005438}
5439
5440void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005441 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5442}
5443
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005444void LocationsBuilderX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Marko70e97462016-08-09 11:04:26 +01005445 LocationSummary* locations =
5446 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
5447 locations->SetCustomSlowPathCallerSaves(RegisterSet()); // No caller-save registers.
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005448}
5449
5450void InstructionCodeGeneratorX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005451 HBasicBlock* block = instruction->GetBlock();
5452 if (block->GetLoopInformation() != nullptr) {
5453 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5454 // The back edge will generate the suspend check.
5455 return;
5456 }
5457 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5458 // The goto will generate the suspend check.
5459 return;
5460 }
5461 GenerateSuspendCheck(instruction, nullptr);
5462}
5463
5464void InstructionCodeGeneratorX86::GenerateSuspendCheck(HSuspendCheck* instruction,
5465 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005466 SuspendCheckSlowPathX86* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005467 down_cast<SuspendCheckSlowPathX86*>(instruction->GetSlowPath());
5468 if (slow_path == nullptr) {
5469 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathX86(instruction, successor);
5470 instruction->SetSlowPath(slow_path);
5471 codegen_->AddSlowPath(slow_path);
5472 if (successor != nullptr) {
5473 DCHECK(successor->IsLoopHeader());
5474 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
5475 }
5476 } else {
5477 DCHECK_EQ(slow_path->GetSuccessor(), successor);
5478 }
5479
Andreas Gampe542451c2016-07-26 09:02:02 -07005480 __ fs()->cmpw(Address::Absolute(Thread::ThreadFlagsOffset<kX86PointerSize>().Int32Value()),
Roland Levillain7c1559a2015-12-15 10:55:36 +00005481 Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005482 if (successor == nullptr) {
5483 __ j(kNotEqual, slow_path->GetEntryLabel());
5484 __ Bind(slow_path->GetReturnLabel());
5485 } else {
5486 __ j(kEqual, codegen_->GetLabelOf(successor));
5487 __ jmp(slow_path->GetEntryLabel());
5488 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005489}
5490
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005491X86Assembler* ParallelMoveResolverX86::GetAssembler() const {
5492 return codegen_->GetAssembler();
5493}
5494
Mark Mendell7c8d0092015-01-26 11:21:33 -05005495void ParallelMoveResolverX86::MoveMemoryToMemory32(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005496 ScratchRegisterScope ensure_scratch(
5497 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5498 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5499 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5500 __ movl(temp_reg, Address(ESP, src + stack_offset));
5501 __ movl(Address(ESP, dst + stack_offset), temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005502}
5503
5504void ParallelMoveResolverX86::MoveMemoryToMemory64(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005505 ScratchRegisterScope ensure_scratch(
5506 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5507 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5508 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5509 __ movl(temp_reg, Address(ESP, src + stack_offset));
5510 __ movl(Address(ESP, dst + stack_offset), temp_reg);
5511 __ movl(temp_reg, Address(ESP, src + stack_offset + kX86WordSize));
5512 __ movl(Address(ESP, dst + stack_offset + kX86WordSize), temp_reg);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005513}
5514
5515void ParallelMoveResolverX86::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005516 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005517 Location source = move->GetSource();
5518 Location destination = move->GetDestination();
5519
5520 if (source.IsRegister()) {
5521 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005522 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
David Brazdil74eb1b22015-12-14 11:44:01 +00005523 } else if (destination.IsFpuRegister()) {
5524 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005525 } else {
5526 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00005527 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005528 }
David Brazdil74eb1b22015-12-14 11:44:01 +00005529 } else if (source.IsRegisterPair()) {
5530 size_t elem_size = Primitive::ComponentSize(Primitive::kPrimInt);
5531 // Create stack space for 2 elements.
5532 __ subl(ESP, Immediate(2 * elem_size));
5533 __ movl(Address(ESP, 0), source.AsRegisterPairLow<Register>());
5534 __ movl(Address(ESP, elem_size), source.AsRegisterPairHigh<Register>());
5535 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
5536 // And remove the temporary stack space we allocated.
5537 __ addl(ESP, Immediate(2 * elem_size));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005538 } else if (source.IsFpuRegister()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00005539 if (destination.IsRegister()) {
5540 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
5541 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005542 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
David Brazdil74eb1b22015-12-14 11:44:01 +00005543 } else if (destination.IsRegisterPair()) {
5544 XmmRegister src_reg = source.AsFpuRegister<XmmRegister>();
5545 __ movd(destination.AsRegisterPairLow<Register>(), src_reg);
5546 __ psrlq(src_reg, Immediate(32));
5547 __ movd(destination.AsRegisterPairHigh<Register>(), src_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005548 } else if (destination.IsStackSlot()) {
5549 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
5550 } else {
5551 DCHECK(destination.IsDoubleStackSlot());
5552 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
5553 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005554 } else if (source.IsStackSlot()) {
5555 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005556 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005557 } else if (destination.IsFpuRegister()) {
5558 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005559 } else {
5560 DCHECK(destination.IsStackSlot());
Mark Mendell7c8d0092015-01-26 11:21:33 -05005561 MoveMemoryToMemory32(destination.GetStackIndex(), source.GetStackIndex());
5562 }
5563 } else if (source.IsDoubleStackSlot()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00005564 if (destination.IsRegisterPair()) {
5565 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
5566 __ movl(destination.AsRegisterPairHigh<Register>(),
5567 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
5568 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005569 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
5570 } else {
5571 DCHECK(destination.IsDoubleStackSlot()) << destination;
5572 MoveMemoryToMemory64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005573 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005574 } else if (source.IsConstant()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005575 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00005576 if (constant->IsIntConstant() || constant->IsNullConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05005577 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005578 if (destination.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05005579 if (value == 0) {
5580 __ xorl(destination.AsRegister<Register>(), destination.AsRegister<Register>());
5581 } else {
5582 __ movl(destination.AsRegister<Register>(), Immediate(value));
5583 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005584 } else {
5585 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell09b84632015-02-13 17:48:38 -05005586 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005587 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005588 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005589 float fp_value = constant->AsFloatConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00005590 int32_t value = bit_cast<int32_t, float>(fp_value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005591 Immediate imm(value);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005592 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005593 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
5594 if (value == 0) {
5595 // Easy handling of 0.0.
5596 __ xorps(dest, dest);
5597 } else {
5598 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005599 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5600 Register temp = static_cast<Register>(ensure_scratch.GetRegister());
5601 __ movl(temp, Immediate(value));
5602 __ movd(dest, temp);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005603 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005604 } else {
5605 DCHECK(destination.IsStackSlot()) << destination;
5606 __ movl(Address(ESP, destination.GetStackIndex()), imm);
5607 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005608 } else if (constant->IsLongConstant()) {
5609 int64_t value = constant->AsLongConstant()->GetValue();
5610 int32_t low_value = Low32Bits(value);
5611 int32_t high_value = High32Bits(value);
5612 Immediate low(low_value);
5613 Immediate high(high_value);
5614 if (destination.IsDoubleStackSlot()) {
5615 __ movl(Address(ESP, destination.GetStackIndex()), low);
5616 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
5617 } else {
5618 __ movl(destination.AsRegisterPairLow<Register>(), low);
5619 __ movl(destination.AsRegisterPairHigh<Register>(), high);
5620 }
5621 } else {
5622 DCHECK(constant->IsDoubleConstant());
5623 double dbl_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00005624 int64_t value = bit_cast<int64_t, double>(dbl_value);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005625 int32_t low_value = Low32Bits(value);
5626 int32_t high_value = High32Bits(value);
5627 Immediate low(low_value);
5628 Immediate high(high_value);
5629 if (destination.IsFpuRegister()) {
5630 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
5631 if (value == 0) {
5632 // Easy handling of 0.0.
5633 __ xorpd(dest, dest);
5634 } else {
5635 __ pushl(high);
5636 __ pushl(low);
5637 __ movsd(dest, Address(ESP, 0));
5638 __ addl(ESP, Immediate(8));
5639 }
5640 } else {
5641 DCHECK(destination.IsDoubleStackSlot()) << destination;
5642 __ movl(Address(ESP, destination.GetStackIndex()), low);
5643 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
5644 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005645 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005646 } else {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00005647 LOG(FATAL) << "Unimplemented move: " << destination << " <- " << source;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005648 }
5649}
5650
Mark Mendella5c19ce2015-04-01 12:51:05 -04005651void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005652 Register suggested_scratch = reg == EAX ? EBX : EAX;
5653 ScratchRegisterScope ensure_scratch(
5654 this, reg, suggested_scratch, codegen_->GetNumberOfCoreRegisters());
5655
5656 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5657 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset));
5658 __ movl(Address(ESP, mem + stack_offset), reg);
5659 __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005660}
5661
Mark Mendell7c8d0092015-01-26 11:21:33 -05005662void ParallelMoveResolverX86::Exchange32(XmmRegister reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005663 ScratchRegisterScope ensure_scratch(
5664 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5665
5666 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5667 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5668 __ movl(temp_reg, Address(ESP, mem + stack_offset));
5669 __ movss(Address(ESP, mem + stack_offset), reg);
5670 __ movd(reg, temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005671}
5672
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005673void ParallelMoveResolverX86::Exchange(int mem1, int mem2) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005674 ScratchRegisterScope ensure_scratch1(
5675 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005676
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005677 Register suggested_scratch = ensure_scratch1.GetRegister() == EAX ? EBX : EAX;
5678 ScratchRegisterScope ensure_scratch2(
5679 this, ensure_scratch1.GetRegister(), suggested_scratch, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005680
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005681 int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0;
5682 stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0;
5683 __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset));
5684 __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset));
5685 __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister()));
5686 __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005687}
5688
5689void ParallelMoveResolverX86::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005690 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005691 Location source = move->GetSource();
5692 Location destination = move->GetDestination();
5693
5694 if (source.IsRegister() && destination.IsRegister()) {
Mark Mendell90979812015-07-28 16:41:21 -04005695 // Use XOR swap algorithm to avoid serializing XCHG instruction or using a temporary.
5696 DCHECK_NE(destination.AsRegister<Register>(), source.AsRegister<Register>());
5697 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
5698 __ xorl(source.AsRegister<Register>(), destination.AsRegister<Register>());
5699 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005700 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005701 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005702 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005703 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005704 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
5705 Exchange(destination.GetStackIndex(), source.GetStackIndex());
Mark Mendell7c8d0092015-01-26 11:21:33 -05005706 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
5707 // Use XOR Swap algorithm to avoid a temporary.
5708 DCHECK_NE(source.reg(), destination.reg());
5709 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
5710 __ xorpd(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
5711 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
5712 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
5713 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
5714 } else if (destination.IsFpuRegister() && source.IsStackSlot()) {
5715 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005716 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
5717 // Take advantage of the 16 bytes in the XMM register.
5718 XmmRegister reg = source.AsFpuRegister<XmmRegister>();
5719 Address stack(ESP, destination.GetStackIndex());
5720 // Load the double into the high doubleword.
5721 __ movhpd(reg, stack);
5722
5723 // Store the low double into the destination.
5724 __ movsd(stack, reg);
5725
5726 // Move the high double to the low double.
5727 __ psrldq(reg, Immediate(8));
5728 } else if (destination.IsFpuRegister() && source.IsDoubleStackSlot()) {
5729 // Take advantage of the 16 bytes in the XMM register.
5730 XmmRegister reg = destination.AsFpuRegister<XmmRegister>();
5731 Address stack(ESP, source.GetStackIndex());
5732 // Load the double into the high doubleword.
5733 __ movhpd(reg, stack);
5734
5735 // Store the low double into the destination.
5736 __ movsd(stack, reg);
5737
5738 // Move the high double to the low double.
5739 __ psrldq(reg, Immediate(8));
5740 } else if (destination.IsDoubleStackSlot() && source.IsDoubleStackSlot()) {
5741 Exchange(destination.GetStackIndex(), source.GetStackIndex());
5742 Exchange(destination.GetHighStackIndex(kX86WordSize), source.GetHighStackIndex(kX86WordSize));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005743 } else {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005744 LOG(FATAL) << "Unimplemented: source: " << source << ", destination: " << destination;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005745 }
5746}
5747
5748void ParallelMoveResolverX86::SpillScratch(int reg) {
5749 __ pushl(static_cast<Register>(reg));
5750}
5751
5752void ParallelMoveResolverX86::RestoreScratch(int reg) {
5753 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005754}
5755
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005756HLoadClass::LoadKind CodeGeneratorX86::GetSupportedLoadClassKind(
5757 HLoadClass::LoadKind desired_class_load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005758 switch (desired_class_load_kind) {
5759 case HLoadClass::LoadKind::kReferrersClass:
5760 break;
5761 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
5762 DCHECK(!GetCompilerOptions().GetCompilePic());
5763 break;
5764 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
5765 DCHECK(GetCompilerOptions().GetCompilePic());
5766 FALLTHROUGH_INTENDED;
5767 case HLoadClass::LoadKind::kDexCachePcRelative:
5768 DCHECK(!Runtime::Current()->UseJitCompilation()); // Note: boot image is also non-JIT.
5769 // We disable pc-relative load when there is an irreducible loop, as the optimization
5770 // is incompatible with it.
5771 // TODO: Create as many X86ComputeBaseMethodAddress instructions as needed for methods
5772 // with irreducible loops.
5773 if (GetGraph()->HasIrreducibleLoops()) {
5774 return HLoadClass::LoadKind::kDexCacheViaMethod;
5775 }
5776 break;
5777 case HLoadClass::LoadKind::kBootImageAddress:
5778 break;
5779 case HLoadClass::LoadKind::kDexCacheAddress:
5780 DCHECK(Runtime::Current()->UseJitCompilation());
5781 break;
5782 case HLoadClass::LoadKind::kDexCacheViaMethod:
5783 break;
5784 }
5785 return desired_class_load_kind;
5786}
5787
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005788void LocationsBuilderX86::VisitLoadClass(HLoadClass* cls) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005789 if (cls->NeedsAccessCheck()) {
5790 InvokeRuntimeCallingConvention calling_convention;
5791 CodeGenerator::CreateLoadClassLocationSummary(
5792 cls,
5793 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
5794 Location::RegisterLocation(EAX),
5795 /* code_generator_supports_read_barrier */ true);
5796 return;
5797 }
5798
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005799 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
5800 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005801 ? LocationSummary::kCallOnSlowPath
5802 : LocationSummary::kNoCall;
5803 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005804 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Vladimir Marko70e97462016-08-09 11:04:26 +01005805 locations->SetCustomSlowPathCallerSaves(RegisterSet()); // No caller-save registers.
5806 }
5807
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005808 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
5809 if (load_kind == HLoadClass::LoadKind::kReferrersClass ||
5810 load_kind == HLoadClass::LoadKind::kDexCacheViaMethod ||
5811 load_kind == HLoadClass::LoadKind::kBootImageLinkTimePcRelative ||
5812 load_kind == HLoadClass::LoadKind::kDexCachePcRelative) {
5813 locations->SetInAt(0, Location::RequiresRegister());
5814 }
5815 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005816}
5817
5818void InstructionCodeGeneratorX86::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005819 LocationSummary* locations = cls->GetLocations();
Calin Juravle98893e12015-10-02 21:05:03 +01005820 if (cls->NeedsAccessCheck()) {
5821 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex());
Serban Constantinescuba45db02016-07-12 22:53:02 +01005822 codegen_->InvokeRuntime(kQuickInitializeTypeAndVerifyAccess, cls, cls->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00005823 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Calin Juravle580b6092015-10-06 17:35:58 +01005824 return;
5825 }
5826
Roland Levillain0d5a2812015-11-13 10:07:31 +00005827 Location out_loc = locations->Out();
5828 Register out = out_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005829
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005830 bool generate_null_check = false;
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005831 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005832 switch (cls->GetLoadKind()) {
5833 case HLoadClass::LoadKind::kReferrersClass: {
5834 DCHECK(!cls->CanCallRuntime());
5835 DCHECK(!cls->MustGenerateClinitCheck());
5836 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5837 Register current_method = locations->InAt(0).AsRegister<Register>();
5838 GenerateGcRootFieldLoad(
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005839 cls,
5840 out_loc,
5841 Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()),
5842 /*fixup_label*/ nullptr,
5843 requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005844 break;
5845 }
5846 case HLoadClass::LoadKind::kBootImageLinkTimeAddress: {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005847 DCHECK(!requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005848 __ movl(out, Immediate(/* placeholder */ 0));
5849 codegen_->RecordTypePatch(cls);
5850 break;
5851 }
5852 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005853 DCHECK(!requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005854 Register method_address = locations->InAt(0).AsRegister<Register>();
5855 __ leal(out, Address(method_address, CodeGeneratorX86::kDummy32BitOffset));
5856 codegen_->RecordTypePatch(cls);
5857 break;
5858 }
5859 case HLoadClass::LoadKind::kBootImageAddress: {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005860 DCHECK(!requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005861 DCHECK_NE(cls->GetAddress(), 0u);
5862 uint32_t address = dchecked_integral_cast<uint32_t>(cls->GetAddress());
5863 __ movl(out, Immediate(address));
5864 codegen_->RecordSimplePatch();
5865 break;
5866 }
5867 case HLoadClass::LoadKind::kDexCacheAddress: {
5868 DCHECK_NE(cls->GetAddress(), 0u);
5869 uint32_t address = dchecked_integral_cast<uint32_t>(cls->GetAddress());
5870 // /* GcRoot<mirror::Class> */ out = *address
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005871 GenerateGcRootFieldLoad(cls,
5872 out_loc,
5873 Address::Absolute(address),
5874 /*fixup_label*/ nullptr,
5875 requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005876 generate_null_check = !cls->IsInDexCache();
5877 break;
5878 }
5879 case HLoadClass::LoadKind::kDexCachePcRelative: {
5880 Register base_reg = locations->InAt(0).AsRegister<Register>();
5881 uint32_t offset = cls->GetDexCacheElementOffset();
5882 Label* fixup_label = codegen_->NewPcRelativeDexCacheArrayPatch(cls->GetDexFile(), offset);
5883 // /* GcRoot<mirror::Class> */ out = *(base + offset) /* PC-relative */
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005884 GenerateGcRootFieldLoad(cls,
5885 out_loc,
5886 Address(base_reg, CodeGeneratorX86::kDummy32BitOffset),
5887 fixup_label,
5888 requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005889 generate_null_check = !cls->IsInDexCache();
5890 break;
5891 }
5892 case HLoadClass::LoadKind::kDexCacheViaMethod: {
5893 // /* GcRoot<mirror::Class>[] */ out =
5894 // current_method.ptr_sized_fields_->dex_cache_resolved_types_
5895 Register current_method = locations->InAt(0).AsRegister<Register>();
5896 __ movl(out, Address(current_method,
5897 ArtMethod::DexCacheResolvedTypesOffset(kX86PointerSize).Int32Value()));
5898 // /* GcRoot<mirror::Class> */ out = out[type_index]
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005899 GenerateGcRootFieldLoad(cls,
5900 out_loc,
5901 Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())),
5902 /*fixup_label*/ nullptr,
5903 requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005904 generate_null_check = !cls->IsInDexCache();
5905 break;
5906 }
5907 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005908
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005909 if (generate_null_check || cls->MustGenerateClinitCheck()) {
5910 DCHECK(cls->CanCallRuntime());
5911 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
5912 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
5913 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005914
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005915 if (generate_null_check) {
5916 __ testl(out, out);
5917 __ j(kEqual, slow_path->GetEntryLabel());
5918 }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005919
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005920 if (cls->MustGenerateClinitCheck()) {
5921 GenerateClassInitializationCheck(slow_path, out);
5922 } else {
5923 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005924 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005925 }
5926}
5927
5928void LocationsBuilderX86::VisitClinitCheck(HClinitCheck* check) {
5929 LocationSummary* locations =
5930 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
5931 locations->SetInAt(0, Location::RequiresRegister());
5932 if (check->HasUses()) {
5933 locations->SetOut(Location::SameAsFirstInput());
5934 }
5935}
5936
5937void InstructionCodeGeneratorX86::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005938 // We assume the class to not be null.
Andreas Gampe85b62f22015-09-09 13:15:38 -07005939 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005940 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005941 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00005942 GenerateClassInitializationCheck(slow_path,
5943 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005944}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005945
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005946void InstructionCodeGeneratorX86::GenerateClassInitializationCheck(
Andreas Gampe85b62f22015-09-09 13:15:38 -07005947 SlowPathCode* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005948 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
5949 Immediate(mirror::Class::kStatusInitialized));
5950 __ j(kLess, slow_path->GetEntryLabel());
5951 __ Bind(slow_path->GetExitLabel());
5952 // No need for memory fence, thanks to the X86 memory model.
5953}
5954
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005955HLoadString::LoadKind CodeGeneratorX86::GetSupportedLoadStringKind(
5956 HLoadString::LoadKind desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005957 switch (desired_string_load_kind) {
5958 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
5959 DCHECK(!GetCompilerOptions().GetCompilePic());
5960 break;
5961 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
5962 DCHECK(GetCompilerOptions().GetCompilePic());
5963 FALLTHROUGH_INTENDED;
5964 case HLoadString::LoadKind::kDexCachePcRelative:
Calin Juravleffc87072016-04-20 14:22:09 +01005965 DCHECK(!Runtime::Current()->UseJitCompilation()); // Note: boot image is also non-JIT.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005966 // We disable pc-relative load when there is an irreducible loop, as the optimization
5967 // is incompatible with it.
5968 // TODO: Create as many X86ComputeBaseMethodAddress instructions as needed for methods
5969 // with irreducible loops.
5970 if (GetGraph()->HasIrreducibleLoops()) {
5971 return HLoadString::LoadKind::kDexCacheViaMethod;
5972 }
5973 break;
5974 case HLoadString::LoadKind::kBootImageAddress:
5975 break;
5976 case HLoadString::LoadKind::kDexCacheAddress:
Calin Juravleffc87072016-04-20 14:22:09 +01005977 DCHECK(Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005978 break;
5979 case HLoadString::LoadKind::kDexCacheViaMethod:
5980 break;
5981 }
5982 return desired_string_load_kind;
5983}
5984
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005985void LocationsBuilderX86::VisitLoadString(HLoadString* load) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005986 LocationSummary::CallKind call_kind = (load->NeedsEnvironment() || kEmitCompilerReadBarrier)
Christina Wadsworth175d09b2016-08-31 16:26:01 -07005987 ? LocationSummary::kCallOnMainOnly
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005988 : LocationSummary::kNoCall;
5989 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005990 HLoadString::LoadKind load_kind = load->GetLoadKind();
5991 if (load_kind == HLoadString::LoadKind::kDexCacheViaMethod ||
5992 load_kind == HLoadString::LoadKind::kBootImageLinkTimePcRelative ||
5993 load_kind == HLoadString::LoadKind::kDexCachePcRelative) {
5994 locations->SetInAt(0, Location::RequiresRegister());
5995 }
Christina Wadsworth175d09b2016-08-31 16:26:01 -07005996 if (load_kind == HLoadString::LoadKind::kDexCacheViaMethod) {
5997 locations->SetOut(Location::RegisterLocation(EAX));
5998 } else {
5999 locations->SetOut(Location::RequiresRegister());
6000 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006001}
6002
6003void InstructionCodeGeneratorX86::VisitLoadString(HLoadString* load) {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01006004 LocationSummary* locations = load->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006005 Location out_loc = locations->Out();
6006 Register out = out_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006007
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006008 switch (load->GetLoadKind()) {
6009 case HLoadString::LoadKind::kBootImageLinkTimeAddress: {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006010 __ movl(out, Immediate(/* placeholder */ 0));
6011 codegen_->RecordStringPatch(load);
6012 return; // No dex cache slow path.
6013 }
6014 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006015 Register method_address = locations->InAt(0).AsRegister<Register>();
6016 __ leal(out, Address(method_address, CodeGeneratorX86::kDummy32BitOffset));
6017 codegen_->RecordStringPatch(load);
6018 return; // No dex cache slow path.
6019 }
6020 case HLoadString::LoadKind::kBootImageAddress: {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006021 DCHECK_NE(load->GetAddress(), 0u);
6022 uint32_t address = dchecked_integral_cast<uint32_t>(load->GetAddress());
6023 __ movl(out, Immediate(address));
6024 codegen_->RecordSimplePatch();
6025 return; // No dex cache slow path.
6026 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006027 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07006028 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006029 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006030
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07006031 // TODO: Re-add the compiler code to do string dex cache lookup again.
Christina Wadsworth175d09b2016-08-31 16:26:01 -07006032 InvokeRuntimeCallingConvention calling_convention;
6033 __ movl(calling_convention.GetRegisterAt(0), Immediate(load->GetStringIndex()));
6034 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
6035 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006036}
6037
David Brazdilcb1c0552015-08-04 16:22:25 +01006038static Address GetExceptionTlsAddress() {
Andreas Gampe542451c2016-07-26 09:02:02 -07006039 return Address::Absolute(Thread::ExceptionOffset<kX86PointerSize>().Int32Value());
David Brazdilcb1c0552015-08-04 16:22:25 +01006040}
6041
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006042void LocationsBuilderX86::VisitLoadException(HLoadException* load) {
6043 LocationSummary* locations =
6044 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
6045 locations->SetOut(Location::RequiresRegister());
6046}
6047
6048void InstructionCodeGeneratorX86::VisitLoadException(HLoadException* load) {
David Brazdilcb1c0552015-08-04 16:22:25 +01006049 __ fs()->movl(load->GetLocations()->Out().AsRegister<Register>(), GetExceptionTlsAddress());
6050}
6051
6052void LocationsBuilderX86::VisitClearException(HClearException* clear) {
6053 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
6054}
6055
6056void InstructionCodeGeneratorX86::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
6057 __ fs()->movl(GetExceptionTlsAddress(), Immediate(0));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006058}
6059
6060void LocationsBuilderX86::VisitThrow(HThrow* instruction) {
6061 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006062 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006063 InvokeRuntimeCallingConvention calling_convention;
6064 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6065}
6066
6067void InstructionCodeGeneratorX86::VisitThrow(HThrow* instruction) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01006068 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006069 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006070}
6071
Roland Levillain7c1559a2015-12-15 10:55:36 +00006072static bool TypeCheckNeedsATemporary(TypeCheckKind type_check_kind) {
6073 return kEmitCompilerReadBarrier &&
Vladimir Marko953437b2016-08-24 08:30:46 +00006074 !kUseBakerReadBarrier &&
6075 (type_check_kind == TypeCheckKind::kAbstractClassCheck ||
Roland Levillain7c1559a2015-12-15 10:55:36 +00006076 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
6077 type_check_kind == TypeCheckKind::kArrayObjectCheck);
6078}
6079
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006080void LocationsBuilderX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006081 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain0d5a2812015-11-13 10:07:31 +00006082 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01006083 bool baker_read_barrier_slow_path = false;
Roland Levillain0d5a2812015-11-13 10:07:31 +00006084 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006085 case TypeCheckKind::kExactCheck:
6086 case TypeCheckKind::kAbstractClassCheck:
6087 case TypeCheckKind::kClassHierarchyCheck:
6088 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006089 call_kind =
6090 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Vladimir Marko70e97462016-08-09 11:04:26 +01006091 baker_read_barrier_slow_path = kUseBakerReadBarrier;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006092 break;
6093 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006094 case TypeCheckKind::kUnresolvedCheck:
6095 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006096 call_kind = LocationSummary::kCallOnSlowPath;
6097 break;
6098 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006099
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006100 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01006101 if (baker_read_barrier_slow_path) {
6102 locations->SetCustomSlowPathCallerSaves(RegisterSet()); // No caller-save registers.
6103 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006104 locations->SetInAt(0, Location::RequiresRegister());
6105 locations->SetInAt(1, Location::Any());
6106 // Note that TypeCheckSlowPathX86 uses this "out" register too.
6107 locations->SetOut(Location::RequiresRegister());
6108 // When read barriers are enabled, we need a temporary register for
6109 // some cases.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006110 if (TypeCheckNeedsATemporary(type_check_kind)) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006111 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006112 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006113}
6114
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006115void InstructionCodeGeneratorX86::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006116 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006117 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006118 Location obj_loc = locations->InAt(0);
6119 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006120 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006121 Location out_loc = locations->Out();
6122 Register out = out_loc.AsRegister<Register>();
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006123 Location maybe_temp_loc = TypeCheckNeedsATemporary(type_check_kind) ?
Roland Levillain7c1559a2015-12-15 10:55:36 +00006124 locations->GetTemp(0) :
6125 Location::NoLocation();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006126 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006127 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6128 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6129 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Andreas Gampe85b62f22015-09-09 13:15:38 -07006130 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006131 NearLabel done, zero;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006132
6133 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006134 // Avoid null check if we know obj is not null.
6135 if (instruction->MustDoNullCheck()) {
6136 __ testl(obj, obj);
6137 __ j(kEqual, &zero);
6138 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006139
Roland Levillain0d5a2812015-11-13 10:07:31 +00006140 // /* HeapReference<Class> */ out = obj->klass_
Vladimir Marko953437b2016-08-24 08:30:46 +00006141 GenerateReferenceLoadTwoRegisters(instruction, out_loc, obj_loc, class_offset);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006142
Roland Levillain7c1559a2015-12-15 10:55:36 +00006143 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006144 case TypeCheckKind::kExactCheck: {
6145 if (cls.IsRegister()) {
6146 __ cmpl(out, cls.AsRegister<Register>());
6147 } else {
6148 DCHECK(cls.IsStackSlot()) << cls;
6149 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6150 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006151
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006152 // Classes must be equal for the instanceof to succeed.
6153 __ j(kNotEqual, &zero);
6154 __ movl(out, Immediate(1));
6155 __ jmp(&done);
6156 break;
6157 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006158
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006159 case TypeCheckKind::kAbstractClassCheck: {
6160 // If the class is abstract, we eagerly fetch the super class of the
6161 // object to avoid doing a comparison we know will fail.
6162 NearLabel loop;
6163 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006164 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006165 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006166 __ testl(out, out);
6167 // If `out` is null, we use it for the result, and jump to `done`.
6168 __ j(kEqual, &done);
6169 if (cls.IsRegister()) {
6170 __ cmpl(out, cls.AsRegister<Register>());
6171 } else {
6172 DCHECK(cls.IsStackSlot()) << cls;
6173 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6174 }
6175 __ j(kNotEqual, &loop);
6176 __ movl(out, Immediate(1));
6177 if (zero.IsLinked()) {
6178 __ jmp(&done);
6179 }
6180 break;
6181 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006182
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006183 case TypeCheckKind::kClassHierarchyCheck: {
6184 // Walk over the class hierarchy to find a match.
6185 NearLabel loop, success;
6186 __ Bind(&loop);
6187 if (cls.IsRegister()) {
6188 __ cmpl(out, cls.AsRegister<Register>());
6189 } else {
6190 DCHECK(cls.IsStackSlot()) << cls;
6191 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6192 }
6193 __ j(kEqual, &success);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006194 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006195 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006196 __ testl(out, out);
6197 __ j(kNotEqual, &loop);
6198 // If `out` is null, we use it for the result, and jump to `done`.
6199 __ jmp(&done);
6200 __ Bind(&success);
6201 __ movl(out, Immediate(1));
6202 if (zero.IsLinked()) {
6203 __ jmp(&done);
6204 }
6205 break;
6206 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006207
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006208 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006209 // Do an exact check.
6210 NearLabel exact_check;
6211 if (cls.IsRegister()) {
6212 __ cmpl(out, cls.AsRegister<Register>());
6213 } else {
6214 DCHECK(cls.IsStackSlot()) << cls;
6215 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6216 }
6217 __ j(kEqual, &exact_check);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006218 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006219 // /* HeapReference<Class> */ out = out->component_type_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006220 GenerateReferenceLoadOneRegister(instruction, out_loc, component_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006221 __ testl(out, out);
6222 // If `out` is null, we use it for the result, and jump to `done`.
6223 __ j(kEqual, &done);
6224 __ cmpw(Address(out, primitive_offset), Immediate(Primitive::kPrimNot));
6225 __ j(kNotEqual, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006226 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006227 __ movl(out, Immediate(1));
6228 __ jmp(&done);
6229 break;
6230 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006231
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006232 case TypeCheckKind::kArrayCheck: {
6233 if (cls.IsRegister()) {
6234 __ cmpl(out, cls.AsRegister<Register>());
6235 } else {
6236 DCHECK(cls.IsStackSlot()) << cls;
6237 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6238 }
6239 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain0d5a2812015-11-13 10:07:31 +00006240 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
6241 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006242 codegen_->AddSlowPath(slow_path);
6243 __ j(kNotEqual, slow_path->GetEntryLabel());
6244 __ movl(out, Immediate(1));
6245 if (zero.IsLinked()) {
6246 __ jmp(&done);
6247 }
6248 break;
6249 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006250
Calin Juravle98893e12015-10-02 21:05:03 +01006251 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006252 case TypeCheckKind::kInterfaceCheck: {
6253 // Note that we indeed only call on slow path, but we always go
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006254 // into the slow path for the unresolved and interface check
Roland Levillain0d5a2812015-11-13 10:07:31 +00006255 // cases.
6256 //
6257 // We cannot directly call the InstanceofNonTrivial runtime
6258 // entry point without resorting to a type checking slow path
6259 // here (i.e. by calling InvokeRuntime directly), as it would
6260 // require to assign fixed registers for the inputs of this
6261 // HInstanceOf instruction (following the runtime calling
6262 // convention), which might be cluttered by the potential first
6263 // read barrier emission at the beginning of this method.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006264 //
6265 // TODO: Introduce a new runtime entry point taking the object
6266 // to test (instead of its class) as argument, and let it deal
6267 // with the read barrier issues. This will let us refactor this
6268 // case of the `switch` code as it was previously (with a direct
6269 // call to the runtime not using a type checking slow path).
6270 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006271 DCHECK(locations->OnlyCallsOnSlowPath());
6272 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
6273 /* is_fatal */ false);
6274 codegen_->AddSlowPath(slow_path);
6275 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006276 if (zero.IsLinked()) {
6277 __ jmp(&done);
6278 }
6279 break;
6280 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006281 }
6282
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006283 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006284 __ Bind(&zero);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006285 __ xorl(out, out);
6286 }
6287
6288 if (done.IsLinked()) {
6289 __ Bind(&done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006290 }
6291
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006292 if (slow_path != nullptr) {
6293 __ Bind(slow_path->GetExitLabel());
6294 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006295}
6296
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006297void LocationsBuilderX86::VisitCheckCast(HCheckCast* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006298 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
6299 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006300 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
6301 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006302 case TypeCheckKind::kExactCheck:
6303 case TypeCheckKind::kAbstractClassCheck:
6304 case TypeCheckKind::kClassHierarchyCheck:
6305 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006306 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
6307 LocationSummary::kCallOnSlowPath :
6308 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006309 break;
6310 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006311 case TypeCheckKind::kUnresolvedCheck:
6312 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006313 call_kind = LocationSummary::kCallOnSlowPath;
6314 break;
6315 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006316 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
6317 locations->SetInAt(0, Location::RequiresRegister());
6318 locations->SetInAt(1, Location::Any());
6319 // Note that TypeCheckSlowPathX86 uses this "temp" register too.
6320 locations->AddTemp(Location::RequiresRegister());
6321 // When read barriers are enabled, we need an additional temporary
6322 // register for some cases.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006323 if (TypeCheckNeedsATemporary(type_check_kind)) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006324 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006325 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006326}
6327
6328void InstructionCodeGeneratorX86::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006329 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006330 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006331 Location obj_loc = locations->InAt(0);
6332 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006333 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006334 Location temp_loc = locations->GetTemp(0);
6335 Register temp = temp_loc.AsRegister<Register>();
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006336 Location maybe_temp2_loc = TypeCheckNeedsATemporary(type_check_kind) ?
Roland Levillain7c1559a2015-12-15 10:55:36 +00006337 locations->GetTemp(1) :
6338 Location::NoLocation();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006339 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
6340 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6341 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6342 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006343
Roland Levillain0d5a2812015-11-13 10:07:31 +00006344 bool is_type_check_slow_path_fatal =
6345 (type_check_kind == TypeCheckKind::kExactCheck ||
6346 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
6347 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
6348 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
6349 !instruction->CanThrowIntoCatchBlock();
6350 SlowPathCode* type_check_slow_path =
6351 new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
6352 is_type_check_slow_path_fatal);
6353 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006354
Roland Levillain0d5a2812015-11-13 10:07:31 +00006355 NearLabel done;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006356 // Avoid null check if we know obj is not null.
6357 if (instruction->MustDoNullCheck()) {
6358 __ testl(obj, obj);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006359 __ j(kEqual, &done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006360 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006361
Roland Levillain0d5a2812015-11-13 10:07:31 +00006362 // /* HeapReference<Class> */ temp = obj->klass_
Vladimir Marko953437b2016-08-24 08:30:46 +00006363 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006364
Roland Levillain0d5a2812015-11-13 10:07:31 +00006365 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006366 case TypeCheckKind::kExactCheck:
6367 case TypeCheckKind::kArrayCheck: {
6368 if (cls.IsRegister()) {
6369 __ cmpl(temp, cls.AsRegister<Register>());
6370 } else {
6371 DCHECK(cls.IsStackSlot()) << cls;
6372 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6373 }
6374 // Jump to slow path for throwing the exception or doing a
6375 // more involved array check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006376 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006377 break;
6378 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006379
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006380 case TypeCheckKind::kAbstractClassCheck: {
6381 // If the class is abstract, we eagerly fetch the super class of the
6382 // object to avoid doing a comparison we know will fail.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006383 NearLabel loop, compare_classes;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006384 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006385 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006386 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006387
6388 // If the class reference currently in `temp` is not null, jump
6389 // to the `compare_classes` label to compare it with the checked
6390 // class.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006391 __ testl(temp, temp);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006392 __ j(kNotEqual, &compare_classes);
6393 // Otherwise, jump to the slow path to throw the exception.
6394 //
6395 // But before, move back the object's class into `temp` before
6396 // going into the slow path, as it has been overwritten in the
6397 // meantime.
6398 // /* HeapReference<Class> */ temp = obj->klass_
Vladimir Marko953437b2016-08-24 08:30:46 +00006399 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006400 __ jmp(type_check_slow_path->GetEntryLabel());
6401
6402 __ Bind(&compare_classes);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006403 if (cls.IsRegister()) {
6404 __ cmpl(temp, cls.AsRegister<Register>());
6405 } else {
6406 DCHECK(cls.IsStackSlot()) << cls;
6407 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6408 }
6409 __ j(kNotEqual, &loop);
6410 break;
6411 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006412
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006413 case TypeCheckKind::kClassHierarchyCheck: {
6414 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006415 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006416 __ Bind(&loop);
6417 if (cls.IsRegister()) {
6418 __ cmpl(temp, cls.AsRegister<Register>());
6419 } else {
6420 DCHECK(cls.IsStackSlot()) << cls;
6421 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6422 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006423 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006424
Roland Levillain0d5a2812015-11-13 10:07:31 +00006425 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006426 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006427
6428 // If the class reference currently in `temp` is not null, jump
6429 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006430 __ testl(temp, temp);
6431 __ j(kNotEqual, &loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006432 // Otherwise, jump to the slow path to throw the exception.
6433 //
6434 // But before, move back the object's class into `temp` before
6435 // going into the slow path, as it has been overwritten in the
6436 // meantime.
6437 // /* HeapReference<Class> */ temp = obj->klass_
Vladimir Marko953437b2016-08-24 08:30:46 +00006438 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006439 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006440 break;
6441 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006442
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006443 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006444 // Do an exact check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006445 NearLabel check_non_primitive_component_type;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006446 if (cls.IsRegister()) {
6447 __ cmpl(temp, cls.AsRegister<Register>());
6448 } else {
6449 DCHECK(cls.IsStackSlot()) << cls;
6450 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6451 }
6452 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006453
6454 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006455 // /* HeapReference<Class> */ temp = temp->component_type_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006456 GenerateReferenceLoadOneRegister(instruction, temp_loc, component_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006457
6458 // If the component type is not null (i.e. the object is indeed
6459 // an array), jump to label `check_non_primitive_component_type`
6460 // to further check that this component type is not a primitive
6461 // type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006462 __ testl(temp, temp);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006463 __ j(kNotEqual, &check_non_primitive_component_type);
6464 // Otherwise, jump to the slow path to throw the exception.
6465 //
6466 // But before, move back the object's class into `temp` before
6467 // going into the slow path, as it has been overwritten in the
6468 // meantime.
6469 // /* HeapReference<Class> */ temp = obj->klass_
Vladimir Marko953437b2016-08-24 08:30:46 +00006470 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006471 __ jmp(type_check_slow_path->GetEntryLabel());
6472
6473 __ Bind(&check_non_primitive_component_type);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006474 __ cmpw(Address(temp, primitive_offset), Immediate(Primitive::kPrimNot));
Roland Levillain0d5a2812015-11-13 10:07:31 +00006475 __ j(kEqual, &done);
6476 // Same comment as above regarding `temp` and the slow path.
6477 // /* HeapReference<Class> */ temp = obj->klass_
Vladimir Marko953437b2016-08-24 08:30:46 +00006478 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006479 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006480 break;
6481 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006482
Calin Juravle98893e12015-10-02 21:05:03 +01006483 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006484 case TypeCheckKind::kInterfaceCheck:
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006485 // We always go into the type check slow path for the unresolved
6486 // and interface check cases.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006487 //
6488 // We cannot directly call the CheckCast runtime entry point
6489 // without resorting to a type checking slow path here (i.e. by
6490 // calling InvokeRuntime directly), as it would require to
6491 // assign fixed registers for the inputs of this HInstanceOf
6492 // instruction (following the runtime calling convention), which
6493 // might be cluttered by the potential first read barrier
6494 // emission at the beginning of this method.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006495 //
6496 // TODO: Introduce a new runtime entry point taking the object
6497 // to test (instead of its class) as argument, and let it deal
6498 // with the read barrier issues. This will let us refactor this
6499 // case of the `switch` code as it was previously (with a direct
6500 // call to the runtime not using a type checking slow path).
6501 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006502 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006503 break;
6504 }
6505 __ Bind(&done);
6506
Roland Levillain0d5a2812015-11-13 10:07:31 +00006507 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006508}
6509
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006510void LocationsBuilderX86::VisitMonitorOperation(HMonitorOperation* instruction) {
6511 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006512 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006513 InvokeRuntimeCallingConvention calling_convention;
6514 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6515}
6516
6517void InstructionCodeGeneratorX86::VisitMonitorOperation(HMonitorOperation* instruction) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01006518 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject
6519 : kQuickUnlockObject,
Alexandre Rames8158f282015-08-07 10:26:17 +01006520 instruction,
Serban Constantinescuba45db02016-07-12 22:53:02 +01006521 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006522 if (instruction->IsEnter()) {
6523 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
6524 } else {
6525 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
6526 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006527}
6528
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006529void LocationsBuilderX86::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
6530void LocationsBuilderX86::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
6531void LocationsBuilderX86::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
6532
6533void LocationsBuilderX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
6534 LocationSummary* locations =
6535 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6536 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
6537 || instruction->GetResultType() == Primitive::kPrimLong);
6538 locations->SetInAt(0, Location::RequiresRegister());
6539 locations->SetInAt(1, Location::Any());
6540 locations->SetOut(Location::SameAsFirstInput());
6541}
6542
6543void InstructionCodeGeneratorX86::VisitAnd(HAnd* instruction) {
6544 HandleBitwiseOperation(instruction);
6545}
6546
6547void InstructionCodeGeneratorX86::VisitOr(HOr* instruction) {
6548 HandleBitwiseOperation(instruction);
6549}
6550
6551void InstructionCodeGeneratorX86::VisitXor(HXor* instruction) {
6552 HandleBitwiseOperation(instruction);
6553}
6554
6555void InstructionCodeGeneratorX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
6556 LocationSummary* locations = instruction->GetLocations();
6557 Location first = locations->InAt(0);
6558 Location second = locations->InAt(1);
6559 DCHECK(first.Equals(locations->Out()));
6560
6561 if (instruction->GetResultType() == Primitive::kPrimInt) {
6562 if (second.IsRegister()) {
6563 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006564 __ andl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006565 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006566 __ orl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006567 } else {
6568 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006569 __ xorl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006570 }
6571 } else if (second.IsConstant()) {
6572 if (instruction->IsAnd()) {
Roland Levillain199f3362014-11-27 17:15:16 +00006573 __ andl(first.AsRegister<Register>(),
6574 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006575 } else if (instruction->IsOr()) {
Roland Levillain199f3362014-11-27 17:15:16 +00006576 __ orl(first.AsRegister<Register>(),
6577 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006578 } else {
6579 DCHECK(instruction->IsXor());
Roland Levillain199f3362014-11-27 17:15:16 +00006580 __ xorl(first.AsRegister<Register>(),
6581 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006582 }
6583 } else {
6584 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006585 __ andl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006586 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006587 __ orl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006588 } else {
6589 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006590 __ xorl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006591 }
6592 }
6593 } else {
6594 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
6595 if (second.IsRegisterPair()) {
6596 if (instruction->IsAnd()) {
6597 __ andl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
6598 __ andl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
6599 } else if (instruction->IsOr()) {
6600 __ orl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
6601 __ orl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
6602 } else {
6603 DCHECK(instruction->IsXor());
6604 __ xorl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
6605 __ xorl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
6606 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006607 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006608 if (instruction->IsAnd()) {
6609 __ andl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
6610 __ andl(first.AsRegisterPairHigh<Register>(),
6611 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
6612 } else if (instruction->IsOr()) {
6613 __ orl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
6614 __ orl(first.AsRegisterPairHigh<Register>(),
6615 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
6616 } else {
6617 DCHECK(instruction->IsXor());
6618 __ xorl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
6619 __ xorl(first.AsRegisterPairHigh<Register>(),
6620 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
6621 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006622 } else {
6623 DCHECK(second.IsConstant()) << second;
6624 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006625 int32_t low_value = Low32Bits(value);
6626 int32_t high_value = High32Bits(value);
6627 Immediate low(low_value);
6628 Immediate high(high_value);
6629 Register first_low = first.AsRegisterPairLow<Register>();
6630 Register first_high = first.AsRegisterPairHigh<Register>();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006631 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006632 if (low_value == 0) {
6633 __ xorl(first_low, first_low);
6634 } else if (low_value != -1) {
6635 __ andl(first_low, low);
6636 }
6637 if (high_value == 0) {
6638 __ xorl(first_high, first_high);
6639 } else if (high_value != -1) {
6640 __ andl(first_high, high);
6641 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006642 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006643 if (low_value != 0) {
6644 __ orl(first_low, low);
6645 }
6646 if (high_value != 0) {
6647 __ orl(first_high, high);
6648 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006649 } else {
6650 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006651 if (low_value != 0) {
6652 __ xorl(first_low, low);
6653 }
6654 if (high_value != 0) {
6655 __ xorl(first_high, high);
6656 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006657 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006658 }
6659 }
6660}
6661
Roland Levillain7c1559a2015-12-15 10:55:36 +00006662void InstructionCodeGeneratorX86::GenerateReferenceLoadOneRegister(HInstruction* instruction,
6663 Location out,
6664 uint32_t offset,
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006665 Location maybe_temp) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006666 Register out_reg = out.AsRegister<Register>();
6667 if (kEmitCompilerReadBarrier) {
6668 if (kUseBakerReadBarrier) {
6669 // Load with fast path based Baker's read barrier.
6670 // /* HeapReference<Object> */ out = *(out + offset)
6671 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00006672 instruction, out, out_reg, offset, /* needs_null_check */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006673 } else {
6674 // Load with slow path based read barrier.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006675 // Save the value of `out` into `maybe_temp` before overwriting it
Roland Levillain7c1559a2015-12-15 10:55:36 +00006676 // in the following move operation, as we will need it for the
6677 // read barrier below.
Vladimir Marko953437b2016-08-24 08:30:46 +00006678 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006679 __ movl(maybe_temp.AsRegister<Register>(), out_reg);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006680 // /* HeapReference<Object> */ out = *(out + offset)
6681 __ movl(out_reg, Address(out_reg, offset));
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006682 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006683 }
6684 } else {
6685 // Plain load with no read barrier.
6686 // /* HeapReference<Object> */ out = *(out + offset)
6687 __ movl(out_reg, Address(out_reg, offset));
6688 __ MaybeUnpoisonHeapReference(out_reg);
6689 }
6690}
6691
6692void InstructionCodeGeneratorX86::GenerateReferenceLoadTwoRegisters(HInstruction* instruction,
6693 Location out,
6694 Location obj,
Vladimir Marko953437b2016-08-24 08:30:46 +00006695 uint32_t offset) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006696 Register out_reg = out.AsRegister<Register>();
6697 Register obj_reg = obj.AsRegister<Register>();
6698 if (kEmitCompilerReadBarrier) {
6699 if (kUseBakerReadBarrier) {
6700 // Load with fast path based Baker's read barrier.
6701 // /* HeapReference<Object> */ out = *(obj + offset)
6702 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00006703 instruction, out, obj_reg, offset, /* needs_null_check */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006704 } else {
6705 // Load with slow path based read barrier.
6706 // /* HeapReference<Object> */ out = *(obj + offset)
6707 __ movl(out_reg, Address(obj_reg, offset));
6708 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
6709 }
6710 } else {
6711 // Plain load with no read barrier.
6712 // /* HeapReference<Object> */ out = *(obj + offset)
6713 __ movl(out_reg, Address(obj_reg, offset));
6714 __ MaybeUnpoisonHeapReference(out_reg);
6715 }
6716}
6717
6718void InstructionCodeGeneratorX86::GenerateGcRootFieldLoad(HInstruction* instruction,
6719 Location root,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006720 const Address& address,
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006721 Label* fixup_label,
6722 bool requires_read_barrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006723 Register root_reg = root.AsRegister<Register>();
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006724 if (requires_read_barrier) {
6725 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006726 if (kUseBakerReadBarrier) {
6727 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
6728 // Baker's read barrier are used:
6729 //
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006730 // root = *address;
Roland Levillain7c1559a2015-12-15 10:55:36 +00006731 // if (Thread::Current()->GetIsGcMarking()) {
6732 // root = ReadBarrier::Mark(root)
6733 // }
6734
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006735 // /* GcRoot<mirror::Object> */ root = *address
6736 __ movl(root_reg, address);
6737 if (fixup_label != nullptr) {
6738 __ Bind(fixup_label);
6739 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00006740 static_assert(
6741 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
6742 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
6743 "have different sizes.");
6744 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
6745 "art::mirror::CompressedReference<mirror::Object> and int32_t "
6746 "have different sizes.");
6747
Vladimir Marko953437b2016-08-24 08:30:46 +00006748 // Slow path marking the GC root `root`.
6749 SlowPathCode* slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathX86(
6750 instruction, root, /* unpoison */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006751 codegen_->AddSlowPath(slow_path);
6752
Andreas Gampe542451c2016-07-26 09:02:02 -07006753 __ fs()->cmpl(Address::Absolute(Thread::IsGcMarkingOffset<kX86PointerSize>().Int32Value()),
Roland Levillain7c1559a2015-12-15 10:55:36 +00006754 Immediate(0));
6755 __ j(kNotEqual, slow_path->GetEntryLabel());
6756 __ Bind(slow_path->GetExitLabel());
6757 } else {
6758 // GC root loaded through a slow path for read barriers other
6759 // than Baker's.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006760 // /* GcRoot<mirror::Object>* */ root = address
6761 __ leal(root_reg, address);
6762 if (fixup_label != nullptr) {
6763 __ Bind(fixup_label);
6764 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00006765 // /* mirror::Object* */ root = root->Read()
6766 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
6767 }
6768 } else {
6769 // Plain GC root load with no read barrier.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006770 // /* GcRoot<mirror::Object> */ root = *address
6771 __ movl(root_reg, address);
6772 if (fixup_label != nullptr) {
6773 __ Bind(fixup_label);
6774 }
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006775 // Note that GC roots are not affected by heap poisoning, thus we
6776 // do not have to unpoison `root_reg` here.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006777 }
6778}
6779
6780void CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
6781 Location ref,
6782 Register obj,
6783 uint32_t offset,
Roland Levillain7c1559a2015-12-15 10:55:36 +00006784 bool needs_null_check) {
6785 DCHECK(kEmitCompilerReadBarrier);
6786 DCHECK(kUseBakerReadBarrier);
6787
6788 // /* HeapReference<Object> */ ref = *(obj + offset)
6789 Address src(obj, offset);
Vladimir Marko953437b2016-08-24 08:30:46 +00006790 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, needs_null_check);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006791}
6792
6793void CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
6794 Location ref,
6795 Register obj,
6796 uint32_t data_offset,
6797 Location index,
Roland Levillain7c1559a2015-12-15 10:55:36 +00006798 bool needs_null_check) {
6799 DCHECK(kEmitCompilerReadBarrier);
6800 DCHECK(kUseBakerReadBarrier);
6801
Roland Levillain3d312422016-06-23 13:53:42 +01006802 static_assert(
6803 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
6804 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain7c1559a2015-12-15 10:55:36 +00006805 // /* HeapReference<Object> */ ref =
6806 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006807 Address src = CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset);
Vladimir Marko953437b2016-08-24 08:30:46 +00006808 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, needs_null_check);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006809}
6810
6811void CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
6812 Location ref,
6813 Register obj,
6814 const Address& src,
Roland Levillain7c1559a2015-12-15 10:55:36 +00006815 bool needs_null_check) {
6816 DCHECK(kEmitCompilerReadBarrier);
6817 DCHECK(kUseBakerReadBarrier);
6818
6819 // In slow path based read barriers, the read barrier call is
6820 // inserted after the original load. However, in fast path based
6821 // Baker's read barriers, we need to perform the load of
6822 // mirror::Object::monitor_ *before* the original reference load.
6823 // This load-load ordering is required by the read barrier.
6824 // The fast path/slow path (for Baker's algorithm) should look like:
6825 //
6826 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
6827 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
6828 // HeapReference<Object> ref = *src; // Original reference load.
6829 // bool is_gray = (rb_state == ReadBarrier::gray_ptr_);
6830 // if (is_gray) {
6831 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
6832 // }
6833 //
6834 // Note: the original implementation in ReadBarrier::Barrier is
6835 // slightly more complex as:
6836 // - it implements the load-load fence using a data dependency on
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006837 // the high-bits of rb_state, which are expected to be all zeroes
6838 // (we use CodeGeneratorX86::GenerateMemoryBarrier instead here,
6839 // which is a no-op thanks to the x86 memory model);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006840 // - it performs additional checks that we do not do here for
6841 // performance reasons.
6842
6843 Register ref_reg = ref.AsRegister<Register>();
Roland Levillain7c1559a2015-12-15 10:55:36 +00006844 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
6845
Vladimir Marko953437b2016-08-24 08:30:46 +00006846 // Given the numeric representation, it's enough to check the low bit of the rb_state.
6847 static_assert(ReadBarrier::white_ptr_ == 0, "Expecting white to have value 0");
6848 static_assert(ReadBarrier::gray_ptr_ == 1, "Expecting gray to have value 1");
6849 static_assert(ReadBarrier::black_ptr_ == 2, "Expecting black to have value 2");
6850 constexpr uint32_t gray_byte_position = LockWord::kReadBarrierStateShift / kBitsPerByte;
6851 constexpr uint32_t gray_bit_position = LockWord::kReadBarrierStateShift % kBitsPerByte;
6852 constexpr int32_t test_value = static_cast<int8_t>(1 << gray_bit_position);
6853
6854 // if (rb_state == ReadBarrier::gray_ptr_)
6855 // ref = ReadBarrier::Mark(ref);
6856 // At this point, just do the "if" and make sure that flags are preserved until the branch.
6857 __ testb(Address(obj, monitor_offset + gray_byte_position), Immediate(test_value));
Roland Levillain7c1559a2015-12-15 10:55:36 +00006858 if (needs_null_check) {
6859 MaybeRecordImplicitNullCheck(instruction);
6860 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00006861
6862 // Load fence to prevent load-load reordering.
6863 // Note that this is a no-op, thanks to the x86 memory model.
6864 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
6865
6866 // The actual reference load.
6867 // /* HeapReference<Object> */ ref = *src
Vladimir Marko953437b2016-08-24 08:30:46 +00006868 __ movl(ref_reg, src); // Flags are unaffected.
6869
6870 // Note: Reference unpoisoning modifies the flags, so we need to delay it after the branch.
6871 // Slow path marking the object `ref` when it is gray.
6872 SlowPathCode* slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathX86(
6873 instruction, ref, /* unpoison */ true);
6874 AddSlowPath(slow_path);
6875
6876 // We have done the "if" of the gray bit check above, now branch based on the flags.
6877 __ j(kNotZero, slow_path->GetEntryLabel());
Roland Levillain7c1559a2015-12-15 10:55:36 +00006878
6879 // Object* ref = ref_addr->AsMirrorPtr()
6880 __ MaybeUnpoisonHeapReference(ref_reg);
6881
Roland Levillain7c1559a2015-12-15 10:55:36 +00006882 __ Bind(slow_path->GetExitLabel());
6883}
6884
6885void CodeGeneratorX86::GenerateReadBarrierSlow(HInstruction* instruction,
6886 Location out,
6887 Location ref,
6888 Location obj,
6889 uint32_t offset,
6890 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006891 DCHECK(kEmitCompilerReadBarrier);
6892
Roland Levillain7c1559a2015-12-15 10:55:36 +00006893 // Insert a slow path based read barrier *after* the reference load.
6894 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00006895 // If heap poisoning is enabled, the unpoisoning of the loaded
6896 // reference will be carried out by the runtime within the slow
6897 // path.
6898 //
6899 // Note that `ref` currently does not get unpoisoned (when heap
6900 // poisoning is enabled), which is alright as the `ref` argument is
6901 // not used by the artReadBarrierSlow entry point.
6902 //
6903 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
6904 SlowPathCode* slow_path = new (GetGraph()->GetArena())
6905 ReadBarrierForHeapReferenceSlowPathX86(instruction, out, ref, obj, offset, index);
6906 AddSlowPath(slow_path);
6907
Roland Levillain0d5a2812015-11-13 10:07:31 +00006908 __ jmp(slow_path->GetEntryLabel());
6909 __ Bind(slow_path->GetExitLabel());
6910}
6911
Roland Levillain7c1559a2015-12-15 10:55:36 +00006912void CodeGeneratorX86::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
6913 Location out,
6914 Location ref,
6915 Location obj,
6916 uint32_t offset,
6917 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006918 if (kEmitCompilerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006919 // Baker's read barriers shall be handled by the fast path
6920 // (CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier).
6921 DCHECK(!kUseBakerReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006922 // If heap poisoning is enabled, unpoisoning will be taken care of
6923 // by the runtime within the slow path.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006924 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006925 } else if (kPoisonHeapReferences) {
6926 __ UnpoisonHeapReference(out.AsRegister<Register>());
6927 }
6928}
6929
Roland Levillain7c1559a2015-12-15 10:55:36 +00006930void CodeGeneratorX86::GenerateReadBarrierForRootSlow(HInstruction* instruction,
6931 Location out,
6932 Location root) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006933 DCHECK(kEmitCompilerReadBarrier);
6934
Roland Levillain7c1559a2015-12-15 10:55:36 +00006935 // Insert a slow path based read barrier *after* the GC root load.
6936 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00006937 // Note that GC roots are not affected by heap poisoning, so we do
6938 // not need to do anything special for this here.
6939 SlowPathCode* slow_path =
6940 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathX86(instruction, out, root);
6941 AddSlowPath(slow_path);
6942
Roland Levillain0d5a2812015-11-13 10:07:31 +00006943 __ jmp(slow_path->GetEntryLabel());
6944 __ Bind(slow_path->GetExitLabel());
6945}
6946
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006947void LocationsBuilderX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00006948 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00006949 LOG(FATAL) << "Unreachable";
6950}
6951
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006952void InstructionCodeGeneratorX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00006953 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00006954 LOG(FATAL) << "Unreachable";
6955}
6956
Mark Mendellfe57faa2015-09-18 09:26:15 -04006957// Simple implementation of packed switch - generate cascaded compare/jumps.
6958void LocationsBuilderX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6959 LocationSummary* locations =
6960 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
6961 locations->SetInAt(0, Location::RequiresRegister());
6962}
6963
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006964void InstructionCodeGeneratorX86::GenPackedSwitchWithCompares(Register value_reg,
6965 int32_t lower_bound,
6966 uint32_t num_entries,
6967 HBasicBlock* switch_block,
6968 HBasicBlock* default_block) {
6969 // Figure out the correct compare values and jump conditions.
6970 // Handle the first compare/branch as a special case because it might
6971 // jump to the default case.
6972 DCHECK_GT(num_entries, 2u);
6973 Condition first_condition;
6974 uint32_t index;
6975 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
6976 if (lower_bound != 0) {
6977 first_condition = kLess;
6978 __ cmpl(value_reg, Immediate(lower_bound));
6979 __ j(first_condition, codegen_->GetLabelOf(default_block));
6980 __ j(kEqual, codegen_->GetLabelOf(successors[0]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04006981
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006982 index = 1;
6983 } else {
6984 // Handle all the compare/jumps below.
6985 first_condition = kBelow;
6986 index = 0;
6987 }
6988
6989 // Handle the rest of the compare/jumps.
6990 for (; index + 1 < num_entries; index += 2) {
6991 int32_t compare_to_value = lower_bound + index + 1;
6992 __ cmpl(value_reg, Immediate(compare_to_value));
6993 // Jump to successors[index] if value < case_value[index].
6994 __ j(first_condition, codegen_->GetLabelOf(successors[index]));
6995 // Jump to successors[index + 1] if value == case_value[index + 1].
6996 __ j(kEqual, codegen_->GetLabelOf(successors[index + 1]));
6997 }
6998
6999 if (index != num_entries) {
7000 // There are an odd number of entries. Handle the last one.
7001 DCHECK_EQ(index + 1, num_entries);
7002 __ cmpl(value_reg, Immediate(lower_bound + index));
7003 __ j(kEqual, codegen_->GetLabelOf(successors[index]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007004 }
7005
7006 // And the default for any other value.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007007 if (!codegen_->GoesToNextBlock(switch_block, default_block)) {
7008 __ jmp(codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007009 }
7010}
7011
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007012void InstructionCodeGeneratorX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7013 int32_t lower_bound = switch_instr->GetStartValue();
7014 uint32_t num_entries = switch_instr->GetNumEntries();
7015 LocationSummary* locations = switch_instr->GetLocations();
7016 Register value_reg = locations->InAt(0).AsRegister<Register>();
7017
7018 GenPackedSwitchWithCompares(value_reg,
7019 lower_bound,
7020 num_entries,
7021 switch_instr->GetBlock(),
7022 switch_instr->GetDefaultBlock());
7023}
7024
Mark Mendell805b3b52015-09-18 14:10:29 -04007025void LocationsBuilderX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
7026 LocationSummary* locations =
7027 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
7028 locations->SetInAt(0, Location::RequiresRegister());
7029
7030 // Constant area pointer.
7031 locations->SetInAt(1, Location::RequiresRegister());
7032
7033 // And the temporary we need.
7034 locations->AddTemp(Location::RequiresRegister());
7035}
7036
7037void InstructionCodeGeneratorX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
7038 int32_t lower_bound = switch_instr->GetStartValue();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007039 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendell805b3b52015-09-18 14:10:29 -04007040 LocationSummary* locations = switch_instr->GetLocations();
7041 Register value_reg = locations->InAt(0).AsRegister<Register>();
7042 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
7043
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007044 if (num_entries <= kPackedSwitchJumpTableThreshold) {
7045 GenPackedSwitchWithCompares(value_reg,
7046 lower_bound,
7047 num_entries,
7048 switch_instr->GetBlock(),
7049 default_block);
7050 return;
7051 }
7052
Mark Mendell805b3b52015-09-18 14:10:29 -04007053 // Optimizing has a jump area.
7054 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
7055 Register constant_area = locations->InAt(1).AsRegister<Register>();
7056
7057 // Remove the bias, if needed.
7058 if (lower_bound != 0) {
7059 __ leal(temp_reg, Address(value_reg, -lower_bound));
7060 value_reg = temp_reg;
7061 }
7062
7063 // Is the value in range?
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007064 DCHECK_GE(num_entries, 1u);
Mark Mendell805b3b52015-09-18 14:10:29 -04007065 __ cmpl(value_reg, Immediate(num_entries - 1));
7066 __ j(kAbove, codegen_->GetLabelOf(default_block));
7067
7068 // We are in the range of the table.
7069 // Load (target-constant_area) from the jump table, indexing by the value.
7070 __ movl(temp_reg, codegen_->LiteralCaseTable(switch_instr, constant_area, value_reg));
7071
7072 // Compute the actual target address by adding in constant_area.
7073 __ addl(temp_reg, constant_area);
7074
7075 // And jump.
7076 __ jmp(temp_reg);
7077}
7078
Mark Mendell0616ae02015-04-17 12:49:27 -04007079void LocationsBuilderX86::VisitX86ComputeBaseMethodAddress(
7080 HX86ComputeBaseMethodAddress* insn) {
7081 LocationSummary* locations =
7082 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
7083 locations->SetOut(Location::RequiresRegister());
7084}
7085
7086void InstructionCodeGeneratorX86::VisitX86ComputeBaseMethodAddress(
7087 HX86ComputeBaseMethodAddress* insn) {
7088 LocationSummary* locations = insn->GetLocations();
7089 Register reg = locations->Out().AsRegister<Register>();
7090
7091 // Generate call to next instruction.
7092 Label next_instruction;
7093 __ call(&next_instruction);
7094 __ Bind(&next_instruction);
7095
7096 // Remember this offset for later use with constant area.
7097 codegen_->SetMethodAddressOffset(GetAssembler()->CodeSize());
7098
7099 // Grab the return address off the stack.
7100 __ popl(reg);
7101}
7102
7103void LocationsBuilderX86::VisitX86LoadFromConstantTable(
7104 HX86LoadFromConstantTable* insn) {
7105 LocationSummary* locations =
7106 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
7107
7108 locations->SetInAt(0, Location::RequiresRegister());
7109 locations->SetInAt(1, Location::ConstantLocation(insn->GetConstant()));
7110
7111 // If we don't need to be materialized, we only need the inputs to be set.
David Brazdilb3e773e2016-01-26 11:28:37 +00007112 if (insn->IsEmittedAtUseSite()) {
Mark Mendell0616ae02015-04-17 12:49:27 -04007113 return;
7114 }
7115
7116 switch (insn->GetType()) {
7117 case Primitive::kPrimFloat:
7118 case Primitive::kPrimDouble:
7119 locations->SetOut(Location::RequiresFpuRegister());
7120 break;
7121
7122 case Primitive::kPrimInt:
7123 locations->SetOut(Location::RequiresRegister());
7124 break;
7125
7126 default:
7127 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
7128 }
7129}
7130
7131void InstructionCodeGeneratorX86::VisitX86LoadFromConstantTable(HX86LoadFromConstantTable* insn) {
David Brazdilb3e773e2016-01-26 11:28:37 +00007132 if (insn->IsEmittedAtUseSite()) {
Mark Mendell0616ae02015-04-17 12:49:27 -04007133 return;
7134 }
7135
7136 LocationSummary* locations = insn->GetLocations();
7137 Location out = locations->Out();
7138 Register const_area = locations->InAt(0).AsRegister<Register>();
7139 HConstant *value = insn->GetConstant();
7140
7141 switch (insn->GetType()) {
7142 case Primitive::kPrimFloat:
7143 __ movss(out.AsFpuRegister<XmmRegister>(),
7144 codegen_->LiteralFloatAddress(value->AsFloatConstant()->GetValue(), const_area));
7145 break;
7146
7147 case Primitive::kPrimDouble:
7148 __ movsd(out.AsFpuRegister<XmmRegister>(),
7149 codegen_->LiteralDoubleAddress(value->AsDoubleConstant()->GetValue(), const_area));
7150 break;
7151
7152 case Primitive::kPrimInt:
7153 __ movl(out.AsRegister<Register>(),
7154 codegen_->LiteralInt32Address(value->AsIntConstant()->GetValue(), const_area));
7155 break;
7156
7157 default:
7158 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
7159 }
7160}
7161
Mark Mendell0616ae02015-04-17 12:49:27 -04007162/**
7163 * Class to handle late fixup of offsets into constant area.
7164 */
Vladimir Marko5233f932015-09-29 19:01:15 +01007165class RIPFixup : public AssemblerFixup, public ArenaObject<kArenaAllocCodeGenerator> {
Mark Mendell0616ae02015-04-17 12:49:27 -04007166 public:
Mark Mendell805b3b52015-09-18 14:10:29 -04007167 RIPFixup(CodeGeneratorX86& codegen, size_t offset)
7168 : codegen_(&codegen), offset_into_constant_area_(offset) {}
7169
7170 protected:
7171 void SetOffset(size_t offset) { offset_into_constant_area_ = offset; }
7172
7173 CodeGeneratorX86* codegen_;
Mark Mendell0616ae02015-04-17 12:49:27 -04007174
7175 private:
7176 void Process(const MemoryRegion& region, int pos) OVERRIDE {
7177 // Patch the correct offset for the instruction. The place to patch is the
7178 // last 4 bytes of the instruction.
7179 // The value to patch is the distance from the offset in the constant area
7180 // from the address computed by the HX86ComputeBaseMethodAddress instruction.
Mark Mendell805b3b52015-09-18 14:10:29 -04007181 int32_t constant_offset = codegen_->ConstantAreaStart() + offset_into_constant_area_;
7182 int32_t relative_position = constant_offset - codegen_->GetMethodAddressOffset();;
Mark Mendell0616ae02015-04-17 12:49:27 -04007183
7184 // Patch in the right value.
7185 region.StoreUnaligned<int32_t>(pos - 4, relative_position);
7186 }
7187
Mark Mendell0616ae02015-04-17 12:49:27 -04007188 // Location in constant area that the fixup refers to.
Mark Mendell805b3b52015-09-18 14:10:29 -04007189 int32_t offset_into_constant_area_;
Mark Mendell0616ae02015-04-17 12:49:27 -04007190};
7191
Mark Mendell805b3b52015-09-18 14:10:29 -04007192/**
7193 * Class to handle late fixup of offsets to a jump table that will be created in the
7194 * constant area.
7195 */
7196class JumpTableRIPFixup : public RIPFixup {
7197 public:
7198 JumpTableRIPFixup(CodeGeneratorX86& codegen, HX86PackedSwitch* switch_instr)
7199 : RIPFixup(codegen, static_cast<size_t>(-1)), switch_instr_(switch_instr) {}
7200
7201 void CreateJumpTable() {
7202 X86Assembler* assembler = codegen_->GetAssembler();
7203
7204 // Ensure that the reference to the jump table has the correct offset.
7205 const int32_t offset_in_constant_table = assembler->ConstantAreaSize();
7206 SetOffset(offset_in_constant_table);
7207
7208 // The label values in the jump table are computed relative to the
7209 // instruction addressing the constant area.
7210 const int32_t relative_offset = codegen_->GetMethodAddressOffset();
7211
7212 // Populate the jump table with the correct values for the jump table.
7213 int32_t num_entries = switch_instr_->GetNumEntries();
7214 HBasicBlock* block = switch_instr_->GetBlock();
7215 const ArenaVector<HBasicBlock*>& successors = block->GetSuccessors();
7216 // The value that we want is the target offset - the position of the table.
7217 for (int32_t i = 0; i < num_entries; i++) {
7218 HBasicBlock* b = successors[i];
7219 Label* l = codegen_->GetLabelOf(b);
7220 DCHECK(l->IsBound());
7221 int32_t offset_to_block = l->Position() - relative_offset;
7222 assembler->AppendInt32(offset_to_block);
7223 }
7224 }
7225
7226 private:
7227 const HX86PackedSwitch* switch_instr_;
7228};
7229
7230void CodeGeneratorX86::Finalize(CodeAllocator* allocator) {
7231 // Generate the constant area if needed.
7232 X86Assembler* assembler = GetAssembler();
7233 if (!assembler->IsConstantAreaEmpty() || !fixups_to_jump_tables_.empty()) {
7234 // Align to 4 byte boundary to reduce cache misses, as the data is 4 and 8
7235 // byte values.
7236 assembler->Align(4, 0);
7237 constant_area_start_ = assembler->CodeSize();
7238
7239 // Populate any jump tables.
7240 for (auto jump_table : fixups_to_jump_tables_) {
7241 jump_table->CreateJumpTable();
7242 }
7243
7244 // And now add the constant area to the generated code.
7245 assembler->AddConstantArea();
7246 }
7247
7248 // And finish up.
7249 CodeGenerator::Finalize(allocator);
7250}
7251
Mark Mendell0616ae02015-04-17 12:49:27 -04007252Address CodeGeneratorX86::LiteralDoubleAddress(double v, Register reg) {
7253 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddDouble(v));
7254 return Address(reg, kDummy32BitOffset, fixup);
7255}
7256
7257Address CodeGeneratorX86::LiteralFloatAddress(float v, Register reg) {
7258 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddFloat(v));
7259 return Address(reg, kDummy32BitOffset, fixup);
7260}
7261
7262Address CodeGeneratorX86::LiteralInt32Address(int32_t v, Register reg) {
7263 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt32(v));
7264 return Address(reg, kDummy32BitOffset, fixup);
7265}
7266
7267Address CodeGeneratorX86::LiteralInt64Address(int64_t v, Register reg) {
7268 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt64(v));
7269 return Address(reg, kDummy32BitOffset, fixup);
7270}
7271
Aart Bika19616e2016-02-01 18:57:58 -08007272void CodeGeneratorX86::Load32BitValue(Register dest, int32_t value) {
7273 if (value == 0) {
7274 __ xorl(dest, dest);
7275 } else {
7276 __ movl(dest, Immediate(value));
7277 }
7278}
7279
7280void CodeGeneratorX86::Compare32BitValue(Register dest, int32_t value) {
7281 if (value == 0) {
7282 __ testl(dest, dest);
7283 } else {
7284 __ cmpl(dest, Immediate(value));
7285 }
7286}
7287
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007288void CodeGeneratorX86::GenerateIntCompare(Location lhs, Location rhs) {
7289 Register lhs_reg = lhs.AsRegister<Register>();
7290 if (rhs.IsConstant()) {
7291 int32_t value = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
7292 Compare32BitValue(lhs_reg, value);
7293 } else if (rhs.IsStackSlot()) {
7294 __ cmpl(lhs_reg, Address(ESP, rhs.GetStackIndex()));
7295 } else {
7296 __ cmpl(lhs_reg, rhs.AsRegister<Register>());
7297 }
7298}
7299
7300Address CodeGeneratorX86::ArrayAddress(Register obj,
7301 Location index,
7302 ScaleFactor scale,
7303 uint32_t data_offset) {
7304 return index.IsConstant() ?
7305 Address(obj, (index.GetConstant()->AsIntConstant()->GetValue() << scale) + data_offset) :
7306 Address(obj, index.AsRegister<Register>(), scale, data_offset);
7307}
7308
Mark Mendell805b3b52015-09-18 14:10:29 -04007309Address CodeGeneratorX86::LiteralCaseTable(HX86PackedSwitch* switch_instr,
7310 Register reg,
7311 Register value) {
7312 // Create a fixup to be used to create and address the jump table.
7313 JumpTableRIPFixup* table_fixup =
7314 new (GetGraph()->GetArena()) JumpTableRIPFixup(*this, switch_instr);
7315
7316 // We have to populate the jump tables.
7317 fixups_to_jump_tables_.push_back(table_fixup);
7318
7319 // We want a scaled address, as we are extracting the correct offset from the table.
7320 return Address(reg, value, TIMES_4, kDummy32BitOffset, table_fixup);
7321}
7322
Andreas Gampe85b62f22015-09-09 13:15:38 -07007323// TODO: target as memory.
7324void CodeGeneratorX86::MoveFromReturnRegister(Location target, Primitive::Type type) {
7325 if (!target.IsValid()) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007326 DCHECK_EQ(type, Primitive::kPrimVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07007327 return;
7328 }
7329
7330 DCHECK_NE(type, Primitive::kPrimVoid);
7331
7332 Location return_loc = InvokeDexCallingConventionVisitorX86().GetReturnLocation(type);
7333 if (target.Equals(return_loc)) {
7334 return;
7335 }
7336
7337 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
7338 // with the else branch.
7339 if (type == Primitive::kPrimLong) {
7340 HParallelMove parallel_move(GetGraph()->GetArena());
7341 parallel_move.AddMove(return_loc.ToLow(), target.ToLow(), Primitive::kPrimInt, nullptr);
7342 parallel_move.AddMove(return_loc.ToHigh(), target.ToHigh(), Primitive::kPrimInt, nullptr);
7343 GetMoveResolver()->EmitNativeCode(&parallel_move);
7344 } else {
7345 // Let the parallel move resolver take care of all of this.
7346 HParallelMove parallel_move(GetGraph()->GetArena());
7347 parallel_move.AddMove(return_loc, target, type, nullptr);
7348 GetMoveResolver()->EmitNativeCode(&parallel_move);
7349 }
7350}
7351
Roland Levillain4d027112015-07-01 15:41:14 +01007352#undef __
7353
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00007354} // namespace x86
7355} // namespace art