blob: 28db29cb586b2d63cfa4b0a7a5d75d86a9e04b4a [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 Rames78e3ef62015-08-12 13:43:29 +0100757 ValidateInvokeRuntime(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();
1072 int64_t value;
1073 if (constant->IsLongConstant()) {
1074 value = constant->AsLongConstant()->GetValue();
1075 } else {
1076 DCHECK(constant->IsDoubleConstant());
Roland Levillainda4d79b2015-03-24 14:36:11 +00001077 value = bit_cast<int64_t, double>(constant->AsDoubleConstant()->GetValue());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001078 }
1079 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(Low32Bits(value)));
1080 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), Immediate(High32Bits(value)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001081 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001082 DCHECK(source.IsDoubleStackSlot()) << source;
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001083 EmitParallelMoves(
1084 Location::StackSlot(source.GetStackIndex()),
1085 Location::StackSlot(destination.GetStackIndex()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001086 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001087 Location::StackSlot(source.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001088 Location::StackSlot(destination.GetHighStackIndex(kX86WordSize)),
1089 Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001090 }
1091 }
1092}
1093
Calin Juravle175dc732015-08-25 15:42:32 +01001094void CodeGeneratorX86::MoveConstant(Location location, int32_t value) {
1095 DCHECK(location.IsRegister());
1096 __ movl(location.AsRegister<Register>(), Immediate(value));
1097}
1098
Calin Juravlee460d1d2015-09-29 04:52:17 +01001099void CodeGeneratorX86::MoveLocation(Location dst, Location src, Primitive::Type dst_type) {
David Brazdil74eb1b22015-12-14 11:44:01 +00001100 HParallelMove move(GetGraph()->GetArena());
1101 if (dst_type == Primitive::kPrimLong && !src.IsConstant() && !src.IsFpuRegister()) {
1102 move.AddMove(src.ToLow(), dst.ToLow(), Primitive::kPrimInt, nullptr);
1103 move.AddMove(src.ToHigh(), dst.ToHigh(), Primitive::kPrimInt, nullptr);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001104 } else {
David Brazdil74eb1b22015-12-14 11:44:01 +00001105 move.AddMove(src, dst, dst_type, nullptr);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001106 }
David Brazdil74eb1b22015-12-14 11:44:01 +00001107 GetMoveResolver()->EmitNativeCode(&move);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001108}
1109
1110void CodeGeneratorX86::AddLocationAsTemp(Location location, LocationSummary* locations) {
1111 if (location.IsRegister()) {
1112 locations->AddTemp(location);
1113 } else if (location.IsRegisterPair()) {
1114 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
1115 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
1116 } else {
1117 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1118 }
1119}
1120
David Brazdilfc6a86a2015-06-26 10:33:45 +00001121void InstructionCodeGeneratorX86::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001122 DCHECK(!successor->IsExitBlock());
1123
1124 HBasicBlock* block = got->GetBlock();
1125 HInstruction* previous = got->GetPrevious();
1126
1127 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +00001128 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001129 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1130 return;
1131 }
1132
1133 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1134 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1135 }
1136 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001137 __ jmp(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001138 }
1139}
1140
David Brazdilfc6a86a2015-06-26 10:33:45 +00001141void LocationsBuilderX86::VisitGoto(HGoto* got) {
1142 got->SetLocations(nullptr);
1143}
1144
1145void InstructionCodeGeneratorX86::VisitGoto(HGoto* got) {
1146 HandleGoto(got, got->GetSuccessor());
1147}
1148
1149void LocationsBuilderX86::VisitTryBoundary(HTryBoundary* try_boundary) {
1150 try_boundary->SetLocations(nullptr);
1151}
1152
1153void InstructionCodeGeneratorX86::VisitTryBoundary(HTryBoundary* try_boundary) {
1154 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
1155 if (!successor->IsExitBlock()) {
1156 HandleGoto(try_boundary, successor);
1157 }
1158}
1159
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001160void LocationsBuilderX86::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001161 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001162}
1163
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001164void InstructionCodeGeneratorX86::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001165}
1166
Mark Mendell152408f2015-12-31 12:28:50 -05001167template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001168void InstructionCodeGeneratorX86::GenerateFPJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001169 LabelType* true_label,
1170 LabelType* false_label) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001171 if (cond->IsFPConditionTrueIfNaN()) {
1172 __ j(kUnordered, true_label);
1173 } else if (cond->IsFPConditionFalseIfNaN()) {
1174 __ j(kUnordered, false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001175 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001176 __ j(X86UnsignedOrFPCondition(cond->GetCondition()), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001177}
1178
Mark Mendell152408f2015-12-31 12:28:50 -05001179template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001180void InstructionCodeGeneratorX86::GenerateLongComparesAndJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001181 LabelType* true_label,
1182 LabelType* false_label) {
Mark Mendellc4701932015-04-10 13:18:51 -04001183 LocationSummary* locations = cond->GetLocations();
1184 Location left = locations->InAt(0);
1185 Location right = locations->InAt(1);
1186 IfCondition if_cond = cond->GetCondition();
1187
Mark Mendellc4701932015-04-10 13:18:51 -04001188 Register left_high = left.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001189 Register left_low = left.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001190 IfCondition true_high_cond = if_cond;
1191 IfCondition false_high_cond = cond->GetOppositeCondition();
Aart Bike9f37602015-10-09 11:15:55 -07001192 Condition final_condition = X86UnsignedOrFPCondition(if_cond); // unsigned on lower part
Mark Mendellc4701932015-04-10 13:18:51 -04001193
1194 // Set the conditions for the test, remembering that == needs to be
1195 // decided using the low words.
1196 switch (if_cond) {
1197 case kCondEQ:
Mark Mendellc4701932015-04-10 13:18:51 -04001198 case kCondNE:
Roland Levillain4fa13f62015-07-06 18:11:54 +01001199 // Nothing to do.
Mark Mendellc4701932015-04-10 13:18:51 -04001200 break;
1201 case kCondLT:
1202 false_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -04001203 break;
1204 case kCondLE:
1205 true_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -04001206 break;
1207 case kCondGT:
1208 false_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -04001209 break;
1210 case kCondGE:
1211 true_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -04001212 break;
Aart Bike9f37602015-10-09 11:15:55 -07001213 case kCondB:
1214 false_high_cond = kCondA;
1215 break;
1216 case kCondBE:
1217 true_high_cond = kCondB;
1218 break;
1219 case kCondA:
1220 false_high_cond = kCondB;
1221 break;
1222 case kCondAE:
1223 true_high_cond = kCondA;
1224 break;
Mark Mendellc4701932015-04-10 13:18:51 -04001225 }
1226
1227 if (right.IsConstant()) {
1228 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
Mark Mendellc4701932015-04-10 13:18:51 -04001229 int32_t val_high = High32Bits(value);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001230 int32_t val_low = Low32Bits(value);
Mark Mendellc4701932015-04-10 13:18:51 -04001231
Aart Bika19616e2016-02-01 18:57:58 -08001232 codegen_->Compare32BitValue(left_high, val_high);
Mark Mendellc4701932015-04-10 13:18:51 -04001233 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001234 __ j(X86Condition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001235 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001236 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001237 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001238 __ j(X86Condition(true_high_cond), true_label);
1239 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001240 }
1241 // Must be equal high, so compare the lows.
Aart Bika19616e2016-02-01 18:57:58 -08001242 codegen_->Compare32BitValue(left_low, val_low);
Mark Mendell8659e842016-02-16 10:41:46 -05001243 } else if (right.IsRegisterPair()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001244 Register right_high = right.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001245 Register right_low = right.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001246
1247 __ cmpl(left_high, right_high);
1248 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001249 __ j(X86Condition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001250 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001251 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001252 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001253 __ j(X86Condition(true_high_cond), true_label);
1254 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001255 }
1256 // Must be equal high, so compare the lows.
1257 __ cmpl(left_low, right_low);
Mark Mendell8659e842016-02-16 10:41:46 -05001258 } else {
1259 DCHECK(right.IsDoubleStackSlot());
1260 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
1261 if (if_cond == kCondNE) {
1262 __ j(X86Condition(true_high_cond), true_label);
1263 } else if (if_cond == kCondEQ) {
1264 __ j(X86Condition(false_high_cond), false_label);
1265 } else {
1266 __ j(X86Condition(true_high_cond), true_label);
1267 __ j(X86Condition(false_high_cond), false_label);
1268 }
1269 // Must be equal high, so compare the lows.
1270 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Mark Mendellc4701932015-04-10 13:18:51 -04001271 }
1272 // The last comparison might be unsigned.
1273 __ j(final_condition, true_label);
1274}
1275
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001276void InstructionCodeGeneratorX86::GenerateFPCompare(Location lhs,
1277 Location rhs,
1278 HInstruction* insn,
1279 bool is_double) {
1280 HX86LoadFromConstantTable* const_area = insn->InputAt(1)->AsX86LoadFromConstantTable();
1281 if (is_double) {
1282 if (rhs.IsFpuRegister()) {
1283 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1284 } else if (const_area != nullptr) {
1285 DCHECK(const_area->IsEmittedAtUseSite());
1286 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(),
1287 codegen_->LiteralDoubleAddress(
1288 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
1289 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
1290 } else {
1291 DCHECK(rhs.IsDoubleStackSlot());
1292 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(), Address(ESP, rhs.GetStackIndex()));
1293 }
1294 } else {
1295 if (rhs.IsFpuRegister()) {
1296 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1297 } else if (const_area != nullptr) {
1298 DCHECK(const_area->IsEmittedAtUseSite());
1299 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(),
1300 codegen_->LiteralFloatAddress(
1301 const_area->GetConstant()->AsFloatConstant()->GetValue(),
1302 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
1303 } else {
1304 DCHECK(rhs.IsStackSlot());
1305 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(), Address(ESP, rhs.GetStackIndex()));
1306 }
1307 }
1308}
1309
Mark Mendell152408f2015-12-31 12:28:50 -05001310template<class LabelType>
David Brazdil0debae72015-11-12 18:37:00 +00001311void InstructionCodeGeneratorX86::GenerateCompareTestAndBranch(HCondition* condition,
Mark Mendell152408f2015-12-31 12:28:50 -05001312 LabelType* true_target_in,
1313 LabelType* false_target_in) {
David Brazdil0debae72015-11-12 18:37:00 +00001314 // Generated branching requires both targets to be explicit. If either of the
1315 // targets is nullptr (fallthrough) use and bind `fallthrough_target` instead.
Mark Mendell152408f2015-12-31 12:28:50 -05001316 LabelType fallthrough_target;
1317 LabelType* true_target = true_target_in == nullptr ? &fallthrough_target : true_target_in;
1318 LabelType* false_target = false_target_in == nullptr ? &fallthrough_target : false_target_in;
David Brazdil0debae72015-11-12 18:37:00 +00001319
Mark Mendellc4701932015-04-10 13:18:51 -04001320 LocationSummary* locations = condition->GetLocations();
1321 Location left = locations->InAt(0);
1322 Location right = locations->InAt(1);
1323
Mark Mendellc4701932015-04-10 13:18:51 -04001324 Primitive::Type type = condition->InputAt(0)->GetType();
1325 switch (type) {
1326 case Primitive::kPrimLong:
1327 GenerateLongComparesAndJumps(condition, true_target, false_target);
1328 break;
1329 case Primitive::kPrimFloat:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001330 GenerateFPCompare(left, right, condition, false);
Mark Mendellc4701932015-04-10 13:18:51 -04001331 GenerateFPJumps(condition, true_target, false_target);
1332 break;
1333 case Primitive::kPrimDouble:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001334 GenerateFPCompare(left, right, condition, true);
Mark Mendellc4701932015-04-10 13:18:51 -04001335 GenerateFPJumps(condition, true_target, false_target);
1336 break;
1337 default:
1338 LOG(FATAL) << "Unexpected compare type " << type;
1339 }
1340
David Brazdil0debae72015-11-12 18:37:00 +00001341 if (false_target != &fallthrough_target) {
Mark Mendellc4701932015-04-10 13:18:51 -04001342 __ jmp(false_target);
1343 }
David Brazdil0debae72015-11-12 18:37:00 +00001344
1345 if (fallthrough_target.IsLinked()) {
1346 __ Bind(&fallthrough_target);
1347 }
Mark Mendellc4701932015-04-10 13:18:51 -04001348}
1349
David Brazdil0debae72015-11-12 18:37:00 +00001350static bool AreEflagsSetFrom(HInstruction* cond, HInstruction* branch) {
1351 // Moves may affect the eflags register (move zero uses xorl), so the EFLAGS
1352 // are set only strictly before `branch`. We can't use the eflags on long/FP
1353 // conditions if they are materialized due to the complex branching.
1354 return cond->IsCondition() &&
1355 cond->GetNext() == branch &&
1356 cond->InputAt(0)->GetType() != Primitive::kPrimLong &&
1357 !Primitive::IsFloatingPointType(cond->InputAt(0)->GetType());
1358}
1359
Mark Mendell152408f2015-12-31 12:28:50 -05001360template<class LabelType>
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001361void InstructionCodeGeneratorX86::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00001362 size_t condition_input_index,
Mark Mendell152408f2015-12-31 12:28:50 -05001363 LabelType* true_target,
1364 LabelType* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00001365 HInstruction* cond = instruction->InputAt(condition_input_index);
1366
1367 if (true_target == nullptr && false_target == nullptr) {
1368 // Nothing to do. The code always falls through.
1369 return;
1370 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00001371 // Constant condition, statically compared against "true" (integer value 1).
1372 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00001373 if (true_target != nullptr) {
1374 __ jmp(true_target);
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001375 }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001376 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00001377 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00001378 if (false_target != nullptr) {
1379 __ jmp(false_target);
1380 }
1381 }
1382 return;
1383 }
1384
1385 // The following code generates these patterns:
1386 // (1) true_target == nullptr && false_target != nullptr
1387 // - opposite condition true => branch to false_target
1388 // (2) true_target != nullptr && false_target == nullptr
1389 // - condition true => branch to true_target
1390 // (3) true_target != nullptr && false_target != nullptr
1391 // - condition true => branch to true_target
1392 // - branch to false_target
1393 if (IsBooleanValueOrMaterializedCondition(cond)) {
1394 if (AreEflagsSetFrom(cond, instruction)) {
1395 if (true_target == nullptr) {
1396 __ j(X86Condition(cond->AsCondition()->GetOppositeCondition()), false_target);
1397 } else {
1398 __ j(X86Condition(cond->AsCondition()->GetCondition()), true_target);
1399 }
1400 } else {
1401 // Materialized condition, compare against 0.
1402 Location lhs = instruction->GetLocations()->InAt(condition_input_index);
1403 if (lhs.IsRegister()) {
1404 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
1405 } else {
1406 __ cmpl(Address(ESP, lhs.GetStackIndex()), Immediate(0));
1407 }
1408 if (true_target == nullptr) {
1409 __ j(kEqual, false_target);
1410 } else {
1411 __ j(kNotEqual, true_target);
1412 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001413 }
1414 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001415 // Condition has not been materialized, use its inputs as the comparison and
1416 // its condition as the branch condition.
Mark Mendellb8b97692015-05-22 16:58:19 -04001417 HCondition* condition = cond->AsCondition();
David Brazdil0debae72015-11-12 18:37:00 +00001418
1419 // If this is a long or FP comparison that has been folded into
1420 // the HCondition, generate the comparison directly.
1421 Primitive::Type type = condition->InputAt(0)->GetType();
1422 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
1423 GenerateCompareTestAndBranch(condition, true_target, false_target);
1424 return;
1425 }
1426
1427 Location lhs = condition->GetLocations()->InAt(0);
1428 Location rhs = condition->GetLocations()->InAt(1);
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001429 // LHS is guaranteed to be in a register (see LocationsBuilderX86::HandleCondition).
David Brazdil0debae72015-11-12 18:37:00 +00001430 if (rhs.IsRegister()) {
1431 __ cmpl(lhs.AsRegister<Register>(), rhs.AsRegister<Register>());
1432 } else if (rhs.IsConstant()) {
1433 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
Aart Bika19616e2016-02-01 18:57:58 -08001434 codegen_->Compare32BitValue(lhs.AsRegister<Register>(), constant);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001435 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001436 __ cmpl(lhs.AsRegister<Register>(), Address(ESP, rhs.GetStackIndex()));
1437 }
1438 if (true_target == nullptr) {
1439 __ j(X86Condition(condition->GetOppositeCondition()), false_target);
1440 } else {
Mark Mendellb8b97692015-05-22 16:58:19 -04001441 __ j(X86Condition(condition->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -07001442 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001443 }
David Brazdil0debae72015-11-12 18:37:00 +00001444
1445 // If neither branch falls through (case 3), the conditional branch to `true_target`
1446 // was already emitted (case 2) and we need to emit a jump to `false_target`.
1447 if (true_target != nullptr && false_target != nullptr) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001448 __ jmp(false_target);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001449 }
1450}
1451
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001452void LocationsBuilderX86::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001453 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
1454 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001455 locations->SetInAt(0, Location::Any());
1456 }
1457}
1458
1459void InstructionCodeGeneratorX86::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001460 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
1461 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
1462 Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
1463 nullptr : codegen_->GetLabelOf(true_successor);
1464 Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
1465 nullptr : codegen_->GetLabelOf(false_successor);
1466 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001467}
1468
1469void LocationsBuilderX86::VisitDeoptimize(HDeoptimize* deoptimize) {
1470 LocationSummary* locations = new (GetGraph()->GetArena())
1471 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Vladimir Marko239d6ea2016-09-05 10:44:04 +01001472 locations->SetCustomSlowPathCallerSaves(RegisterSet()); // No caller-save registers.
David Brazdil0debae72015-11-12 18:37:00 +00001473 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001474 locations->SetInAt(0, Location::Any());
1475 }
1476}
1477
1478void InstructionCodeGeneratorX86::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08001479 SlowPathCode* slow_path = deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathX86>(deoptimize);
David Brazdil74eb1b22015-12-14 11:44:01 +00001480 GenerateTestAndBranch<Label>(deoptimize,
1481 /* condition_input_index */ 0,
1482 slow_path->GetEntryLabel(),
1483 /* false_target */ nullptr);
1484}
1485
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001486static bool SelectCanUseCMOV(HSelect* select) {
1487 // There are no conditional move instructions for XMMs.
1488 if (Primitive::IsFloatingPointType(select->GetType())) {
1489 return false;
1490 }
1491
1492 // A FP condition doesn't generate the single CC that we need.
1493 // In 32 bit mode, a long condition doesn't generate a single CC either.
1494 HInstruction* condition = select->GetCondition();
1495 if (condition->IsCondition()) {
1496 Primitive::Type compare_type = condition->InputAt(0)->GetType();
1497 if (compare_type == Primitive::kPrimLong ||
1498 Primitive::IsFloatingPointType(compare_type)) {
1499 return false;
1500 }
1501 }
1502
1503 // We can generate a CMOV for this Select.
1504 return true;
1505}
1506
David Brazdil74eb1b22015-12-14 11:44:01 +00001507void LocationsBuilderX86::VisitSelect(HSelect* select) {
1508 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001509 if (Primitive::IsFloatingPointType(select->GetType())) {
David Brazdil74eb1b22015-12-14 11:44:01 +00001510 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001511 locations->SetInAt(1, Location::Any());
David Brazdil74eb1b22015-12-14 11:44:01 +00001512 } else {
1513 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001514 if (SelectCanUseCMOV(select)) {
1515 if (select->InputAt(1)->IsConstant()) {
1516 // Cmov can't handle a constant value.
1517 locations->SetInAt(1, Location::RequiresRegister());
1518 } else {
1519 locations->SetInAt(1, Location::Any());
1520 }
1521 } else {
1522 locations->SetInAt(1, Location::Any());
1523 }
David Brazdil74eb1b22015-12-14 11:44:01 +00001524 }
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001525 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
1526 locations->SetInAt(2, Location::RequiresRegister());
David Brazdil74eb1b22015-12-14 11:44:01 +00001527 }
1528 locations->SetOut(Location::SameAsFirstInput());
1529}
1530
Roland Levillain0b671c02016-08-19 12:02:34 +01001531void CodeGeneratorX86::GenerateIntCompare(Location lhs, Location rhs) {
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001532 Register lhs_reg = lhs.AsRegister<Register>();
1533 if (rhs.IsConstant()) {
1534 int32_t value = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
Roland Levillain0b671c02016-08-19 12:02:34 +01001535 Compare32BitValue(lhs_reg, value);
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001536 } else if (rhs.IsStackSlot()) {
Roland Levillain0b671c02016-08-19 12:02:34 +01001537 assembler_.cmpl(lhs_reg, Address(ESP, rhs.GetStackIndex()));
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001538 } else {
Roland Levillain0b671c02016-08-19 12:02:34 +01001539 assembler_.cmpl(lhs_reg, rhs.AsRegister<Register>());
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001540 }
1541}
1542
David Brazdil74eb1b22015-12-14 11:44:01 +00001543void InstructionCodeGeneratorX86::VisitSelect(HSelect* select) {
1544 LocationSummary* locations = select->GetLocations();
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001545 DCHECK(locations->InAt(0).Equals(locations->Out()));
1546 if (SelectCanUseCMOV(select)) {
1547 // If both the condition and the source types are integer, we can generate
1548 // a CMOV to implement Select.
1549
1550 HInstruction* select_condition = select->GetCondition();
1551 Condition cond = kNotEqual;
1552
1553 // Figure out how to test the 'condition'.
1554 if (select_condition->IsCondition()) {
1555 HCondition* condition = select_condition->AsCondition();
1556 if (!condition->IsEmittedAtUseSite()) {
1557 // This was a previously materialized condition.
1558 // Can we use the existing condition code?
1559 if (AreEflagsSetFrom(condition, select)) {
1560 // Materialization was the previous instruction. Condition codes are right.
1561 cond = X86Condition(condition->GetCondition());
1562 } else {
1563 // No, we have to recreate the condition code.
1564 Register cond_reg = locations->InAt(2).AsRegister<Register>();
1565 __ testl(cond_reg, cond_reg);
1566 }
1567 } else {
1568 // We can't handle FP or long here.
1569 DCHECK_NE(condition->InputAt(0)->GetType(), Primitive::kPrimLong);
1570 DCHECK(!Primitive::IsFloatingPointType(condition->InputAt(0)->GetType()));
1571 LocationSummary* cond_locations = condition->GetLocations();
Roland Levillain0b671c02016-08-19 12:02:34 +01001572 codegen_->GenerateIntCompare(cond_locations->InAt(0), cond_locations->InAt(1));
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001573 cond = X86Condition(condition->GetCondition());
1574 }
1575 } else {
1576 // Must be a boolean condition, which needs to be compared to 0.
1577 Register cond_reg = locations->InAt(2).AsRegister<Register>();
1578 __ testl(cond_reg, cond_reg);
1579 }
1580
1581 // If the condition is true, overwrite the output, which already contains false.
1582 Location false_loc = locations->InAt(0);
1583 Location true_loc = locations->InAt(1);
1584 if (select->GetType() == Primitive::kPrimLong) {
1585 // 64 bit conditional move.
1586 Register false_high = false_loc.AsRegisterPairHigh<Register>();
1587 Register false_low = false_loc.AsRegisterPairLow<Register>();
1588 if (true_loc.IsRegisterPair()) {
1589 __ cmovl(cond, false_high, true_loc.AsRegisterPairHigh<Register>());
1590 __ cmovl(cond, false_low, true_loc.AsRegisterPairLow<Register>());
1591 } else {
1592 __ cmovl(cond, false_high, Address(ESP, true_loc.GetHighStackIndex(kX86WordSize)));
1593 __ cmovl(cond, false_low, Address(ESP, true_loc.GetStackIndex()));
1594 }
1595 } else {
1596 // 32 bit conditional move.
1597 Register false_reg = false_loc.AsRegister<Register>();
1598 if (true_loc.IsRegister()) {
1599 __ cmovl(cond, false_reg, true_loc.AsRegister<Register>());
1600 } else {
1601 __ cmovl(cond, false_reg, Address(ESP, true_loc.GetStackIndex()));
1602 }
1603 }
1604 } else {
1605 NearLabel false_target;
1606 GenerateTestAndBranch<NearLabel>(
1607 select, /* condition_input_index */ 2, /* true_target */ nullptr, &false_target);
1608 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
1609 __ Bind(&false_target);
1610 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001611}
1612
David Srbecky0cf44932015-12-09 14:09:59 +00001613void LocationsBuilderX86::VisitNativeDebugInfo(HNativeDebugInfo* info) {
1614 new (GetGraph()->GetArena()) LocationSummary(info);
1615}
1616
David Srbeckyd28f4a02016-03-14 17:14:24 +00001617void InstructionCodeGeneratorX86::VisitNativeDebugInfo(HNativeDebugInfo*) {
1618 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00001619}
1620
1621void CodeGeneratorX86::GenerateNop() {
1622 __ nop();
David Srbecky0cf44932015-12-09 14:09:59 +00001623}
1624
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001625void LocationsBuilderX86::HandleCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001626 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +01001627 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Mark Mendellc4701932015-04-10 13:18:51 -04001628 // Handle the long/FP comparisons made in instruction simplification.
1629 switch (cond->InputAt(0)->GetType()) {
1630 case Primitive::kPrimLong: {
1631 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell8659e842016-02-16 10:41:46 -05001632 locations->SetInAt(1, Location::Any());
David Brazdilb3e773e2016-01-26 11:28:37 +00001633 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001634 locations->SetOut(Location::RequiresRegister());
1635 }
1636 break;
1637 }
1638 case Primitive::kPrimFloat:
1639 case Primitive::kPrimDouble: {
1640 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001641 if (cond->InputAt(1)->IsX86LoadFromConstantTable()) {
1642 DCHECK(cond->InputAt(1)->IsEmittedAtUseSite());
1643 } else if (cond->InputAt(1)->IsConstant()) {
1644 locations->SetInAt(1, Location::RequiresFpuRegister());
1645 } else {
1646 locations->SetInAt(1, Location::Any());
1647 }
David Brazdilb3e773e2016-01-26 11:28:37 +00001648 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001649 locations->SetOut(Location::RequiresRegister());
1650 }
1651 break;
1652 }
1653 default:
1654 locations->SetInAt(0, Location::RequiresRegister());
1655 locations->SetInAt(1, Location::Any());
David Brazdilb3e773e2016-01-26 11:28:37 +00001656 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001657 // We need a byte register.
1658 locations->SetOut(Location::RegisterLocation(ECX));
1659 }
1660 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001661 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001662}
1663
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001664void InstructionCodeGeneratorX86::HandleCondition(HCondition* cond) {
David Brazdilb3e773e2016-01-26 11:28:37 +00001665 if (cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001666 return;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001667 }
Mark Mendellc4701932015-04-10 13:18:51 -04001668
1669 LocationSummary* locations = cond->GetLocations();
1670 Location lhs = locations->InAt(0);
1671 Location rhs = locations->InAt(1);
1672 Register reg = locations->Out().AsRegister<Register>();
Mark Mendell152408f2015-12-31 12:28:50 -05001673 NearLabel true_label, false_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001674
1675 switch (cond->InputAt(0)->GetType()) {
1676 default: {
1677 // Integer case.
1678
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01001679 // Clear output register: setb only sets the low byte.
Mark Mendellc4701932015-04-10 13:18:51 -04001680 __ xorl(reg, reg);
Roland Levillain0b671c02016-08-19 12:02:34 +01001681 codegen_->GenerateIntCompare(lhs, rhs);
Aart Bike9f37602015-10-09 11:15:55 -07001682 __ setb(X86Condition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001683 return;
1684 }
1685 case Primitive::kPrimLong:
1686 GenerateLongComparesAndJumps(cond, &true_label, &false_label);
1687 break;
1688 case Primitive::kPrimFloat:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001689 GenerateFPCompare(lhs, rhs, cond, false);
Mark Mendellc4701932015-04-10 13:18:51 -04001690 GenerateFPJumps(cond, &true_label, &false_label);
1691 break;
1692 case Primitive::kPrimDouble:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001693 GenerateFPCompare(lhs, rhs, cond, true);
Mark Mendellc4701932015-04-10 13:18:51 -04001694 GenerateFPJumps(cond, &true_label, &false_label);
1695 break;
1696 }
1697
1698 // Convert the jumps into the result.
Mark Mendell0c9497d2015-08-21 09:30:05 -04001699 NearLabel done_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001700
Roland Levillain4fa13f62015-07-06 18:11:54 +01001701 // False case: result = 0.
Mark Mendellc4701932015-04-10 13:18:51 -04001702 __ Bind(&false_label);
1703 __ xorl(reg, reg);
1704 __ jmp(&done_label);
1705
Roland Levillain4fa13f62015-07-06 18:11:54 +01001706 // True case: result = 1.
Mark Mendellc4701932015-04-10 13:18:51 -04001707 __ Bind(&true_label);
1708 __ movl(reg, Immediate(1));
1709 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07001710}
1711
1712void LocationsBuilderX86::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001713 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001714}
1715
1716void InstructionCodeGeneratorX86::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001717 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001718}
1719
1720void LocationsBuilderX86::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001721 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001722}
1723
1724void InstructionCodeGeneratorX86::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001725 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001726}
1727
1728void LocationsBuilderX86::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001729 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001730}
1731
1732void InstructionCodeGeneratorX86::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001733 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001734}
1735
1736void LocationsBuilderX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001737 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001738}
1739
1740void InstructionCodeGeneratorX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001741 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001742}
1743
1744void LocationsBuilderX86::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001745 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001746}
1747
1748void InstructionCodeGeneratorX86::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001749 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001750}
1751
1752void LocationsBuilderX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001753 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001754}
1755
1756void InstructionCodeGeneratorX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001757 HandleCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001758}
1759
Aart Bike9f37602015-10-09 11:15:55 -07001760void LocationsBuilderX86::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001761 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001762}
1763
1764void InstructionCodeGeneratorX86::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001765 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001766}
1767
1768void LocationsBuilderX86::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001769 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001770}
1771
1772void InstructionCodeGeneratorX86::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001773 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001774}
1775
1776void LocationsBuilderX86::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001777 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001778}
1779
1780void InstructionCodeGeneratorX86::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001781 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001782}
1783
1784void LocationsBuilderX86::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001785 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001786}
1787
1788void InstructionCodeGeneratorX86::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001789 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001790}
1791
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001792void LocationsBuilderX86::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001793 LocationSummary* locations =
1794 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001795 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001796}
1797
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001798void InstructionCodeGeneratorX86::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001799 // Will be generated at use site.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001800}
1801
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001802void LocationsBuilderX86::VisitNullConstant(HNullConstant* constant) {
1803 LocationSummary* locations =
1804 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1805 locations->SetOut(Location::ConstantLocation(constant));
1806}
1807
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001808void InstructionCodeGeneratorX86::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001809 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001810}
1811
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001812void LocationsBuilderX86::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001813 LocationSummary* locations =
1814 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001815 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001816}
1817
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001818void InstructionCodeGeneratorX86::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001819 // Will be generated at use site.
1820}
1821
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001822void LocationsBuilderX86::VisitFloatConstant(HFloatConstant* constant) {
1823 LocationSummary* locations =
1824 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1825 locations->SetOut(Location::ConstantLocation(constant));
1826}
1827
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001828void InstructionCodeGeneratorX86::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001829 // Will be generated at use site.
1830}
1831
1832void LocationsBuilderX86::VisitDoubleConstant(HDoubleConstant* constant) {
1833 LocationSummary* locations =
1834 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1835 locations->SetOut(Location::ConstantLocation(constant));
1836}
1837
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001838void InstructionCodeGeneratorX86::VisitDoubleConstant(HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001839 // Will be generated at use site.
1840}
1841
Calin Juravle27df7582015-04-17 19:12:31 +01001842void LocationsBuilderX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1843 memory_barrier->SetLocations(nullptr);
1844}
1845
1846void InstructionCodeGeneratorX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00001847 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01001848}
1849
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001850void LocationsBuilderX86::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001851 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001852}
1853
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001854void InstructionCodeGeneratorX86::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001855 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001856}
1857
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001858void LocationsBuilderX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001859 LocationSummary* locations =
1860 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001861 switch (ret->InputAt(0)->GetType()) {
1862 case Primitive::kPrimBoolean:
1863 case Primitive::kPrimByte:
1864 case Primitive::kPrimChar:
1865 case Primitive::kPrimShort:
1866 case Primitive::kPrimInt:
1867 case Primitive::kPrimNot:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001868 locations->SetInAt(0, Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001869 break;
1870
1871 case Primitive::kPrimLong:
1872 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001873 0, Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001874 break;
1875
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001876 case Primitive::kPrimFloat:
1877 case Primitive::kPrimDouble:
1878 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001879 0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001880 break;
1881
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001882 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001883 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001884 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001885}
1886
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001887void InstructionCodeGeneratorX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001888 if (kIsDebugBuild) {
1889 switch (ret->InputAt(0)->GetType()) {
1890 case Primitive::kPrimBoolean:
1891 case Primitive::kPrimByte:
1892 case Primitive::kPrimChar:
1893 case Primitive::kPrimShort:
1894 case Primitive::kPrimInt:
1895 case Primitive::kPrimNot:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001896 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<Register>(), EAX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001897 break;
1898
1899 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001900 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairLow<Register>(), EAX);
1901 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairHigh<Register>(), EDX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001902 break;
1903
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001904 case Primitive::kPrimFloat:
1905 case Primitive::kPrimDouble:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001906 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>(), XMM0);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001907 break;
1908
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001909 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001910 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001911 }
1912 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001913 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001914}
1915
Calin Juravle175dc732015-08-25 15:42:32 +01001916void LocationsBuilderX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
1917 // The trampoline uses the same calling convention as dex calling conventions,
1918 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
1919 // the method_idx.
1920 HandleInvoke(invoke);
1921}
1922
1923void InstructionCodeGeneratorX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
1924 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
1925}
1926
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001927void LocationsBuilderX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00001928 // Explicit clinit checks triggered by static invokes must have been pruned by
1929 // art::PrepareForRegisterAllocation.
1930 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001931
Mark Mendellfb8d2792015-03-31 22:16:59 -04001932 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001933 if (intrinsic.TryDispatch(invoke)) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00001934 if (invoke->GetLocations()->CanCall() && invoke->HasPcRelativeDexCache()) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00001935 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00001936 }
Mark Mendell09ed1a32015-03-25 08:30:06 -04001937 return;
1938 }
1939
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001940 HandleInvoke(invoke);
Nicolas Geoffray94015b92015-06-04 18:21:04 +01001941
Vladimir Marko0f7dca42015-11-02 14:36:43 +00001942 // For PC-relative dex cache the invoke has an extra input, the PC-relative address base.
1943 if (invoke->HasPcRelativeDexCache()) {
Vladimir Markob4536b72015-11-24 13:45:23 +00001944 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00001945 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001946}
1947
Mark Mendell09ed1a32015-03-25 08:30:06 -04001948static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86* codegen) {
1949 if (invoke->GetLocations()->Intrinsified()) {
1950 IntrinsicCodeGeneratorX86 intrinsic(codegen);
1951 intrinsic.Dispatch(invoke);
1952 return true;
1953 }
1954 return false;
1955}
1956
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001957void InstructionCodeGeneratorX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00001958 // Explicit clinit checks triggered by static invokes must have been pruned by
1959 // art::PrepareForRegisterAllocation.
1960 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001961
Mark Mendell09ed1a32015-03-25 08:30:06 -04001962 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1963 return;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001964 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001965
Nicolas Geoffray94015b92015-06-04 18:21:04 +01001966 LocationSummary* locations = invoke->GetLocations();
Mark Mendell09ed1a32015-03-25 08:30:06 -04001967 codegen_->GenerateStaticOrDirectCall(
Nicolas Geoffray94015b92015-06-04 18:21:04 +01001968 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Mingyao Yang8693fe12015-04-17 16:51:08 -07001969 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001970}
1971
1972void LocationsBuilderX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00001973 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
1974 if (intrinsic.TryDispatch(invoke)) {
1975 return;
1976 }
1977
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001978 HandleInvoke(invoke);
1979}
1980
1981void LocationsBuilderX86::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001982 InvokeDexCallingConventionVisitorX86 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001983 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001984}
1985
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001986void InstructionCodeGeneratorX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampebfb5ba92015-09-01 15:45:02 +00001987 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1988 return;
1989 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001990
Andreas Gampebfb5ba92015-09-01 15:45:02 +00001991 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001992 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001993 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001994}
1995
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001996void LocationsBuilderX86::VisitInvokeInterface(HInvokeInterface* invoke) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00001997 // This call to HandleInvoke allocates a temporary (core) register
1998 // which is also used to transfer the hidden argument from FP to
1999 // core register.
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002000 HandleInvoke(invoke);
2001 // Add the hidden argument.
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002002 invoke->GetLocations()->AddTemp(Location::FpuRegisterLocation(XMM7));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002003}
2004
2005void InstructionCodeGeneratorX86::VisitInvokeInterface(HInvokeInterface* invoke) {
2006 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain0d5a2812015-11-13 10:07:31 +00002007 LocationSummary* locations = invoke->GetLocations();
2008 Register temp = locations->GetTemp(0).AsRegister<Register>();
2009 XmmRegister hidden_reg = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002010 Location receiver = locations->InAt(0);
2011 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2012
Roland Levillain0d5a2812015-11-13 10:07:31 +00002013 // Set the hidden argument. This is safe to do this here, as XMM7
2014 // won't be modified thereafter, before the `call` instruction.
2015 DCHECK_EQ(XMM7, hidden_reg);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002016 __ movl(temp, Immediate(invoke->GetDexMethodIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002017 __ movd(hidden_reg, temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002018
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002019 if (receiver.IsStackSlot()) {
2020 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002021 // /* HeapReference<Class> */ temp = temp->klass_
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002022 __ movl(temp, Address(temp, class_offset));
2023 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002024 // /* HeapReference<Class> */ temp = receiver->klass_
Roland Levillain271ab9c2014-11-27 15:23:57 +00002025 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002026 }
Roland Levillain4d027112015-07-01 15:41:14 +01002027 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00002028 // Instead of simply (possibly) unpoisoning `temp` here, we should
2029 // emit a read barrier for the previous class reference load.
2030 // However this is not required in practice, as this is an
2031 // intermediate/temporary reference and because the current
2032 // concurrent copying collector keeps the from-space memory
2033 // intact/accessible until the end of the marking phase (the
2034 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01002035 __ MaybeUnpoisonHeapReference(temp);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002036 // temp = temp->GetAddressOfIMT()
2037 __ movl(temp,
2038 Address(temp, mirror::Class::ImtPtrOffset(kX86PointerSize).Uint32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002039 // temp = temp->GetImtEntryAt(method_offset);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002040 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00002041 invoke->GetImtIndex(), kX86PointerSize));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002042 __ movl(temp, Address(temp, method_offset));
2043 // call temp->GetEntryPoint();
Roland Levillain0d5a2812015-11-13 10:07:31 +00002044 __ call(Address(temp,
Andreas Gampe542451c2016-07-26 09:02:02 -07002045 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86PointerSize).Int32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002046
2047 DCHECK(!codegen_->IsLeafMethod());
2048 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2049}
2050
Roland Levillain88cb1752014-10-20 16:36:47 +01002051void LocationsBuilderX86::VisitNeg(HNeg* neg) {
2052 LocationSummary* locations =
2053 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2054 switch (neg->GetResultType()) {
2055 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002056 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01002057 locations->SetInAt(0, Location::RequiresRegister());
2058 locations->SetOut(Location::SameAsFirstInput());
2059 break;
2060
Roland Levillain88cb1752014-10-20 16:36:47 +01002061 case Primitive::kPrimFloat:
Roland Levillain5368c212014-11-27 15:03:41 +00002062 locations->SetInAt(0, Location::RequiresFpuRegister());
2063 locations->SetOut(Location::SameAsFirstInput());
2064 locations->AddTemp(Location::RequiresRegister());
2065 locations->AddTemp(Location::RequiresFpuRegister());
2066 break;
2067
Roland Levillain88cb1752014-10-20 16:36:47 +01002068 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002069 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00002070 locations->SetOut(Location::SameAsFirstInput());
2071 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01002072 break;
2073
2074 default:
2075 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2076 }
2077}
2078
2079void InstructionCodeGeneratorX86::VisitNeg(HNeg* neg) {
2080 LocationSummary* locations = neg->GetLocations();
2081 Location out = locations->Out();
2082 Location in = locations->InAt(0);
2083 switch (neg->GetResultType()) {
2084 case Primitive::kPrimInt:
2085 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002086 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002087 __ negl(out.AsRegister<Register>());
Roland Levillain88cb1752014-10-20 16:36:47 +01002088 break;
2089
2090 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002091 DCHECK(in.IsRegisterPair());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002092 DCHECK(in.Equals(out));
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002093 __ negl(out.AsRegisterPairLow<Register>());
2094 // Negation is similar to subtraction from zero. The least
2095 // significant byte triggers a borrow when it is different from
2096 // zero; to take it into account, add 1 to the most significant
2097 // byte if the carry flag (CF) is set to 1 after the first NEGL
2098 // operation.
2099 __ adcl(out.AsRegisterPairHigh<Register>(), Immediate(0));
2100 __ negl(out.AsRegisterPairHigh<Register>());
2101 break;
2102
Roland Levillain5368c212014-11-27 15:03:41 +00002103 case Primitive::kPrimFloat: {
2104 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002105 Register constant = locations->GetTemp(0).AsRegister<Register>();
2106 XmmRegister mask = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002107 // Implement float negation with an exclusive or with value
2108 // 0x80000000 (mask for bit 31, representing the sign of a
2109 // single-precision floating-point number).
2110 __ movl(constant, Immediate(INT32_C(0x80000000)));
2111 __ movd(mask, constant);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002112 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00002113 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002114 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00002115
Roland Levillain5368c212014-11-27 15:03:41 +00002116 case Primitive::kPrimDouble: {
2117 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002118 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002119 // Implement double negation with an exclusive or with value
2120 // 0x8000000000000000 (mask for bit 63, representing the sign of
2121 // a double-precision floating-point number).
2122 __ LoadLongConstant(mask, INT64_C(0x8000000000000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002123 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01002124 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002125 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002126
2127 default:
2128 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2129 }
2130}
2131
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002132void LocationsBuilderX86::VisitX86FPNeg(HX86FPNeg* neg) {
2133 LocationSummary* locations =
2134 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2135 DCHECK(Primitive::IsFloatingPointType(neg->GetType()));
2136 locations->SetInAt(0, Location::RequiresFpuRegister());
2137 locations->SetInAt(1, Location::RequiresRegister());
2138 locations->SetOut(Location::SameAsFirstInput());
2139 locations->AddTemp(Location::RequiresFpuRegister());
2140}
2141
2142void InstructionCodeGeneratorX86::VisitX86FPNeg(HX86FPNeg* neg) {
2143 LocationSummary* locations = neg->GetLocations();
2144 Location out = locations->Out();
2145 DCHECK(locations->InAt(0).Equals(out));
2146
2147 Register constant_area = locations->InAt(1).AsRegister<Register>();
2148 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
2149 if (neg->GetType() == Primitive::kPrimFloat) {
2150 __ movss(mask, codegen_->LiteralInt32Address(INT32_C(0x80000000), constant_area));
2151 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
2152 } else {
2153 __ movsd(mask, codegen_->LiteralInt64Address(INT64_C(0x8000000000000000), constant_area));
2154 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
2155 }
2156}
2157
Roland Levillaindff1f282014-11-05 14:15:05 +00002158void LocationsBuilderX86::VisitTypeConversion(HTypeConversion* conversion) {
Roland Levillaindff1f282014-11-05 14:15:05 +00002159 Primitive::Type result_type = conversion->GetResultType();
2160 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002161 DCHECK_NE(result_type, input_type);
Roland Levillain624279f2014-12-04 11:54:28 +00002162
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002163 // The float-to-long and double-to-long type conversions rely on a
2164 // call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00002165 LocationSummary::CallKind call_kind =
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002166 ((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
2167 && result_type == Primitive::kPrimLong)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01002168 ? LocationSummary::kCallOnMainOnly
Roland Levillain624279f2014-12-04 11:54:28 +00002169 : LocationSummary::kNoCall;
2170 LocationSummary* locations =
2171 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
2172
David Brazdilb2bd1c52015-03-25 11:17:37 +00002173 // The Java language does not allow treating boolean as an integral type but
2174 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00002175
Roland Levillaindff1f282014-11-05 14:15:05 +00002176 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002177 case Primitive::kPrimByte:
2178 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002179 case Primitive::kPrimLong: {
2180 // Type conversion from long to byte is a result of code transformations.
2181 HInstruction* input = conversion->InputAt(0);
2182 Location input_location = input->IsConstant()
2183 ? Location::ConstantLocation(input->AsConstant())
2184 : Location::RegisterPairLocation(EAX, EDX);
2185 locations->SetInAt(0, input_location);
2186 // Make the output overlap to please the register allocator. This greatly simplifies
2187 // the validation of the linear scan implementation
2188 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2189 break;
2190 }
David Brazdil46e2a392015-03-16 17:31:52 +00002191 case Primitive::kPrimBoolean:
2192 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002193 case Primitive::kPrimShort:
2194 case Primitive::kPrimInt:
2195 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002196 // Processing a Dex `int-to-byte' instruction.
Mark Mendell5f874182015-03-04 15:42:45 -05002197 locations->SetInAt(0, Location::ByteRegisterOrConstant(ECX, conversion->InputAt(0)));
2198 // Make the output overlap to please the register allocator. This greatly simplifies
2199 // the validation of the linear scan implementation
2200 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Roland Levillain51d3fc42014-11-13 14:11:42 +00002201 break;
2202
2203 default:
2204 LOG(FATAL) << "Unexpected type conversion from " << input_type
2205 << " to " << result_type;
2206 }
2207 break;
2208
Roland Levillain01a8d712014-11-14 16:27:39 +00002209 case Primitive::kPrimShort:
2210 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002211 case Primitive::kPrimLong:
2212 // Type conversion from long to short is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002213 case Primitive::kPrimBoolean:
2214 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002215 case Primitive::kPrimByte:
2216 case Primitive::kPrimInt:
2217 case Primitive::kPrimChar:
2218 // Processing a Dex `int-to-short' instruction.
2219 locations->SetInAt(0, Location::Any());
2220 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2221 break;
2222
2223 default:
2224 LOG(FATAL) << "Unexpected type conversion from " << input_type
2225 << " to " << result_type;
2226 }
2227 break;
2228
Roland Levillain946e1432014-11-11 17:35:19 +00002229 case Primitive::kPrimInt:
2230 switch (input_type) {
2231 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002232 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002233 locations->SetInAt(0, Location::Any());
2234 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2235 break;
2236
2237 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00002238 // Processing a Dex `float-to-int' instruction.
2239 locations->SetInAt(0, Location::RequiresFpuRegister());
2240 locations->SetOut(Location::RequiresRegister());
2241 locations->AddTemp(Location::RequiresFpuRegister());
2242 break;
2243
Roland Levillain946e1432014-11-11 17:35:19 +00002244 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002245 // Processing a Dex `double-to-int' instruction.
2246 locations->SetInAt(0, Location::RequiresFpuRegister());
2247 locations->SetOut(Location::RequiresRegister());
2248 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00002249 break;
2250
2251 default:
2252 LOG(FATAL) << "Unexpected type conversion from " << input_type
2253 << " to " << result_type;
2254 }
2255 break;
2256
Roland Levillaindff1f282014-11-05 14:15:05 +00002257 case Primitive::kPrimLong:
2258 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002259 case Primitive::kPrimBoolean:
2260 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002261 case Primitive::kPrimByte:
2262 case Primitive::kPrimShort:
2263 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002264 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002265 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002266 locations->SetInAt(0, Location::RegisterLocation(EAX));
2267 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
2268 break;
2269
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002270 case Primitive::kPrimFloat:
Vladimir Marko949c91f2015-01-27 10:48:44 +00002271 case Primitive::kPrimDouble: {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002272 // Processing a Dex `float-to-long' or 'double-to-long' instruction.
Vladimir Marko949c91f2015-01-27 10:48:44 +00002273 InvokeRuntimeCallingConvention calling_convention;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002274 XmmRegister parameter = calling_convention.GetFpuRegisterAt(0);
2275 locations->SetInAt(0, Location::FpuRegisterLocation(parameter));
2276
Vladimir Marko949c91f2015-01-27 10:48:44 +00002277 // The runtime helper puts the result in EAX, EDX.
2278 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Vladimir Marko949c91f2015-01-27 10:48:44 +00002279 }
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002280 break;
Roland Levillaindff1f282014-11-05 14:15:05 +00002281
2282 default:
2283 LOG(FATAL) << "Unexpected type conversion from " << input_type
2284 << " to " << result_type;
2285 }
2286 break;
2287
Roland Levillain981e4542014-11-14 11:47:14 +00002288 case Primitive::kPrimChar:
2289 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002290 case Primitive::kPrimLong:
2291 // Type conversion from long to char is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002292 case Primitive::kPrimBoolean:
2293 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002294 case Primitive::kPrimByte:
2295 case Primitive::kPrimShort:
2296 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002297 // Processing a Dex `int-to-char' instruction.
2298 locations->SetInAt(0, Location::Any());
2299 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2300 break;
2301
2302 default:
2303 LOG(FATAL) << "Unexpected type conversion from " << input_type
2304 << " to " << result_type;
2305 }
2306 break;
2307
Roland Levillaindff1f282014-11-05 14:15:05 +00002308 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002309 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002310 case Primitive::kPrimBoolean:
2311 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002312 case Primitive::kPrimByte:
2313 case Primitive::kPrimShort:
2314 case Primitive::kPrimInt:
2315 case Primitive::kPrimChar:
2316 // Processing a Dex `int-to-float' instruction.
2317 locations->SetInAt(0, Location::RequiresRegister());
2318 locations->SetOut(Location::RequiresFpuRegister());
2319 break;
2320
2321 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002322 // Processing a Dex `long-to-float' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002323 locations->SetInAt(0, Location::Any());
2324 locations->SetOut(Location::Any());
Roland Levillain6d0e4832014-11-27 18:31:21 +00002325 break;
2326
Roland Levillaincff13742014-11-17 14:32:17 +00002327 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002328 // Processing a Dex `double-to-float' instruction.
2329 locations->SetInAt(0, Location::RequiresFpuRegister());
2330 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002331 break;
2332
2333 default:
2334 LOG(FATAL) << "Unexpected type conversion from " << input_type
2335 << " to " << result_type;
2336 };
2337 break;
2338
Roland Levillaindff1f282014-11-05 14:15:05 +00002339 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002340 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002341 case Primitive::kPrimBoolean:
2342 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002343 case Primitive::kPrimByte:
2344 case Primitive::kPrimShort:
2345 case Primitive::kPrimInt:
2346 case Primitive::kPrimChar:
2347 // Processing a Dex `int-to-double' instruction.
2348 locations->SetInAt(0, Location::RequiresRegister());
2349 locations->SetOut(Location::RequiresFpuRegister());
2350 break;
2351
2352 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002353 // Processing a Dex `long-to-double' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002354 locations->SetInAt(0, Location::Any());
2355 locations->SetOut(Location::Any());
Roland Levillain647b9ed2014-11-27 12:06:00 +00002356 break;
2357
Roland Levillaincff13742014-11-17 14:32:17 +00002358 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002359 // Processing a Dex `float-to-double' instruction.
2360 locations->SetInAt(0, Location::RequiresFpuRegister());
2361 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002362 break;
2363
2364 default:
2365 LOG(FATAL) << "Unexpected type conversion from " << input_type
2366 << " to " << result_type;
2367 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002368 break;
2369
2370 default:
2371 LOG(FATAL) << "Unexpected type conversion from " << input_type
2372 << " to " << result_type;
2373 }
2374}
2375
2376void InstructionCodeGeneratorX86::VisitTypeConversion(HTypeConversion* conversion) {
2377 LocationSummary* locations = conversion->GetLocations();
2378 Location out = locations->Out();
2379 Location in = locations->InAt(0);
2380 Primitive::Type result_type = conversion->GetResultType();
2381 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002382 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00002383 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002384 case Primitive::kPrimByte:
2385 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002386 case Primitive::kPrimLong:
2387 // Type conversion from long to byte is a result of code transformations.
2388 if (in.IsRegisterPair()) {
2389 __ movsxb(out.AsRegister<Register>(), in.AsRegisterPairLow<ByteRegister>());
2390 } else {
2391 DCHECK(in.GetConstant()->IsLongConstant());
2392 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2393 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
2394 }
2395 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002396 case Primitive::kPrimBoolean:
2397 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002398 case Primitive::kPrimShort:
2399 case Primitive::kPrimInt:
2400 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002401 // Processing a Dex `int-to-byte' instruction.
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002402 if (in.IsRegister()) {
2403 __ movsxb(out.AsRegister<Register>(), in.AsRegister<ByteRegister>());
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002404 } else {
2405 DCHECK(in.GetConstant()->IsIntConstant());
2406 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
2407 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
2408 }
Roland Levillain51d3fc42014-11-13 14:11:42 +00002409 break;
2410
2411 default:
2412 LOG(FATAL) << "Unexpected type conversion from " << input_type
2413 << " to " << result_type;
2414 }
2415 break;
2416
Roland Levillain01a8d712014-11-14 16:27:39 +00002417 case Primitive::kPrimShort:
2418 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002419 case Primitive::kPrimLong:
2420 // Type conversion from long to short is a result of code transformations.
2421 if (in.IsRegisterPair()) {
2422 __ movsxw(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
2423 } else if (in.IsDoubleStackSlot()) {
2424 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
2425 } else {
2426 DCHECK(in.GetConstant()->IsLongConstant());
2427 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2428 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
2429 }
2430 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002431 case Primitive::kPrimBoolean:
2432 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002433 case Primitive::kPrimByte:
2434 case Primitive::kPrimInt:
2435 case Primitive::kPrimChar:
2436 // Processing a Dex `int-to-short' instruction.
2437 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002438 __ movsxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain01a8d712014-11-14 16:27:39 +00002439 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002440 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain01a8d712014-11-14 16:27:39 +00002441 } else {
2442 DCHECK(in.GetConstant()->IsIntConstant());
2443 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002444 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
Roland Levillain01a8d712014-11-14 16:27:39 +00002445 }
2446 break;
2447
2448 default:
2449 LOG(FATAL) << "Unexpected type conversion from " << input_type
2450 << " to " << result_type;
2451 }
2452 break;
2453
Roland Levillain946e1432014-11-11 17:35:19 +00002454 case Primitive::kPrimInt:
2455 switch (input_type) {
2456 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002457 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002458 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002459 __ movl(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00002460 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002461 __ movl(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain946e1432014-11-11 17:35:19 +00002462 } else {
2463 DCHECK(in.IsConstant());
2464 DCHECK(in.GetConstant()->IsLongConstant());
2465 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002466 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00002467 }
2468 break;
2469
Roland Levillain3f8f9362014-12-02 17:45:01 +00002470 case Primitive::kPrimFloat: {
2471 // Processing a Dex `float-to-int' instruction.
2472 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2473 Register output = out.AsRegister<Register>();
2474 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002475 NearLabel done, nan;
Roland Levillain3f8f9362014-12-02 17:45:01 +00002476
2477 __ movl(output, Immediate(kPrimIntMax));
2478 // temp = int-to-float(output)
2479 __ cvtsi2ss(temp, output);
2480 // if input >= temp goto done
2481 __ comiss(input, temp);
2482 __ j(kAboveEqual, &done);
2483 // if input == NaN goto nan
2484 __ j(kUnordered, &nan);
2485 // output = float-to-int-truncate(input)
2486 __ cvttss2si(output, input);
2487 __ jmp(&done);
2488 __ Bind(&nan);
2489 // output = 0
2490 __ xorl(output, output);
2491 __ Bind(&done);
2492 break;
2493 }
2494
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002495 case Primitive::kPrimDouble: {
2496 // Processing a Dex `double-to-int' instruction.
2497 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2498 Register output = out.AsRegister<Register>();
2499 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002500 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002501
2502 __ movl(output, Immediate(kPrimIntMax));
2503 // temp = int-to-double(output)
2504 __ cvtsi2sd(temp, output);
2505 // if input >= temp goto done
2506 __ comisd(input, temp);
2507 __ j(kAboveEqual, &done);
2508 // if input == NaN goto nan
2509 __ j(kUnordered, &nan);
2510 // output = double-to-int-truncate(input)
2511 __ cvttsd2si(output, input);
2512 __ jmp(&done);
2513 __ Bind(&nan);
2514 // output = 0
2515 __ xorl(output, output);
2516 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00002517 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002518 }
Roland Levillain946e1432014-11-11 17:35:19 +00002519
2520 default:
2521 LOG(FATAL) << "Unexpected type conversion from " << input_type
2522 << " to " << result_type;
2523 }
2524 break;
2525
Roland Levillaindff1f282014-11-05 14:15:05 +00002526 case Primitive::kPrimLong:
2527 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002528 case Primitive::kPrimBoolean:
2529 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002530 case Primitive::kPrimByte:
2531 case Primitive::kPrimShort:
2532 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002533 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002534 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002535 DCHECK_EQ(out.AsRegisterPairLow<Register>(), EAX);
2536 DCHECK_EQ(out.AsRegisterPairHigh<Register>(), EDX);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002537 DCHECK_EQ(in.AsRegister<Register>(), EAX);
Roland Levillaindff1f282014-11-05 14:15:05 +00002538 __ cdq();
2539 break;
2540
2541 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00002542 // Processing a Dex `float-to-long' instruction.
Serban Constantinescuba45db02016-07-12 22:53:02 +01002543 codegen_->InvokeRuntime(kQuickF2l, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00002544 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
Roland Levillain624279f2014-12-04 11:54:28 +00002545 break;
2546
Roland Levillaindff1f282014-11-05 14:15:05 +00002547 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002548 // Processing a Dex `double-to-long' instruction.
Serban Constantinescuba45db02016-07-12 22:53:02 +01002549 codegen_->InvokeRuntime(kQuickD2l, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00002550 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
Roland Levillaindff1f282014-11-05 14:15:05 +00002551 break;
2552
2553 default:
2554 LOG(FATAL) << "Unexpected type conversion from " << input_type
2555 << " to " << result_type;
2556 }
2557 break;
2558
Roland Levillain981e4542014-11-14 11:47:14 +00002559 case Primitive::kPrimChar:
2560 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002561 case Primitive::kPrimLong:
2562 // Type conversion from long to short is a result of code transformations.
2563 if (in.IsRegisterPair()) {
2564 __ movzxw(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
2565 } else if (in.IsDoubleStackSlot()) {
2566 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
2567 } else {
2568 DCHECK(in.GetConstant()->IsLongConstant());
2569 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2570 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
2571 }
2572 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002573 case Primitive::kPrimBoolean:
2574 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002575 case Primitive::kPrimByte:
2576 case Primitive::kPrimShort:
2577 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002578 // Processing a Dex `Process a Dex `int-to-char'' instruction.
2579 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002580 __ movzxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain981e4542014-11-14 11:47:14 +00002581 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002582 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain981e4542014-11-14 11:47:14 +00002583 } else {
2584 DCHECK(in.GetConstant()->IsIntConstant());
2585 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002586 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
Roland Levillain981e4542014-11-14 11:47:14 +00002587 }
2588 break;
2589
2590 default:
2591 LOG(FATAL) << "Unexpected type conversion from " << input_type
2592 << " to " << result_type;
2593 }
2594 break;
2595
Roland Levillaindff1f282014-11-05 14:15:05 +00002596 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002597 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002598 case Primitive::kPrimBoolean:
2599 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002600 case Primitive::kPrimByte:
2601 case Primitive::kPrimShort:
2602 case Primitive::kPrimInt:
2603 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002604 // Processing a Dex `int-to-float' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002605 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002606 break;
2607
Roland Levillain6d0e4832014-11-27 18:31:21 +00002608 case Primitive::kPrimLong: {
2609 // Processing a Dex `long-to-float' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002610 size_t adjustment = 0;
Roland Levillain6d0e4832014-11-27 18:31:21 +00002611
Roland Levillain232ade02015-04-20 15:14:36 +01002612 // Create stack space for the call to
2613 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstps below.
2614 // TODO: enhance register allocator to ask for stack temporaries.
2615 if (!in.IsDoubleStackSlot() || !out.IsStackSlot()) {
2616 adjustment = Primitive::ComponentSize(Primitive::kPrimLong);
2617 __ subl(ESP, Immediate(adjustment));
2618 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002619
Roland Levillain232ade02015-04-20 15:14:36 +01002620 // Load the value to the FP stack, using temporaries if needed.
2621 PushOntoFPStack(in, 0, adjustment, false, true);
2622
2623 if (out.IsStackSlot()) {
2624 __ fstps(Address(ESP, out.GetStackIndex() + adjustment));
2625 } else {
2626 __ fstps(Address(ESP, 0));
2627 Location stack_temp = Location::StackSlot(0);
2628 codegen_->Move32(out, stack_temp);
2629 }
2630
2631 // Remove the temporary stack space we allocated.
2632 if (adjustment != 0) {
2633 __ addl(ESP, Immediate(adjustment));
2634 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002635 break;
2636 }
2637
Roland Levillaincff13742014-11-17 14:32:17 +00002638 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002639 // Processing a Dex `double-to-float' instruction.
2640 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002641 break;
2642
2643 default:
2644 LOG(FATAL) << "Unexpected type conversion from " << input_type
2645 << " to " << result_type;
2646 };
2647 break;
2648
Roland Levillaindff1f282014-11-05 14:15:05 +00002649 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002650 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002651 case Primitive::kPrimBoolean:
2652 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002653 case Primitive::kPrimByte:
2654 case Primitive::kPrimShort:
2655 case Primitive::kPrimInt:
2656 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002657 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002658 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002659 break;
2660
Roland Levillain647b9ed2014-11-27 12:06:00 +00002661 case Primitive::kPrimLong: {
2662 // Processing a Dex `long-to-double' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002663 size_t adjustment = 0;
Roland Levillain647b9ed2014-11-27 12:06:00 +00002664
Roland Levillain232ade02015-04-20 15:14:36 +01002665 // Create stack space for the call to
2666 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstpl below.
2667 // TODO: enhance register allocator to ask for stack temporaries.
2668 if (!in.IsDoubleStackSlot() || !out.IsDoubleStackSlot()) {
2669 adjustment = Primitive::ComponentSize(Primitive::kPrimLong);
2670 __ subl(ESP, Immediate(adjustment));
2671 }
2672
2673 // Load the value to the FP stack, using temporaries if needed.
2674 PushOntoFPStack(in, 0, adjustment, false, true);
2675
2676 if (out.IsDoubleStackSlot()) {
2677 __ fstpl(Address(ESP, out.GetStackIndex() + adjustment));
2678 } else {
2679 __ fstpl(Address(ESP, 0));
2680 Location stack_temp = Location::DoubleStackSlot(0);
2681 codegen_->Move64(out, stack_temp);
2682 }
2683
2684 // Remove the temporary stack space we allocated.
2685 if (adjustment != 0) {
2686 __ addl(ESP, Immediate(adjustment));
2687 }
Roland Levillain647b9ed2014-11-27 12:06:00 +00002688 break;
2689 }
2690
Roland Levillaincff13742014-11-17 14:32:17 +00002691 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002692 // Processing a Dex `float-to-double' instruction.
2693 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002694 break;
2695
2696 default:
2697 LOG(FATAL) << "Unexpected type conversion from " << input_type
2698 << " to " << result_type;
2699 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002700 break;
2701
2702 default:
2703 LOG(FATAL) << "Unexpected type conversion from " << input_type
2704 << " to " << result_type;
2705 }
2706}
2707
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002708void LocationsBuilderX86::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002709 LocationSummary* locations =
2710 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002711 switch (add->GetResultType()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002712 case Primitive::kPrimInt: {
2713 locations->SetInAt(0, Location::RequiresRegister());
2714 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
2715 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2716 break;
2717 }
2718
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002719 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002720 locations->SetInAt(0, Location::RequiresRegister());
2721 locations->SetInAt(1, Location::Any());
2722 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002723 break;
2724 }
2725
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002726 case Primitive::kPrimFloat:
2727 case Primitive::kPrimDouble: {
2728 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002729 if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
2730 DCHECK(add->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00002731 } else if (add->InputAt(1)->IsConstant()) {
2732 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002733 } else {
2734 locations->SetInAt(1, Location::Any());
2735 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002736 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002737 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002738 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002739
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002740 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002741 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
2742 break;
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002743 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002744}
2745
2746void InstructionCodeGeneratorX86::VisitAdd(HAdd* add) {
2747 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002748 Location first = locations->InAt(0);
2749 Location second = locations->InAt(1);
Mark Mendell09b84632015-02-13 17:48:38 -05002750 Location out = locations->Out();
2751
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002752 switch (add->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002753 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002754 if (second.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002755 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2756 __ addl(out.AsRegister<Register>(), second.AsRegister<Register>());
Mark Mendell33bf2452015-05-27 10:08:24 -04002757 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
2758 __ addl(out.AsRegister<Register>(), first.AsRegister<Register>());
Mark Mendell09b84632015-02-13 17:48:38 -05002759 } else {
2760 __ leal(out.AsRegister<Register>(), Address(
2761 first.AsRegister<Register>(), second.AsRegister<Register>(), TIMES_1, 0));
2762 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002763 } else if (second.IsConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002764 int32_t value = second.GetConstant()->AsIntConstant()->GetValue();
2765 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2766 __ addl(out.AsRegister<Register>(), Immediate(value));
2767 } else {
2768 __ leal(out.AsRegister<Register>(), Address(first.AsRegister<Register>(), value));
2769 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002770 } else {
Mark Mendell09b84632015-02-13 17:48:38 -05002771 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002772 __ addl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002773 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002774 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002775 }
2776
2777 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002778 if (second.IsRegisterPair()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002779 __ addl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
2780 __ adcl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002781 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002782 __ addl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
2783 __ adcl(first.AsRegisterPairHigh<Register>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002784 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002785 } else {
2786 DCHECK(second.IsConstant()) << second;
2787 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2788 __ addl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
2789 __ adcl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002790 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002791 break;
2792 }
2793
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002794 case Primitive::kPrimFloat: {
2795 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002796 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04002797 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
2798 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00002799 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04002800 __ addss(first.AsFpuRegister<XmmRegister>(),
2801 codegen_->LiteralFloatAddress(
2802 const_area->GetConstant()->AsFloatConstant()->GetValue(),
2803 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2804 } else {
2805 DCHECK(second.IsStackSlot());
2806 __ addss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002807 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002808 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002809 }
2810
2811 case Primitive::kPrimDouble: {
2812 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002813 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04002814 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
2815 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00002816 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04002817 __ addsd(first.AsFpuRegister<XmmRegister>(),
2818 codegen_->LiteralDoubleAddress(
2819 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
2820 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2821 } else {
2822 DCHECK(second.IsDoubleStackSlot());
2823 __ addsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002824 }
2825 break;
2826 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002827
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002828 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002829 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002830 }
2831}
2832
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002833void LocationsBuilderX86::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002834 LocationSummary* locations =
2835 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002836 switch (sub->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002837 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002838 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002839 locations->SetInAt(0, Location::RequiresRegister());
2840 locations->SetInAt(1, Location::Any());
2841 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002842 break;
2843 }
Calin Juravle11351682014-10-23 15:38:15 +01002844 case Primitive::kPrimFloat:
2845 case Primitive::kPrimDouble: {
2846 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002847 if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
2848 DCHECK(sub->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00002849 } else if (sub->InputAt(1)->IsConstant()) {
2850 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002851 } else {
2852 locations->SetInAt(1, Location::Any());
2853 }
Calin Juravle11351682014-10-23 15:38:15 +01002854 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002855 break;
Calin Juravle11351682014-10-23 15:38:15 +01002856 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002857
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002858 default:
Calin Juravle11351682014-10-23 15:38:15 +01002859 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002860 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002861}
2862
2863void InstructionCodeGeneratorX86::VisitSub(HSub* sub) {
2864 LocationSummary* locations = sub->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002865 Location first = locations->InAt(0);
2866 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01002867 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002868 switch (sub->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002869 case Primitive::kPrimInt: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002870 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002871 __ subl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002872 } else if (second.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002873 __ subl(first.AsRegister<Register>(),
2874 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002875 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002876 __ subl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002877 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002878 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002879 }
2880
2881 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002882 if (second.IsRegisterPair()) {
Calin Juravle11351682014-10-23 15:38:15 +01002883 __ subl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
2884 __ sbbl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002885 } else if (second.IsDoubleStackSlot()) {
Calin Juravle11351682014-10-23 15:38:15 +01002886 __ subl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002887 __ sbbl(first.AsRegisterPairHigh<Register>(),
2888 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002889 } else {
2890 DCHECK(second.IsConstant()) << second;
2891 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2892 __ subl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
2893 __ sbbl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002894 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002895 break;
2896 }
2897
Calin Juravle11351682014-10-23 15:38:15 +01002898 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04002899 if (second.IsFpuRegister()) {
2900 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2901 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
2902 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00002903 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04002904 __ subss(first.AsFpuRegister<XmmRegister>(),
2905 codegen_->LiteralFloatAddress(
2906 const_area->GetConstant()->AsFloatConstant()->GetValue(),
2907 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2908 } else {
2909 DCHECK(second.IsStackSlot());
2910 __ subss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
2911 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002912 break;
Calin Juravle11351682014-10-23 15:38:15 +01002913 }
2914
2915 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04002916 if (second.IsFpuRegister()) {
2917 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2918 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
2919 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00002920 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04002921 __ subsd(first.AsFpuRegister<XmmRegister>(),
2922 codegen_->LiteralDoubleAddress(
2923 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
2924 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2925 } else {
2926 DCHECK(second.IsDoubleStackSlot());
2927 __ subsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
2928 }
Calin Juravle11351682014-10-23 15:38:15 +01002929 break;
2930 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002931
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002932 default:
Calin Juravle11351682014-10-23 15:38:15 +01002933 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002934 }
2935}
2936
Calin Juravle34bacdf2014-10-07 20:23:36 +01002937void LocationsBuilderX86::VisitMul(HMul* mul) {
2938 LocationSummary* locations =
2939 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
2940 switch (mul->GetResultType()) {
2941 case Primitive::kPrimInt:
2942 locations->SetInAt(0, Location::RequiresRegister());
2943 locations->SetInAt(1, Location::Any());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002944 if (mul->InputAt(1)->IsIntConstant()) {
2945 // Can use 3 operand multiply.
2946 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2947 } else {
2948 locations->SetOut(Location::SameAsFirstInput());
2949 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002950 break;
2951 case Primitive::kPrimLong: {
2952 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002953 locations->SetInAt(1, Location::Any());
2954 locations->SetOut(Location::SameAsFirstInput());
2955 // Needed for imul on 32bits with 64bits output.
2956 locations->AddTemp(Location::RegisterLocation(EAX));
2957 locations->AddTemp(Location::RegisterLocation(EDX));
2958 break;
2959 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002960 case Primitive::kPrimFloat:
2961 case Primitive::kPrimDouble: {
2962 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002963 if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
2964 DCHECK(mul->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00002965 } else if (mul->InputAt(1)->IsConstant()) {
2966 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002967 } else {
2968 locations->SetInAt(1, Location::Any());
2969 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002970 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002971 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002972 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002973
2974 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002975 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002976 }
2977}
2978
2979void InstructionCodeGeneratorX86::VisitMul(HMul* mul) {
2980 LocationSummary* locations = mul->GetLocations();
2981 Location first = locations->InAt(0);
2982 Location second = locations->InAt(1);
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002983 Location out = locations->Out();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002984
2985 switch (mul->GetResultType()) {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002986 case Primitive::kPrimInt:
2987 // The constant may have ended up in a register, so test explicitly to avoid
2988 // problems where the output may not be the same as the first operand.
2989 if (mul->InputAt(1)->IsIntConstant()) {
2990 Immediate imm(mul->InputAt(1)->AsIntConstant()->GetValue());
2991 __ imull(out.AsRegister<Register>(), first.AsRegister<Register>(), imm);
2992 } else if (second.IsRegister()) {
2993 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002994 __ imull(first.AsRegister<Register>(), second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002995 } else {
2996 DCHECK(second.IsStackSlot());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002997 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002998 __ imull(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01002999 }
3000 break;
Calin Juravle34bacdf2014-10-07 20:23:36 +01003001
3002 case Primitive::kPrimLong: {
Calin Juravle34bacdf2014-10-07 20:23:36 +01003003 Register in1_hi = first.AsRegisterPairHigh<Register>();
3004 Register in1_lo = first.AsRegisterPairLow<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003005 Register eax = locations->GetTemp(0).AsRegister<Register>();
3006 Register edx = locations->GetTemp(1).AsRegister<Register>();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003007
3008 DCHECK_EQ(EAX, eax);
3009 DCHECK_EQ(EDX, edx);
3010
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003011 // input: in1 - 64 bits, in2 - 64 bits.
Calin Juravle34bacdf2014-10-07 20:23:36 +01003012 // output: in1
3013 // formula: in1.hi : in1.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
3014 // parts: in1.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
3015 // parts: in1.lo = (in1.lo * in2.lo)[31:0]
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003016 if (second.IsConstant()) {
3017 DCHECK(second.GetConstant()->IsLongConstant());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003018
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003019 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3020 int32_t low_value = Low32Bits(value);
3021 int32_t high_value = High32Bits(value);
3022 Immediate low(low_value);
3023 Immediate high(high_value);
3024
3025 __ movl(eax, high);
3026 // eax <- in1.lo * in2.hi
3027 __ imull(eax, in1_lo);
3028 // in1.hi <- in1.hi * in2.lo
3029 __ imull(in1_hi, low);
3030 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3031 __ addl(in1_hi, eax);
3032 // move in2_lo to eax to prepare for double precision
3033 __ movl(eax, low);
3034 // edx:eax <- in1.lo * in2.lo
3035 __ mull(in1_lo);
3036 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3037 __ addl(in1_hi, edx);
3038 // in1.lo <- (in1.lo * in2.lo)[31:0];
3039 __ movl(in1_lo, eax);
3040 } else if (second.IsRegisterPair()) {
3041 Register in2_hi = second.AsRegisterPairHigh<Register>();
3042 Register in2_lo = second.AsRegisterPairLow<Register>();
3043
3044 __ movl(eax, in2_hi);
3045 // eax <- in1.lo * in2.hi
3046 __ imull(eax, in1_lo);
3047 // in1.hi <- in1.hi * in2.lo
3048 __ imull(in1_hi, in2_lo);
3049 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3050 __ addl(in1_hi, eax);
3051 // move in1_lo to eax to prepare for double precision
3052 __ movl(eax, in1_lo);
3053 // edx:eax <- in1.lo * in2.lo
3054 __ mull(in2_lo);
3055 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3056 __ addl(in1_hi, edx);
3057 // in1.lo <- (in1.lo * in2.lo)[31:0];
3058 __ movl(in1_lo, eax);
3059 } else {
3060 DCHECK(second.IsDoubleStackSlot()) << second;
3061 Address in2_hi(ESP, second.GetHighStackIndex(kX86WordSize));
3062 Address in2_lo(ESP, second.GetStackIndex());
3063
3064 __ movl(eax, in2_hi);
3065 // eax <- in1.lo * in2.hi
3066 __ imull(eax, in1_lo);
3067 // in1.hi <- in1.hi * in2.lo
3068 __ imull(in1_hi, in2_lo);
3069 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3070 __ addl(in1_hi, eax);
3071 // move in1_lo to eax to prepare for double precision
3072 __ movl(eax, in1_lo);
3073 // edx:eax <- in1.lo * in2.lo
3074 __ mull(in2_lo);
3075 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3076 __ addl(in1_hi, edx);
3077 // in1.lo <- (in1.lo * in2.lo)[31:0];
3078 __ movl(in1_lo, eax);
3079 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003080
3081 break;
3082 }
3083
Calin Juravleb5bfa962014-10-21 18:02:24 +01003084 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003085 DCHECK(first.Equals(locations->Out()));
3086 if (second.IsFpuRegister()) {
3087 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3088 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3089 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003090 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003091 __ mulss(first.AsFpuRegister<XmmRegister>(),
3092 codegen_->LiteralFloatAddress(
3093 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3094 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3095 } else {
3096 DCHECK(second.IsStackSlot());
3097 __ mulss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3098 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003099 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003100 }
3101
3102 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003103 DCHECK(first.Equals(locations->Out()));
3104 if (second.IsFpuRegister()) {
3105 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3106 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3107 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003108 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003109 __ mulsd(first.AsFpuRegister<XmmRegister>(),
3110 codegen_->LiteralDoubleAddress(
3111 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3112 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3113 } else {
3114 DCHECK(second.IsDoubleStackSlot());
3115 __ mulsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3116 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003117 break;
3118 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003119
3120 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003121 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003122 }
3123}
3124
Roland Levillain232ade02015-04-20 15:14:36 +01003125void InstructionCodeGeneratorX86::PushOntoFPStack(Location source,
3126 uint32_t temp_offset,
3127 uint32_t stack_adjustment,
3128 bool is_fp,
3129 bool is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003130 if (source.IsStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01003131 DCHECK(!is_wide);
3132 if (is_fp) {
3133 __ flds(Address(ESP, source.GetStackIndex() + stack_adjustment));
3134 } else {
3135 __ filds(Address(ESP, source.GetStackIndex() + stack_adjustment));
3136 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003137 } else if (source.IsDoubleStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01003138 DCHECK(is_wide);
3139 if (is_fp) {
3140 __ fldl(Address(ESP, source.GetStackIndex() + stack_adjustment));
3141 } else {
3142 __ fildl(Address(ESP, source.GetStackIndex() + stack_adjustment));
3143 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003144 } else {
3145 // Write the value to the temporary location on the stack and load to FP stack.
Roland Levillain232ade02015-04-20 15:14:36 +01003146 if (!is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003147 Location stack_temp = Location::StackSlot(temp_offset);
3148 codegen_->Move32(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01003149 if (is_fp) {
3150 __ flds(Address(ESP, temp_offset));
3151 } else {
3152 __ filds(Address(ESP, temp_offset));
3153 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003154 } else {
3155 Location stack_temp = Location::DoubleStackSlot(temp_offset);
3156 codegen_->Move64(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01003157 if (is_fp) {
3158 __ fldl(Address(ESP, temp_offset));
3159 } else {
3160 __ fildl(Address(ESP, temp_offset));
3161 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003162 }
3163 }
3164}
3165
3166void InstructionCodeGeneratorX86::GenerateRemFP(HRem *rem) {
3167 Primitive::Type type = rem->GetResultType();
3168 bool is_float = type == Primitive::kPrimFloat;
3169 size_t elem_size = Primitive::ComponentSize(type);
3170 LocationSummary* locations = rem->GetLocations();
3171 Location first = locations->InAt(0);
3172 Location second = locations->InAt(1);
3173 Location out = locations->Out();
3174
3175 // Create stack space for 2 elements.
3176 // TODO: enhance register allocator to ask for stack temporaries.
3177 __ subl(ESP, Immediate(2 * elem_size));
3178
3179 // Load the values to the FP stack in reverse order, using temporaries if needed.
Roland Levillain232ade02015-04-20 15:14:36 +01003180 const bool is_wide = !is_float;
3181 PushOntoFPStack(second, elem_size, 2 * elem_size, /* is_fp */ true, is_wide);
3182 PushOntoFPStack(first, 0, 2 * elem_size, /* is_fp */ true, is_wide);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003183
3184 // Loop doing FPREM until we stabilize.
Mark Mendell0c9497d2015-08-21 09:30:05 -04003185 NearLabel retry;
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003186 __ Bind(&retry);
3187 __ fprem();
3188
3189 // Move FP status to AX.
3190 __ fstsw();
3191
3192 // And see if the argument reduction is complete. This is signaled by the
3193 // C2 FPU flag bit set to 0.
3194 __ andl(EAX, Immediate(kC2ConditionMask));
3195 __ j(kNotEqual, &retry);
3196
3197 // We have settled on the final value. Retrieve it into an XMM register.
3198 // Store FP top of stack to real stack.
3199 if (is_float) {
3200 __ fsts(Address(ESP, 0));
3201 } else {
3202 __ fstl(Address(ESP, 0));
3203 }
3204
3205 // Pop the 2 items from the FP stack.
3206 __ fucompp();
3207
3208 // Load the value from the stack into an XMM register.
3209 DCHECK(out.IsFpuRegister()) << out;
3210 if (is_float) {
3211 __ movss(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
3212 } else {
3213 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
3214 }
3215
3216 // And remove the temporary stack space we allocated.
3217 __ addl(ESP, Immediate(2 * elem_size));
3218}
3219
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003220
3221void InstructionCodeGeneratorX86::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3222 DCHECK(instruction->IsDiv() || instruction->IsRem());
3223
3224 LocationSummary* locations = instruction->GetLocations();
3225 DCHECK(locations->InAt(1).IsConstant());
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003226 DCHECK(locations->InAt(1).GetConstant()->IsIntConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003227
3228 Register out_register = locations->Out().AsRegister<Register>();
3229 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003230 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003231
3232 DCHECK(imm == 1 || imm == -1);
3233
3234 if (instruction->IsRem()) {
3235 __ xorl(out_register, out_register);
3236 } else {
3237 __ movl(out_register, input_register);
3238 if (imm == -1) {
3239 __ negl(out_register);
3240 }
3241 }
3242}
3243
3244
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003245void InstructionCodeGeneratorX86::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003246 LocationSummary* locations = instruction->GetLocations();
3247
3248 Register out_register = locations->Out().AsRegister<Register>();
3249 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003250 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003251 DCHECK(IsPowerOfTwo(AbsOrMin(imm)));
3252 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003253
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003254 Register num = locations->GetTemp(0).AsRegister<Register>();
3255
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003256 __ leal(num, Address(input_register, abs_imm - 1));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003257 __ testl(input_register, input_register);
3258 __ cmovl(kGreaterEqual, num, input_register);
3259 int shift = CTZ(imm);
3260 __ sarl(num, Immediate(shift));
3261
3262 if (imm < 0) {
3263 __ negl(num);
3264 }
3265
3266 __ movl(out_register, num);
3267}
3268
3269void InstructionCodeGeneratorX86::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3270 DCHECK(instruction->IsDiv() || instruction->IsRem());
3271
3272 LocationSummary* locations = instruction->GetLocations();
3273 int imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
3274
3275 Register eax = locations->InAt(0).AsRegister<Register>();
3276 Register out = locations->Out().AsRegister<Register>();
3277 Register num;
3278 Register edx;
3279
3280 if (instruction->IsDiv()) {
3281 edx = locations->GetTemp(0).AsRegister<Register>();
3282 num = locations->GetTemp(1).AsRegister<Register>();
3283 } else {
3284 edx = locations->Out().AsRegister<Register>();
3285 num = locations->GetTemp(0).AsRegister<Register>();
3286 }
3287
3288 DCHECK_EQ(EAX, eax);
3289 DCHECK_EQ(EDX, edx);
3290 if (instruction->IsDiv()) {
3291 DCHECK_EQ(EAX, out);
3292 } else {
3293 DCHECK_EQ(EDX, out);
3294 }
3295
3296 int64_t magic;
3297 int shift;
3298 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
3299
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003300 // Save the numerator.
3301 __ movl(num, eax);
3302
3303 // EAX = magic
3304 __ movl(eax, Immediate(magic));
3305
3306 // EDX:EAX = magic * numerator
3307 __ imull(num);
3308
3309 if (imm > 0 && magic < 0) {
3310 // EDX += num
3311 __ addl(edx, num);
3312 } else if (imm < 0 && magic > 0) {
3313 __ subl(edx, num);
3314 }
3315
3316 // Shift if needed.
3317 if (shift != 0) {
3318 __ sarl(edx, Immediate(shift));
3319 }
3320
3321 // EDX += 1 if EDX < 0
3322 __ movl(eax, edx);
3323 __ shrl(edx, Immediate(31));
3324 __ addl(edx, eax);
3325
3326 if (instruction->IsRem()) {
3327 __ movl(eax, num);
3328 __ imull(edx, Immediate(imm));
3329 __ subl(eax, edx);
3330 __ movl(edx, eax);
3331 } else {
3332 __ movl(eax, edx);
3333 }
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003334}
3335
Calin Juravlebacfec32014-11-14 15:54:36 +00003336void InstructionCodeGeneratorX86::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3337 DCHECK(instruction->IsDiv() || instruction->IsRem());
3338
3339 LocationSummary* locations = instruction->GetLocations();
3340 Location out = locations->Out();
3341 Location first = locations->InAt(0);
3342 Location second = locations->InAt(1);
3343 bool is_div = instruction->IsDiv();
3344
3345 switch (instruction->GetResultType()) {
3346 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003347 DCHECK_EQ(EAX, first.AsRegister<Register>());
3348 DCHECK_EQ(is_div ? EAX : EDX, out.AsRegister<Register>());
Calin Juravlebacfec32014-11-14 15:54:36 +00003349
Vladimir Marko13c86fd2015-11-11 12:37:46 +00003350 if (second.IsConstant()) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003351 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003352
3353 if (imm == 0) {
3354 // Do not generate anything for 0. DivZeroCheck would forbid any generated code.
3355 } else if (imm == 1 || imm == -1) {
3356 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003357 } else if (is_div && IsPowerOfTwo(AbsOrMin(imm))) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003358 DivByPowerOfTwo(instruction->AsDiv());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003359 } else {
3360 DCHECK(imm <= -2 || imm >= 2);
3361 GenerateDivRemWithAnyConstant(instruction);
3362 }
3363 } else {
David Srbecky9cd6d372016-02-09 15:24:47 +00003364 SlowPathCode* slow_path = new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86(
3365 instruction, out.AsRegister<Register>(), is_div);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003366 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00003367
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003368 Register second_reg = second.AsRegister<Register>();
3369 // 0x80000000/-1 triggers an arithmetic exception!
3370 // Dividing by -1 is actually negation and -0x800000000 = 0x80000000 so
3371 // it's safe to just use negl instead of more complex comparisons.
Calin Juravlebacfec32014-11-14 15:54:36 +00003372
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003373 __ cmpl(second_reg, Immediate(-1));
3374 __ j(kEqual, slow_path->GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +00003375
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003376 // edx:eax <- sign-extended of eax
3377 __ cdq();
3378 // eax = quotient, edx = remainder
3379 __ idivl(second_reg);
3380 __ Bind(slow_path->GetExitLabel());
3381 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003382 break;
3383 }
3384
3385 case Primitive::kPrimLong: {
3386 InvokeRuntimeCallingConvention calling_convention;
3387 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
3388 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
3389 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
3390 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
3391 DCHECK_EQ(EAX, out.AsRegisterPairLow<Register>());
3392 DCHECK_EQ(EDX, out.AsRegisterPairHigh<Register>());
3393
3394 if (is_div) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01003395 codegen_->InvokeRuntime(kQuickLdiv, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003396 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003397 } else {
Serban Constantinescuba45db02016-07-12 22:53:02 +01003398 codegen_->InvokeRuntime(kQuickLmod, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003399 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003400 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003401 break;
3402 }
3403
3404 default:
3405 LOG(FATAL) << "Unexpected type for GenerateDivRemIntegral " << instruction->GetResultType();
3406 }
3407}
3408
Calin Juravle7c4954d2014-10-28 16:57:40 +00003409void LocationsBuilderX86::VisitDiv(HDiv* div) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003410 LocationSummary::CallKind call_kind = (div->GetResultType() == Primitive::kPrimLong)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003411 ? LocationSummary::kCallOnMainOnly
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003412 : LocationSummary::kNoCall;
3413 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
3414
Calin Juravle7c4954d2014-10-28 16:57:40 +00003415 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00003416 case Primitive::kPrimInt: {
3417 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003418 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00003419 locations->SetOut(Location::SameAsFirstInput());
3420 // Intel uses edx:eax as the dividend.
3421 locations->AddTemp(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003422 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3423 // which enforces results to be in EAX and EDX, things are simpler if we use EAX also as
3424 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003425 if (div->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003426 locations->AddTemp(Location::RequiresRegister());
3427 }
Calin Juravled0d48522014-11-04 16:40:20 +00003428 break;
3429 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003430 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003431 InvokeRuntimeCallingConvention calling_convention;
3432 locations->SetInAt(0, Location::RegisterPairLocation(
3433 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3434 locations->SetInAt(1, Location::RegisterPairLocation(
3435 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3436 // Runtime helper puts the result in EAX, EDX.
3437 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Calin Juravle7c4954d2014-10-28 16:57:40 +00003438 break;
3439 }
3440 case Primitive::kPrimFloat:
3441 case Primitive::kPrimDouble: {
3442 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003443 if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3444 DCHECK(div->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003445 } else if (div->InputAt(1)->IsConstant()) {
3446 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003447 } else {
3448 locations->SetInAt(1, Location::Any());
3449 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003450 locations->SetOut(Location::SameAsFirstInput());
3451 break;
3452 }
3453
3454 default:
3455 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3456 }
3457}
3458
3459void InstructionCodeGeneratorX86::VisitDiv(HDiv* div) {
3460 LocationSummary* locations = div->GetLocations();
3461 Location first = locations->InAt(0);
3462 Location second = locations->InAt(1);
Calin Juravle7c4954d2014-10-28 16:57:40 +00003463
3464 switch (div->GetResultType()) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003465 case Primitive::kPrimInt:
Calin Juravle7c4954d2014-10-28 16:57:40 +00003466 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003467 GenerateDivRemIntegral(div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00003468 break;
3469 }
3470
3471 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003472 if (second.IsFpuRegister()) {
3473 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3474 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3475 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003476 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003477 __ divss(first.AsFpuRegister<XmmRegister>(),
3478 codegen_->LiteralFloatAddress(
3479 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3480 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3481 } else {
3482 DCHECK(second.IsStackSlot());
3483 __ divss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3484 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003485 break;
3486 }
3487
3488 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003489 if (second.IsFpuRegister()) {
3490 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3491 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3492 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003493 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003494 __ divsd(first.AsFpuRegister<XmmRegister>(),
3495 codegen_->LiteralDoubleAddress(
3496 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3497 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3498 } else {
3499 DCHECK(second.IsDoubleStackSlot());
3500 __ divsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3501 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003502 break;
3503 }
3504
3505 default:
3506 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3507 }
3508}
3509
Calin Juravlebacfec32014-11-14 15:54:36 +00003510void LocationsBuilderX86::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00003511 Primitive::Type type = rem->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003512
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003513 LocationSummary::CallKind call_kind = (rem->GetResultType() == Primitive::kPrimLong)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003514 ? LocationSummary::kCallOnMainOnly
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003515 : LocationSummary::kNoCall;
3516 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
Calin Juravlebacfec32014-11-14 15:54:36 +00003517
Calin Juravled2ec87d2014-12-08 14:24:46 +00003518 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003519 case Primitive::kPrimInt: {
3520 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003521 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00003522 locations->SetOut(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003523 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3524 // which enforces results to be in EAX and EDX, things are simpler if we use EDX also as
3525 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003526 if (rem->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003527 locations->AddTemp(Location::RequiresRegister());
3528 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003529 break;
3530 }
3531 case Primitive::kPrimLong: {
3532 InvokeRuntimeCallingConvention calling_convention;
3533 locations->SetInAt(0, Location::RegisterPairLocation(
3534 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3535 locations->SetInAt(1, Location::RegisterPairLocation(
3536 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3537 // Runtime helper puts the result in EAX, EDX.
3538 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
3539 break;
3540 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003541 case Primitive::kPrimDouble:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003542 case Primitive::kPrimFloat: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003543 locations->SetInAt(0, Location::Any());
3544 locations->SetInAt(1, Location::Any());
3545 locations->SetOut(Location::RequiresFpuRegister());
3546 locations->AddTemp(Location::RegisterLocation(EAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00003547 break;
3548 }
3549
3550 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003551 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003552 }
3553}
3554
3555void InstructionCodeGeneratorX86::VisitRem(HRem* rem) {
3556 Primitive::Type type = rem->GetResultType();
3557 switch (type) {
3558 case Primitive::kPrimInt:
3559 case Primitive::kPrimLong: {
3560 GenerateDivRemIntegral(rem);
3561 break;
3562 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003563 case Primitive::kPrimFloat:
Calin Juravlebacfec32014-11-14 15:54:36 +00003564 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003565 GenerateRemFP(rem);
Calin Juravlebacfec32014-11-14 15:54:36 +00003566 break;
3567 }
3568 default:
3569 LOG(FATAL) << "Unexpected rem type " << type;
3570 }
3571}
3572
Calin Juravled0d48522014-11-04 16:40:20 +00003573void LocationsBuilderX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00003574 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
3575 ? LocationSummary::kCallOnSlowPath
3576 : LocationSummary::kNoCall;
3577 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003578 switch (instruction->GetType()) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003579 case Primitive::kPrimBoolean:
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003580 case Primitive::kPrimByte:
3581 case Primitive::kPrimChar:
3582 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003583 case Primitive::kPrimInt: {
3584 locations->SetInAt(0, Location::Any());
3585 break;
3586 }
3587 case Primitive::kPrimLong: {
3588 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
3589 if (!instruction->IsConstant()) {
3590 locations->AddTemp(Location::RequiresRegister());
3591 }
3592 break;
3593 }
3594 default:
3595 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
3596 }
Calin Juravled0d48522014-11-04 16:40:20 +00003597 if (instruction->HasUses()) {
3598 locations->SetOut(Location::SameAsFirstInput());
3599 }
3600}
3601
3602void InstructionCodeGeneratorX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003603 SlowPathCode* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86(instruction);
Calin Juravled0d48522014-11-04 16:40:20 +00003604 codegen_->AddSlowPath(slow_path);
3605
3606 LocationSummary* locations = instruction->GetLocations();
3607 Location value = locations->InAt(0);
3608
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003609 switch (instruction->GetType()) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003610 case Primitive::kPrimBoolean:
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003611 case Primitive::kPrimByte:
3612 case Primitive::kPrimChar:
3613 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003614 case Primitive::kPrimInt: {
3615 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003616 __ testl(value.AsRegister<Register>(), value.AsRegister<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003617 __ j(kEqual, slow_path->GetEntryLabel());
3618 } else if (value.IsStackSlot()) {
3619 __ cmpl(Address(ESP, value.GetStackIndex()), Immediate(0));
3620 __ j(kEqual, slow_path->GetEntryLabel());
3621 } else {
3622 DCHECK(value.IsConstant()) << value;
3623 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
3624 __ jmp(slow_path->GetEntryLabel());
3625 }
3626 }
3627 break;
Calin Juravled0d48522014-11-04 16:40:20 +00003628 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003629 case Primitive::kPrimLong: {
3630 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003631 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003632 __ movl(temp, value.AsRegisterPairLow<Register>());
3633 __ orl(temp, value.AsRegisterPairHigh<Register>());
3634 __ j(kEqual, slow_path->GetEntryLabel());
3635 } else {
3636 DCHECK(value.IsConstant()) << value;
3637 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
3638 __ jmp(slow_path->GetEntryLabel());
3639 }
3640 }
3641 break;
3642 }
3643 default:
3644 LOG(FATAL) << "Unexpected type for HDivZeroCheck" << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00003645 }
Calin Juravled0d48522014-11-04 16:40:20 +00003646}
3647
Calin Juravle9aec02f2014-11-18 23:06:35 +00003648void LocationsBuilderX86::HandleShift(HBinaryOperation* op) {
3649 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3650
3651 LocationSummary* locations =
3652 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
3653
3654 switch (op->GetResultType()) {
Mark P Mendell73945692015-04-29 14:56:17 +00003655 case Primitive::kPrimInt:
Calin Juravle9aec02f2014-11-18 23:06:35 +00003656 case Primitive::kPrimLong: {
Mark P Mendell73945692015-04-29 14:56:17 +00003657 // Can't have Location::Any() and output SameAsFirstInput()
Calin Juravle9aec02f2014-11-18 23:06:35 +00003658 locations->SetInAt(0, Location::RequiresRegister());
Mark P Mendell73945692015-04-29 14:56:17 +00003659 // The shift count needs to be in CL or a constant.
3660 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, op->InputAt(1)));
Calin Juravle9aec02f2014-11-18 23:06:35 +00003661 locations->SetOut(Location::SameAsFirstInput());
3662 break;
3663 }
3664 default:
3665 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
3666 }
3667}
3668
3669void InstructionCodeGeneratorX86::HandleShift(HBinaryOperation* op) {
3670 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3671
3672 LocationSummary* locations = op->GetLocations();
3673 Location first = locations->InAt(0);
3674 Location second = locations->InAt(1);
3675 DCHECK(first.Equals(locations->Out()));
3676
3677 switch (op->GetResultType()) {
3678 case Primitive::kPrimInt: {
Mark P Mendell73945692015-04-29 14:56:17 +00003679 DCHECK(first.IsRegister());
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003680 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003681 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003682 Register second_reg = second.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003683 DCHECK_EQ(ECX, second_reg);
3684 if (op->IsShl()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003685 __ shll(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003686 } else if (op->IsShr()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003687 __ sarl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003688 } else {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003689 __ shrl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003690 }
3691 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003692 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance;
Mark P Mendell73945692015-04-29 14:56:17 +00003693 if (shift == 0) {
3694 return;
3695 }
3696 Immediate imm(shift);
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003697 if (op->IsShl()) {
3698 __ shll(first_reg, imm);
3699 } else if (op->IsShr()) {
3700 __ sarl(first_reg, imm);
3701 } else {
3702 __ shrl(first_reg, imm);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003703 }
3704 }
3705 break;
3706 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003707 case Primitive::kPrimLong: {
Mark P Mendell73945692015-04-29 14:56:17 +00003708 if (second.IsRegister()) {
3709 Register second_reg = second.AsRegister<Register>();
3710 DCHECK_EQ(ECX, second_reg);
3711 if (op->IsShl()) {
3712 GenerateShlLong(first, second_reg);
3713 } else if (op->IsShr()) {
3714 GenerateShrLong(first, second_reg);
3715 } else {
3716 GenerateUShrLong(first, second_reg);
3717 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003718 } else {
Mark P Mendell73945692015-04-29 14:56:17 +00003719 // Shift by a constant.
Roland Levillain5b5b9312016-03-22 14:57:31 +00003720 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance;
Mark P Mendell73945692015-04-29 14:56:17 +00003721 // Nothing to do if the shift is 0, as the input is already the output.
3722 if (shift != 0) {
3723 if (op->IsShl()) {
3724 GenerateShlLong(first, shift);
3725 } else if (op->IsShr()) {
3726 GenerateShrLong(first, shift);
3727 } else {
3728 GenerateUShrLong(first, shift);
3729 }
3730 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003731 }
3732 break;
3733 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003734 default:
3735 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
3736 }
3737}
3738
Mark P Mendell73945692015-04-29 14:56:17 +00003739void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, int shift) {
3740 Register low = loc.AsRegisterPairLow<Register>();
3741 Register high = loc.AsRegisterPairHigh<Register>();
Mark Mendellba56d062015-05-05 21:34:03 -04003742 if (shift == 1) {
3743 // This is just an addition.
3744 __ addl(low, low);
3745 __ adcl(high, high);
3746 } else if (shift == 32) {
Mark P Mendell73945692015-04-29 14:56:17 +00003747 // Shift by 32 is easy. High gets low, and low gets 0.
3748 codegen_->EmitParallelMoves(
3749 loc.ToLow(),
3750 loc.ToHigh(),
3751 Primitive::kPrimInt,
3752 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
3753 loc.ToLow(),
3754 Primitive::kPrimInt);
3755 } else if (shift > 32) {
3756 // Low part becomes 0. High part is low part << (shift-32).
3757 __ movl(high, low);
3758 __ shll(high, Immediate(shift - 32));
3759 __ xorl(low, low);
3760 } else {
3761 // Between 1 and 31.
3762 __ shld(high, low, Immediate(shift));
3763 __ shll(low, Immediate(shift));
3764 }
3765}
3766
Calin Juravle9aec02f2014-11-18 23:06:35 +00003767void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04003768 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00003769 __ shld(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>(), shifter);
3770 __ shll(loc.AsRegisterPairLow<Register>(), shifter);
3771 __ testl(shifter, Immediate(32));
3772 __ j(kEqual, &done);
3773 __ movl(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>());
3774 __ movl(loc.AsRegisterPairLow<Register>(), Immediate(0));
3775 __ Bind(&done);
3776}
3777
Mark P Mendell73945692015-04-29 14:56:17 +00003778void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, int shift) {
3779 Register low = loc.AsRegisterPairLow<Register>();
3780 Register high = loc.AsRegisterPairHigh<Register>();
3781 if (shift == 32) {
3782 // Need to copy the sign.
3783 DCHECK_NE(low, high);
3784 __ movl(low, high);
3785 __ sarl(high, Immediate(31));
3786 } else if (shift > 32) {
3787 DCHECK_NE(low, high);
3788 // High part becomes sign. Low part is shifted by shift - 32.
3789 __ movl(low, high);
3790 __ sarl(high, Immediate(31));
3791 __ sarl(low, Immediate(shift - 32));
3792 } else {
3793 // Between 1 and 31.
3794 __ shrd(low, high, Immediate(shift));
3795 __ sarl(high, Immediate(shift));
3796 }
3797}
3798
Calin Juravle9aec02f2014-11-18 23:06:35 +00003799void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04003800 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00003801 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
3802 __ sarl(loc.AsRegisterPairHigh<Register>(), shifter);
3803 __ testl(shifter, Immediate(32));
3804 __ j(kEqual, &done);
3805 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
3806 __ sarl(loc.AsRegisterPairHigh<Register>(), Immediate(31));
3807 __ Bind(&done);
3808}
3809
Mark P Mendell73945692015-04-29 14:56:17 +00003810void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, int shift) {
3811 Register low = loc.AsRegisterPairLow<Register>();
3812 Register high = loc.AsRegisterPairHigh<Register>();
3813 if (shift == 32) {
3814 // Shift by 32 is easy. Low gets high, and high gets 0.
3815 codegen_->EmitParallelMoves(
3816 loc.ToHigh(),
3817 loc.ToLow(),
3818 Primitive::kPrimInt,
3819 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
3820 loc.ToHigh(),
3821 Primitive::kPrimInt);
3822 } else if (shift > 32) {
3823 // Low part is high >> (shift - 32). High part becomes 0.
3824 __ movl(low, high);
3825 __ shrl(low, Immediate(shift - 32));
3826 __ xorl(high, high);
3827 } else {
3828 // Between 1 and 31.
3829 __ shrd(low, high, Immediate(shift));
3830 __ shrl(high, Immediate(shift));
3831 }
3832}
3833
Calin Juravle9aec02f2014-11-18 23:06:35 +00003834void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04003835 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00003836 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
3837 __ shrl(loc.AsRegisterPairHigh<Register>(), shifter);
3838 __ testl(shifter, Immediate(32));
3839 __ j(kEqual, &done);
3840 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
3841 __ movl(loc.AsRegisterPairHigh<Register>(), Immediate(0));
3842 __ Bind(&done);
3843}
3844
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003845void LocationsBuilderX86::VisitRor(HRor* ror) {
3846 LocationSummary* locations =
3847 new (GetGraph()->GetArena()) LocationSummary(ror, LocationSummary::kNoCall);
3848
3849 switch (ror->GetResultType()) {
3850 case Primitive::kPrimLong:
3851 // Add the temporary needed.
3852 locations->AddTemp(Location::RequiresRegister());
3853 FALLTHROUGH_INTENDED;
3854 case Primitive::kPrimInt:
3855 locations->SetInAt(0, Location::RequiresRegister());
3856 // The shift count needs to be in CL (unless it is a constant).
3857 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, ror->InputAt(1)));
3858 locations->SetOut(Location::SameAsFirstInput());
3859 break;
3860 default:
3861 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
3862 UNREACHABLE();
3863 }
3864}
3865
3866void InstructionCodeGeneratorX86::VisitRor(HRor* ror) {
3867 LocationSummary* locations = ror->GetLocations();
3868 Location first = locations->InAt(0);
3869 Location second = locations->InAt(1);
3870
3871 if (ror->GetResultType() == Primitive::kPrimInt) {
3872 Register first_reg = first.AsRegister<Register>();
3873 if (second.IsRegister()) {
3874 Register second_reg = second.AsRegister<Register>();
3875 __ rorl(first_reg, second_reg);
3876 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003877 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003878 __ rorl(first_reg, imm);
3879 }
3880 return;
3881 }
3882
3883 DCHECK_EQ(ror->GetResultType(), Primitive::kPrimLong);
3884 Register first_reg_lo = first.AsRegisterPairLow<Register>();
3885 Register first_reg_hi = first.AsRegisterPairHigh<Register>();
3886 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
3887 if (second.IsRegister()) {
3888 Register second_reg = second.AsRegister<Register>();
3889 DCHECK_EQ(second_reg, ECX);
3890 __ movl(temp_reg, first_reg_hi);
3891 __ shrd(first_reg_hi, first_reg_lo, second_reg);
3892 __ shrd(first_reg_lo, temp_reg, second_reg);
3893 __ movl(temp_reg, first_reg_hi);
3894 __ testl(second_reg, Immediate(32));
3895 __ cmovl(kNotEqual, first_reg_hi, first_reg_lo);
3896 __ cmovl(kNotEqual, first_reg_lo, temp_reg);
3897 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003898 int32_t shift_amt = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance;
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003899 if (shift_amt == 0) {
3900 // Already fine.
3901 return;
3902 }
3903 if (shift_amt == 32) {
3904 // Just swap.
3905 __ movl(temp_reg, first_reg_lo);
3906 __ movl(first_reg_lo, first_reg_hi);
3907 __ movl(first_reg_hi, temp_reg);
3908 return;
3909 }
3910
3911 Immediate imm(shift_amt);
3912 // Save the constents of the low value.
3913 __ movl(temp_reg, first_reg_lo);
3914
3915 // Shift right into low, feeding bits from high.
3916 __ shrd(first_reg_lo, first_reg_hi, imm);
3917
3918 // Shift right into high, feeding bits from the original low.
3919 __ shrd(first_reg_hi, temp_reg, imm);
3920
3921 // Swap if needed.
3922 if (shift_amt > 32) {
3923 __ movl(temp_reg, first_reg_lo);
3924 __ movl(first_reg_lo, first_reg_hi);
3925 __ movl(first_reg_hi, temp_reg);
3926 }
3927 }
3928}
3929
Calin Juravle9aec02f2014-11-18 23:06:35 +00003930void LocationsBuilderX86::VisitShl(HShl* shl) {
3931 HandleShift(shl);
3932}
3933
3934void InstructionCodeGeneratorX86::VisitShl(HShl* shl) {
3935 HandleShift(shl);
3936}
3937
3938void LocationsBuilderX86::VisitShr(HShr* shr) {
3939 HandleShift(shr);
3940}
3941
3942void InstructionCodeGeneratorX86::VisitShr(HShr* shr) {
3943 HandleShift(shr);
3944}
3945
3946void LocationsBuilderX86::VisitUShr(HUShr* ushr) {
3947 HandleShift(ushr);
3948}
3949
3950void InstructionCodeGeneratorX86::VisitUShr(HUShr* ushr) {
3951 HandleShift(ushr);
3952}
3953
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003954void LocationsBuilderX86::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003955 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003956 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003957 locations->SetOut(Location::RegisterLocation(EAX));
David Brazdil6de19382016-01-08 17:37:10 +00003958 if (instruction->IsStringAlloc()) {
3959 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
3960 } else {
3961 InvokeRuntimeCallingConvention calling_convention;
3962 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3963 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3964 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003965}
3966
3967void InstructionCodeGeneratorX86::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01003968 // Note: if heap poisoning is enabled, the entry point takes cares
3969 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00003970 if (instruction->IsStringAlloc()) {
3971 // String is allocated through StringFactory. Call NewEmptyString entry point.
3972 Register temp = instruction->GetLocations()->GetTemp(0).AsRegister<Register>();
Andreas Gampe542451c2016-07-26 09:02:02 -07003973 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86PointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00003974 __ fs()->movl(temp, Address::Absolute(QUICK_ENTRY_POINT(pNewEmptyString)));
3975 __ call(Address(temp, code_offset.Int32Value()));
3976 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3977 } else {
Serban Constantinescuba45db02016-07-12 22:53:02 +01003978 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
David Brazdil6de19382016-01-08 17:37:10 +00003979 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
3980 DCHECK(!codegen_->IsLeafMethod());
3981 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003982}
3983
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003984void LocationsBuilderX86::VisitNewArray(HNewArray* instruction) {
3985 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003986 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003987 locations->SetOut(Location::RegisterLocation(EAX));
3988 InvokeRuntimeCallingConvention calling_convention;
3989 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003990 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003991 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003992}
3993
3994void InstructionCodeGeneratorX86::VisitNewArray(HNewArray* instruction) {
3995 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003996 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
Roland Levillain4d027112015-07-01 15:41:14 +01003997 // Note: if heap poisoning is enabled, the entry point takes cares
3998 // of poisoning the reference.
Serban Constantinescuba45db02016-07-12 22:53:02 +01003999 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00004000 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004001 DCHECK(!codegen_->IsLeafMethod());
4002}
4003
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004004void LocationsBuilderX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004005 LocationSummary* locations =
4006 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01004007 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
4008 if (location.IsStackSlot()) {
4009 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4010 } else if (location.IsDoubleStackSlot()) {
4011 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004012 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01004013 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004014}
4015
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004016void InstructionCodeGeneratorX86::VisitParameterValue(
4017 HParameterValue* instruction ATTRIBUTE_UNUSED) {
4018}
4019
4020void LocationsBuilderX86::VisitCurrentMethod(HCurrentMethod* instruction) {
4021 LocationSummary* locations =
4022 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4023 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
4024}
4025
4026void InstructionCodeGeneratorX86::VisitCurrentMethod(HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004027}
4028
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004029void LocationsBuilderX86::VisitClassTableGet(HClassTableGet* instruction) {
4030 LocationSummary* locations =
4031 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4032 locations->SetInAt(0, Location::RequiresRegister());
4033 locations->SetOut(Location::RequiresRegister());
4034}
4035
4036void InstructionCodeGeneratorX86::VisitClassTableGet(HClassTableGet* instruction) {
4037 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004038 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004039 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004040 instruction->GetIndex(), kX86PointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004041 __ movl(locations->Out().AsRegister<Register>(),
4042 Address(locations->InAt(0).AsRegister<Register>(), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004043 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004044 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00004045 instruction->GetIndex(), kX86PointerSize));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004046 __ movl(locations->Out().AsRegister<Register>(),
4047 Address(locations->InAt(0).AsRegister<Register>(),
4048 mirror::Class::ImtPtrOffset(kX86PointerSize).Uint32Value()));
4049 // temp = temp->GetImtEntryAt(method_offset);
4050 __ movl(locations->Out().AsRegister<Register>(),
4051 Address(locations->Out().AsRegister<Register>(), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004052 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004053}
4054
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004055void LocationsBuilderX86::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004056 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004057 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01004058 locations->SetInAt(0, Location::RequiresRegister());
4059 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004060}
4061
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004062void InstructionCodeGeneratorX86::VisitNot(HNot* not_) {
4063 LocationSummary* locations = not_->GetLocations();
Roland Levillain70566432014-10-24 16:20:17 +01004064 Location in = locations->InAt(0);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01004065 Location out = locations->Out();
Roland Levillain70566432014-10-24 16:20:17 +01004066 DCHECK(in.Equals(out));
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00004067 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004068 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00004069 __ notl(out.AsRegister<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004070 break;
4071
4072 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01004073 __ notl(out.AsRegisterPairLow<Register>());
4074 __ notl(out.AsRegisterPairHigh<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004075 break;
4076
4077 default:
4078 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
4079 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004080}
4081
David Brazdil66d126e2015-04-03 16:02:44 +01004082void LocationsBuilderX86::VisitBooleanNot(HBooleanNot* bool_not) {
4083 LocationSummary* locations =
4084 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
4085 locations->SetInAt(0, Location::RequiresRegister());
4086 locations->SetOut(Location::SameAsFirstInput());
4087}
4088
4089void InstructionCodeGeneratorX86::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01004090 LocationSummary* locations = bool_not->GetLocations();
4091 Location in = locations->InAt(0);
4092 Location out = locations->Out();
4093 DCHECK(in.Equals(out));
4094 __ xorl(out.AsRegister<Register>(), Immediate(1));
4095}
4096
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004097void LocationsBuilderX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004098 LocationSummary* locations =
4099 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00004100 switch (compare->InputAt(0)->GetType()) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00004101 case Primitive::kPrimBoolean:
4102 case Primitive::kPrimByte:
4103 case Primitive::kPrimShort:
4104 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08004105 case Primitive::kPrimInt:
Calin Juravleddb7df22014-11-25 20:56:51 +00004106 case Primitive::kPrimLong: {
4107 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravleddb7df22014-11-25 20:56:51 +00004108 locations->SetInAt(1, Location::Any());
4109 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4110 break;
4111 }
4112 case Primitive::kPrimFloat:
4113 case Primitive::kPrimDouble: {
4114 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004115 if (compare->InputAt(1)->IsX86LoadFromConstantTable()) {
4116 DCHECK(compare->InputAt(1)->IsEmittedAtUseSite());
4117 } else if (compare->InputAt(1)->IsConstant()) {
4118 locations->SetInAt(1, Location::RequiresFpuRegister());
4119 } else {
4120 locations->SetInAt(1, Location::Any());
4121 }
Calin Juravleddb7df22014-11-25 20:56:51 +00004122 locations->SetOut(Location::RequiresRegister());
4123 break;
4124 }
4125 default:
4126 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
4127 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004128}
4129
4130void InstructionCodeGeneratorX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004131 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004132 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00004133 Location left = locations->InAt(0);
4134 Location right = locations->InAt(1);
4135
Mark Mendell0c9497d2015-08-21 09:30:05 -04004136 NearLabel less, greater, done;
Aart Bika19616e2016-02-01 18:57:58 -08004137 Condition less_cond = kLess;
4138
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004139 switch (compare->InputAt(0)->GetType()) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00004140 case Primitive::kPrimBoolean:
4141 case Primitive::kPrimByte:
4142 case Primitive::kPrimShort:
4143 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08004144 case Primitive::kPrimInt: {
Roland Levillain0b671c02016-08-19 12:02:34 +01004145 codegen_->GenerateIntCompare(left, right);
Aart Bika19616e2016-02-01 18:57:58 -08004146 break;
4147 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004148 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004149 Register left_low = left.AsRegisterPairLow<Register>();
4150 Register left_high = left.AsRegisterPairHigh<Register>();
4151 int32_t val_low = 0;
4152 int32_t val_high = 0;
4153 bool right_is_const = false;
4154
4155 if (right.IsConstant()) {
4156 DCHECK(right.GetConstant()->IsLongConstant());
4157 right_is_const = true;
4158 int64_t val = right.GetConstant()->AsLongConstant()->GetValue();
4159 val_low = Low32Bits(val);
4160 val_high = High32Bits(val);
4161 }
4162
Calin Juravleddb7df22014-11-25 20:56:51 +00004163 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004164 __ cmpl(left_high, right.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004165 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004166 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004167 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004168 DCHECK(right_is_const) << right;
Aart Bika19616e2016-02-01 18:57:58 -08004169 codegen_->Compare32BitValue(left_high, val_high);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004170 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004171 __ j(kLess, &less); // Signed compare.
4172 __ j(kGreater, &greater); // Signed compare.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004173 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004174 __ cmpl(left_low, right.AsRegisterPairLow<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004175 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004176 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004177 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004178 DCHECK(right_is_const) << right;
Aart Bika19616e2016-02-01 18:57:58 -08004179 codegen_->Compare32BitValue(left_low, val_low);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004180 }
Aart Bika19616e2016-02-01 18:57:58 -08004181 less_cond = kBelow; // for CF (unsigned).
Calin Juravleddb7df22014-11-25 20:56:51 +00004182 break;
4183 }
4184 case Primitive::kPrimFloat: {
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004185 GenerateFPCompare(left, right, compare, false);
Calin Juravleddb7df22014-11-25 20:56:51 +00004186 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08004187 less_cond = kBelow; // for CF (floats).
Calin Juravleddb7df22014-11-25 20:56:51 +00004188 break;
4189 }
4190 case Primitive::kPrimDouble: {
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004191 GenerateFPCompare(left, right, compare, true);
Calin Juravleddb7df22014-11-25 20:56:51 +00004192 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08004193 less_cond = kBelow; // for CF (floats).
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004194 break;
4195 }
4196 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00004197 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004198 }
Aart Bika19616e2016-02-01 18:57:58 -08004199
Calin Juravleddb7df22014-11-25 20:56:51 +00004200 __ movl(out, Immediate(0));
4201 __ j(kEqual, &done);
Aart Bika19616e2016-02-01 18:57:58 -08004202 __ j(less_cond, &less);
Calin Juravleddb7df22014-11-25 20:56:51 +00004203
4204 __ Bind(&greater);
4205 __ movl(out, Immediate(1));
4206 __ jmp(&done);
4207
4208 __ Bind(&less);
4209 __ movl(out, Immediate(-1));
4210
4211 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004212}
4213
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004214void LocationsBuilderX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004215 LocationSummary* locations =
4216 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko372f10e2016-05-17 16:30:10 +01004217 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01004218 locations->SetInAt(i, Location::Any());
4219 }
4220 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004221}
4222
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004223void InstructionCodeGeneratorX86::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004224 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004225}
4226
Roland Levillain7c1559a2015-12-15 10:55:36 +00004227void CodeGeneratorX86::GenerateMemoryBarrier(MemBarrierKind kind) {
Calin Juravle52c48962014-12-16 17:02:57 +00004228 /*
4229 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
4230 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
4231 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
4232 */
4233 switch (kind) {
4234 case MemBarrierKind::kAnyAny: {
Mark P Mendell17077d82015-12-16 19:15:59 +00004235 MemoryFence();
Calin Juravle52c48962014-12-16 17:02:57 +00004236 break;
4237 }
4238 case MemBarrierKind::kAnyStore:
4239 case MemBarrierKind::kLoadAny:
4240 case MemBarrierKind::kStoreStore: {
4241 // nop
4242 break;
4243 }
Mark Mendell7aa04a12016-01-27 22:39:07 -05004244 case MemBarrierKind::kNTStoreStore:
4245 // Non-Temporal Store/Store needs an explicit fence.
4246 MemoryFence(/* non-temporal */ true);
4247 break;
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004248 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004249}
4250
Vladimir Markodc151b22015-10-15 18:02:30 +01004251HInvokeStaticOrDirect::DispatchInfo CodeGeneratorX86::GetSupportedInvokeStaticOrDirectDispatch(
4252 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
4253 MethodReference target_method ATTRIBUTE_UNUSED) {
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00004254 HInvokeStaticOrDirect::DispatchInfo dispatch_info = desired_dispatch_info;
4255
4256 // We disable pc-relative load when there is an irreducible loop, as the optimization
4257 // is incompatible with it.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004258 // TODO: Create as many X86ComputeBaseMethodAddress instructions
4259 // as needed for methods with irreducible loops.
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00004260 if (GetGraph()->HasIrreducibleLoops() &&
4261 (dispatch_info.method_load_kind ==
4262 HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative)) {
4263 dispatch_info.method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod;
4264 }
4265 switch (dispatch_info.code_ptr_location) {
Vladimir Markodc151b22015-10-15 18:02:30 +01004266 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
4267 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
4268 // For direct code, we actually prefer to call via the code pointer from ArtMethod*.
4269 // (Though the direct CALL ptr16:32 is available for consideration).
4270 return HInvokeStaticOrDirect::DispatchInfo {
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00004271 dispatch_info.method_load_kind,
Vladimir Markodc151b22015-10-15 18:02:30 +01004272 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00004273 dispatch_info.method_load_data,
Vladimir Markodc151b22015-10-15 18:02:30 +01004274 0u
4275 };
4276 default:
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00004277 return dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01004278 }
4279}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004280
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004281Register CodeGeneratorX86::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
4282 Register temp) {
4283 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
Vladimir Markoc53c0792015-11-19 15:48:33 +00004284 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004285 if (!invoke->GetLocations()->Intrinsified()) {
4286 return location.AsRegister<Register>();
4287 }
4288 // For intrinsics we allow any location, so it may be on the stack.
4289 if (!location.IsRegister()) {
4290 __ movl(temp, Address(ESP, location.GetStackIndex()));
4291 return temp;
4292 }
4293 // For register locations, check if the register was saved. If so, get it from the stack.
4294 // Note: There is a chance that the register was saved but not overwritten, so we could
4295 // save one load. However, since this is just an intrinsic slow path we prefer this
4296 // simple and more robust approach rather that trying to determine if that's the case.
4297 SlowPathCode* slow_path = GetCurrentSlowPath();
Serguei Katkov288c7a82016-05-16 11:53:15 +06004298 if (slow_path != nullptr) {
4299 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
4300 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
4301 __ movl(temp, Address(ESP, stack_offset));
4302 return temp;
4303 }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004304 }
4305 return location.AsRegister<Register>();
4306}
4307
Serguei Katkov288c7a82016-05-16 11:53:15 +06004308Location CodeGeneratorX86::GenerateCalleeMethodStaticOrDirectCall(HInvokeStaticOrDirect* invoke,
4309 Location temp) {
Vladimir Marko58155012015-08-19 12:49:41 +00004310 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
4311 switch (invoke->GetMethodLoadKind()) {
4312 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
4313 // temp = thread->string_init_entrypoint
4314 __ fs()->movl(temp.AsRegister<Register>(), Address::Absolute(invoke->GetStringInitOffset()));
4315 break;
4316 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00004317 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004318 break;
4319 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
4320 __ movl(temp.AsRegister<Register>(), Immediate(invoke->GetMethodAddress()));
4321 break;
4322 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004323 __ movl(temp.AsRegister<Register>(), Immediate(/* placeholder */ 0));
Vladimir Marko58155012015-08-19 12:49:41 +00004324 method_patches_.emplace_back(invoke->GetTargetMethod());
4325 __ Bind(&method_patches_.back().label); // Bind the label at the end of the "movl" insn.
4326 break;
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004327 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
4328 Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
4329 temp.AsRegister<Register>());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004330 __ movl(temp.AsRegister<Register>(), Address(base_reg, kDummy32BitOffset));
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004331 // Bind a new fixup label at the end of the "movl" insn.
4332 uint32_t offset = invoke->GetDexCacheArrayOffset();
4333 __ Bind(NewPcRelativeDexCacheArrayPatch(*invoke->GetTargetMethod().dex_file, offset));
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004334 break;
4335 }
Vladimir Marko58155012015-08-19 12:49:41 +00004336 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004337 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004338 Register method_reg;
4339 Register reg = temp.AsRegister<Register>();
4340 if (current_method.IsRegister()) {
4341 method_reg = current_method.AsRegister<Register>();
4342 } else {
David Brazdil58282f42016-01-14 12:45:10 +00004343 DCHECK(invoke->GetLocations()->Intrinsified());
Vladimir Marko58155012015-08-19 12:49:41 +00004344 DCHECK(!current_method.IsValid());
4345 method_reg = reg;
4346 __ movl(reg, Address(ESP, kCurrentMethodStackOffset));
4347 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00004348 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +01004349 __ movl(reg, Address(method_reg,
4350 ArtMethod::DexCacheResolvedMethodsOffset(kX86PointerSize).Int32Value()));
Vladimir Marko40ecb122016-04-06 17:33:41 +01004351 // temp = temp[index_in_cache];
4352 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
4353 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Vladimir Marko58155012015-08-19 12:49:41 +00004354 __ movl(reg, Address(reg, CodeGenerator::GetCachePointerOffset(index_in_cache)));
4355 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +01004356 }
Vladimir Marko58155012015-08-19 12:49:41 +00004357 }
Serguei Katkov288c7a82016-05-16 11:53:15 +06004358 return callee_method;
4359}
4360
4361void CodeGeneratorX86::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
4362 Location callee_method = GenerateCalleeMethodStaticOrDirectCall(invoke, temp);
Vladimir Marko58155012015-08-19 12:49:41 +00004363
4364 switch (invoke->GetCodePtrLocation()) {
4365 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
4366 __ call(GetFrameEntryLabel());
4367 break;
4368 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative: {
4369 relative_call_patches_.emplace_back(invoke->GetTargetMethod());
4370 Label* label = &relative_call_patches_.back().label;
4371 __ call(label); // Bind to the patch label, override at link time.
4372 __ Bind(label); // Bind the label at the end of the "call" insn.
4373 break;
4374 }
4375 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
4376 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
Vladimir Markodc151b22015-10-15 18:02:30 +01004377 // Filtered out by GetSupportedInvokeStaticOrDirectDispatch().
4378 LOG(FATAL) << "Unsupported";
4379 UNREACHABLE();
Vladimir Marko58155012015-08-19 12:49:41 +00004380 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
4381 // (callee_method + offset_of_quick_compiled_code)()
4382 __ call(Address(callee_method.AsRegister<Register>(),
4383 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07004384 kX86PointerSize).Int32Value()));
Vladimir Marko58155012015-08-19 12:49:41 +00004385 break;
Mark Mendell09ed1a32015-03-25 08:30:06 -04004386 }
4387
4388 DCHECK(!IsLeafMethod());
Mark Mendell09ed1a32015-03-25 08:30:06 -04004389}
4390
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004391void CodeGeneratorX86::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_in) {
4392 Register temp = temp_in.AsRegister<Register>();
4393 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
4394 invoke->GetVTableIndex(), kX86PointerSize).Uint32Value();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004395
4396 // Use the calling convention instead of the location of the receiver, as
4397 // intrinsics may have put the receiver in a different register. In the intrinsics
4398 // slow path, the arguments have been moved to the right place, so here we are
4399 // guaranteed that the receiver is the first register of the calling convention.
4400 InvokeDexCallingConvention calling_convention;
4401 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004402 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004403 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004404 __ movl(temp, Address(receiver, class_offset));
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004405 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00004406 // Instead of simply (possibly) unpoisoning `temp` here, we should
4407 // emit a read barrier for the previous class reference load.
4408 // However this is not required in practice, as this is an
4409 // intermediate/temporary reference and because the current
4410 // concurrent copying collector keeps the from-space memory
4411 // intact/accessible until the end of the marking phase (the
4412 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004413 __ MaybeUnpoisonHeapReference(temp);
4414 // temp = temp->GetMethodAt(method_offset);
4415 __ movl(temp, Address(temp, method_offset));
4416 // call temp->GetEntryPoint();
4417 __ call(Address(
Andreas Gampe542451c2016-07-26 09:02:02 -07004418 temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86PointerSize).Int32Value()));
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004419}
4420
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004421void CodeGeneratorX86::RecordSimplePatch() {
4422 if (GetCompilerOptions().GetIncludePatchInformation()) {
4423 simple_patches_.emplace_back();
4424 __ Bind(&simple_patches_.back());
4425 }
4426}
4427
4428void CodeGeneratorX86::RecordStringPatch(HLoadString* load_string) {
4429 string_patches_.emplace_back(load_string->GetDexFile(), load_string->GetStringIndex());
4430 __ Bind(&string_patches_.back().label);
4431}
4432
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004433void CodeGeneratorX86::RecordTypePatch(HLoadClass* load_class) {
4434 type_patches_.emplace_back(load_class->GetDexFile(), load_class->GetTypeIndex());
4435 __ Bind(&type_patches_.back().label);
4436}
4437
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004438Label* CodeGeneratorX86::NewPcRelativeDexCacheArrayPatch(const DexFile& dex_file,
4439 uint32_t element_offset) {
4440 // Add the patch entry and bind its label at the end of the instruction.
4441 pc_relative_dex_cache_patches_.emplace_back(dex_file, element_offset);
4442 return &pc_relative_dex_cache_patches_.back().label;
4443}
4444
Vladimir Marko58155012015-08-19 12:49:41 +00004445void CodeGeneratorX86::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
4446 DCHECK(linker_patches->empty());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004447 size_t size =
4448 method_patches_.size() +
4449 relative_call_patches_.size() +
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004450 pc_relative_dex_cache_patches_.size() +
4451 simple_patches_.size() +
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004452 string_patches_.size() +
4453 type_patches_.size();
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004454 linker_patches->reserve(size);
4455 // The label points to the end of the "movl" insn but the literal offset for method
4456 // patch needs to point to the embedded constant which occupies the last 4 bytes.
4457 constexpr uint32_t kLabelPositionToLiteralOffsetAdjustment = 4u;
Vladimir Marko58155012015-08-19 12:49:41 +00004458 for (const MethodPatchInfo<Label>& info : method_patches_) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004459 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Marko58155012015-08-19 12:49:41 +00004460 linker_patches->push_back(LinkerPatch::MethodPatch(literal_offset,
4461 info.target_method.dex_file,
4462 info.target_method.dex_method_index));
4463 }
4464 for (const MethodPatchInfo<Label>& info : relative_call_patches_) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004465 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Marko58155012015-08-19 12:49:41 +00004466 linker_patches->push_back(LinkerPatch::RelativeCodePatch(literal_offset,
4467 info.target_method.dex_file,
4468 info.target_method.dex_method_index));
4469 }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004470 for (const PcRelativeDexCacheAccessInfo& info : pc_relative_dex_cache_patches_) {
4471 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
4472 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(literal_offset,
4473 &info.target_dex_file,
4474 GetMethodAddressOffset(),
4475 info.element_offset));
4476 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004477 for (const Label& label : simple_patches_) {
4478 uint32_t literal_offset = label.Position() - kLabelPositionToLiteralOffsetAdjustment;
4479 linker_patches->push_back(LinkerPatch::RecordPosition(literal_offset));
4480 }
4481 if (GetCompilerOptions().GetCompilePic()) {
4482 for (const StringPatchInfo<Label>& info : string_patches_) {
4483 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
4484 linker_patches->push_back(LinkerPatch::RelativeStringPatch(literal_offset,
4485 &info.dex_file,
4486 GetMethodAddressOffset(),
4487 info.string_index));
4488 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004489 for (const TypePatchInfo<Label>& info : type_patches_) {
4490 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
4491 linker_patches->push_back(LinkerPatch::RelativeTypePatch(literal_offset,
4492 &info.dex_file,
4493 GetMethodAddressOffset(),
4494 info.type_index));
4495 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004496 } else {
4497 for (const StringPatchInfo<Label>& info : string_patches_) {
4498 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
4499 linker_patches->push_back(LinkerPatch::StringPatch(literal_offset,
4500 &info.dex_file,
4501 info.string_index));
4502 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004503 for (const TypePatchInfo<Label>& info : type_patches_) {
4504 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
4505 linker_patches->push_back(LinkerPatch::TypePatch(literal_offset,
4506 &info.dex_file,
4507 info.type_index));
4508 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004509 }
Vladimir Marko58155012015-08-19 12:49:41 +00004510}
4511
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004512void CodeGeneratorX86::MarkGCCard(Register temp,
4513 Register card,
4514 Register object,
4515 Register value,
4516 bool value_can_be_null) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004517 NearLabel is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004518 if (value_can_be_null) {
4519 __ testl(value, value);
4520 __ j(kEqual, &is_null);
4521 }
Andreas Gampe542451c2016-07-26 09:02:02 -07004522 __ fs()->movl(card, Address::Absolute(Thread::CardTableOffset<kX86PointerSize>().Int32Value()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004523 __ movl(temp, object);
4524 __ shrl(temp, Immediate(gc::accounting::CardTable::kCardShift));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00004525 __ movb(Address(temp, card, TIMES_1, 0),
4526 X86ManagedRegister::FromCpuRegister(card).AsByteRegister());
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004527 if (value_can_be_null) {
4528 __ Bind(&is_null);
4529 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004530}
4531
Calin Juravle52c48962014-12-16 17:02:57 +00004532void LocationsBuilderX86::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
4533 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain0d5a2812015-11-13 10:07:31 +00004534
4535 bool object_field_get_with_read_barrier =
4536 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004537 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00004538 new (GetGraph()->GetArena()) LocationSummary(instruction,
4539 kEmitCompilerReadBarrier ?
4540 LocationSummary::kCallOnSlowPath :
4541 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01004542 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
4543 locations->SetCustomSlowPathCallerSaves(RegisterSet()); // No caller-save registers.
4544 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004545 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004546
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004547 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4548 locations->SetOut(Location::RequiresFpuRegister());
4549 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004550 // The output overlaps in case of long: we don't want the low move
4551 // to overwrite the object's location. Likewise, in the case of
4552 // an object field get with read barriers enabled, we do not want
4553 // the move to overwrite the object's location, as we need it to emit
4554 // the read barrier.
4555 locations->SetOut(
4556 Location::RequiresRegister(),
4557 (object_field_get_with_read_barrier || instruction->GetType() == Primitive::kPrimLong) ?
4558 Location::kOutputOverlap :
4559 Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004560 }
Calin Juravle52c48962014-12-16 17:02:57 +00004561
4562 if (field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong)) {
4563 // Long values can be loaded atomically into an XMM using movsd.
Roland Levillain7c1559a2015-12-15 10:55:36 +00004564 // So we use an XMM register as a temp to achieve atomicity (first
4565 // load the temp into the XMM and then copy the XMM into the
4566 // output, 32 bits at a time).
Calin Juravle52c48962014-12-16 17:02:57 +00004567 locations->AddTemp(Location::RequiresFpuRegister());
4568 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004569}
4570
Calin Juravle52c48962014-12-16 17:02:57 +00004571void InstructionCodeGeneratorX86::HandleFieldGet(HInstruction* instruction,
4572 const FieldInfo& field_info) {
4573 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004574
Calin Juravle52c48962014-12-16 17:02:57 +00004575 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004576 Location base_loc = locations->InAt(0);
4577 Register base = base_loc.AsRegister<Register>();
Calin Juravle52c48962014-12-16 17:02:57 +00004578 Location out = locations->Out();
4579 bool is_volatile = field_info.IsVolatile();
4580 Primitive::Type field_type = field_info.GetFieldType();
4581 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4582
4583 switch (field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004584 case Primitive::kPrimBoolean: {
Calin Juravle52c48962014-12-16 17:02:57 +00004585 __ movzxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004586 break;
4587 }
4588
4589 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00004590 __ movsxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004591 break;
4592 }
4593
4594 case Primitive::kPrimShort: {
Calin Juravle52c48962014-12-16 17:02:57 +00004595 __ movsxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004596 break;
4597 }
4598
4599 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00004600 __ movzxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004601 break;
4602 }
4603
4604 case Primitive::kPrimInt:
Calin Juravle52c48962014-12-16 17:02:57 +00004605 __ movl(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004606 break;
Roland Levillain7c1559a2015-12-15 10:55:36 +00004607
4608 case Primitive::kPrimNot: {
4609 // /* HeapReference<Object> */ out = *(base + offset)
4610 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004611 // Note that a potential implicit null check is handled in this
4612 // CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier call.
4613 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00004614 instruction, out, base, offset, /* needs_null_check */ true);
Roland Levillain7c1559a2015-12-15 10:55:36 +00004615 if (is_volatile) {
4616 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4617 }
4618 } else {
4619 __ movl(out.AsRegister<Register>(), Address(base, offset));
4620 codegen_->MaybeRecordImplicitNullCheck(instruction);
4621 if (is_volatile) {
4622 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4623 }
4624 // If read barriers are enabled, emit read barriers other than
4625 // Baker's using a slow path (and also unpoison the loaded
4626 // reference, if heap poisoning is enabled).
4627 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
4628 }
4629 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004630 }
4631
4632 case Primitive::kPrimLong: {
Calin Juravle52c48962014-12-16 17:02:57 +00004633 if (is_volatile) {
4634 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4635 __ movsd(temp, Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004636 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004637 __ movd(out.AsRegisterPairLow<Register>(), temp);
4638 __ psrlq(temp, Immediate(32));
4639 __ movd(out.AsRegisterPairHigh<Register>(), temp);
4640 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004641 DCHECK_NE(base, out.AsRegisterPairLow<Register>());
Calin Juravle52c48962014-12-16 17:02:57 +00004642 __ movl(out.AsRegisterPairLow<Register>(), Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004643 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004644 __ movl(out.AsRegisterPairHigh<Register>(), Address(base, kX86WordSize + offset));
4645 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004646 break;
4647 }
4648
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004649 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00004650 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004651 break;
4652 }
4653
4654 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00004655 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004656 break;
4657 }
4658
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004659 case Primitive::kPrimVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00004660 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004661 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004662 }
Calin Juravle52c48962014-12-16 17:02:57 +00004663
Roland Levillain7c1559a2015-12-15 10:55:36 +00004664 if (field_type == Primitive::kPrimNot || field_type == Primitive::kPrimLong) {
4665 // Potential implicit null checks, in the case of reference or
4666 // long fields, are handled in the previous switch statement.
4667 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00004668 codegen_->MaybeRecordImplicitNullCheck(instruction);
4669 }
4670
Calin Juravle52c48962014-12-16 17:02:57 +00004671 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004672 if (field_type == Primitive::kPrimNot) {
4673 // Memory barriers, in the case of references, are also handled
4674 // in the previous switch statement.
4675 } else {
4676 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4677 }
Roland Levillain4d027112015-07-01 15:41:14 +01004678 }
Calin Juravle52c48962014-12-16 17:02:57 +00004679}
4680
4681void LocationsBuilderX86::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
4682 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4683
4684 LocationSummary* locations =
4685 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4686 locations->SetInAt(0, Location::RequiresRegister());
4687 bool is_volatile = field_info.IsVolatile();
4688 Primitive::Type field_type = field_info.GetFieldType();
4689 bool is_byte_type = (field_type == Primitive::kPrimBoolean)
4690 || (field_type == Primitive::kPrimByte);
4691
4692 // The register allocator does not support multiple
4693 // inputs that die at entry with one in a specific register.
4694 if (is_byte_type) {
4695 // Ensure the value is in a byte register.
4696 locations->SetInAt(1, Location::RegisterLocation(EAX));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004697 } else if (Primitive::IsFloatingPointType(field_type)) {
Mark Mendell81489372015-11-04 11:30:41 -05004698 if (is_volatile && field_type == Primitive::kPrimDouble) {
4699 // In order to satisfy the semantics of volatile, this must be a single instruction store.
4700 locations->SetInAt(1, Location::RequiresFpuRegister());
4701 } else {
4702 locations->SetInAt(1, Location::FpuRegisterOrConstant(instruction->InputAt(1)));
4703 }
4704 } else if (is_volatile && field_type == Primitive::kPrimLong) {
4705 // In order to satisfy the semantics of volatile, this must be a single instruction store.
Calin Juravle52c48962014-12-16 17:02:57 +00004706 locations->SetInAt(1, Location::RequiresRegister());
Mark Mendell81489372015-11-04 11:30:41 -05004707
Calin Juravle52c48962014-12-16 17:02:57 +00004708 // 64bits value can be atomically written to an address with movsd and an XMM register.
4709 // We need two XMM registers because there's no easier way to (bit) copy a register pair
4710 // into a single XMM register (we copy each pair part into the XMMs and then interleave them).
4711 // NB: We could make the register allocator understand fp_reg <-> core_reg moves but given the
4712 // isolated cases when we need this it isn't worth adding the extra complexity.
4713 locations->AddTemp(Location::RequiresFpuRegister());
4714 locations->AddTemp(Location::RequiresFpuRegister());
Mark Mendell81489372015-11-04 11:30:41 -05004715 } else {
4716 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4717
4718 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
4719 // Temporary registers for the write barrier.
4720 locations->AddTemp(Location::RequiresRegister()); // May be used for reference poisoning too.
4721 // Ensure the card is in a byte register.
4722 locations->AddTemp(Location::RegisterLocation(ECX));
4723 }
Calin Juravle52c48962014-12-16 17:02:57 +00004724 }
4725}
4726
4727void InstructionCodeGeneratorX86::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004728 const FieldInfo& field_info,
4729 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00004730 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4731
4732 LocationSummary* locations = instruction->GetLocations();
4733 Register base = locations->InAt(0).AsRegister<Register>();
4734 Location value = locations->InAt(1);
4735 bool is_volatile = field_info.IsVolatile();
4736 Primitive::Type field_type = field_info.GetFieldType();
4737 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01004738 bool needs_write_barrier =
4739 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00004740
4741 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004742 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
Calin Juravle52c48962014-12-16 17:02:57 +00004743 }
4744
Mark Mendell81489372015-11-04 11:30:41 -05004745 bool maybe_record_implicit_null_check_done = false;
4746
Calin Juravle52c48962014-12-16 17:02:57 +00004747 switch (field_type) {
4748 case Primitive::kPrimBoolean:
4749 case Primitive::kPrimByte: {
4750 __ movb(Address(base, offset), value.AsRegister<ByteRegister>());
4751 break;
4752 }
4753
4754 case Primitive::kPrimShort:
4755 case Primitive::kPrimChar: {
Mark Mendell81489372015-11-04 11:30:41 -05004756 if (value.IsConstant()) {
4757 int16_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4758 __ movw(Address(base, offset), Immediate(v));
4759 } else {
4760 __ movw(Address(base, offset), value.AsRegister<Register>());
4761 }
Calin Juravle52c48962014-12-16 17:02:57 +00004762 break;
4763 }
4764
4765 case Primitive::kPrimInt:
4766 case Primitive::kPrimNot: {
Roland Levillain4d027112015-07-01 15:41:14 +01004767 if (kPoisonHeapReferences && needs_write_barrier) {
4768 // Note that in the case where `value` is a null reference,
4769 // we do not enter this block, as the reference does not
4770 // need poisoning.
4771 DCHECK_EQ(field_type, Primitive::kPrimNot);
4772 Register temp = locations->GetTemp(0).AsRegister<Register>();
4773 __ movl(temp, value.AsRegister<Register>());
4774 __ PoisonHeapReference(temp);
4775 __ movl(Address(base, offset), temp);
Mark Mendell81489372015-11-04 11:30:41 -05004776 } else if (value.IsConstant()) {
4777 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4778 __ movl(Address(base, offset), Immediate(v));
Roland Levillain4d027112015-07-01 15:41:14 +01004779 } else {
Nicolas Geoffray03971632016-03-17 10:44:24 +00004780 DCHECK(value.IsRegister()) << value;
Roland Levillain4d027112015-07-01 15:41:14 +01004781 __ movl(Address(base, offset), value.AsRegister<Register>());
4782 }
Calin Juravle52c48962014-12-16 17:02:57 +00004783 break;
4784 }
4785
4786 case Primitive::kPrimLong: {
4787 if (is_volatile) {
4788 XmmRegister temp1 = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4789 XmmRegister temp2 = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
4790 __ movd(temp1, value.AsRegisterPairLow<Register>());
4791 __ movd(temp2, value.AsRegisterPairHigh<Register>());
4792 __ punpckldq(temp1, temp2);
4793 __ movsd(Address(base, offset), temp1);
Calin Juravle77520bc2015-01-12 18:45:46 +00004794 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell81489372015-11-04 11:30:41 -05004795 } else if (value.IsConstant()) {
4796 int64_t v = CodeGenerator::GetInt64ValueOf(value.GetConstant());
4797 __ movl(Address(base, offset), Immediate(Low32Bits(v)));
4798 codegen_->MaybeRecordImplicitNullCheck(instruction);
4799 __ movl(Address(base, kX86WordSize + offset), Immediate(High32Bits(v)));
Calin Juravle52c48962014-12-16 17:02:57 +00004800 } else {
4801 __ movl(Address(base, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00004802 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004803 __ movl(Address(base, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
4804 }
Mark Mendell81489372015-11-04 11:30:41 -05004805 maybe_record_implicit_null_check_done = true;
Calin Juravle52c48962014-12-16 17:02:57 +00004806 break;
4807 }
4808
4809 case Primitive::kPrimFloat: {
Mark Mendell81489372015-11-04 11:30:41 -05004810 if (value.IsConstant()) {
4811 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4812 __ movl(Address(base, offset), Immediate(v));
4813 } else {
4814 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4815 }
Calin Juravle52c48962014-12-16 17:02:57 +00004816 break;
4817 }
4818
4819 case Primitive::kPrimDouble: {
Mark Mendell81489372015-11-04 11:30:41 -05004820 if (value.IsConstant()) {
4821 int64_t v = CodeGenerator::GetInt64ValueOf(value.GetConstant());
4822 __ movl(Address(base, offset), Immediate(Low32Bits(v)));
4823 codegen_->MaybeRecordImplicitNullCheck(instruction);
4824 __ movl(Address(base, kX86WordSize + offset), Immediate(High32Bits(v)));
4825 maybe_record_implicit_null_check_done = true;
4826 } else {
4827 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4828 }
Calin Juravle52c48962014-12-16 17:02:57 +00004829 break;
4830 }
4831
4832 case Primitive::kPrimVoid:
4833 LOG(FATAL) << "Unreachable type " << field_type;
4834 UNREACHABLE();
4835 }
4836
Mark Mendell81489372015-11-04 11:30:41 -05004837 if (!maybe_record_implicit_null_check_done) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004838 codegen_->MaybeRecordImplicitNullCheck(instruction);
4839 }
4840
Roland Levillain4d027112015-07-01 15:41:14 +01004841 if (needs_write_barrier) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004842 Register temp = locations->GetTemp(0).AsRegister<Register>();
4843 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004844 codegen_->MarkGCCard(temp, card, base, value.AsRegister<Register>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00004845 }
4846
Calin Juravle52c48962014-12-16 17:02:57 +00004847 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004848 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
Calin Juravle52c48962014-12-16 17:02:57 +00004849 }
4850}
4851
4852void LocationsBuilderX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4853 HandleFieldGet(instruction, instruction->GetFieldInfo());
4854}
4855
4856void InstructionCodeGeneratorX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4857 HandleFieldGet(instruction, instruction->GetFieldInfo());
4858}
4859
4860void LocationsBuilderX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
4861 HandleFieldSet(instruction, instruction->GetFieldInfo());
4862}
4863
4864void InstructionCodeGeneratorX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004865 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00004866}
4867
4868void LocationsBuilderX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
4869 HandleFieldSet(instruction, instruction->GetFieldInfo());
4870}
4871
4872void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004873 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00004874}
4875
4876void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4877 HandleFieldGet(instruction, instruction->GetFieldInfo());
4878}
4879
4880void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4881 HandleFieldGet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004882}
4883
Calin Juravlee460d1d2015-09-29 04:52:17 +01004884void LocationsBuilderX86::VisitUnresolvedInstanceFieldGet(
4885 HUnresolvedInstanceFieldGet* instruction) {
4886 FieldAccessCallingConventionX86 calling_convention;
4887 codegen_->CreateUnresolvedFieldLocationSummary(
4888 instruction, instruction->GetFieldType(), calling_convention);
4889}
4890
4891void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldGet(
4892 HUnresolvedInstanceFieldGet* instruction) {
4893 FieldAccessCallingConventionX86 calling_convention;
4894 codegen_->GenerateUnresolvedFieldAccess(instruction,
4895 instruction->GetFieldType(),
4896 instruction->GetFieldIndex(),
4897 instruction->GetDexPc(),
4898 calling_convention);
4899}
4900
4901void LocationsBuilderX86::VisitUnresolvedInstanceFieldSet(
4902 HUnresolvedInstanceFieldSet* instruction) {
4903 FieldAccessCallingConventionX86 calling_convention;
4904 codegen_->CreateUnresolvedFieldLocationSummary(
4905 instruction, instruction->GetFieldType(), calling_convention);
4906}
4907
4908void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldSet(
4909 HUnresolvedInstanceFieldSet* instruction) {
4910 FieldAccessCallingConventionX86 calling_convention;
4911 codegen_->GenerateUnresolvedFieldAccess(instruction,
4912 instruction->GetFieldType(),
4913 instruction->GetFieldIndex(),
4914 instruction->GetDexPc(),
4915 calling_convention);
4916}
4917
4918void LocationsBuilderX86::VisitUnresolvedStaticFieldGet(
4919 HUnresolvedStaticFieldGet* instruction) {
4920 FieldAccessCallingConventionX86 calling_convention;
4921 codegen_->CreateUnresolvedFieldLocationSummary(
4922 instruction, instruction->GetFieldType(), calling_convention);
4923}
4924
4925void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldGet(
4926 HUnresolvedStaticFieldGet* instruction) {
4927 FieldAccessCallingConventionX86 calling_convention;
4928 codegen_->GenerateUnresolvedFieldAccess(instruction,
4929 instruction->GetFieldType(),
4930 instruction->GetFieldIndex(),
4931 instruction->GetDexPc(),
4932 calling_convention);
4933}
4934
4935void LocationsBuilderX86::VisitUnresolvedStaticFieldSet(
4936 HUnresolvedStaticFieldSet* instruction) {
4937 FieldAccessCallingConventionX86 calling_convention;
4938 codegen_->CreateUnresolvedFieldLocationSummary(
4939 instruction, instruction->GetFieldType(), calling_convention);
4940}
4941
4942void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldSet(
4943 HUnresolvedStaticFieldSet* instruction) {
4944 FieldAccessCallingConventionX86 calling_convention;
4945 codegen_->GenerateUnresolvedFieldAccess(instruction,
4946 instruction->GetFieldType(),
4947 instruction->GetFieldIndex(),
4948 instruction->GetDexPc(),
4949 calling_convention);
4950}
4951
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004952void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko3b7537b2016-09-13 11:56:01 +00004953 LocationSummary* locations = codegen_->CreateNullCheckLocations(instruction);
4954 if (!codegen_->GetCompilerOptions().GetImplicitNullChecks()) {
4955 // Explicit null checks can use any location.
4956 locations->SetInAt(0, Location::Any());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004957 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004958}
4959
Calin Juravle2ae48182016-03-16 14:05:09 +00004960void CodeGeneratorX86::GenerateImplicitNullCheck(HNullCheck* instruction) {
4961 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004962 return;
4963 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004964 LocationSummary* locations = instruction->GetLocations();
4965 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00004966
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004967 __ testl(EAX, Address(obj.AsRegister<Register>(), 0));
Calin Juravle2ae48182016-03-16 14:05:09 +00004968 RecordPcInfo(instruction, instruction->GetDexPc());
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004969}
4970
Calin Juravle2ae48182016-03-16 14:05:09 +00004971void CodeGeneratorX86::GenerateExplicitNullCheck(HNullCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07004972 SlowPathCode* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00004973 AddSlowPath(slow_path);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004974
4975 LocationSummary* locations = instruction->GetLocations();
4976 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004977
4978 if (obj.IsRegister()) {
Mark Mendell42514f62015-03-31 11:34:22 -04004979 __ testl(obj.AsRegister<Register>(), obj.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004980 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004981 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004982 } else {
4983 DCHECK(obj.IsConstant()) << obj;
David Brazdil77a48ae2015-09-15 12:34:04 +00004984 DCHECK(obj.GetConstant()->IsNullConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004985 __ jmp(slow_path->GetEntryLabel());
4986 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004987 }
4988 __ j(kEqual, slow_path->GetEntryLabel());
4989}
4990
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004991void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00004992 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004993}
4994
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004995void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004996 bool object_array_get_with_read_barrier =
4997 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004998 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00004999 new (GetGraph()->GetArena()) LocationSummary(instruction,
5000 object_array_get_with_read_barrier ?
5001 LocationSummary::kCallOnSlowPath :
5002 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01005003 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
5004 locations->SetCustomSlowPathCallerSaves(RegisterSet()); // No caller-save registers.
5005 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005006 locations->SetInAt(0, Location::RequiresRegister());
5007 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005008 if (Primitive::IsFloatingPointType(instruction->GetType())) {
5009 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5010 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005011 // The output overlaps in case of long: we don't want the low move
5012 // to overwrite the array's location. Likewise, in the case of an
5013 // object array get with read barriers enabled, we do not want the
5014 // move to overwrite the array's location, as we need it to emit
5015 // the read barrier.
5016 locations->SetOut(
5017 Location::RequiresRegister(),
5018 (instruction->GetType() == Primitive::kPrimLong || object_array_get_with_read_barrier) ?
5019 Location::kOutputOverlap :
5020 Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005021 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005022}
5023
5024void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
5025 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005026 Location obj_loc = locations->InAt(0);
5027 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005028 Location index = locations->InAt(1);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005029 Location out_loc = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005030 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005031
Calin Juravle77520bc2015-01-12 18:45:46 +00005032 Primitive::Type type = instruction->GetType();
5033 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005034 case Primitive::kPrimBoolean: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005035 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005036 if (index.IsConstant()) {
5037 __ movzxb(out, Address(obj,
5038 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
5039 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005040 __ movzxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005041 }
5042 break;
5043 }
5044
5045 case Primitive::kPrimByte: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005046 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005047 if (index.IsConstant()) {
5048 __ movsxb(out, Address(obj,
5049 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
5050 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005051 __ movsxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005052 }
5053 break;
5054 }
5055
5056 case Primitive::kPrimShort: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005057 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005058 if (index.IsConstant()) {
5059 __ movsxw(out, Address(obj,
5060 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
5061 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005062 __ movsxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005063 }
5064 break;
5065 }
5066
5067 case Primitive::kPrimChar: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005068 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005069 if (index.IsConstant()) {
5070 __ movzxw(out, Address(obj,
5071 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
5072 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005073 __ movzxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005074 }
5075 break;
5076 }
5077
Roland Levillain7c1559a2015-12-15 10:55:36 +00005078 case Primitive::kPrimInt: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005079 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005080 if (index.IsConstant()) {
5081 __ movl(out, Address(obj,
5082 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
5083 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005084 __ movl(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005085 }
5086 break;
5087 }
5088
Roland Levillain7c1559a2015-12-15 10:55:36 +00005089 case Primitive::kPrimNot: {
5090 static_assert(
5091 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
5092 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain7c1559a2015-12-15 10:55:36 +00005093 // /* HeapReference<Object> */ out =
5094 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
5095 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005096 // Note that a potential implicit null check is handled in this
5097 // CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier call.
5098 codegen_->GenerateArrayLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00005099 instruction, out_loc, obj, data_offset, index, /* needs_null_check */ true);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005100 } else {
5101 Register out = out_loc.AsRegister<Register>();
5102 if (index.IsConstant()) {
5103 uint32_t offset =
5104 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
5105 __ movl(out, Address(obj, offset));
5106 codegen_->MaybeRecordImplicitNullCheck(instruction);
5107 // If read barriers are enabled, emit read barriers other than
5108 // Baker's using a slow path (and also unpoison the loaded
5109 // reference, if heap poisoning is enabled).
5110 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
5111 } else {
5112 __ movl(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
5113 codegen_->MaybeRecordImplicitNullCheck(instruction);
5114 // If read barriers are enabled, emit read barriers other than
5115 // Baker's using a slow path (and also unpoison the loaded
5116 // reference, if heap poisoning is enabled).
5117 codegen_->MaybeGenerateReadBarrierSlow(
5118 instruction, out_loc, out_loc, obj_loc, data_offset, index);
5119 }
5120 }
5121 break;
5122 }
5123
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005124 case Primitive::kPrimLong: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005125 DCHECK_NE(obj, out_loc.AsRegisterPairLow<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005126 if (index.IsConstant()) {
5127 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Roland Levillain7c1559a2015-12-15 10:55:36 +00005128 __ movl(out_loc.AsRegisterPairLow<Register>(), Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005129 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005130 __ movl(out_loc.AsRegisterPairHigh<Register>(), Address(obj, offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005131 } else {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005132 __ movl(out_loc.AsRegisterPairLow<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005133 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005134 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005135 __ movl(out_loc.AsRegisterPairHigh<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005136 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005137 }
5138 break;
5139 }
5140
Mark Mendell7c8d0092015-01-26 11:21:33 -05005141 case Primitive::kPrimFloat: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005142 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Mark Mendell7c8d0092015-01-26 11:21:33 -05005143 if (index.IsConstant()) {
5144 __ movss(out, Address(obj,
5145 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
5146 } else {
5147 __ movss(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
5148 }
5149 break;
5150 }
5151
5152 case Primitive::kPrimDouble: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005153 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Mark Mendell7c8d0092015-01-26 11:21:33 -05005154 if (index.IsConstant()) {
5155 __ movsd(out, Address(obj,
5156 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
5157 } else {
5158 __ movsd(out, Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
5159 }
5160 break;
5161 }
5162
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005163 case Primitive::kPrimVoid:
Calin Juravle77520bc2015-01-12 18:45:46 +00005164 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07005165 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005166 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005167
Roland Levillain7c1559a2015-12-15 10:55:36 +00005168 if (type == Primitive::kPrimNot || type == Primitive::kPrimLong) {
5169 // Potential implicit null checks, in the case of reference or
5170 // long arrays, are handled in the previous switch statement.
5171 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00005172 codegen_->MaybeRecordImplicitNullCheck(instruction);
5173 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005174}
5175
5176void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01005177 Primitive::Type value_type = instruction->GetComponentType();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005178
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005179 bool needs_write_barrier =
5180 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Roland Levillain0d5a2812015-11-13 10:07:31 +00005181 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005182
Nicolas Geoffray39468442014-09-02 15:17:15 +01005183 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
5184 instruction,
Vladimir Marko8d49fd72016-08-25 15:20:47 +01005185 may_need_runtime_call_for_type_check ?
Roland Levillain0d5a2812015-11-13 10:07:31 +00005186 LocationSummary::kCallOnSlowPath :
5187 LocationSummary::kNoCall);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005188
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005189 bool is_byte_type = (value_type == Primitive::kPrimBoolean)
5190 || (value_type == Primitive::kPrimByte);
5191 // We need the inputs to be different than the output in case of long operation.
5192 // In case of a byte operation, the register allocator does not support multiple
5193 // inputs that die at entry with one in a specific register.
5194 locations->SetInAt(0, Location::RequiresRegister());
5195 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
5196 if (is_byte_type) {
5197 // Ensure the value is in a byte register.
5198 locations->SetInAt(2, Location::ByteRegisterOrConstant(EAX, instruction->InputAt(2)));
5199 } else if (Primitive::IsFloatingPointType(value_type)) {
Mark Mendell81489372015-11-04 11:30:41 -05005200 locations->SetInAt(2, Location::FpuRegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005201 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005202 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
5203 }
5204 if (needs_write_barrier) {
5205 // Temporary registers for the write barrier.
5206 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
5207 // Ensure the card is in a byte register.
Roland Levillain4f6b0b52015-11-23 19:29:22 +00005208 locations->AddTemp(Location::RegisterLocation(ECX));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005209 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005210}
5211
5212void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
5213 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005214 Location array_loc = locations->InAt(0);
5215 Register array = array_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005216 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005217 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005218 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005219 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
5220 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5221 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005222 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005223 bool needs_write_barrier =
5224 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005225
5226 switch (value_type) {
5227 case Primitive::kPrimBoolean:
5228 case Primitive::kPrimByte: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005229 uint32_t offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
5230 Address address = index.IsConstant()
5231 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + offset)
5232 : Address(array, index.AsRegister<Register>(), TIMES_1, offset);
5233 if (value.IsRegister()) {
5234 __ movb(address, value.AsRegister<ByteRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005235 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005236 __ movb(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005237 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005238 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005239 break;
5240 }
5241
5242 case Primitive::kPrimShort:
5243 case Primitive::kPrimChar: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005244 uint32_t offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
5245 Address address = index.IsConstant()
5246 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + offset)
5247 : Address(array, index.AsRegister<Register>(), TIMES_2, offset);
5248 if (value.IsRegister()) {
5249 __ movw(address, value.AsRegister<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005250 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005251 __ movw(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005252 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005253 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005254 break;
5255 }
5256
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005257 case Primitive::kPrimNot: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005258 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
5259 Address address = index.IsConstant()
5260 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
5261 : Address(array, index.AsRegister<Register>(), TIMES_4, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005262
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005263 if (!value.IsRegister()) {
5264 // Just setting null.
5265 DCHECK(instruction->InputAt(2)->IsNullConstant());
5266 DCHECK(value.IsConstant()) << value;
5267 __ movl(address, Immediate(0));
Calin Juravle77520bc2015-01-12 18:45:46 +00005268 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005269 DCHECK(!needs_write_barrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005270 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005271 break;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005272 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005273
5274 DCHECK(needs_write_barrier);
5275 Register register_value = value.AsRegister<Register>();
Roland Levillain16d9f942016-08-25 17:27:56 +01005276 // We cannot use a NearLabel for `done`, as its range may be too
5277 // short when Baker read barriers are enabled.
5278 Label done;
5279 NearLabel not_null, do_put;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005280 SlowPathCode* slow_path = nullptr;
Roland Levillain16d9f942016-08-25 17:27:56 +01005281 Location temp_loc = locations->GetTemp(0);
5282 Register temp = temp_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005283 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005284 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathX86(instruction);
5285 codegen_->AddSlowPath(slow_path);
5286 if (instruction->GetValueCanBeNull()) {
5287 __ testl(register_value, register_value);
5288 __ j(kNotEqual, &not_null);
5289 __ movl(address, Immediate(0));
5290 codegen_->MaybeRecordImplicitNullCheck(instruction);
5291 __ jmp(&done);
5292 __ Bind(&not_null);
5293 }
5294
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005295 // Note that when Baker read barriers are enabled, the type
5296 // checks are performed without read barriers. This is fine,
5297 // even in the case where a class object is in the from-space
5298 // after the flip, as a comparison involving such a type would
5299 // not produce a false positive; it may of course produce a
5300 // false negative, in which case we would take the ArraySet
5301 // slow path.
Roland Levillain16d9f942016-08-25 17:27:56 +01005302
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005303 // /* HeapReference<Class> */ temp = array->klass_
5304 __ movl(temp, Address(array, class_offset));
5305 codegen_->MaybeRecordImplicitNullCheck(instruction);
5306 __ MaybeUnpoisonHeapReference(temp);
Roland Levillain16d9f942016-08-25 17:27:56 +01005307
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005308 // /* HeapReference<Class> */ temp = temp->component_type_
5309 __ movl(temp, Address(temp, component_offset));
5310 // If heap poisoning is enabled, no need to unpoison `temp`
5311 // nor the object reference in `register_value->klass`, as
5312 // we are comparing two poisoned references.
5313 __ cmpl(temp, Address(register_value, class_offset));
Roland Levillain16d9f942016-08-25 17:27:56 +01005314
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005315 if (instruction->StaticTypeOfArrayIsObjectArray()) {
5316 __ j(kEqual, &do_put);
5317 // If heap poisoning is enabled, the `temp` reference has
5318 // not been unpoisoned yet; unpoison it now.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005319 __ MaybeUnpoisonHeapReference(temp);
5320
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005321 // If heap poisoning is enabled, no need to unpoison the
5322 // heap reference loaded below, as it is only used for a
5323 // comparison with null.
5324 __ cmpl(Address(temp, super_offset), Immediate(0));
5325 __ j(kNotEqual, slow_path->GetEntryLabel());
5326 __ Bind(&do_put);
5327 } else {
5328 __ j(kNotEqual, slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005329 }
5330 }
5331
5332 if (kPoisonHeapReferences) {
5333 __ movl(temp, register_value);
5334 __ PoisonHeapReference(temp);
5335 __ movl(address, temp);
5336 } else {
5337 __ movl(address, register_value);
5338 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005339 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005340 codegen_->MaybeRecordImplicitNullCheck(instruction);
5341 }
5342
5343 Register card = locations->GetTemp(1).AsRegister<Register>();
5344 codegen_->MarkGCCard(
5345 temp, card, array, value.AsRegister<Register>(), instruction->GetValueCanBeNull());
5346 __ Bind(&done);
5347
5348 if (slow_path != nullptr) {
5349 __ Bind(slow_path->GetExitLabel());
5350 }
5351
5352 break;
5353 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005354
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005355 case Primitive::kPrimInt: {
5356 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
5357 Address address = index.IsConstant()
5358 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
5359 : Address(array, index.AsRegister<Register>(), TIMES_4, offset);
5360 if (value.IsRegister()) {
5361 __ movl(address, value.AsRegister<Register>());
5362 } else {
5363 DCHECK(value.IsConstant()) << value;
5364 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
5365 __ movl(address, Immediate(v));
5366 }
5367 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005368 break;
5369 }
5370
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005371 case Primitive::kPrimLong: {
5372 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005373 if (index.IsConstant()) {
5374 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005375 if (value.IsRegisterPair()) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005376 __ movl(Address(array, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00005377 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005378 __ movl(Address(array, offset + kX86WordSize), value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005379 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005380 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005381 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005382 __ movl(Address(array, offset), Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00005383 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005384 __ movl(Address(array, offset + kX86WordSize), Immediate(High32Bits(val)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005385 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005386 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005387 if (value.IsRegisterPair()) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005388 __ movl(Address(array, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005389 value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00005390 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005391 __ movl(Address(array, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005392 value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005393 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005394 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005395 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005396 __ movl(Address(array, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005397 Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00005398 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005399 __ movl(Address(array, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005400 Immediate(High32Bits(val)));
5401 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005402 }
5403 break;
5404 }
5405
Mark Mendell7c8d0092015-01-26 11:21:33 -05005406 case Primitive::kPrimFloat: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005407 uint32_t offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
5408 Address address = index.IsConstant()
5409 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
5410 : Address(array, index.AsRegister<Register>(), TIMES_4, offset);
Mark Mendell81489372015-11-04 11:30:41 -05005411 if (value.IsFpuRegister()) {
5412 __ movss(address, value.AsFpuRegister<XmmRegister>());
5413 } else {
5414 DCHECK(value.IsConstant());
5415 int32_t v = bit_cast<int32_t, float>(value.GetConstant()->AsFloatConstant()->GetValue());
5416 __ movl(address, Immediate(v));
5417 }
5418 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005419 break;
5420 }
5421
5422 case Primitive::kPrimDouble: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005423 uint32_t offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
5424 Address address = index.IsConstant()
5425 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + offset)
5426 : Address(array, index.AsRegister<Register>(), TIMES_8, offset);
Mark Mendell81489372015-11-04 11:30:41 -05005427 if (value.IsFpuRegister()) {
5428 __ movsd(address, value.AsFpuRegister<XmmRegister>());
5429 } else {
5430 DCHECK(value.IsConstant());
5431 Address address_hi = index.IsConstant() ?
5432 Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) +
5433 offset + kX86WordSize) :
5434 Address(array, index.AsRegister<Register>(), TIMES_8, offset + kX86WordSize);
5435 int64_t v = bit_cast<int64_t, double>(value.GetConstant()->AsDoubleConstant()->GetValue());
5436 __ movl(address, Immediate(Low32Bits(v)));
5437 codegen_->MaybeRecordImplicitNullCheck(instruction);
5438 __ movl(address_hi, Immediate(High32Bits(v)));
5439 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005440 break;
5441 }
5442
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005443 case Primitive::kPrimVoid:
5444 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07005445 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005446 }
5447}
5448
5449void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
5450 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005451 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellee8d9712016-07-12 11:13:15 -04005452 if (!instruction->IsEmittedAtUseSite()) {
5453 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5454 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005455}
5456
5457void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
Mark Mendellee8d9712016-07-12 11:13:15 -04005458 if (instruction->IsEmittedAtUseSite()) {
5459 return;
5460 }
5461
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005462 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01005463 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00005464 Register obj = locations->InAt(0).AsRegister<Register>();
5465 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005466 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005467 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005468}
5469
5470void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00005471 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
5472 ? LocationSummary::kCallOnSlowPath
5473 : LocationSummary::kNoCall;
5474 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Mark Mendellf60c90b2015-03-04 15:12:59 -05005475 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendellee8d9712016-07-12 11:13:15 -04005476 HInstruction* length = instruction->InputAt(1);
5477 if (!length->IsEmittedAtUseSite()) {
5478 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
5479 }
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005480 if (instruction->HasUses()) {
5481 locations->SetOut(Location::SameAsFirstInput());
5482 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005483}
5484
5485void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
5486 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05005487 Location index_loc = locations->InAt(0);
5488 Location length_loc = locations->InAt(1);
Andreas Gampe85b62f22015-09-09 13:15:38 -07005489 SlowPathCode* slow_path =
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01005490 new (GetGraph()->GetArena()) BoundsCheckSlowPathX86(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005491
Mark Mendell99dbd682015-04-22 16:18:52 -04005492 if (length_loc.IsConstant()) {
5493 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
5494 if (index_loc.IsConstant()) {
5495 // BCE will remove the bounds check if we are guarenteed to pass.
5496 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5497 if (index < 0 || index >= length) {
5498 codegen_->AddSlowPath(slow_path);
5499 __ jmp(slow_path->GetEntryLabel());
5500 } else {
5501 // Some optimization after BCE may have generated this, and we should not
5502 // generate a bounds check if it is a valid range.
5503 }
5504 return;
5505 }
5506
5507 // We have to reverse the jump condition because the length is the constant.
5508 Register index_reg = index_loc.AsRegister<Register>();
5509 __ cmpl(index_reg, Immediate(length));
5510 codegen_->AddSlowPath(slow_path);
5511 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005512 } else {
Mark Mendellee8d9712016-07-12 11:13:15 -04005513 HInstruction* array_length = instruction->InputAt(1);
5514 if (array_length->IsEmittedAtUseSite()) {
5515 // Address the length field in the array.
5516 DCHECK(array_length->IsArrayLength());
5517 uint32_t len_offset = CodeGenerator::GetArrayLengthOffset(array_length->AsArrayLength());
5518 Location array_loc = array_length->GetLocations()->InAt(0);
5519 Address array_len(array_loc.AsRegister<Register>(), len_offset);
5520 if (index_loc.IsConstant()) {
5521 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5522 __ cmpl(array_len, Immediate(value));
5523 } else {
5524 __ cmpl(array_len, index_loc.AsRegister<Register>());
5525 }
5526 codegen_->MaybeRecordImplicitNullCheck(array_length);
Mark Mendell99dbd682015-04-22 16:18:52 -04005527 } else {
Mark Mendellee8d9712016-07-12 11:13:15 -04005528 Register length = length_loc.AsRegister<Register>();
5529 if (index_loc.IsConstant()) {
5530 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5531 __ cmpl(length, Immediate(value));
5532 } else {
5533 __ cmpl(length, index_loc.AsRegister<Register>());
5534 }
Mark Mendell99dbd682015-04-22 16:18:52 -04005535 }
5536 codegen_->AddSlowPath(slow_path);
5537 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005538 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005539}
5540
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005541void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005542 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005543}
5544
5545void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005546 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5547}
5548
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005549void LocationsBuilderX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Marko70e97462016-08-09 11:04:26 +01005550 LocationSummary* locations =
5551 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
5552 locations->SetCustomSlowPathCallerSaves(RegisterSet()); // No caller-save registers.
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005553}
5554
5555void InstructionCodeGeneratorX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005556 HBasicBlock* block = instruction->GetBlock();
5557 if (block->GetLoopInformation() != nullptr) {
5558 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5559 // The back edge will generate the suspend check.
5560 return;
5561 }
5562 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5563 // The goto will generate the suspend check.
5564 return;
5565 }
5566 GenerateSuspendCheck(instruction, nullptr);
5567}
5568
5569void InstructionCodeGeneratorX86::GenerateSuspendCheck(HSuspendCheck* instruction,
5570 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005571 SuspendCheckSlowPathX86* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005572 down_cast<SuspendCheckSlowPathX86*>(instruction->GetSlowPath());
5573 if (slow_path == nullptr) {
5574 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathX86(instruction, successor);
5575 instruction->SetSlowPath(slow_path);
5576 codegen_->AddSlowPath(slow_path);
5577 if (successor != nullptr) {
5578 DCHECK(successor->IsLoopHeader());
5579 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
5580 }
5581 } else {
5582 DCHECK_EQ(slow_path->GetSuccessor(), successor);
5583 }
5584
Andreas Gampe542451c2016-07-26 09:02:02 -07005585 __ fs()->cmpw(Address::Absolute(Thread::ThreadFlagsOffset<kX86PointerSize>().Int32Value()),
Roland Levillain7c1559a2015-12-15 10:55:36 +00005586 Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005587 if (successor == nullptr) {
5588 __ j(kNotEqual, slow_path->GetEntryLabel());
5589 __ Bind(slow_path->GetReturnLabel());
5590 } else {
5591 __ j(kEqual, codegen_->GetLabelOf(successor));
5592 __ jmp(slow_path->GetEntryLabel());
5593 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005594}
5595
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005596X86Assembler* ParallelMoveResolverX86::GetAssembler() const {
5597 return codegen_->GetAssembler();
5598}
5599
Mark Mendell7c8d0092015-01-26 11:21:33 -05005600void ParallelMoveResolverX86::MoveMemoryToMemory32(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005601 ScratchRegisterScope ensure_scratch(
5602 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5603 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5604 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5605 __ movl(temp_reg, Address(ESP, src + stack_offset));
5606 __ movl(Address(ESP, dst + stack_offset), temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005607}
5608
5609void ParallelMoveResolverX86::MoveMemoryToMemory64(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005610 ScratchRegisterScope ensure_scratch(
5611 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5612 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5613 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5614 __ movl(temp_reg, Address(ESP, src + stack_offset));
5615 __ movl(Address(ESP, dst + stack_offset), temp_reg);
5616 __ movl(temp_reg, Address(ESP, src + stack_offset + kX86WordSize));
5617 __ movl(Address(ESP, dst + stack_offset + kX86WordSize), temp_reg);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005618}
5619
5620void ParallelMoveResolverX86::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005621 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005622 Location source = move->GetSource();
5623 Location destination = move->GetDestination();
5624
5625 if (source.IsRegister()) {
5626 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005627 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
David Brazdil74eb1b22015-12-14 11:44:01 +00005628 } else if (destination.IsFpuRegister()) {
5629 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005630 } else {
5631 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00005632 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005633 }
David Brazdil74eb1b22015-12-14 11:44:01 +00005634 } else if (source.IsRegisterPair()) {
5635 size_t elem_size = Primitive::ComponentSize(Primitive::kPrimInt);
5636 // Create stack space for 2 elements.
5637 __ subl(ESP, Immediate(2 * elem_size));
5638 __ movl(Address(ESP, 0), source.AsRegisterPairLow<Register>());
5639 __ movl(Address(ESP, elem_size), source.AsRegisterPairHigh<Register>());
5640 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
5641 // And remove the temporary stack space we allocated.
5642 __ addl(ESP, Immediate(2 * elem_size));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005643 } else if (source.IsFpuRegister()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00005644 if (destination.IsRegister()) {
5645 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
5646 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005647 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
David Brazdil74eb1b22015-12-14 11:44:01 +00005648 } else if (destination.IsRegisterPair()) {
5649 XmmRegister src_reg = source.AsFpuRegister<XmmRegister>();
5650 __ movd(destination.AsRegisterPairLow<Register>(), src_reg);
5651 __ psrlq(src_reg, Immediate(32));
5652 __ movd(destination.AsRegisterPairHigh<Register>(), src_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005653 } else if (destination.IsStackSlot()) {
5654 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
5655 } else {
5656 DCHECK(destination.IsDoubleStackSlot());
5657 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
5658 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005659 } else if (source.IsStackSlot()) {
5660 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005661 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005662 } else if (destination.IsFpuRegister()) {
5663 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005664 } else {
5665 DCHECK(destination.IsStackSlot());
Mark Mendell7c8d0092015-01-26 11:21:33 -05005666 MoveMemoryToMemory32(destination.GetStackIndex(), source.GetStackIndex());
5667 }
5668 } else if (source.IsDoubleStackSlot()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00005669 if (destination.IsRegisterPair()) {
5670 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
5671 __ movl(destination.AsRegisterPairHigh<Register>(),
5672 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
5673 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005674 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
5675 } else {
5676 DCHECK(destination.IsDoubleStackSlot()) << destination;
5677 MoveMemoryToMemory64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005678 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005679 } else if (source.IsConstant()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005680 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00005681 if (constant->IsIntConstant() || constant->IsNullConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05005682 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005683 if (destination.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05005684 if (value == 0) {
5685 __ xorl(destination.AsRegister<Register>(), destination.AsRegister<Register>());
5686 } else {
5687 __ movl(destination.AsRegister<Register>(), Immediate(value));
5688 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005689 } else {
5690 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell09b84632015-02-13 17:48:38 -05005691 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005692 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005693 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005694 float fp_value = constant->AsFloatConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00005695 int32_t value = bit_cast<int32_t, float>(fp_value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005696 Immediate imm(value);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005697 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005698 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
5699 if (value == 0) {
5700 // Easy handling of 0.0.
5701 __ xorps(dest, dest);
5702 } else {
5703 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005704 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5705 Register temp = static_cast<Register>(ensure_scratch.GetRegister());
5706 __ movl(temp, Immediate(value));
5707 __ movd(dest, temp);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005708 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005709 } else {
5710 DCHECK(destination.IsStackSlot()) << destination;
5711 __ movl(Address(ESP, destination.GetStackIndex()), imm);
5712 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005713 } else if (constant->IsLongConstant()) {
5714 int64_t value = constant->AsLongConstant()->GetValue();
5715 int32_t low_value = Low32Bits(value);
5716 int32_t high_value = High32Bits(value);
5717 Immediate low(low_value);
5718 Immediate high(high_value);
5719 if (destination.IsDoubleStackSlot()) {
5720 __ movl(Address(ESP, destination.GetStackIndex()), low);
5721 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
5722 } else {
5723 __ movl(destination.AsRegisterPairLow<Register>(), low);
5724 __ movl(destination.AsRegisterPairHigh<Register>(), high);
5725 }
5726 } else {
5727 DCHECK(constant->IsDoubleConstant());
5728 double dbl_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00005729 int64_t value = bit_cast<int64_t, double>(dbl_value);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005730 int32_t low_value = Low32Bits(value);
5731 int32_t high_value = High32Bits(value);
5732 Immediate low(low_value);
5733 Immediate high(high_value);
5734 if (destination.IsFpuRegister()) {
5735 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
5736 if (value == 0) {
5737 // Easy handling of 0.0.
5738 __ xorpd(dest, dest);
5739 } else {
5740 __ pushl(high);
5741 __ pushl(low);
5742 __ movsd(dest, Address(ESP, 0));
5743 __ addl(ESP, Immediate(8));
5744 }
5745 } else {
5746 DCHECK(destination.IsDoubleStackSlot()) << destination;
5747 __ movl(Address(ESP, destination.GetStackIndex()), low);
5748 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
5749 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005750 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005751 } else {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00005752 LOG(FATAL) << "Unimplemented move: " << destination << " <- " << source;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005753 }
5754}
5755
Mark Mendella5c19ce2015-04-01 12:51:05 -04005756void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005757 Register suggested_scratch = reg == EAX ? EBX : EAX;
5758 ScratchRegisterScope ensure_scratch(
5759 this, reg, suggested_scratch, codegen_->GetNumberOfCoreRegisters());
5760
5761 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5762 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset));
5763 __ movl(Address(ESP, mem + stack_offset), reg);
5764 __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005765}
5766
Mark Mendell7c8d0092015-01-26 11:21:33 -05005767void ParallelMoveResolverX86::Exchange32(XmmRegister reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005768 ScratchRegisterScope ensure_scratch(
5769 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5770
5771 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5772 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5773 __ movl(temp_reg, Address(ESP, mem + stack_offset));
5774 __ movss(Address(ESP, mem + stack_offset), reg);
5775 __ movd(reg, temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005776}
5777
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005778void ParallelMoveResolverX86::Exchange(int mem1, int mem2) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005779 ScratchRegisterScope ensure_scratch1(
5780 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005781
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005782 Register suggested_scratch = ensure_scratch1.GetRegister() == EAX ? EBX : EAX;
5783 ScratchRegisterScope ensure_scratch2(
5784 this, ensure_scratch1.GetRegister(), suggested_scratch, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005785
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005786 int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0;
5787 stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0;
5788 __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset));
5789 __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset));
5790 __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister()));
5791 __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005792}
5793
5794void ParallelMoveResolverX86::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005795 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005796 Location source = move->GetSource();
5797 Location destination = move->GetDestination();
5798
5799 if (source.IsRegister() && destination.IsRegister()) {
Mark Mendell90979812015-07-28 16:41:21 -04005800 // Use XOR swap algorithm to avoid serializing XCHG instruction or using a temporary.
5801 DCHECK_NE(destination.AsRegister<Register>(), source.AsRegister<Register>());
5802 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
5803 __ xorl(source.AsRegister<Register>(), destination.AsRegister<Register>());
5804 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005805 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005806 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005807 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005808 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005809 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
5810 Exchange(destination.GetStackIndex(), source.GetStackIndex());
Mark Mendell7c8d0092015-01-26 11:21:33 -05005811 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
5812 // Use XOR Swap algorithm to avoid a temporary.
5813 DCHECK_NE(source.reg(), destination.reg());
5814 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
5815 __ xorpd(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
5816 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
5817 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
5818 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
5819 } else if (destination.IsFpuRegister() && source.IsStackSlot()) {
5820 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005821 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
5822 // Take advantage of the 16 bytes in the XMM register.
5823 XmmRegister reg = source.AsFpuRegister<XmmRegister>();
5824 Address stack(ESP, destination.GetStackIndex());
5825 // Load the double into the high doubleword.
5826 __ movhpd(reg, stack);
5827
5828 // Store the low double into the destination.
5829 __ movsd(stack, reg);
5830
5831 // Move the high double to the low double.
5832 __ psrldq(reg, Immediate(8));
5833 } else if (destination.IsFpuRegister() && source.IsDoubleStackSlot()) {
5834 // Take advantage of the 16 bytes in the XMM register.
5835 XmmRegister reg = destination.AsFpuRegister<XmmRegister>();
5836 Address stack(ESP, source.GetStackIndex());
5837 // Load the double into the high doubleword.
5838 __ movhpd(reg, stack);
5839
5840 // Store the low double into the destination.
5841 __ movsd(stack, reg);
5842
5843 // Move the high double to the low double.
5844 __ psrldq(reg, Immediate(8));
5845 } else if (destination.IsDoubleStackSlot() && source.IsDoubleStackSlot()) {
5846 Exchange(destination.GetStackIndex(), source.GetStackIndex());
5847 Exchange(destination.GetHighStackIndex(kX86WordSize), source.GetHighStackIndex(kX86WordSize));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005848 } else {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005849 LOG(FATAL) << "Unimplemented: source: " << source << ", destination: " << destination;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005850 }
5851}
5852
5853void ParallelMoveResolverX86::SpillScratch(int reg) {
5854 __ pushl(static_cast<Register>(reg));
5855}
5856
5857void ParallelMoveResolverX86::RestoreScratch(int reg) {
5858 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005859}
5860
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005861HLoadClass::LoadKind CodeGeneratorX86::GetSupportedLoadClassKind(
5862 HLoadClass::LoadKind desired_class_load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005863 switch (desired_class_load_kind) {
5864 case HLoadClass::LoadKind::kReferrersClass:
5865 break;
5866 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
5867 DCHECK(!GetCompilerOptions().GetCompilePic());
5868 break;
5869 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
5870 DCHECK(GetCompilerOptions().GetCompilePic());
5871 FALLTHROUGH_INTENDED;
5872 case HLoadClass::LoadKind::kDexCachePcRelative:
5873 DCHECK(!Runtime::Current()->UseJitCompilation()); // Note: boot image is also non-JIT.
5874 // We disable pc-relative load when there is an irreducible loop, as the optimization
5875 // is incompatible with it.
5876 // TODO: Create as many X86ComputeBaseMethodAddress instructions as needed for methods
5877 // with irreducible loops.
5878 if (GetGraph()->HasIrreducibleLoops()) {
5879 return HLoadClass::LoadKind::kDexCacheViaMethod;
5880 }
5881 break;
5882 case HLoadClass::LoadKind::kBootImageAddress:
5883 break;
5884 case HLoadClass::LoadKind::kDexCacheAddress:
5885 DCHECK(Runtime::Current()->UseJitCompilation());
5886 break;
5887 case HLoadClass::LoadKind::kDexCacheViaMethod:
5888 break;
5889 }
5890 return desired_class_load_kind;
5891}
5892
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005893void LocationsBuilderX86::VisitLoadClass(HLoadClass* cls) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005894 if (cls->NeedsAccessCheck()) {
5895 InvokeRuntimeCallingConvention calling_convention;
5896 CodeGenerator::CreateLoadClassLocationSummary(
5897 cls,
5898 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
5899 Location::RegisterLocation(EAX),
5900 /* code_generator_supports_read_barrier */ true);
5901 return;
5902 }
5903
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005904 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
5905 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005906 ? LocationSummary::kCallOnSlowPath
5907 : LocationSummary::kNoCall;
5908 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005909 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Vladimir Marko70e97462016-08-09 11:04:26 +01005910 locations->SetCustomSlowPathCallerSaves(RegisterSet()); // No caller-save registers.
5911 }
5912
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005913 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
5914 if (load_kind == HLoadClass::LoadKind::kReferrersClass ||
5915 load_kind == HLoadClass::LoadKind::kDexCacheViaMethod ||
5916 load_kind == HLoadClass::LoadKind::kBootImageLinkTimePcRelative ||
5917 load_kind == HLoadClass::LoadKind::kDexCachePcRelative) {
5918 locations->SetInAt(0, Location::RequiresRegister());
5919 }
5920 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005921}
5922
5923void InstructionCodeGeneratorX86::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005924 LocationSummary* locations = cls->GetLocations();
Calin Juravle98893e12015-10-02 21:05:03 +01005925 if (cls->NeedsAccessCheck()) {
5926 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex());
Serban Constantinescuba45db02016-07-12 22:53:02 +01005927 codegen_->InvokeRuntime(kQuickInitializeTypeAndVerifyAccess, cls, cls->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00005928 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Calin Juravle580b6092015-10-06 17:35:58 +01005929 return;
5930 }
5931
Roland Levillain0d5a2812015-11-13 10:07:31 +00005932 Location out_loc = locations->Out();
5933 Register out = out_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005934
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005935 bool generate_null_check = false;
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005936 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005937 switch (cls->GetLoadKind()) {
5938 case HLoadClass::LoadKind::kReferrersClass: {
5939 DCHECK(!cls->CanCallRuntime());
5940 DCHECK(!cls->MustGenerateClinitCheck());
5941 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5942 Register current_method = locations->InAt(0).AsRegister<Register>();
5943 GenerateGcRootFieldLoad(
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005944 cls,
5945 out_loc,
5946 Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()),
5947 /*fixup_label*/ nullptr,
5948 requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005949 break;
5950 }
5951 case HLoadClass::LoadKind::kBootImageLinkTimeAddress: {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005952 DCHECK(!requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005953 __ movl(out, Immediate(/* placeholder */ 0));
5954 codegen_->RecordTypePatch(cls);
5955 break;
5956 }
5957 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005958 DCHECK(!requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005959 Register method_address = locations->InAt(0).AsRegister<Register>();
5960 __ leal(out, Address(method_address, CodeGeneratorX86::kDummy32BitOffset));
5961 codegen_->RecordTypePatch(cls);
5962 break;
5963 }
5964 case HLoadClass::LoadKind::kBootImageAddress: {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005965 DCHECK(!requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005966 DCHECK_NE(cls->GetAddress(), 0u);
5967 uint32_t address = dchecked_integral_cast<uint32_t>(cls->GetAddress());
5968 __ movl(out, Immediate(address));
5969 codegen_->RecordSimplePatch();
5970 break;
5971 }
5972 case HLoadClass::LoadKind::kDexCacheAddress: {
5973 DCHECK_NE(cls->GetAddress(), 0u);
5974 uint32_t address = dchecked_integral_cast<uint32_t>(cls->GetAddress());
5975 // /* GcRoot<mirror::Class> */ out = *address
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005976 GenerateGcRootFieldLoad(cls,
5977 out_loc,
5978 Address::Absolute(address),
5979 /*fixup_label*/ nullptr,
5980 requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005981 generate_null_check = !cls->IsInDexCache();
5982 break;
5983 }
5984 case HLoadClass::LoadKind::kDexCachePcRelative: {
5985 Register base_reg = locations->InAt(0).AsRegister<Register>();
5986 uint32_t offset = cls->GetDexCacheElementOffset();
5987 Label* fixup_label = codegen_->NewPcRelativeDexCacheArrayPatch(cls->GetDexFile(), offset);
5988 // /* GcRoot<mirror::Class> */ out = *(base + offset) /* PC-relative */
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005989 GenerateGcRootFieldLoad(cls,
5990 out_loc,
5991 Address(base_reg, CodeGeneratorX86::kDummy32BitOffset),
5992 fixup_label,
5993 requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005994 generate_null_check = !cls->IsInDexCache();
5995 break;
5996 }
5997 case HLoadClass::LoadKind::kDexCacheViaMethod: {
5998 // /* GcRoot<mirror::Class>[] */ out =
5999 // current_method.ptr_sized_fields_->dex_cache_resolved_types_
6000 Register current_method = locations->InAt(0).AsRegister<Register>();
6001 __ movl(out, Address(current_method,
6002 ArtMethod::DexCacheResolvedTypesOffset(kX86PointerSize).Int32Value()));
6003 // /* GcRoot<mirror::Class> */ out = out[type_index]
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006004 GenerateGcRootFieldLoad(cls,
6005 out_loc,
6006 Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())),
6007 /*fixup_label*/ nullptr,
6008 requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006009 generate_null_check = !cls->IsInDexCache();
6010 break;
6011 }
6012 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006013
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006014 if (generate_null_check || cls->MustGenerateClinitCheck()) {
6015 DCHECK(cls->CanCallRuntime());
6016 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
6017 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
6018 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006019
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006020 if (generate_null_check) {
6021 __ testl(out, out);
6022 __ j(kEqual, slow_path->GetEntryLabel());
6023 }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006024
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006025 if (cls->MustGenerateClinitCheck()) {
6026 GenerateClassInitializationCheck(slow_path, out);
6027 } else {
6028 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006029 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006030 }
6031}
6032
6033void LocationsBuilderX86::VisitClinitCheck(HClinitCheck* check) {
6034 LocationSummary* locations =
6035 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
6036 locations->SetInAt(0, Location::RequiresRegister());
6037 if (check->HasUses()) {
6038 locations->SetOut(Location::SameAsFirstInput());
6039 }
6040}
6041
6042void InstructionCodeGeneratorX86::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006043 // We assume the class to not be null.
Andreas Gampe85b62f22015-09-09 13:15:38 -07006044 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006045 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006046 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00006047 GenerateClassInitializationCheck(slow_path,
6048 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006049}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006050
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006051void InstructionCodeGeneratorX86::GenerateClassInitializationCheck(
Andreas Gampe85b62f22015-09-09 13:15:38 -07006052 SlowPathCode* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006053 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
6054 Immediate(mirror::Class::kStatusInitialized));
6055 __ j(kLess, slow_path->GetEntryLabel());
6056 __ Bind(slow_path->GetExitLabel());
6057 // No need for memory fence, thanks to the X86 memory model.
6058}
6059
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006060HLoadString::LoadKind CodeGeneratorX86::GetSupportedLoadStringKind(
6061 HLoadString::LoadKind desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006062 switch (desired_string_load_kind) {
6063 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
6064 DCHECK(!GetCompilerOptions().GetCompilePic());
6065 break;
6066 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
6067 DCHECK(GetCompilerOptions().GetCompilePic());
6068 FALLTHROUGH_INTENDED;
6069 case HLoadString::LoadKind::kDexCachePcRelative:
Calin Juravleffc87072016-04-20 14:22:09 +01006070 DCHECK(!Runtime::Current()->UseJitCompilation()); // Note: boot image is also non-JIT.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006071 // We disable pc-relative load when there is an irreducible loop, as the optimization
6072 // is incompatible with it.
6073 // TODO: Create as many X86ComputeBaseMethodAddress instructions as needed for methods
6074 // with irreducible loops.
6075 if (GetGraph()->HasIrreducibleLoops()) {
6076 return HLoadString::LoadKind::kDexCacheViaMethod;
6077 }
6078 break;
6079 case HLoadString::LoadKind::kBootImageAddress:
6080 break;
6081 case HLoadString::LoadKind::kDexCacheAddress:
Calin Juravleffc87072016-04-20 14:22:09 +01006082 DCHECK(Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006083 break;
6084 case HLoadString::LoadKind::kDexCacheViaMethod:
6085 break;
6086 }
6087 return desired_string_load_kind;
6088}
6089
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006090void LocationsBuilderX86::VisitLoadString(HLoadString* load) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006091 LocationSummary::CallKind call_kind = (load->NeedsEnvironment() || kEmitCompilerReadBarrier)
Christina Wadsworth175d09b2016-08-31 16:26:01 -07006092 ? LocationSummary::kCallOnMainOnly
Nicolas Geoffray917d0162015-11-24 18:25:35 +00006093 : LocationSummary::kNoCall;
6094 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006095 HLoadString::LoadKind load_kind = load->GetLoadKind();
6096 if (load_kind == HLoadString::LoadKind::kDexCacheViaMethod ||
6097 load_kind == HLoadString::LoadKind::kBootImageLinkTimePcRelative ||
6098 load_kind == HLoadString::LoadKind::kDexCachePcRelative) {
6099 locations->SetInAt(0, Location::RequiresRegister());
6100 }
Christina Wadsworth175d09b2016-08-31 16:26:01 -07006101 if (load_kind == HLoadString::LoadKind::kDexCacheViaMethod) {
6102 locations->SetOut(Location::RegisterLocation(EAX));
6103 } else {
6104 locations->SetOut(Location::RequiresRegister());
6105 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006106}
6107
6108void InstructionCodeGeneratorX86::VisitLoadString(HLoadString* load) {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01006109 LocationSummary* locations = load->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006110 Location out_loc = locations->Out();
6111 Register out = out_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006112
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006113 switch (load->GetLoadKind()) {
6114 case HLoadString::LoadKind::kBootImageLinkTimeAddress: {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006115 __ movl(out, Immediate(/* placeholder */ 0));
6116 codegen_->RecordStringPatch(load);
6117 return; // No dex cache slow path.
6118 }
6119 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006120 Register method_address = locations->InAt(0).AsRegister<Register>();
6121 __ leal(out, Address(method_address, CodeGeneratorX86::kDummy32BitOffset));
6122 codegen_->RecordStringPatch(load);
6123 return; // No dex cache slow path.
6124 }
6125 case HLoadString::LoadKind::kBootImageAddress: {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006126 DCHECK_NE(load->GetAddress(), 0u);
6127 uint32_t address = dchecked_integral_cast<uint32_t>(load->GetAddress());
6128 __ movl(out, Immediate(address));
6129 codegen_->RecordSimplePatch();
6130 return; // No dex cache slow path.
6131 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006132 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07006133 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006134 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006135
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07006136 // TODO: Re-add the compiler code to do string dex cache lookup again.
Christina Wadsworth175d09b2016-08-31 16:26:01 -07006137 InvokeRuntimeCallingConvention calling_convention;
6138 __ movl(calling_convention.GetRegisterAt(0), Immediate(load->GetStringIndex()));
6139 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
6140 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006141}
6142
David Brazdilcb1c0552015-08-04 16:22:25 +01006143static Address GetExceptionTlsAddress() {
Andreas Gampe542451c2016-07-26 09:02:02 -07006144 return Address::Absolute(Thread::ExceptionOffset<kX86PointerSize>().Int32Value());
David Brazdilcb1c0552015-08-04 16:22:25 +01006145}
6146
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006147void LocationsBuilderX86::VisitLoadException(HLoadException* load) {
6148 LocationSummary* locations =
6149 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
6150 locations->SetOut(Location::RequiresRegister());
6151}
6152
6153void InstructionCodeGeneratorX86::VisitLoadException(HLoadException* load) {
David Brazdilcb1c0552015-08-04 16:22:25 +01006154 __ fs()->movl(load->GetLocations()->Out().AsRegister<Register>(), GetExceptionTlsAddress());
6155}
6156
6157void LocationsBuilderX86::VisitClearException(HClearException* clear) {
6158 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
6159}
6160
6161void InstructionCodeGeneratorX86::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
6162 __ fs()->movl(GetExceptionTlsAddress(), Immediate(0));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006163}
6164
6165void LocationsBuilderX86::VisitThrow(HThrow* instruction) {
6166 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006167 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006168 InvokeRuntimeCallingConvention calling_convention;
6169 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6170}
6171
6172void InstructionCodeGeneratorX86::VisitThrow(HThrow* instruction) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01006173 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006174 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006175}
6176
Roland Levillain7c1559a2015-12-15 10:55:36 +00006177static bool TypeCheckNeedsATemporary(TypeCheckKind type_check_kind) {
6178 return kEmitCompilerReadBarrier &&
Vladimir Marko953437b2016-08-24 08:30:46 +00006179 !kUseBakerReadBarrier &&
6180 (type_check_kind == TypeCheckKind::kAbstractClassCheck ||
Roland Levillain7c1559a2015-12-15 10:55:36 +00006181 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
6182 type_check_kind == TypeCheckKind::kArrayObjectCheck);
6183}
6184
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006185void LocationsBuilderX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006186 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain0d5a2812015-11-13 10:07:31 +00006187 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01006188 bool baker_read_barrier_slow_path = false;
Roland Levillain0d5a2812015-11-13 10:07:31 +00006189 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006190 case TypeCheckKind::kExactCheck:
6191 case TypeCheckKind::kAbstractClassCheck:
6192 case TypeCheckKind::kClassHierarchyCheck:
6193 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006194 call_kind =
6195 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Vladimir Marko70e97462016-08-09 11:04:26 +01006196 baker_read_barrier_slow_path = kUseBakerReadBarrier;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006197 break;
6198 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006199 case TypeCheckKind::kUnresolvedCheck:
6200 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006201 call_kind = LocationSummary::kCallOnSlowPath;
6202 break;
6203 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006204
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006205 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01006206 if (baker_read_barrier_slow_path) {
6207 locations->SetCustomSlowPathCallerSaves(RegisterSet()); // No caller-save registers.
6208 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006209 locations->SetInAt(0, Location::RequiresRegister());
6210 locations->SetInAt(1, Location::Any());
6211 // Note that TypeCheckSlowPathX86 uses this "out" register too.
6212 locations->SetOut(Location::RequiresRegister());
6213 // When read barriers are enabled, we need a temporary register for
6214 // some cases.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006215 if (TypeCheckNeedsATemporary(type_check_kind)) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006216 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006217 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006218}
6219
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006220void InstructionCodeGeneratorX86::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006221 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006222 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006223 Location obj_loc = locations->InAt(0);
6224 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006225 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006226 Location out_loc = locations->Out();
6227 Register out = out_loc.AsRegister<Register>();
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006228 Location maybe_temp_loc = TypeCheckNeedsATemporary(type_check_kind) ?
Roland Levillain7c1559a2015-12-15 10:55:36 +00006229 locations->GetTemp(0) :
6230 Location::NoLocation();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006231 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006232 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6233 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6234 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Andreas Gampe85b62f22015-09-09 13:15:38 -07006235 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006236 NearLabel done, zero;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006237
6238 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006239 // Avoid null check if we know obj is not null.
6240 if (instruction->MustDoNullCheck()) {
6241 __ testl(obj, obj);
6242 __ j(kEqual, &zero);
6243 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006244
Roland Levillain0d5a2812015-11-13 10:07:31 +00006245 // /* HeapReference<Class> */ out = obj->klass_
Vladimir Marko953437b2016-08-24 08:30:46 +00006246 GenerateReferenceLoadTwoRegisters(instruction, out_loc, obj_loc, class_offset);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006247
Roland Levillain7c1559a2015-12-15 10:55:36 +00006248 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006249 case TypeCheckKind::kExactCheck: {
6250 if (cls.IsRegister()) {
6251 __ cmpl(out, cls.AsRegister<Register>());
6252 } else {
6253 DCHECK(cls.IsStackSlot()) << cls;
6254 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6255 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006256
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006257 // Classes must be equal for the instanceof to succeed.
6258 __ j(kNotEqual, &zero);
6259 __ movl(out, Immediate(1));
6260 __ jmp(&done);
6261 break;
6262 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006263
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006264 case TypeCheckKind::kAbstractClassCheck: {
6265 // If the class is abstract, we eagerly fetch the super class of the
6266 // object to avoid doing a comparison we know will fail.
6267 NearLabel loop;
6268 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006269 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006270 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006271 __ testl(out, out);
6272 // If `out` is null, we use it for the result, and jump to `done`.
6273 __ j(kEqual, &done);
6274 if (cls.IsRegister()) {
6275 __ cmpl(out, cls.AsRegister<Register>());
6276 } else {
6277 DCHECK(cls.IsStackSlot()) << cls;
6278 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6279 }
6280 __ j(kNotEqual, &loop);
6281 __ movl(out, Immediate(1));
6282 if (zero.IsLinked()) {
6283 __ jmp(&done);
6284 }
6285 break;
6286 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006287
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006288 case TypeCheckKind::kClassHierarchyCheck: {
6289 // Walk over the class hierarchy to find a match.
6290 NearLabel loop, success;
6291 __ Bind(&loop);
6292 if (cls.IsRegister()) {
6293 __ cmpl(out, cls.AsRegister<Register>());
6294 } else {
6295 DCHECK(cls.IsStackSlot()) << cls;
6296 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6297 }
6298 __ j(kEqual, &success);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006299 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006300 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006301 __ testl(out, out);
6302 __ j(kNotEqual, &loop);
6303 // If `out` is null, we use it for the result, and jump to `done`.
6304 __ jmp(&done);
6305 __ Bind(&success);
6306 __ movl(out, Immediate(1));
6307 if (zero.IsLinked()) {
6308 __ jmp(&done);
6309 }
6310 break;
6311 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006312
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006313 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006314 // Do an exact check.
6315 NearLabel exact_check;
6316 if (cls.IsRegister()) {
6317 __ cmpl(out, cls.AsRegister<Register>());
6318 } else {
6319 DCHECK(cls.IsStackSlot()) << cls;
6320 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6321 }
6322 __ j(kEqual, &exact_check);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006323 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006324 // /* HeapReference<Class> */ out = out->component_type_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006325 GenerateReferenceLoadOneRegister(instruction, out_loc, component_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006326 __ testl(out, out);
6327 // If `out` is null, we use it for the result, and jump to `done`.
6328 __ j(kEqual, &done);
6329 __ cmpw(Address(out, primitive_offset), Immediate(Primitive::kPrimNot));
6330 __ j(kNotEqual, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006331 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006332 __ movl(out, Immediate(1));
6333 __ jmp(&done);
6334 break;
6335 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006336
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006337 case TypeCheckKind::kArrayCheck: {
6338 if (cls.IsRegister()) {
6339 __ cmpl(out, cls.AsRegister<Register>());
6340 } else {
6341 DCHECK(cls.IsStackSlot()) << cls;
6342 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6343 }
6344 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain0d5a2812015-11-13 10:07:31 +00006345 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
6346 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006347 codegen_->AddSlowPath(slow_path);
6348 __ j(kNotEqual, slow_path->GetEntryLabel());
6349 __ movl(out, Immediate(1));
6350 if (zero.IsLinked()) {
6351 __ jmp(&done);
6352 }
6353 break;
6354 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006355
Calin Juravle98893e12015-10-02 21:05:03 +01006356 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006357 case TypeCheckKind::kInterfaceCheck: {
6358 // Note that we indeed only call on slow path, but we always go
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006359 // into the slow path for the unresolved and interface check
Roland Levillain0d5a2812015-11-13 10:07:31 +00006360 // cases.
6361 //
6362 // We cannot directly call the InstanceofNonTrivial runtime
6363 // entry point without resorting to a type checking slow path
6364 // here (i.e. by calling InvokeRuntime directly), as it would
6365 // require to assign fixed registers for the inputs of this
6366 // HInstanceOf instruction (following the runtime calling
6367 // convention), which might be cluttered by the potential first
6368 // read barrier emission at the beginning of this method.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006369 //
6370 // TODO: Introduce a new runtime entry point taking the object
6371 // to test (instead of its class) as argument, and let it deal
6372 // with the read barrier issues. This will let us refactor this
6373 // case of the `switch` code as it was previously (with a direct
6374 // call to the runtime not using a type checking slow path).
6375 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006376 DCHECK(locations->OnlyCallsOnSlowPath());
6377 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
6378 /* is_fatal */ false);
6379 codegen_->AddSlowPath(slow_path);
6380 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006381 if (zero.IsLinked()) {
6382 __ jmp(&done);
6383 }
6384 break;
6385 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006386 }
6387
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006388 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006389 __ Bind(&zero);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006390 __ xorl(out, out);
6391 }
6392
6393 if (done.IsLinked()) {
6394 __ Bind(&done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006395 }
6396
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006397 if (slow_path != nullptr) {
6398 __ Bind(slow_path->GetExitLabel());
6399 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006400}
6401
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006402void LocationsBuilderX86::VisitCheckCast(HCheckCast* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006403 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
6404 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006405 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
6406 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006407 case TypeCheckKind::kExactCheck:
6408 case TypeCheckKind::kAbstractClassCheck:
6409 case TypeCheckKind::kClassHierarchyCheck:
6410 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006411 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
6412 LocationSummary::kCallOnSlowPath :
6413 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006414 break;
6415 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006416 case TypeCheckKind::kUnresolvedCheck:
6417 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006418 call_kind = LocationSummary::kCallOnSlowPath;
6419 break;
6420 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006421 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
6422 locations->SetInAt(0, Location::RequiresRegister());
6423 locations->SetInAt(1, Location::Any());
6424 // Note that TypeCheckSlowPathX86 uses this "temp" register too.
6425 locations->AddTemp(Location::RequiresRegister());
6426 // When read barriers are enabled, we need an additional temporary
6427 // register for some cases.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006428 if (TypeCheckNeedsATemporary(type_check_kind)) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006429 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006430 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006431}
6432
6433void InstructionCodeGeneratorX86::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006434 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006435 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006436 Location obj_loc = locations->InAt(0);
6437 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006438 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006439 Location temp_loc = locations->GetTemp(0);
6440 Register temp = temp_loc.AsRegister<Register>();
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006441 Location maybe_temp2_loc = TypeCheckNeedsATemporary(type_check_kind) ?
Roland Levillain7c1559a2015-12-15 10:55:36 +00006442 locations->GetTemp(1) :
6443 Location::NoLocation();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006444 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
6445 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6446 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6447 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006448
Roland Levillain0d5a2812015-11-13 10:07:31 +00006449 bool is_type_check_slow_path_fatal =
6450 (type_check_kind == TypeCheckKind::kExactCheck ||
6451 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
6452 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
6453 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
6454 !instruction->CanThrowIntoCatchBlock();
6455 SlowPathCode* type_check_slow_path =
6456 new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
6457 is_type_check_slow_path_fatal);
6458 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006459
Roland Levillain0d5a2812015-11-13 10:07:31 +00006460 NearLabel done;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006461 // Avoid null check if we know obj is not null.
6462 if (instruction->MustDoNullCheck()) {
6463 __ testl(obj, obj);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006464 __ j(kEqual, &done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006465 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006466
Roland Levillain0d5a2812015-11-13 10:07:31 +00006467 // /* HeapReference<Class> */ temp = obj->klass_
Vladimir Marko953437b2016-08-24 08:30:46 +00006468 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006469
Roland Levillain0d5a2812015-11-13 10:07:31 +00006470 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006471 case TypeCheckKind::kExactCheck:
6472 case TypeCheckKind::kArrayCheck: {
6473 if (cls.IsRegister()) {
6474 __ cmpl(temp, cls.AsRegister<Register>());
6475 } else {
6476 DCHECK(cls.IsStackSlot()) << cls;
6477 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6478 }
6479 // Jump to slow path for throwing the exception or doing a
6480 // more involved array check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006481 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006482 break;
6483 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006484
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006485 case TypeCheckKind::kAbstractClassCheck: {
6486 // If the class is abstract, we eagerly fetch the super class of the
6487 // object to avoid doing a comparison we know will fail.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006488 NearLabel loop, compare_classes;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006489 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006490 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006491 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006492
6493 // If the class reference currently in `temp` is not null, jump
6494 // to the `compare_classes` label to compare it with the checked
6495 // class.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006496 __ testl(temp, temp);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006497 __ j(kNotEqual, &compare_classes);
6498 // Otherwise, jump to the slow path to throw the exception.
6499 //
6500 // But before, move back the object's class into `temp` before
6501 // going into the slow path, as it has been overwritten in the
6502 // meantime.
6503 // /* HeapReference<Class> */ temp = obj->klass_
Vladimir Marko953437b2016-08-24 08:30:46 +00006504 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006505 __ jmp(type_check_slow_path->GetEntryLabel());
6506
6507 __ Bind(&compare_classes);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006508 if (cls.IsRegister()) {
6509 __ cmpl(temp, cls.AsRegister<Register>());
6510 } else {
6511 DCHECK(cls.IsStackSlot()) << cls;
6512 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6513 }
6514 __ j(kNotEqual, &loop);
6515 break;
6516 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006517
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006518 case TypeCheckKind::kClassHierarchyCheck: {
6519 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006520 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006521 __ Bind(&loop);
6522 if (cls.IsRegister()) {
6523 __ cmpl(temp, cls.AsRegister<Register>());
6524 } else {
6525 DCHECK(cls.IsStackSlot()) << cls;
6526 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6527 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006528 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006529
Roland Levillain0d5a2812015-11-13 10:07:31 +00006530 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006531 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006532
6533 // If the class reference currently in `temp` is not null, jump
6534 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006535 __ testl(temp, temp);
6536 __ j(kNotEqual, &loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006537 // Otherwise, jump to the slow path to throw the exception.
6538 //
6539 // But before, move back the object's class into `temp` before
6540 // going into the slow path, as it has been overwritten in the
6541 // meantime.
6542 // /* HeapReference<Class> */ temp = obj->klass_
Vladimir Marko953437b2016-08-24 08:30:46 +00006543 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006544 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006545 break;
6546 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006547
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006548 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006549 // Do an exact check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006550 NearLabel check_non_primitive_component_type;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006551 if (cls.IsRegister()) {
6552 __ cmpl(temp, cls.AsRegister<Register>());
6553 } else {
6554 DCHECK(cls.IsStackSlot()) << cls;
6555 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6556 }
6557 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006558
6559 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006560 // /* HeapReference<Class> */ temp = temp->component_type_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006561 GenerateReferenceLoadOneRegister(instruction, temp_loc, component_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006562
6563 // If the component type is not null (i.e. the object is indeed
6564 // an array), jump to label `check_non_primitive_component_type`
6565 // to further check that this component type is not a primitive
6566 // type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006567 __ testl(temp, temp);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006568 __ j(kNotEqual, &check_non_primitive_component_type);
6569 // Otherwise, jump to the slow path to throw the exception.
6570 //
6571 // But before, move back the object's class into `temp` before
6572 // going into the slow path, as it has been overwritten in the
6573 // meantime.
6574 // /* HeapReference<Class> */ temp = obj->klass_
Vladimir Marko953437b2016-08-24 08:30:46 +00006575 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006576 __ jmp(type_check_slow_path->GetEntryLabel());
6577
6578 __ Bind(&check_non_primitive_component_type);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006579 __ cmpw(Address(temp, primitive_offset), Immediate(Primitive::kPrimNot));
Roland Levillain0d5a2812015-11-13 10:07:31 +00006580 __ j(kEqual, &done);
6581 // Same comment as above regarding `temp` and the slow path.
6582 // /* HeapReference<Class> */ temp = obj->klass_
Vladimir Marko953437b2016-08-24 08:30:46 +00006583 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006584 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006585 break;
6586 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006587
Calin Juravle98893e12015-10-02 21:05:03 +01006588 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006589 case TypeCheckKind::kInterfaceCheck:
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006590 // We always go into the type check slow path for the unresolved
6591 // and interface check cases.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006592 //
6593 // We cannot directly call the CheckCast runtime entry point
6594 // without resorting to a type checking slow path here (i.e. by
6595 // calling InvokeRuntime directly), as it would require to
6596 // assign fixed registers for the inputs of this HInstanceOf
6597 // instruction (following the runtime calling convention), which
6598 // might be cluttered by the potential first read barrier
6599 // emission at the beginning of this method.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006600 //
6601 // TODO: Introduce a new runtime entry point taking the object
6602 // to test (instead of its class) as argument, and let it deal
6603 // with the read barrier issues. This will let us refactor this
6604 // case of the `switch` code as it was previously (with a direct
6605 // call to the runtime not using a type checking slow path).
6606 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006607 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006608 break;
6609 }
6610 __ Bind(&done);
6611
Roland Levillain0d5a2812015-11-13 10:07:31 +00006612 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006613}
6614
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006615void LocationsBuilderX86::VisitMonitorOperation(HMonitorOperation* instruction) {
6616 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006617 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006618 InvokeRuntimeCallingConvention calling_convention;
6619 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6620}
6621
6622void InstructionCodeGeneratorX86::VisitMonitorOperation(HMonitorOperation* instruction) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01006623 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject
6624 : kQuickUnlockObject,
Alexandre Rames8158f282015-08-07 10:26:17 +01006625 instruction,
Serban Constantinescuba45db02016-07-12 22:53:02 +01006626 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006627 if (instruction->IsEnter()) {
6628 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
6629 } else {
6630 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
6631 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006632}
6633
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006634void LocationsBuilderX86::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
6635void LocationsBuilderX86::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
6636void LocationsBuilderX86::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
6637
6638void LocationsBuilderX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
6639 LocationSummary* locations =
6640 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6641 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
6642 || instruction->GetResultType() == Primitive::kPrimLong);
6643 locations->SetInAt(0, Location::RequiresRegister());
6644 locations->SetInAt(1, Location::Any());
6645 locations->SetOut(Location::SameAsFirstInput());
6646}
6647
6648void InstructionCodeGeneratorX86::VisitAnd(HAnd* instruction) {
6649 HandleBitwiseOperation(instruction);
6650}
6651
6652void InstructionCodeGeneratorX86::VisitOr(HOr* instruction) {
6653 HandleBitwiseOperation(instruction);
6654}
6655
6656void InstructionCodeGeneratorX86::VisitXor(HXor* instruction) {
6657 HandleBitwiseOperation(instruction);
6658}
6659
6660void InstructionCodeGeneratorX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
6661 LocationSummary* locations = instruction->GetLocations();
6662 Location first = locations->InAt(0);
6663 Location second = locations->InAt(1);
6664 DCHECK(first.Equals(locations->Out()));
6665
6666 if (instruction->GetResultType() == Primitive::kPrimInt) {
6667 if (second.IsRegister()) {
6668 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006669 __ andl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006670 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006671 __ orl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006672 } else {
6673 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006674 __ xorl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006675 }
6676 } else if (second.IsConstant()) {
6677 if (instruction->IsAnd()) {
Roland Levillain199f3362014-11-27 17:15:16 +00006678 __ andl(first.AsRegister<Register>(),
6679 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006680 } else if (instruction->IsOr()) {
Roland Levillain199f3362014-11-27 17:15:16 +00006681 __ orl(first.AsRegister<Register>(),
6682 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006683 } else {
6684 DCHECK(instruction->IsXor());
Roland Levillain199f3362014-11-27 17:15:16 +00006685 __ xorl(first.AsRegister<Register>(),
6686 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006687 }
6688 } else {
6689 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006690 __ andl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006691 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006692 __ orl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006693 } else {
6694 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006695 __ xorl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006696 }
6697 }
6698 } else {
6699 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
6700 if (second.IsRegisterPair()) {
6701 if (instruction->IsAnd()) {
6702 __ andl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
6703 __ andl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
6704 } else if (instruction->IsOr()) {
6705 __ orl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
6706 __ orl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
6707 } else {
6708 DCHECK(instruction->IsXor());
6709 __ xorl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
6710 __ xorl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
6711 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006712 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006713 if (instruction->IsAnd()) {
6714 __ andl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
6715 __ andl(first.AsRegisterPairHigh<Register>(),
6716 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
6717 } else if (instruction->IsOr()) {
6718 __ orl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
6719 __ orl(first.AsRegisterPairHigh<Register>(),
6720 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
6721 } else {
6722 DCHECK(instruction->IsXor());
6723 __ xorl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
6724 __ xorl(first.AsRegisterPairHigh<Register>(),
6725 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
6726 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006727 } else {
6728 DCHECK(second.IsConstant()) << second;
6729 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006730 int32_t low_value = Low32Bits(value);
6731 int32_t high_value = High32Bits(value);
6732 Immediate low(low_value);
6733 Immediate high(high_value);
6734 Register first_low = first.AsRegisterPairLow<Register>();
6735 Register first_high = first.AsRegisterPairHigh<Register>();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006736 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006737 if (low_value == 0) {
6738 __ xorl(first_low, first_low);
6739 } else if (low_value != -1) {
6740 __ andl(first_low, low);
6741 }
6742 if (high_value == 0) {
6743 __ xorl(first_high, first_high);
6744 } else if (high_value != -1) {
6745 __ andl(first_high, high);
6746 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006747 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006748 if (low_value != 0) {
6749 __ orl(first_low, low);
6750 }
6751 if (high_value != 0) {
6752 __ orl(first_high, high);
6753 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006754 } else {
6755 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006756 if (low_value != 0) {
6757 __ xorl(first_low, low);
6758 }
6759 if (high_value != 0) {
6760 __ xorl(first_high, high);
6761 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006762 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006763 }
6764 }
6765}
6766
Roland Levillain7c1559a2015-12-15 10:55:36 +00006767void InstructionCodeGeneratorX86::GenerateReferenceLoadOneRegister(HInstruction* instruction,
6768 Location out,
6769 uint32_t offset,
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006770 Location maybe_temp) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006771 Register out_reg = out.AsRegister<Register>();
6772 if (kEmitCompilerReadBarrier) {
6773 if (kUseBakerReadBarrier) {
6774 // Load with fast path based Baker's read barrier.
6775 // /* HeapReference<Object> */ out = *(out + offset)
6776 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00006777 instruction, out, out_reg, offset, /* needs_null_check */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006778 } else {
6779 // Load with slow path based read barrier.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006780 // Save the value of `out` into `maybe_temp` before overwriting it
Roland Levillain7c1559a2015-12-15 10:55:36 +00006781 // in the following move operation, as we will need it for the
6782 // read barrier below.
Vladimir Marko953437b2016-08-24 08:30:46 +00006783 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006784 __ movl(maybe_temp.AsRegister<Register>(), out_reg);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006785 // /* HeapReference<Object> */ out = *(out + offset)
6786 __ movl(out_reg, Address(out_reg, offset));
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006787 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006788 }
6789 } else {
6790 // Plain load with no read barrier.
6791 // /* HeapReference<Object> */ out = *(out + offset)
6792 __ movl(out_reg, Address(out_reg, offset));
6793 __ MaybeUnpoisonHeapReference(out_reg);
6794 }
6795}
6796
6797void InstructionCodeGeneratorX86::GenerateReferenceLoadTwoRegisters(HInstruction* instruction,
6798 Location out,
6799 Location obj,
Vladimir Marko953437b2016-08-24 08:30:46 +00006800 uint32_t offset) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006801 Register out_reg = out.AsRegister<Register>();
6802 Register obj_reg = obj.AsRegister<Register>();
6803 if (kEmitCompilerReadBarrier) {
6804 if (kUseBakerReadBarrier) {
6805 // Load with fast path based Baker's read barrier.
6806 // /* HeapReference<Object> */ out = *(obj + offset)
6807 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00006808 instruction, out, obj_reg, offset, /* needs_null_check */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006809 } else {
6810 // Load with slow path based read barrier.
6811 // /* HeapReference<Object> */ out = *(obj + offset)
6812 __ movl(out_reg, Address(obj_reg, offset));
6813 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
6814 }
6815 } else {
6816 // Plain load with no read barrier.
6817 // /* HeapReference<Object> */ out = *(obj + offset)
6818 __ movl(out_reg, Address(obj_reg, offset));
6819 __ MaybeUnpoisonHeapReference(out_reg);
6820 }
6821}
6822
6823void InstructionCodeGeneratorX86::GenerateGcRootFieldLoad(HInstruction* instruction,
6824 Location root,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006825 const Address& address,
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006826 Label* fixup_label,
6827 bool requires_read_barrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006828 Register root_reg = root.AsRegister<Register>();
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006829 if (requires_read_barrier) {
6830 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006831 if (kUseBakerReadBarrier) {
6832 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
6833 // Baker's read barrier are used:
6834 //
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006835 // root = *address;
Roland Levillain7c1559a2015-12-15 10:55:36 +00006836 // if (Thread::Current()->GetIsGcMarking()) {
6837 // root = ReadBarrier::Mark(root)
6838 // }
6839
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006840 // /* GcRoot<mirror::Object> */ root = *address
6841 __ movl(root_reg, address);
6842 if (fixup_label != nullptr) {
6843 __ Bind(fixup_label);
6844 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00006845 static_assert(
6846 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
6847 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
6848 "have different sizes.");
6849 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
6850 "art::mirror::CompressedReference<mirror::Object> and int32_t "
6851 "have different sizes.");
6852
Vladimir Marko953437b2016-08-24 08:30:46 +00006853 // Slow path marking the GC root `root`.
6854 SlowPathCode* slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathX86(
6855 instruction, root, /* unpoison */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006856 codegen_->AddSlowPath(slow_path);
6857
Andreas Gampe542451c2016-07-26 09:02:02 -07006858 __ fs()->cmpl(Address::Absolute(Thread::IsGcMarkingOffset<kX86PointerSize>().Int32Value()),
Roland Levillain7c1559a2015-12-15 10:55:36 +00006859 Immediate(0));
6860 __ j(kNotEqual, slow_path->GetEntryLabel());
6861 __ Bind(slow_path->GetExitLabel());
6862 } else {
6863 // GC root loaded through a slow path for read barriers other
6864 // than Baker's.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006865 // /* GcRoot<mirror::Object>* */ root = address
6866 __ leal(root_reg, address);
6867 if (fixup_label != nullptr) {
6868 __ Bind(fixup_label);
6869 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00006870 // /* mirror::Object* */ root = root->Read()
6871 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
6872 }
6873 } else {
6874 // Plain GC root load with no read barrier.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006875 // /* GcRoot<mirror::Object> */ root = *address
6876 __ movl(root_reg, address);
6877 if (fixup_label != nullptr) {
6878 __ Bind(fixup_label);
6879 }
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006880 // Note that GC roots are not affected by heap poisoning, thus we
6881 // do not have to unpoison `root_reg` here.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006882 }
6883}
6884
6885void CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
6886 Location ref,
6887 Register obj,
6888 uint32_t offset,
Roland Levillain7c1559a2015-12-15 10:55:36 +00006889 bool needs_null_check) {
6890 DCHECK(kEmitCompilerReadBarrier);
6891 DCHECK(kUseBakerReadBarrier);
6892
6893 // /* HeapReference<Object> */ ref = *(obj + offset)
6894 Address src(obj, offset);
Vladimir Marko953437b2016-08-24 08:30:46 +00006895 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, needs_null_check);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006896}
6897
6898void CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
6899 Location ref,
6900 Register obj,
6901 uint32_t data_offset,
6902 Location index,
Roland Levillain7c1559a2015-12-15 10:55:36 +00006903 bool needs_null_check) {
6904 DCHECK(kEmitCompilerReadBarrier);
6905 DCHECK(kUseBakerReadBarrier);
6906
Roland Levillain3d312422016-06-23 13:53:42 +01006907 static_assert(
6908 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
6909 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain7c1559a2015-12-15 10:55:36 +00006910 // /* HeapReference<Object> */ ref =
6911 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
6912 Address src = index.IsConstant() ?
6913 Address(obj, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset) :
6914 Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset);
Vladimir Marko953437b2016-08-24 08:30:46 +00006915 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, needs_null_check);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006916}
6917
6918void CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
6919 Location ref,
6920 Register obj,
6921 const Address& src,
Roland Levillain7c1559a2015-12-15 10:55:36 +00006922 bool needs_null_check) {
6923 DCHECK(kEmitCompilerReadBarrier);
6924 DCHECK(kUseBakerReadBarrier);
6925
6926 // In slow path based read barriers, the read barrier call is
6927 // inserted after the original load. However, in fast path based
6928 // Baker's read barriers, we need to perform the load of
6929 // mirror::Object::monitor_ *before* the original reference load.
6930 // This load-load ordering is required by the read barrier.
6931 // The fast path/slow path (for Baker's algorithm) should look like:
6932 //
6933 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
6934 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
6935 // HeapReference<Object> ref = *src; // Original reference load.
6936 // bool is_gray = (rb_state == ReadBarrier::gray_ptr_);
6937 // if (is_gray) {
6938 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
6939 // }
6940 //
6941 // Note: the original implementation in ReadBarrier::Barrier is
6942 // slightly more complex as:
6943 // - it implements the load-load fence using a data dependency on
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006944 // the high-bits of rb_state, which are expected to be all zeroes
6945 // (we use CodeGeneratorX86::GenerateMemoryBarrier instead here,
6946 // which is a no-op thanks to the x86 memory model);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006947 // - it performs additional checks that we do not do here for
6948 // performance reasons.
6949
6950 Register ref_reg = ref.AsRegister<Register>();
Roland Levillain7c1559a2015-12-15 10:55:36 +00006951 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
6952
Vladimir Marko953437b2016-08-24 08:30:46 +00006953 // Given the numeric representation, it's enough to check the low bit of the rb_state.
6954 static_assert(ReadBarrier::white_ptr_ == 0, "Expecting white to have value 0");
6955 static_assert(ReadBarrier::gray_ptr_ == 1, "Expecting gray to have value 1");
6956 static_assert(ReadBarrier::black_ptr_ == 2, "Expecting black to have value 2");
6957 constexpr uint32_t gray_byte_position = LockWord::kReadBarrierStateShift / kBitsPerByte;
6958 constexpr uint32_t gray_bit_position = LockWord::kReadBarrierStateShift % kBitsPerByte;
6959 constexpr int32_t test_value = static_cast<int8_t>(1 << gray_bit_position);
6960
6961 // if (rb_state == ReadBarrier::gray_ptr_)
6962 // ref = ReadBarrier::Mark(ref);
6963 // At this point, just do the "if" and make sure that flags are preserved until the branch.
6964 __ testb(Address(obj, monitor_offset + gray_byte_position), Immediate(test_value));
Roland Levillain7c1559a2015-12-15 10:55:36 +00006965 if (needs_null_check) {
6966 MaybeRecordImplicitNullCheck(instruction);
6967 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00006968
6969 // Load fence to prevent load-load reordering.
6970 // Note that this is a no-op, thanks to the x86 memory model.
6971 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
6972
6973 // The actual reference load.
6974 // /* HeapReference<Object> */ ref = *src
Vladimir Marko953437b2016-08-24 08:30:46 +00006975 __ movl(ref_reg, src); // Flags are unaffected.
6976
6977 // Note: Reference unpoisoning modifies the flags, so we need to delay it after the branch.
6978 // Slow path marking the object `ref` when it is gray.
6979 SlowPathCode* slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathX86(
6980 instruction, ref, /* unpoison */ true);
6981 AddSlowPath(slow_path);
6982
6983 // We have done the "if" of the gray bit check above, now branch based on the flags.
6984 __ j(kNotZero, slow_path->GetEntryLabel());
Roland Levillain7c1559a2015-12-15 10:55:36 +00006985
6986 // Object* ref = ref_addr->AsMirrorPtr()
6987 __ MaybeUnpoisonHeapReference(ref_reg);
6988
Roland Levillain7c1559a2015-12-15 10:55:36 +00006989 __ Bind(slow_path->GetExitLabel());
6990}
6991
6992void CodeGeneratorX86::GenerateReadBarrierSlow(HInstruction* instruction,
6993 Location out,
6994 Location ref,
6995 Location obj,
6996 uint32_t offset,
6997 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006998 DCHECK(kEmitCompilerReadBarrier);
6999
Roland Levillain7c1559a2015-12-15 10:55:36 +00007000 // Insert a slow path based read barrier *after* the reference load.
7001 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00007002 // If heap poisoning is enabled, the unpoisoning of the loaded
7003 // reference will be carried out by the runtime within the slow
7004 // path.
7005 //
7006 // Note that `ref` currently does not get unpoisoned (when heap
7007 // poisoning is enabled), which is alright as the `ref` argument is
7008 // not used by the artReadBarrierSlow entry point.
7009 //
7010 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
7011 SlowPathCode* slow_path = new (GetGraph()->GetArena())
7012 ReadBarrierForHeapReferenceSlowPathX86(instruction, out, ref, obj, offset, index);
7013 AddSlowPath(slow_path);
7014
Roland Levillain0d5a2812015-11-13 10:07:31 +00007015 __ jmp(slow_path->GetEntryLabel());
7016 __ Bind(slow_path->GetExitLabel());
7017}
7018
Roland Levillain7c1559a2015-12-15 10:55:36 +00007019void CodeGeneratorX86::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
7020 Location out,
7021 Location ref,
7022 Location obj,
7023 uint32_t offset,
7024 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007025 if (kEmitCompilerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007026 // Baker's read barriers shall be handled by the fast path
7027 // (CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier).
7028 DCHECK(!kUseBakerReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007029 // If heap poisoning is enabled, unpoisoning will be taken care of
7030 // by the runtime within the slow path.
Roland Levillain7c1559a2015-12-15 10:55:36 +00007031 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007032 } else if (kPoisonHeapReferences) {
7033 __ UnpoisonHeapReference(out.AsRegister<Register>());
7034 }
7035}
7036
Roland Levillain7c1559a2015-12-15 10:55:36 +00007037void CodeGeneratorX86::GenerateReadBarrierForRootSlow(HInstruction* instruction,
7038 Location out,
7039 Location root) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007040 DCHECK(kEmitCompilerReadBarrier);
7041
Roland Levillain7c1559a2015-12-15 10:55:36 +00007042 // Insert a slow path based read barrier *after* the GC root load.
7043 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00007044 // Note that GC roots are not affected by heap poisoning, so we do
7045 // not need to do anything special for this here.
7046 SlowPathCode* slow_path =
7047 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathX86(instruction, out, root);
7048 AddSlowPath(slow_path);
7049
Roland Levillain0d5a2812015-11-13 10:07:31 +00007050 __ jmp(slow_path->GetEntryLabel());
7051 __ Bind(slow_path->GetExitLabel());
7052}
7053
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007054void LocationsBuilderX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00007055 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00007056 LOG(FATAL) << "Unreachable";
7057}
7058
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007059void InstructionCodeGeneratorX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00007060 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00007061 LOG(FATAL) << "Unreachable";
7062}
7063
Mark Mendellfe57faa2015-09-18 09:26:15 -04007064// Simple implementation of packed switch - generate cascaded compare/jumps.
7065void LocationsBuilderX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7066 LocationSummary* locations =
7067 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
7068 locations->SetInAt(0, Location::RequiresRegister());
7069}
7070
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007071void InstructionCodeGeneratorX86::GenPackedSwitchWithCompares(Register value_reg,
7072 int32_t lower_bound,
7073 uint32_t num_entries,
7074 HBasicBlock* switch_block,
7075 HBasicBlock* default_block) {
7076 // Figure out the correct compare values and jump conditions.
7077 // Handle the first compare/branch as a special case because it might
7078 // jump to the default case.
7079 DCHECK_GT(num_entries, 2u);
7080 Condition first_condition;
7081 uint32_t index;
7082 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
7083 if (lower_bound != 0) {
7084 first_condition = kLess;
7085 __ cmpl(value_reg, Immediate(lower_bound));
7086 __ j(first_condition, codegen_->GetLabelOf(default_block));
7087 __ j(kEqual, codegen_->GetLabelOf(successors[0]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007088
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007089 index = 1;
7090 } else {
7091 // Handle all the compare/jumps below.
7092 first_condition = kBelow;
7093 index = 0;
7094 }
7095
7096 // Handle the rest of the compare/jumps.
7097 for (; index + 1 < num_entries; index += 2) {
7098 int32_t compare_to_value = lower_bound + index + 1;
7099 __ cmpl(value_reg, Immediate(compare_to_value));
7100 // Jump to successors[index] if value < case_value[index].
7101 __ j(first_condition, codegen_->GetLabelOf(successors[index]));
7102 // Jump to successors[index + 1] if value == case_value[index + 1].
7103 __ j(kEqual, codegen_->GetLabelOf(successors[index + 1]));
7104 }
7105
7106 if (index != num_entries) {
7107 // There are an odd number of entries. Handle the last one.
7108 DCHECK_EQ(index + 1, num_entries);
7109 __ cmpl(value_reg, Immediate(lower_bound + index));
7110 __ j(kEqual, codegen_->GetLabelOf(successors[index]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007111 }
7112
7113 // And the default for any other value.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007114 if (!codegen_->GoesToNextBlock(switch_block, default_block)) {
7115 __ jmp(codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007116 }
7117}
7118
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007119void InstructionCodeGeneratorX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7120 int32_t lower_bound = switch_instr->GetStartValue();
7121 uint32_t num_entries = switch_instr->GetNumEntries();
7122 LocationSummary* locations = switch_instr->GetLocations();
7123 Register value_reg = locations->InAt(0).AsRegister<Register>();
7124
7125 GenPackedSwitchWithCompares(value_reg,
7126 lower_bound,
7127 num_entries,
7128 switch_instr->GetBlock(),
7129 switch_instr->GetDefaultBlock());
7130}
7131
Mark Mendell805b3b52015-09-18 14:10:29 -04007132void LocationsBuilderX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
7133 LocationSummary* locations =
7134 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
7135 locations->SetInAt(0, Location::RequiresRegister());
7136
7137 // Constant area pointer.
7138 locations->SetInAt(1, Location::RequiresRegister());
7139
7140 // And the temporary we need.
7141 locations->AddTemp(Location::RequiresRegister());
7142}
7143
7144void InstructionCodeGeneratorX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
7145 int32_t lower_bound = switch_instr->GetStartValue();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007146 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendell805b3b52015-09-18 14:10:29 -04007147 LocationSummary* locations = switch_instr->GetLocations();
7148 Register value_reg = locations->InAt(0).AsRegister<Register>();
7149 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
7150
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007151 if (num_entries <= kPackedSwitchJumpTableThreshold) {
7152 GenPackedSwitchWithCompares(value_reg,
7153 lower_bound,
7154 num_entries,
7155 switch_instr->GetBlock(),
7156 default_block);
7157 return;
7158 }
7159
Mark Mendell805b3b52015-09-18 14:10:29 -04007160 // Optimizing has a jump area.
7161 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
7162 Register constant_area = locations->InAt(1).AsRegister<Register>();
7163
7164 // Remove the bias, if needed.
7165 if (lower_bound != 0) {
7166 __ leal(temp_reg, Address(value_reg, -lower_bound));
7167 value_reg = temp_reg;
7168 }
7169
7170 // Is the value in range?
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007171 DCHECK_GE(num_entries, 1u);
Mark Mendell805b3b52015-09-18 14:10:29 -04007172 __ cmpl(value_reg, Immediate(num_entries - 1));
7173 __ j(kAbove, codegen_->GetLabelOf(default_block));
7174
7175 // We are in the range of the table.
7176 // Load (target-constant_area) from the jump table, indexing by the value.
7177 __ movl(temp_reg, codegen_->LiteralCaseTable(switch_instr, constant_area, value_reg));
7178
7179 // Compute the actual target address by adding in constant_area.
7180 __ addl(temp_reg, constant_area);
7181
7182 // And jump.
7183 __ jmp(temp_reg);
7184}
7185
Mark Mendell0616ae02015-04-17 12:49:27 -04007186void LocationsBuilderX86::VisitX86ComputeBaseMethodAddress(
7187 HX86ComputeBaseMethodAddress* insn) {
7188 LocationSummary* locations =
7189 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
7190 locations->SetOut(Location::RequiresRegister());
7191}
7192
7193void InstructionCodeGeneratorX86::VisitX86ComputeBaseMethodAddress(
7194 HX86ComputeBaseMethodAddress* insn) {
7195 LocationSummary* locations = insn->GetLocations();
7196 Register reg = locations->Out().AsRegister<Register>();
7197
7198 // Generate call to next instruction.
7199 Label next_instruction;
7200 __ call(&next_instruction);
7201 __ Bind(&next_instruction);
7202
7203 // Remember this offset for later use with constant area.
7204 codegen_->SetMethodAddressOffset(GetAssembler()->CodeSize());
7205
7206 // Grab the return address off the stack.
7207 __ popl(reg);
7208}
7209
7210void LocationsBuilderX86::VisitX86LoadFromConstantTable(
7211 HX86LoadFromConstantTable* insn) {
7212 LocationSummary* locations =
7213 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
7214
7215 locations->SetInAt(0, Location::RequiresRegister());
7216 locations->SetInAt(1, Location::ConstantLocation(insn->GetConstant()));
7217
7218 // If we don't need to be materialized, we only need the inputs to be set.
David Brazdilb3e773e2016-01-26 11:28:37 +00007219 if (insn->IsEmittedAtUseSite()) {
Mark Mendell0616ae02015-04-17 12:49:27 -04007220 return;
7221 }
7222
7223 switch (insn->GetType()) {
7224 case Primitive::kPrimFloat:
7225 case Primitive::kPrimDouble:
7226 locations->SetOut(Location::RequiresFpuRegister());
7227 break;
7228
7229 case Primitive::kPrimInt:
7230 locations->SetOut(Location::RequiresRegister());
7231 break;
7232
7233 default:
7234 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
7235 }
7236}
7237
7238void InstructionCodeGeneratorX86::VisitX86LoadFromConstantTable(HX86LoadFromConstantTable* insn) {
David Brazdilb3e773e2016-01-26 11:28:37 +00007239 if (insn->IsEmittedAtUseSite()) {
Mark Mendell0616ae02015-04-17 12:49:27 -04007240 return;
7241 }
7242
7243 LocationSummary* locations = insn->GetLocations();
7244 Location out = locations->Out();
7245 Register const_area = locations->InAt(0).AsRegister<Register>();
7246 HConstant *value = insn->GetConstant();
7247
7248 switch (insn->GetType()) {
7249 case Primitive::kPrimFloat:
7250 __ movss(out.AsFpuRegister<XmmRegister>(),
7251 codegen_->LiteralFloatAddress(value->AsFloatConstant()->GetValue(), const_area));
7252 break;
7253
7254 case Primitive::kPrimDouble:
7255 __ movsd(out.AsFpuRegister<XmmRegister>(),
7256 codegen_->LiteralDoubleAddress(value->AsDoubleConstant()->GetValue(), const_area));
7257 break;
7258
7259 case Primitive::kPrimInt:
7260 __ movl(out.AsRegister<Register>(),
7261 codegen_->LiteralInt32Address(value->AsIntConstant()->GetValue(), const_area));
7262 break;
7263
7264 default:
7265 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
7266 }
7267}
7268
Mark Mendell0616ae02015-04-17 12:49:27 -04007269/**
7270 * Class to handle late fixup of offsets into constant area.
7271 */
Vladimir Marko5233f932015-09-29 19:01:15 +01007272class RIPFixup : public AssemblerFixup, public ArenaObject<kArenaAllocCodeGenerator> {
Mark Mendell0616ae02015-04-17 12:49:27 -04007273 public:
Mark Mendell805b3b52015-09-18 14:10:29 -04007274 RIPFixup(CodeGeneratorX86& codegen, size_t offset)
7275 : codegen_(&codegen), offset_into_constant_area_(offset) {}
7276
7277 protected:
7278 void SetOffset(size_t offset) { offset_into_constant_area_ = offset; }
7279
7280 CodeGeneratorX86* codegen_;
Mark Mendell0616ae02015-04-17 12:49:27 -04007281
7282 private:
7283 void Process(const MemoryRegion& region, int pos) OVERRIDE {
7284 // Patch the correct offset for the instruction. The place to patch is the
7285 // last 4 bytes of the instruction.
7286 // The value to patch is the distance from the offset in the constant area
7287 // from the address computed by the HX86ComputeBaseMethodAddress instruction.
Mark Mendell805b3b52015-09-18 14:10:29 -04007288 int32_t constant_offset = codegen_->ConstantAreaStart() + offset_into_constant_area_;
7289 int32_t relative_position = constant_offset - codegen_->GetMethodAddressOffset();;
Mark Mendell0616ae02015-04-17 12:49:27 -04007290
7291 // Patch in the right value.
7292 region.StoreUnaligned<int32_t>(pos - 4, relative_position);
7293 }
7294
Mark Mendell0616ae02015-04-17 12:49:27 -04007295 // Location in constant area that the fixup refers to.
Mark Mendell805b3b52015-09-18 14:10:29 -04007296 int32_t offset_into_constant_area_;
Mark Mendell0616ae02015-04-17 12:49:27 -04007297};
7298
Mark Mendell805b3b52015-09-18 14:10:29 -04007299/**
7300 * Class to handle late fixup of offsets to a jump table that will be created in the
7301 * constant area.
7302 */
7303class JumpTableRIPFixup : public RIPFixup {
7304 public:
7305 JumpTableRIPFixup(CodeGeneratorX86& codegen, HX86PackedSwitch* switch_instr)
7306 : RIPFixup(codegen, static_cast<size_t>(-1)), switch_instr_(switch_instr) {}
7307
7308 void CreateJumpTable() {
7309 X86Assembler* assembler = codegen_->GetAssembler();
7310
7311 // Ensure that the reference to the jump table has the correct offset.
7312 const int32_t offset_in_constant_table = assembler->ConstantAreaSize();
7313 SetOffset(offset_in_constant_table);
7314
7315 // The label values in the jump table are computed relative to the
7316 // instruction addressing the constant area.
7317 const int32_t relative_offset = codegen_->GetMethodAddressOffset();
7318
7319 // Populate the jump table with the correct values for the jump table.
7320 int32_t num_entries = switch_instr_->GetNumEntries();
7321 HBasicBlock* block = switch_instr_->GetBlock();
7322 const ArenaVector<HBasicBlock*>& successors = block->GetSuccessors();
7323 // The value that we want is the target offset - the position of the table.
7324 for (int32_t i = 0; i < num_entries; i++) {
7325 HBasicBlock* b = successors[i];
7326 Label* l = codegen_->GetLabelOf(b);
7327 DCHECK(l->IsBound());
7328 int32_t offset_to_block = l->Position() - relative_offset;
7329 assembler->AppendInt32(offset_to_block);
7330 }
7331 }
7332
7333 private:
7334 const HX86PackedSwitch* switch_instr_;
7335};
7336
7337void CodeGeneratorX86::Finalize(CodeAllocator* allocator) {
7338 // Generate the constant area if needed.
7339 X86Assembler* assembler = GetAssembler();
7340 if (!assembler->IsConstantAreaEmpty() || !fixups_to_jump_tables_.empty()) {
7341 // Align to 4 byte boundary to reduce cache misses, as the data is 4 and 8
7342 // byte values.
7343 assembler->Align(4, 0);
7344 constant_area_start_ = assembler->CodeSize();
7345
7346 // Populate any jump tables.
7347 for (auto jump_table : fixups_to_jump_tables_) {
7348 jump_table->CreateJumpTable();
7349 }
7350
7351 // And now add the constant area to the generated code.
7352 assembler->AddConstantArea();
7353 }
7354
7355 // And finish up.
7356 CodeGenerator::Finalize(allocator);
7357}
7358
Mark Mendell0616ae02015-04-17 12:49:27 -04007359Address CodeGeneratorX86::LiteralDoubleAddress(double v, Register reg) {
7360 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddDouble(v));
7361 return Address(reg, kDummy32BitOffset, fixup);
7362}
7363
7364Address CodeGeneratorX86::LiteralFloatAddress(float v, Register reg) {
7365 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddFloat(v));
7366 return Address(reg, kDummy32BitOffset, fixup);
7367}
7368
7369Address CodeGeneratorX86::LiteralInt32Address(int32_t v, Register reg) {
7370 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt32(v));
7371 return Address(reg, kDummy32BitOffset, fixup);
7372}
7373
7374Address CodeGeneratorX86::LiteralInt64Address(int64_t v, Register reg) {
7375 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt64(v));
7376 return Address(reg, kDummy32BitOffset, fixup);
7377}
7378
Aart Bika19616e2016-02-01 18:57:58 -08007379void CodeGeneratorX86::Load32BitValue(Register dest, int32_t value) {
7380 if (value == 0) {
7381 __ xorl(dest, dest);
7382 } else {
7383 __ movl(dest, Immediate(value));
7384 }
7385}
7386
7387void CodeGeneratorX86::Compare32BitValue(Register dest, int32_t value) {
7388 if (value == 0) {
7389 __ testl(dest, dest);
7390 } else {
7391 __ cmpl(dest, Immediate(value));
7392 }
7393}
7394
Mark Mendell805b3b52015-09-18 14:10:29 -04007395Address CodeGeneratorX86::LiteralCaseTable(HX86PackedSwitch* switch_instr,
7396 Register reg,
7397 Register value) {
7398 // Create a fixup to be used to create and address the jump table.
7399 JumpTableRIPFixup* table_fixup =
7400 new (GetGraph()->GetArena()) JumpTableRIPFixup(*this, switch_instr);
7401
7402 // We have to populate the jump tables.
7403 fixups_to_jump_tables_.push_back(table_fixup);
7404
7405 // We want a scaled address, as we are extracting the correct offset from the table.
7406 return Address(reg, value, TIMES_4, kDummy32BitOffset, table_fixup);
7407}
7408
Andreas Gampe85b62f22015-09-09 13:15:38 -07007409// TODO: target as memory.
7410void CodeGeneratorX86::MoveFromReturnRegister(Location target, Primitive::Type type) {
7411 if (!target.IsValid()) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007412 DCHECK_EQ(type, Primitive::kPrimVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07007413 return;
7414 }
7415
7416 DCHECK_NE(type, Primitive::kPrimVoid);
7417
7418 Location return_loc = InvokeDexCallingConventionVisitorX86().GetReturnLocation(type);
7419 if (target.Equals(return_loc)) {
7420 return;
7421 }
7422
7423 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
7424 // with the else branch.
7425 if (type == Primitive::kPrimLong) {
7426 HParallelMove parallel_move(GetGraph()->GetArena());
7427 parallel_move.AddMove(return_loc.ToLow(), target.ToLow(), Primitive::kPrimInt, nullptr);
7428 parallel_move.AddMove(return_loc.ToHigh(), target.ToHigh(), Primitive::kPrimInt, nullptr);
7429 GetMoveResolver()->EmitNativeCode(&parallel_move);
7430 } else {
7431 // Let the parallel move resolver take care of all of this.
7432 HParallelMove parallel_move(GetGraph()->GetArena());
7433 parallel_move.AddMove(return_loc, target, type, nullptr);
7434 GetMoveResolver()->EmitNativeCode(&parallel_move);
7435 }
7436}
7437
Roland Levillain4d027112015-07-01 15:41:14 +01007438#undef __
7439
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00007440} // namespace x86
7441} // namespace art