blob: 5953badb07047e0739c088e80b0c76ec708bd72d [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) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004953 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4954 ? LocationSummary::kCallOnSlowPath
4955 : LocationSummary::kNoCall;
4956 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
4957 Location loc = codegen_->IsImplicitNullCheckAllowed(instruction)
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004958 ? Location::RequiresRegister()
4959 : Location::Any();
4960 locations->SetInAt(0, loc);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004961 if (instruction->HasUses()) {
4962 locations->SetOut(Location::SameAsFirstInput());
4963 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004964}
4965
Calin Juravle2ae48182016-03-16 14:05:09 +00004966void CodeGeneratorX86::GenerateImplicitNullCheck(HNullCheck* instruction) {
4967 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004968 return;
4969 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004970 LocationSummary* locations = instruction->GetLocations();
4971 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00004972
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004973 __ testl(EAX, Address(obj.AsRegister<Register>(), 0));
Calin Juravle2ae48182016-03-16 14:05:09 +00004974 RecordPcInfo(instruction, instruction->GetDexPc());
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004975}
4976
Calin Juravle2ae48182016-03-16 14:05:09 +00004977void CodeGeneratorX86::GenerateExplicitNullCheck(HNullCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07004978 SlowPathCode* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00004979 AddSlowPath(slow_path);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004980
4981 LocationSummary* locations = instruction->GetLocations();
4982 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004983
4984 if (obj.IsRegister()) {
Mark Mendell42514f62015-03-31 11:34:22 -04004985 __ testl(obj.AsRegister<Register>(), obj.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004986 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004987 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004988 } else {
4989 DCHECK(obj.IsConstant()) << obj;
David Brazdil77a48ae2015-09-15 12:34:04 +00004990 DCHECK(obj.GetConstant()->IsNullConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004991 __ jmp(slow_path->GetEntryLabel());
4992 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004993 }
4994 __ j(kEqual, slow_path->GetEntryLabel());
4995}
4996
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004997void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00004998 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004999}
5000
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005001void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005002 bool object_array_get_with_read_barrier =
5003 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005004 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00005005 new (GetGraph()->GetArena()) LocationSummary(instruction,
5006 object_array_get_with_read_barrier ?
5007 LocationSummary::kCallOnSlowPath :
5008 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01005009 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
5010 locations->SetCustomSlowPathCallerSaves(RegisterSet()); // No caller-save registers.
5011 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005012 locations->SetInAt(0, Location::RequiresRegister());
5013 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005014 if (Primitive::IsFloatingPointType(instruction->GetType())) {
5015 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5016 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005017 // The output overlaps in case of long: we don't want the low move
5018 // to overwrite the array's location. Likewise, in the case of an
5019 // object array get with read barriers enabled, we do not want the
5020 // move to overwrite the array's location, as we need it to emit
5021 // the read barrier.
5022 locations->SetOut(
5023 Location::RequiresRegister(),
5024 (instruction->GetType() == Primitive::kPrimLong || object_array_get_with_read_barrier) ?
5025 Location::kOutputOverlap :
5026 Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005027 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005028}
5029
5030void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
5031 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005032 Location obj_loc = locations->InAt(0);
5033 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005034 Location index = locations->InAt(1);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005035 Location out_loc = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005036 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005037
Calin Juravle77520bc2015-01-12 18:45:46 +00005038 Primitive::Type type = instruction->GetType();
5039 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005040 case Primitive::kPrimBoolean: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005041 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005042 if (index.IsConstant()) {
5043 __ movzxb(out, Address(obj,
5044 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
5045 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005046 __ movzxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005047 }
5048 break;
5049 }
5050
5051 case Primitive::kPrimByte: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005052 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005053 if (index.IsConstant()) {
5054 __ movsxb(out, Address(obj,
5055 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
5056 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005057 __ movsxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005058 }
5059 break;
5060 }
5061
5062 case Primitive::kPrimShort: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005063 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005064 if (index.IsConstant()) {
5065 __ movsxw(out, Address(obj,
5066 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
5067 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005068 __ movsxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005069 }
5070 break;
5071 }
5072
5073 case Primitive::kPrimChar: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005074 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005075 if (index.IsConstant()) {
5076 __ movzxw(out, Address(obj,
5077 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
5078 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005079 __ movzxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005080 }
5081 break;
5082 }
5083
Roland Levillain7c1559a2015-12-15 10:55:36 +00005084 case Primitive::kPrimInt: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005085 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005086 if (index.IsConstant()) {
5087 __ movl(out, Address(obj,
5088 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
5089 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005090 __ movl(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005091 }
5092 break;
5093 }
5094
Roland Levillain7c1559a2015-12-15 10:55:36 +00005095 case Primitive::kPrimNot: {
5096 static_assert(
5097 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
5098 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain7c1559a2015-12-15 10:55:36 +00005099 // /* HeapReference<Object> */ out =
5100 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
5101 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005102 // Note that a potential implicit null check is handled in this
5103 // CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier call.
5104 codegen_->GenerateArrayLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00005105 instruction, out_loc, obj, data_offset, index, /* needs_null_check */ true);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005106 } else {
5107 Register out = out_loc.AsRegister<Register>();
5108 if (index.IsConstant()) {
5109 uint32_t offset =
5110 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
5111 __ movl(out, Address(obj, offset));
5112 codegen_->MaybeRecordImplicitNullCheck(instruction);
5113 // If read barriers are enabled, emit read barriers other than
5114 // Baker's using a slow path (and also unpoison the loaded
5115 // reference, if heap poisoning is enabled).
5116 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
5117 } else {
5118 __ movl(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
5119 codegen_->MaybeRecordImplicitNullCheck(instruction);
5120 // If read barriers are enabled, emit read barriers other than
5121 // Baker's using a slow path (and also unpoison the loaded
5122 // reference, if heap poisoning is enabled).
5123 codegen_->MaybeGenerateReadBarrierSlow(
5124 instruction, out_loc, out_loc, obj_loc, data_offset, index);
5125 }
5126 }
5127 break;
5128 }
5129
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005130 case Primitive::kPrimLong: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005131 DCHECK_NE(obj, out_loc.AsRegisterPairLow<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005132 if (index.IsConstant()) {
5133 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Roland Levillain7c1559a2015-12-15 10:55:36 +00005134 __ movl(out_loc.AsRegisterPairLow<Register>(), Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005135 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005136 __ movl(out_loc.AsRegisterPairHigh<Register>(), Address(obj, offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005137 } else {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005138 __ movl(out_loc.AsRegisterPairLow<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005139 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005140 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005141 __ movl(out_loc.AsRegisterPairHigh<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005142 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005143 }
5144 break;
5145 }
5146
Mark Mendell7c8d0092015-01-26 11:21:33 -05005147 case Primitive::kPrimFloat: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005148 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Mark Mendell7c8d0092015-01-26 11:21:33 -05005149 if (index.IsConstant()) {
5150 __ movss(out, Address(obj,
5151 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
5152 } else {
5153 __ movss(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
5154 }
5155 break;
5156 }
5157
5158 case Primitive::kPrimDouble: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005159 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Mark Mendell7c8d0092015-01-26 11:21:33 -05005160 if (index.IsConstant()) {
5161 __ movsd(out, Address(obj,
5162 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
5163 } else {
5164 __ movsd(out, Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
5165 }
5166 break;
5167 }
5168
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005169 case Primitive::kPrimVoid:
Calin Juravle77520bc2015-01-12 18:45:46 +00005170 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07005171 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005172 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005173
Roland Levillain7c1559a2015-12-15 10:55:36 +00005174 if (type == Primitive::kPrimNot || type == Primitive::kPrimLong) {
5175 // Potential implicit null checks, in the case of reference or
5176 // long arrays, are handled in the previous switch statement.
5177 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00005178 codegen_->MaybeRecordImplicitNullCheck(instruction);
5179 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005180}
5181
5182void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01005183 Primitive::Type value_type = instruction->GetComponentType();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005184
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005185 bool needs_write_barrier =
5186 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Roland Levillain0d5a2812015-11-13 10:07:31 +00005187 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005188
Nicolas Geoffray39468442014-09-02 15:17:15 +01005189 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
5190 instruction,
Vladimir Marko8d49fd72016-08-25 15:20:47 +01005191 may_need_runtime_call_for_type_check ?
Roland Levillain0d5a2812015-11-13 10:07:31 +00005192 LocationSummary::kCallOnSlowPath :
5193 LocationSummary::kNoCall);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005194
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005195 bool is_byte_type = (value_type == Primitive::kPrimBoolean)
5196 || (value_type == Primitive::kPrimByte);
5197 // We need the inputs to be different than the output in case of long operation.
5198 // In case of a byte operation, the register allocator does not support multiple
5199 // inputs that die at entry with one in a specific register.
5200 locations->SetInAt(0, Location::RequiresRegister());
5201 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
5202 if (is_byte_type) {
5203 // Ensure the value is in a byte register.
5204 locations->SetInAt(2, Location::ByteRegisterOrConstant(EAX, instruction->InputAt(2)));
5205 } else if (Primitive::IsFloatingPointType(value_type)) {
Mark Mendell81489372015-11-04 11:30:41 -05005206 locations->SetInAt(2, Location::FpuRegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005207 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005208 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
5209 }
5210 if (needs_write_barrier) {
5211 // Temporary registers for the write barrier.
5212 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
5213 // Ensure the card is in a byte register.
Roland Levillain4f6b0b52015-11-23 19:29:22 +00005214 locations->AddTemp(Location::RegisterLocation(ECX));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005215 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005216}
5217
5218void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
5219 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005220 Location array_loc = locations->InAt(0);
5221 Register array = array_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005222 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005223 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005224 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005225 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
5226 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5227 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005228 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005229 bool needs_write_barrier =
5230 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005231
5232 switch (value_type) {
5233 case Primitive::kPrimBoolean:
5234 case Primitive::kPrimByte: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005235 uint32_t offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
5236 Address address = index.IsConstant()
5237 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + offset)
5238 : Address(array, index.AsRegister<Register>(), TIMES_1, offset);
5239 if (value.IsRegister()) {
5240 __ movb(address, value.AsRegister<ByteRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005241 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005242 __ movb(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005243 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005244 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005245 break;
5246 }
5247
5248 case Primitive::kPrimShort:
5249 case Primitive::kPrimChar: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005250 uint32_t offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
5251 Address address = index.IsConstant()
5252 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + offset)
5253 : Address(array, index.AsRegister<Register>(), TIMES_2, offset);
5254 if (value.IsRegister()) {
5255 __ movw(address, value.AsRegister<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005256 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005257 __ movw(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005258 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005259 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005260 break;
5261 }
5262
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005263 case Primitive::kPrimNot: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005264 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
5265 Address address = index.IsConstant()
5266 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
5267 : Address(array, index.AsRegister<Register>(), TIMES_4, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005268
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005269 if (!value.IsRegister()) {
5270 // Just setting null.
5271 DCHECK(instruction->InputAt(2)->IsNullConstant());
5272 DCHECK(value.IsConstant()) << value;
5273 __ movl(address, Immediate(0));
Calin Juravle77520bc2015-01-12 18:45:46 +00005274 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005275 DCHECK(!needs_write_barrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005276 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005277 break;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005278 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005279
5280 DCHECK(needs_write_barrier);
5281 Register register_value = value.AsRegister<Register>();
Roland Levillain16d9f942016-08-25 17:27:56 +01005282 // We cannot use a NearLabel for `done`, as its range may be too
5283 // short when Baker read barriers are enabled.
5284 Label done;
5285 NearLabel not_null, do_put;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005286 SlowPathCode* slow_path = nullptr;
Roland Levillain16d9f942016-08-25 17:27:56 +01005287 Location temp_loc = locations->GetTemp(0);
5288 Register temp = temp_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005289 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005290 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathX86(instruction);
5291 codegen_->AddSlowPath(slow_path);
5292 if (instruction->GetValueCanBeNull()) {
5293 __ testl(register_value, register_value);
5294 __ j(kNotEqual, &not_null);
5295 __ movl(address, Immediate(0));
5296 codegen_->MaybeRecordImplicitNullCheck(instruction);
5297 __ jmp(&done);
5298 __ Bind(&not_null);
5299 }
5300
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005301 // Note that when Baker read barriers are enabled, the type
5302 // checks are performed without read barriers. This is fine,
5303 // even in the case where a class object is in the from-space
5304 // after the flip, as a comparison involving such a type would
5305 // not produce a false positive; it may of course produce a
5306 // false negative, in which case we would take the ArraySet
5307 // slow path.
Roland Levillain16d9f942016-08-25 17:27:56 +01005308
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005309 // /* HeapReference<Class> */ temp = array->klass_
5310 __ movl(temp, Address(array, class_offset));
5311 codegen_->MaybeRecordImplicitNullCheck(instruction);
5312 __ MaybeUnpoisonHeapReference(temp);
Roland Levillain16d9f942016-08-25 17:27:56 +01005313
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005314 // /* HeapReference<Class> */ temp = temp->component_type_
5315 __ movl(temp, Address(temp, component_offset));
5316 // If heap poisoning is enabled, no need to unpoison `temp`
5317 // nor the object reference in `register_value->klass`, as
5318 // we are comparing two poisoned references.
5319 __ cmpl(temp, Address(register_value, class_offset));
Roland Levillain16d9f942016-08-25 17:27:56 +01005320
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005321 if (instruction->StaticTypeOfArrayIsObjectArray()) {
5322 __ j(kEqual, &do_put);
5323 // If heap poisoning is enabled, the `temp` reference has
5324 // not been unpoisoned yet; unpoison it now.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005325 __ MaybeUnpoisonHeapReference(temp);
5326
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005327 // If heap poisoning is enabled, no need to unpoison the
5328 // heap reference loaded below, as it is only used for a
5329 // comparison with null.
5330 __ cmpl(Address(temp, super_offset), Immediate(0));
5331 __ j(kNotEqual, slow_path->GetEntryLabel());
5332 __ Bind(&do_put);
5333 } else {
5334 __ j(kNotEqual, slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005335 }
5336 }
5337
5338 if (kPoisonHeapReferences) {
5339 __ movl(temp, register_value);
5340 __ PoisonHeapReference(temp);
5341 __ movl(address, temp);
5342 } else {
5343 __ movl(address, register_value);
5344 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005345 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005346 codegen_->MaybeRecordImplicitNullCheck(instruction);
5347 }
5348
5349 Register card = locations->GetTemp(1).AsRegister<Register>();
5350 codegen_->MarkGCCard(
5351 temp, card, array, value.AsRegister<Register>(), instruction->GetValueCanBeNull());
5352 __ Bind(&done);
5353
5354 if (slow_path != nullptr) {
5355 __ Bind(slow_path->GetExitLabel());
5356 }
5357
5358 break;
5359 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005360
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005361 case Primitive::kPrimInt: {
5362 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
5363 Address address = index.IsConstant()
5364 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
5365 : Address(array, index.AsRegister<Register>(), TIMES_4, offset);
5366 if (value.IsRegister()) {
5367 __ movl(address, value.AsRegister<Register>());
5368 } else {
5369 DCHECK(value.IsConstant()) << value;
5370 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
5371 __ movl(address, Immediate(v));
5372 }
5373 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005374 break;
5375 }
5376
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005377 case Primitive::kPrimLong: {
5378 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005379 if (index.IsConstant()) {
5380 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005381 if (value.IsRegisterPair()) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005382 __ movl(Address(array, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00005383 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005384 __ movl(Address(array, offset + kX86WordSize), value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005385 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005386 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005387 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005388 __ movl(Address(array, offset), Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00005389 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005390 __ movl(Address(array, offset + kX86WordSize), Immediate(High32Bits(val)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005391 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005392 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005393 if (value.IsRegisterPair()) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005394 __ movl(Address(array, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005395 value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00005396 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005397 __ movl(Address(array, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005398 value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005399 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005400 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005401 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005402 __ movl(Address(array, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005403 Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00005404 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005405 __ movl(Address(array, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005406 Immediate(High32Bits(val)));
5407 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005408 }
5409 break;
5410 }
5411
Mark Mendell7c8d0092015-01-26 11:21:33 -05005412 case Primitive::kPrimFloat: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005413 uint32_t offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
5414 Address address = index.IsConstant()
5415 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
5416 : Address(array, index.AsRegister<Register>(), TIMES_4, offset);
Mark Mendell81489372015-11-04 11:30:41 -05005417 if (value.IsFpuRegister()) {
5418 __ movss(address, value.AsFpuRegister<XmmRegister>());
5419 } else {
5420 DCHECK(value.IsConstant());
5421 int32_t v = bit_cast<int32_t, float>(value.GetConstant()->AsFloatConstant()->GetValue());
5422 __ movl(address, Immediate(v));
5423 }
5424 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005425 break;
5426 }
5427
5428 case Primitive::kPrimDouble: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005429 uint32_t offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
5430 Address address = index.IsConstant()
5431 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + offset)
5432 : Address(array, index.AsRegister<Register>(), TIMES_8, offset);
Mark Mendell81489372015-11-04 11:30:41 -05005433 if (value.IsFpuRegister()) {
5434 __ movsd(address, value.AsFpuRegister<XmmRegister>());
5435 } else {
5436 DCHECK(value.IsConstant());
5437 Address address_hi = index.IsConstant() ?
5438 Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) +
5439 offset + kX86WordSize) :
5440 Address(array, index.AsRegister<Register>(), TIMES_8, offset + kX86WordSize);
5441 int64_t v = bit_cast<int64_t, double>(value.GetConstant()->AsDoubleConstant()->GetValue());
5442 __ movl(address, Immediate(Low32Bits(v)));
5443 codegen_->MaybeRecordImplicitNullCheck(instruction);
5444 __ movl(address_hi, Immediate(High32Bits(v)));
5445 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005446 break;
5447 }
5448
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005449 case Primitive::kPrimVoid:
5450 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07005451 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005452 }
5453}
5454
5455void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
5456 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005457 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellee8d9712016-07-12 11:13:15 -04005458 if (!instruction->IsEmittedAtUseSite()) {
5459 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5460 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005461}
5462
5463void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
Mark Mendellee8d9712016-07-12 11:13:15 -04005464 if (instruction->IsEmittedAtUseSite()) {
5465 return;
5466 }
5467
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005468 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01005469 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00005470 Register obj = locations->InAt(0).AsRegister<Register>();
5471 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005472 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005473 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005474}
5475
5476void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00005477 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
5478 ? LocationSummary::kCallOnSlowPath
5479 : LocationSummary::kNoCall;
5480 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Mark Mendellf60c90b2015-03-04 15:12:59 -05005481 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendellee8d9712016-07-12 11:13:15 -04005482 HInstruction* length = instruction->InputAt(1);
5483 if (!length->IsEmittedAtUseSite()) {
5484 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
5485 }
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005486 if (instruction->HasUses()) {
5487 locations->SetOut(Location::SameAsFirstInput());
5488 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005489}
5490
5491void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
5492 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05005493 Location index_loc = locations->InAt(0);
5494 Location length_loc = locations->InAt(1);
Andreas Gampe85b62f22015-09-09 13:15:38 -07005495 SlowPathCode* slow_path =
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01005496 new (GetGraph()->GetArena()) BoundsCheckSlowPathX86(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005497
Mark Mendell99dbd682015-04-22 16:18:52 -04005498 if (length_loc.IsConstant()) {
5499 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
5500 if (index_loc.IsConstant()) {
5501 // BCE will remove the bounds check if we are guarenteed to pass.
5502 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5503 if (index < 0 || index >= length) {
5504 codegen_->AddSlowPath(slow_path);
5505 __ jmp(slow_path->GetEntryLabel());
5506 } else {
5507 // Some optimization after BCE may have generated this, and we should not
5508 // generate a bounds check if it is a valid range.
5509 }
5510 return;
5511 }
5512
5513 // We have to reverse the jump condition because the length is the constant.
5514 Register index_reg = index_loc.AsRegister<Register>();
5515 __ cmpl(index_reg, Immediate(length));
5516 codegen_->AddSlowPath(slow_path);
5517 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005518 } else {
Mark Mendellee8d9712016-07-12 11:13:15 -04005519 HInstruction* array_length = instruction->InputAt(1);
5520 if (array_length->IsEmittedAtUseSite()) {
5521 // Address the length field in the array.
5522 DCHECK(array_length->IsArrayLength());
5523 uint32_t len_offset = CodeGenerator::GetArrayLengthOffset(array_length->AsArrayLength());
5524 Location array_loc = array_length->GetLocations()->InAt(0);
5525 Address array_len(array_loc.AsRegister<Register>(), len_offset);
5526 if (index_loc.IsConstant()) {
5527 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5528 __ cmpl(array_len, Immediate(value));
5529 } else {
5530 __ cmpl(array_len, index_loc.AsRegister<Register>());
5531 }
5532 codegen_->MaybeRecordImplicitNullCheck(array_length);
Mark Mendell99dbd682015-04-22 16:18:52 -04005533 } else {
Mark Mendellee8d9712016-07-12 11:13:15 -04005534 Register length = length_loc.AsRegister<Register>();
5535 if (index_loc.IsConstant()) {
5536 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5537 __ cmpl(length, Immediate(value));
5538 } else {
5539 __ cmpl(length, index_loc.AsRegister<Register>());
5540 }
Mark Mendell99dbd682015-04-22 16:18:52 -04005541 }
5542 codegen_->AddSlowPath(slow_path);
5543 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005544 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005545}
5546
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005547void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005548 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005549}
5550
5551void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005552 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5553}
5554
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005555void LocationsBuilderX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Marko70e97462016-08-09 11:04:26 +01005556 LocationSummary* locations =
5557 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
5558 locations->SetCustomSlowPathCallerSaves(RegisterSet()); // No caller-save registers.
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005559}
5560
5561void InstructionCodeGeneratorX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005562 HBasicBlock* block = instruction->GetBlock();
5563 if (block->GetLoopInformation() != nullptr) {
5564 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5565 // The back edge will generate the suspend check.
5566 return;
5567 }
5568 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5569 // The goto will generate the suspend check.
5570 return;
5571 }
5572 GenerateSuspendCheck(instruction, nullptr);
5573}
5574
5575void InstructionCodeGeneratorX86::GenerateSuspendCheck(HSuspendCheck* instruction,
5576 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005577 SuspendCheckSlowPathX86* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005578 down_cast<SuspendCheckSlowPathX86*>(instruction->GetSlowPath());
5579 if (slow_path == nullptr) {
5580 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathX86(instruction, successor);
5581 instruction->SetSlowPath(slow_path);
5582 codegen_->AddSlowPath(slow_path);
5583 if (successor != nullptr) {
5584 DCHECK(successor->IsLoopHeader());
5585 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
5586 }
5587 } else {
5588 DCHECK_EQ(slow_path->GetSuccessor(), successor);
5589 }
5590
Andreas Gampe542451c2016-07-26 09:02:02 -07005591 __ fs()->cmpw(Address::Absolute(Thread::ThreadFlagsOffset<kX86PointerSize>().Int32Value()),
Roland Levillain7c1559a2015-12-15 10:55:36 +00005592 Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005593 if (successor == nullptr) {
5594 __ j(kNotEqual, slow_path->GetEntryLabel());
5595 __ Bind(slow_path->GetReturnLabel());
5596 } else {
5597 __ j(kEqual, codegen_->GetLabelOf(successor));
5598 __ jmp(slow_path->GetEntryLabel());
5599 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005600}
5601
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005602X86Assembler* ParallelMoveResolverX86::GetAssembler() const {
5603 return codegen_->GetAssembler();
5604}
5605
Mark Mendell7c8d0092015-01-26 11:21:33 -05005606void ParallelMoveResolverX86::MoveMemoryToMemory32(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005607 ScratchRegisterScope ensure_scratch(
5608 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5609 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5610 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5611 __ movl(temp_reg, Address(ESP, src + stack_offset));
5612 __ movl(Address(ESP, dst + stack_offset), temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005613}
5614
5615void ParallelMoveResolverX86::MoveMemoryToMemory64(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005616 ScratchRegisterScope ensure_scratch(
5617 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5618 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5619 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5620 __ movl(temp_reg, Address(ESP, src + stack_offset));
5621 __ movl(Address(ESP, dst + stack_offset), temp_reg);
5622 __ movl(temp_reg, Address(ESP, src + stack_offset + kX86WordSize));
5623 __ movl(Address(ESP, dst + stack_offset + kX86WordSize), temp_reg);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005624}
5625
5626void ParallelMoveResolverX86::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005627 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005628 Location source = move->GetSource();
5629 Location destination = move->GetDestination();
5630
5631 if (source.IsRegister()) {
5632 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005633 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
David Brazdil74eb1b22015-12-14 11:44:01 +00005634 } else if (destination.IsFpuRegister()) {
5635 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005636 } else {
5637 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00005638 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005639 }
David Brazdil74eb1b22015-12-14 11:44:01 +00005640 } else if (source.IsRegisterPair()) {
5641 size_t elem_size = Primitive::ComponentSize(Primitive::kPrimInt);
5642 // Create stack space for 2 elements.
5643 __ subl(ESP, Immediate(2 * elem_size));
5644 __ movl(Address(ESP, 0), source.AsRegisterPairLow<Register>());
5645 __ movl(Address(ESP, elem_size), source.AsRegisterPairHigh<Register>());
5646 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
5647 // And remove the temporary stack space we allocated.
5648 __ addl(ESP, Immediate(2 * elem_size));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005649 } else if (source.IsFpuRegister()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00005650 if (destination.IsRegister()) {
5651 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
5652 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005653 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
David Brazdil74eb1b22015-12-14 11:44:01 +00005654 } else if (destination.IsRegisterPair()) {
5655 XmmRegister src_reg = source.AsFpuRegister<XmmRegister>();
5656 __ movd(destination.AsRegisterPairLow<Register>(), src_reg);
5657 __ psrlq(src_reg, Immediate(32));
5658 __ movd(destination.AsRegisterPairHigh<Register>(), src_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005659 } else if (destination.IsStackSlot()) {
5660 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
5661 } else {
5662 DCHECK(destination.IsDoubleStackSlot());
5663 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
5664 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005665 } else if (source.IsStackSlot()) {
5666 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005667 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005668 } else if (destination.IsFpuRegister()) {
5669 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005670 } else {
5671 DCHECK(destination.IsStackSlot());
Mark Mendell7c8d0092015-01-26 11:21:33 -05005672 MoveMemoryToMemory32(destination.GetStackIndex(), source.GetStackIndex());
5673 }
5674 } else if (source.IsDoubleStackSlot()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00005675 if (destination.IsRegisterPair()) {
5676 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
5677 __ movl(destination.AsRegisterPairHigh<Register>(),
5678 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
5679 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005680 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
5681 } else {
5682 DCHECK(destination.IsDoubleStackSlot()) << destination;
5683 MoveMemoryToMemory64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005684 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005685 } else if (source.IsConstant()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005686 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00005687 if (constant->IsIntConstant() || constant->IsNullConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05005688 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005689 if (destination.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05005690 if (value == 0) {
5691 __ xorl(destination.AsRegister<Register>(), destination.AsRegister<Register>());
5692 } else {
5693 __ movl(destination.AsRegister<Register>(), Immediate(value));
5694 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005695 } else {
5696 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell09b84632015-02-13 17:48:38 -05005697 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005698 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005699 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005700 float fp_value = constant->AsFloatConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00005701 int32_t value = bit_cast<int32_t, float>(fp_value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005702 Immediate imm(value);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005703 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005704 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
5705 if (value == 0) {
5706 // Easy handling of 0.0.
5707 __ xorps(dest, dest);
5708 } else {
5709 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005710 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5711 Register temp = static_cast<Register>(ensure_scratch.GetRegister());
5712 __ movl(temp, Immediate(value));
5713 __ movd(dest, temp);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005714 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005715 } else {
5716 DCHECK(destination.IsStackSlot()) << destination;
5717 __ movl(Address(ESP, destination.GetStackIndex()), imm);
5718 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005719 } else if (constant->IsLongConstant()) {
5720 int64_t value = constant->AsLongConstant()->GetValue();
5721 int32_t low_value = Low32Bits(value);
5722 int32_t high_value = High32Bits(value);
5723 Immediate low(low_value);
5724 Immediate high(high_value);
5725 if (destination.IsDoubleStackSlot()) {
5726 __ movl(Address(ESP, destination.GetStackIndex()), low);
5727 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
5728 } else {
5729 __ movl(destination.AsRegisterPairLow<Register>(), low);
5730 __ movl(destination.AsRegisterPairHigh<Register>(), high);
5731 }
5732 } else {
5733 DCHECK(constant->IsDoubleConstant());
5734 double dbl_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00005735 int64_t value = bit_cast<int64_t, double>(dbl_value);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005736 int32_t low_value = Low32Bits(value);
5737 int32_t high_value = High32Bits(value);
5738 Immediate low(low_value);
5739 Immediate high(high_value);
5740 if (destination.IsFpuRegister()) {
5741 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
5742 if (value == 0) {
5743 // Easy handling of 0.0.
5744 __ xorpd(dest, dest);
5745 } else {
5746 __ pushl(high);
5747 __ pushl(low);
5748 __ movsd(dest, Address(ESP, 0));
5749 __ addl(ESP, Immediate(8));
5750 }
5751 } else {
5752 DCHECK(destination.IsDoubleStackSlot()) << destination;
5753 __ movl(Address(ESP, destination.GetStackIndex()), low);
5754 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
5755 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005756 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005757 } else {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00005758 LOG(FATAL) << "Unimplemented move: " << destination << " <- " << source;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005759 }
5760}
5761
Mark Mendella5c19ce2015-04-01 12:51:05 -04005762void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005763 Register suggested_scratch = reg == EAX ? EBX : EAX;
5764 ScratchRegisterScope ensure_scratch(
5765 this, reg, suggested_scratch, codegen_->GetNumberOfCoreRegisters());
5766
5767 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5768 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset));
5769 __ movl(Address(ESP, mem + stack_offset), reg);
5770 __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005771}
5772
Mark Mendell7c8d0092015-01-26 11:21:33 -05005773void ParallelMoveResolverX86::Exchange32(XmmRegister reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005774 ScratchRegisterScope ensure_scratch(
5775 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5776
5777 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5778 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5779 __ movl(temp_reg, Address(ESP, mem + stack_offset));
5780 __ movss(Address(ESP, mem + stack_offset), reg);
5781 __ movd(reg, temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005782}
5783
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005784void ParallelMoveResolverX86::Exchange(int mem1, int mem2) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005785 ScratchRegisterScope ensure_scratch1(
5786 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005787
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005788 Register suggested_scratch = ensure_scratch1.GetRegister() == EAX ? EBX : EAX;
5789 ScratchRegisterScope ensure_scratch2(
5790 this, ensure_scratch1.GetRegister(), suggested_scratch, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005791
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005792 int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0;
5793 stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0;
5794 __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset));
5795 __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset));
5796 __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister()));
5797 __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005798}
5799
5800void ParallelMoveResolverX86::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005801 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005802 Location source = move->GetSource();
5803 Location destination = move->GetDestination();
5804
5805 if (source.IsRegister() && destination.IsRegister()) {
Mark Mendell90979812015-07-28 16:41:21 -04005806 // Use XOR swap algorithm to avoid serializing XCHG instruction or using a temporary.
5807 DCHECK_NE(destination.AsRegister<Register>(), source.AsRegister<Register>());
5808 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
5809 __ xorl(source.AsRegister<Register>(), destination.AsRegister<Register>());
5810 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005811 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005812 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005813 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005814 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005815 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
5816 Exchange(destination.GetStackIndex(), source.GetStackIndex());
Mark Mendell7c8d0092015-01-26 11:21:33 -05005817 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
5818 // Use XOR Swap algorithm to avoid a temporary.
5819 DCHECK_NE(source.reg(), destination.reg());
5820 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
5821 __ xorpd(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
5822 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
5823 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
5824 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
5825 } else if (destination.IsFpuRegister() && source.IsStackSlot()) {
5826 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005827 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
5828 // Take advantage of the 16 bytes in the XMM register.
5829 XmmRegister reg = source.AsFpuRegister<XmmRegister>();
5830 Address stack(ESP, destination.GetStackIndex());
5831 // Load the double into the high doubleword.
5832 __ movhpd(reg, stack);
5833
5834 // Store the low double into the destination.
5835 __ movsd(stack, reg);
5836
5837 // Move the high double to the low double.
5838 __ psrldq(reg, Immediate(8));
5839 } else if (destination.IsFpuRegister() && source.IsDoubleStackSlot()) {
5840 // Take advantage of the 16 bytes in the XMM register.
5841 XmmRegister reg = destination.AsFpuRegister<XmmRegister>();
5842 Address stack(ESP, source.GetStackIndex());
5843 // Load the double into the high doubleword.
5844 __ movhpd(reg, stack);
5845
5846 // Store the low double into the destination.
5847 __ movsd(stack, reg);
5848
5849 // Move the high double to the low double.
5850 __ psrldq(reg, Immediate(8));
5851 } else if (destination.IsDoubleStackSlot() && source.IsDoubleStackSlot()) {
5852 Exchange(destination.GetStackIndex(), source.GetStackIndex());
5853 Exchange(destination.GetHighStackIndex(kX86WordSize), source.GetHighStackIndex(kX86WordSize));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005854 } else {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005855 LOG(FATAL) << "Unimplemented: source: " << source << ", destination: " << destination;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005856 }
5857}
5858
5859void ParallelMoveResolverX86::SpillScratch(int reg) {
5860 __ pushl(static_cast<Register>(reg));
5861}
5862
5863void ParallelMoveResolverX86::RestoreScratch(int reg) {
5864 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005865}
5866
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005867HLoadClass::LoadKind CodeGeneratorX86::GetSupportedLoadClassKind(
5868 HLoadClass::LoadKind desired_class_load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005869 switch (desired_class_load_kind) {
5870 case HLoadClass::LoadKind::kReferrersClass:
5871 break;
5872 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
5873 DCHECK(!GetCompilerOptions().GetCompilePic());
5874 break;
5875 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
5876 DCHECK(GetCompilerOptions().GetCompilePic());
5877 FALLTHROUGH_INTENDED;
5878 case HLoadClass::LoadKind::kDexCachePcRelative:
5879 DCHECK(!Runtime::Current()->UseJitCompilation()); // Note: boot image is also non-JIT.
5880 // We disable pc-relative load when there is an irreducible loop, as the optimization
5881 // is incompatible with it.
5882 // TODO: Create as many X86ComputeBaseMethodAddress instructions as needed for methods
5883 // with irreducible loops.
5884 if (GetGraph()->HasIrreducibleLoops()) {
5885 return HLoadClass::LoadKind::kDexCacheViaMethod;
5886 }
5887 break;
5888 case HLoadClass::LoadKind::kBootImageAddress:
5889 break;
5890 case HLoadClass::LoadKind::kDexCacheAddress:
5891 DCHECK(Runtime::Current()->UseJitCompilation());
5892 break;
5893 case HLoadClass::LoadKind::kDexCacheViaMethod:
5894 break;
5895 }
5896 return desired_class_load_kind;
5897}
5898
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005899void LocationsBuilderX86::VisitLoadClass(HLoadClass* cls) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005900 if (cls->NeedsAccessCheck()) {
5901 InvokeRuntimeCallingConvention calling_convention;
5902 CodeGenerator::CreateLoadClassLocationSummary(
5903 cls,
5904 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
5905 Location::RegisterLocation(EAX),
5906 /* code_generator_supports_read_barrier */ true);
5907 return;
5908 }
5909
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005910 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
5911 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005912 ? LocationSummary::kCallOnSlowPath
5913 : LocationSummary::kNoCall;
5914 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005915 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Vladimir Marko70e97462016-08-09 11:04:26 +01005916 locations->SetCustomSlowPathCallerSaves(RegisterSet()); // No caller-save registers.
5917 }
5918
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005919 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
5920 if (load_kind == HLoadClass::LoadKind::kReferrersClass ||
5921 load_kind == HLoadClass::LoadKind::kDexCacheViaMethod ||
5922 load_kind == HLoadClass::LoadKind::kBootImageLinkTimePcRelative ||
5923 load_kind == HLoadClass::LoadKind::kDexCachePcRelative) {
5924 locations->SetInAt(0, Location::RequiresRegister());
5925 }
5926 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005927}
5928
5929void InstructionCodeGeneratorX86::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005930 LocationSummary* locations = cls->GetLocations();
Calin Juravle98893e12015-10-02 21:05:03 +01005931 if (cls->NeedsAccessCheck()) {
5932 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex());
Serban Constantinescuba45db02016-07-12 22:53:02 +01005933 codegen_->InvokeRuntime(kQuickInitializeTypeAndVerifyAccess, cls, cls->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00005934 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Calin Juravle580b6092015-10-06 17:35:58 +01005935 return;
5936 }
5937
Roland Levillain0d5a2812015-11-13 10:07:31 +00005938 Location out_loc = locations->Out();
5939 Register out = out_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005940
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005941 bool generate_null_check = false;
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005942 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005943 switch (cls->GetLoadKind()) {
5944 case HLoadClass::LoadKind::kReferrersClass: {
5945 DCHECK(!cls->CanCallRuntime());
5946 DCHECK(!cls->MustGenerateClinitCheck());
5947 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5948 Register current_method = locations->InAt(0).AsRegister<Register>();
5949 GenerateGcRootFieldLoad(
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005950 cls,
5951 out_loc,
5952 Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()),
5953 /*fixup_label*/ nullptr,
5954 requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005955 break;
5956 }
5957 case HLoadClass::LoadKind::kBootImageLinkTimeAddress: {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005958 DCHECK(!requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005959 __ movl(out, Immediate(/* placeholder */ 0));
5960 codegen_->RecordTypePatch(cls);
5961 break;
5962 }
5963 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005964 DCHECK(!requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005965 Register method_address = locations->InAt(0).AsRegister<Register>();
5966 __ leal(out, Address(method_address, CodeGeneratorX86::kDummy32BitOffset));
5967 codegen_->RecordTypePatch(cls);
5968 break;
5969 }
5970 case HLoadClass::LoadKind::kBootImageAddress: {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005971 DCHECK(!requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005972 DCHECK_NE(cls->GetAddress(), 0u);
5973 uint32_t address = dchecked_integral_cast<uint32_t>(cls->GetAddress());
5974 __ movl(out, Immediate(address));
5975 codegen_->RecordSimplePatch();
5976 break;
5977 }
5978 case HLoadClass::LoadKind::kDexCacheAddress: {
5979 DCHECK_NE(cls->GetAddress(), 0u);
5980 uint32_t address = dchecked_integral_cast<uint32_t>(cls->GetAddress());
5981 // /* GcRoot<mirror::Class> */ out = *address
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005982 GenerateGcRootFieldLoad(cls,
5983 out_loc,
5984 Address::Absolute(address),
5985 /*fixup_label*/ nullptr,
5986 requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005987 generate_null_check = !cls->IsInDexCache();
5988 break;
5989 }
5990 case HLoadClass::LoadKind::kDexCachePcRelative: {
5991 Register base_reg = locations->InAt(0).AsRegister<Register>();
5992 uint32_t offset = cls->GetDexCacheElementOffset();
5993 Label* fixup_label = codegen_->NewPcRelativeDexCacheArrayPatch(cls->GetDexFile(), offset);
5994 // /* GcRoot<mirror::Class> */ out = *(base + offset) /* PC-relative */
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005995 GenerateGcRootFieldLoad(cls,
5996 out_loc,
5997 Address(base_reg, CodeGeneratorX86::kDummy32BitOffset),
5998 fixup_label,
5999 requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006000 generate_null_check = !cls->IsInDexCache();
6001 break;
6002 }
6003 case HLoadClass::LoadKind::kDexCacheViaMethod: {
6004 // /* GcRoot<mirror::Class>[] */ out =
6005 // current_method.ptr_sized_fields_->dex_cache_resolved_types_
6006 Register current_method = locations->InAt(0).AsRegister<Register>();
6007 __ movl(out, Address(current_method,
6008 ArtMethod::DexCacheResolvedTypesOffset(kX86PointerSize).Int32Value()));
6009 // /* GcRoot<mirror::Class> */ out = out[type_index]
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006010 GenerateGcRootFieldLoad(cls,
6011 out_loc,
6012 Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())),
6013 /*fixup_label*/ nullptr,
6014 requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006015 generate_null_check = !cls->IsInDexCache();
6016 break;
6017 }
6018 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006019
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006020 if (generate_null_check || cls->MustGenerateClinitCheck()) {
6021 DCHECK(cls->CanCallRuntime());
6022 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
6023 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
6024 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006025
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006026 if (generate_null_check) {
6027 __ testl(out, out);
6028 __ j(kEqual, slow_path->GetEntryLabel());
6029 }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006030
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006031 if (cls->MustGenerateClinitCheck()) {
6032 GenerateClassInitializationCheck(slow_path, out);
6033 } else {
6034 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006035 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006036 }
6037}
6038
6039void LocationsBuilderX86::VisitClinitCheck(HClinitCheck* check) {
6040 LocationSummary* locations =
6041 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
6042 locations->SetInAt(0, Location::RequiresRegister());
6043 if (check->HasUses()) {
6044 locations->SetOut(Location::SameAsFirstInput());
6045 }
6046}
6047
6048void InstructionCodeGeneratorX86::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006049 // We assume the class to not be null.
Andreas Gampe85b62f22015-09-09 13:15:38 -07006050 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006051 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006052 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00006053 GenerateClassInitializationCheck(slow_path,
6054 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006055}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006056
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006057void InstructionCodeGeneratorX86::GenerateClassInitializationCheck(
Andreas Gampe85b62f22015-09-09 13:15:38 -07006058 SlowPathCode* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006059 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
6060 Immediate(mirror::Class::kStatusInitialized));
6061 __ j(kLess, slow_path->GetEntryLabel());
6062 __ Bind(slow_path->GetExitLabel());
6063 // No need for memory fence, thanks to the X86 memory model.
6064}
6065
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006066HLoadString::LoadKind CodeGeneratorX86::GetSupportedLoadStringKind(
6067 HLoadString::LoadKind desired_string_load_kind) {
6068 if (kEmitCompilerReadBarrier) {
6069 switch (desired_string_load_kind) {
6070 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
6071 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
6072 case HLoadString::LoadKind::kBootImageAddress:
6073 // TODO: Implement for read barrier.
6074 return HLoadString::LoadKind::kDexCacheViaMethod;
6075 default:
6076 break;
6077 }
6078 }
6079 switch (desired_string_load_kind) {
6080 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
6081 DCHECK(!GetCompilerOptions().GetCompilePic());
6082 break;
6083 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
6084 DCHECK(GetCompilerOptions().GetCompilePic());
6085 FALLTHROUGH_INTENDED;
6086 case HLoadString::LoadKind::kDexCachePcRelative:
Calin Juravleffc87072016-04-20 14:22:09 +01006087 DCHECK(!Runtime::Current()->UseJitCompilation()); // Note: boot image is also non-JIT.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006088 // We disable pc-relative load when there is an irreducible loop, as the optimization
6089 // is incompatible with it.
6090 // TODO: Create as many X86ComputeBaseMethodAddress instructions as needed for methods
6091 // with irreducible loops.
6092 if (GetGraph()->HasIrreducibleLoops()) {
6093 return HLoadString::LoadKind::kDexCacheViaMethod;
6094 }
6095 break;
6096 case HLoadString::LoadKind::kBootImageAddress:
6097 break;
6098 case HLoadString::LoadKind::kDexCacheAddress:
Calin Juravleffc87072016-04-20 14:22:09 +01006099 DCHECK(Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006100 break;
6101 case HLoadString::LoadKind::kDexCacheViaMethod:
6102 break;
6103 }
6104 return desired_string_load_kind;
6105}
6106
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006107void LocationsBuilderX86::VisitLoadString(HLoadString* load) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006108 LocationSummary::CallKind call_kind = (load->NeedsEnvironment() || kEmitCompilerReadBarrier)
Christina Wadsworth175d09b2016-08-31 16:26:01 -07006109 ? LocationSummary::kCallOnMainOnly
Nicolas Geoffray917d0162015-11-24 18:25:35 +00006110 : LocationSummary::kNoCall;
6111 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006112 HLoadString::LoadKind load_kind = load->GetLoadKind();
6113 if (load_kind == HLoadString::LoadKind::kDexCacheViaMethod ||
6114 load_kind == HLoadString::LoadKind::kBootImageLinkTimePcRelative ||
6115 load_kind == HLoadString::LoadKind::kDexCachePcRelative) {
6116 locations->SetInAt(0, Location::RequiresRegister());
6117 }
Christina Wadsworth175d09b2016-08-31 16:26:01 -07006118 if (load_kind == HLoadString::LoadKind::kDexCacheViaMethod) {
6119 locations->SetOut(Location::RegisterLocation(EAX));
6120 } else {
6121 locations->SetOut(Location::RequiresRegister());
6122 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006123}
6124
6125void InstructionCodeGeneratorX86::VisitLoadString(HLoadString* load) {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01006126 LocationSummary* locations = load->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006127 Location out_loc = locations->Out();
6128 Register out = out_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006129
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006130 switch (load->GetLoadKind()) {
6131 case HLoadString::LoadKind::kBootImageLinkTimeAddress: {
6132 DCHECK(!kEmitCompilerReadBarrier);
6133 __ movl(out, Immediate(/* placeholder */ 0));
6134 codegen_->RecordStringPatch(load);
6135 return; // No dex cache slow path.
6136 }
6137 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
6138 DCHECK(!kEmitCompilerReadBarrier);
6139 Register method_address = locations->InAt(0).AsRegister<Register>();
6140 __ leal(out, Address(method_address, CodeGeneratorX86::kDummy32BitOffset));
6141 codegen_->RecordStringPatch(load);
6142 return; // No dex cache slow path.
6143 }
6144 case HLoadString::LoadKind::kBootImageAddress: {
6145 DCHECK(!kEmitCompilerReadBarrier);
6146 DCHECK_NE(load->GetAddress(), 0u);
6147 uint32_t address = dchecked_integral_cast<uint32_t>(load->GetAddress());
6148 __ movl(out, Immediate(address));
6149 codegen_->RecordSimplePatch();
6150 return; // No dex cache slow path.
6151 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006152 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07006153 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006154 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006155
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07006156 // TODO: Re-add the compiler code to do string dex cache lookup again.
Christina Wadsworth175d09b2016-08-31 16:26:01 -07006157 InvokeRuntimeCallingConvention calling_convention;
6158 __ movl(calling_convention.GetRegisterAt(0), Immediate(load->GetStringIndex()));
6159 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
6160 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006161}
6162
David Brazdilcb1c0552015-08-04 16:22:25 +01006163static Address GetExceptionTlsAddress() {
Andreas Gampe542451c2016-07-26 09:02:02 -07006164 return Address::Absolute(Thread::ExceptionOffset<kX86PointerSize>().Int32Value());
David Brazdilcb1c0552015-08-04 16:22:25 +01006165}
6166
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006167void LocationsBuilderX86::VisitLoadException(HLoadException* load) {
6168 LocationSummary* locations =
6169 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
6170 locations->SetOut(Location::RequiresRegister());
6171}
6172
6173void InstructionCodeGeneratorX86::VisitLoadException(HLoadException* load) {
David Brazdilcb1c0552015-08-04 16:22:25 +01006174 __ fs()->movl(load->GetLocations()->Out().AsRegister<Register>(), GetExceptionTlsAddress());
6175}
6176
6177void LocationsBuilderX86::VisitClearException(HClearException* clear) {
6178 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
6179}
6180
6181void InstructionCodeGeneratorX86::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
6182 __ fs()->movl(GetExceptionTlsAddress(), Immediate(0));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006183}
6184
6185void LocationsBuilderX86::VisitThrow(HThrow* instruction) {
6186 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006187 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006188 InvokeRuntimeCallingConvention calling_convention;
6189 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6190}
6191
6192void InstructionCodeGeneratorX86::VisitThrow(HThrow* instruction) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01006193 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006194 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006195}
6196
Roland Levillain7c1559a2015-12-15 10:55:36 +00006197static bool TypeCheckNeedsATemporary(TypeCheckKind type_check_kind) {
6198 return kEmitCompilerReadBarrier &&
Vladimir Marko953437b2016-08-24 08:30:46 +00006199 !kUseBakerReadBarrier &&
6200 (type_check_kind == TypeCheckKind::kAbstractClassCheck ||
Roland Levillain7c1559a2015-12-15 10:55:36 +00006201 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
6202 type_check_kind == TypeCheckKind::kArrayObjectCheck);
6203}
6204
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006205void LocationsBuilderX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006206 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain0d5a2812015-11-13 10:07:31 +00006207 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01006208 bool baker_read_barrier_slow_path = false;
Roland Levillain0d5a2812015-11-13 10:07:31 +00006209 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006210 case TypeCheckKind::kExactCheck:
6211 case TypeCheckKind::kAbstractClassCheck:
6212 case TypeCheckKind::kClassHierarchyCheck:
6213 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006214 call_kind =
6215 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Vladimir Marko70e97462016-08-09 11:04:26 +01006216 baker_read_barrier_slow_path = kUseBakerReadBarrier;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006217 break;
6218 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006219 case TypeCheckKind::kUnresolvedCheck:
6220 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006221 call_kind = LocationSummary::kCallOnSlowPath;
6222 break;
6223 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006224
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006225 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01006226 if (baker_read_barrier_slow_path) {
6227 locations->SetCustomSlowPathCallerSaves(RegisterSet()); // No caller-save registers.
6228 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006229 locations->SetInAt(0, Location::RequiresRegister());
6230 locations->SetInAt(1, Location::Any());
6231 // Note that TypeCheckSlowPathX86 uses this "out" register too.
6232 locations->SetOut(Location::RequiresRegister());
6233 // When read barriers are enabled, we need a temporary register for
6234 // some cases.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006235 if (TypeCheckNeedsATemporary(type_check_kind)) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006236 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006237 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006238}
6239
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006240void InstructionCodeGeneratorX86::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006241 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006242 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006243 Location obj_loc = locations->InAt(0);
6244 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006245 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006246 Location out_loc = locations->Out();
6247 Register out = out_loc.AsRegister<Register>();
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006248 Location maybe_temp_loc = TypeCheckNeedsATemporary(type_check_kind) ?
Roland Levillain7c1559a2015-12-15 10:55:36 +00006249 locations->GetTemp(0) :
6250 Location::NoLocation();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006251 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006252 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6253 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6254 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Andreas Gampe85b62f22015-09-09 13:15:38 -07006255 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006256 NearLabel done, zero;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006257
6258 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006259 // Avoid null check if we know obj is not null.
6260 if (instruction->MustDoNullCheck()) {
6261 __ testl(obj, obj);
6262 __ j(kEqual, &zero);
6263 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006264
Roland Levillain0d5a2812015-11-13 10:07:31 +00006265 // /* HeapReference<Class> */ out = obj->klass_
Vladimir Marko953437b2016-08-24 08:30:46 +00006266 GenerateReferenceLoadTwoRegisters(instruction, out_loc, obj_loc, class_offset);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006267
Roland Levillain7c1559a2015-12-15 10:55:36 +00006268 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006269 case TypeCheckKind::kExactCheck: {
6270 if (cls.IsRegister()) {
6271 __ cmpl(out, cls.AsRegister<Register>());
6272 } else {
6273 DCHECK(cls.IsStackSlot()) << cls;
6274 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6275 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006276
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006277 // Classes must be equal for the instanceof to succeed.
6278 __ j(kNotEqual, &zero);
6279 __ movl(out, Immediate(1));
6280 __ jmp(&done);
6281 break;
6282 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006283
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006284 case TypeCheckKind::kAbstractClassCheck: {
6285 // If the class is abstract, we eagerly fetch the super class of the
6286 // object to avoid doing a comparison we know will fail.
6287 NearLabel loop;
6288 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006289 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006290 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006291 __ testl(out, out);
6292 // If `out` is null, we use it for the result, and jump to `done`.
6293 __ j(kEqual, &done);
6294 if (cls.IsRegister()) {
6295 __ cmpl(out, cls.AsRegister<Register>());
6296 } else {
6297 DCHECK(cls.IsStackSlot()) << cls;
6298 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6299 }
6300 __ j(kNotEqual, &loop);
6301 __ movl(out, Immediate(1));
6302 if (zero.IsLinked()) {
6303 __ jmp(&done);
6304 }
6305 break;
6306 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006307
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006308 case TypeCheckKind::kClassHierarchyCheck: {
6309 // Walk over the class hierarchy to find a match.
6310 NearLabel loop, success;
6311 __ Bind(&loop);
6312 if (cls.IsRegister()) {
6313 __ cmpl(out, cls.AsRegister<Register>());
6314 } else {
6315 DCHECK(cls.IsStackSlot()) << cls;
6316 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6317 }
6318 __ j(kEqual, &success);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006319 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006320 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006321 __ testl(out, out);
6322 __ j(kNotEqual, &loop);
6323 // If `out` is null, we use it for the result, and jump to `done`.
6324 __ jmp(&done);
6325 __ Bind(&success);
6326 __ movl(out, Immediate(1));
6327 if (zero.IsLinked()) {
6328 __ jmp(&done);
6329 }
6330 break;
6331 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006332
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006333 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006334 // Do an exact check.
6335 NearLabel exact_check;
6336 if (cls.IsRegister()) {
6337 __ cmpl(out, cls.AsRegister<Register>());
6338 } else {
6339 DCHECK(cls.IsStackSlot()) << cls;
6340 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6341 }
6342 __ j(kEqual, &exact_check);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006343 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006344 // /* HeapReference<Class> */ out = out->component_type_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006345 GenerateReferenceLoadOneRegister(instruction, out_loc, component_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006346 __ testl(out, out);
6347 // If `out` is null, we use it for the result, and jump to `done`.
6348 __ j(kEqual, &done);
6349 __ cmpw(Address(out, primitive_offset), Immediate(Primitive::kPrimNot));
6350 __ j(kNotEqual, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006351 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006352 __ movl(out, Immediate(1));
6353 __ jmp(&done);
6354 break;
6355 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006356
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006357 case TypeCheckKind::kArrayCheck: {
6358 if (cls.IsRegister()) {
6359 __ cmpl(out, cls.AsRegister<Register>());
6360 } else {
6361 DCHECK(cls.IsStackSlot()) << cls;
6362 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6363 }
6364 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain0d5a2812015-11-13 10:07:31 +00006365 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
6366 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006367 codegen_->AddSlowPath(slow_path);
6368 __ j(kNotEqual, slow_path->GetEntryLabel());
6369 __ movl(out, Immediate(1));
6370 if (zero.IsLinked()) {
6371 __ jmp(&done);
6372 }
6373 break;
6374 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006375
Calin Juravle98893e12015-10-02 21:05:03 +01006376 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006377 case TypeCheckKind::kInterfaceCheck: {
6378 // Note that we indeed only call on slow path, but we always go
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006379 // into the slow path for the unresolved and interface check
Roland Levillain0d5a2812015-11-13 10:07:31 +00006380 // cases.
6381 //
6382 // We cannot directly call the InstanceofNonTrivial runtime
6383 // entry point without resorting to a type checking slow path
6384 // here (i.e. by calling InvokeRuntime directly), as it would
6385 // require to assign fixed registers for the inputs of this
6386 // HInstanceOf instruction (following the runtime calling
6387 // convention), which might be cluttered by the potential first
6388 // read barrier emission at the beginning of this method.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006389 //
6390 // TODO: Introduce a new runtime entry point taking the object
6391 // to test (instead of its class) as argument, and let it deal
6392 // with the read barrier issues. This will let us refactor this
6393 // case of the `switch` code as it was previously (with a direct
6394 // call to the runtime not using a type checking slow path).
6395 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006396 DCHECK(locations->OnlyCallsOnSlowPath());
6397 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
6398 /* is_fatal */ false);
6399 codegen_->AddSlowPath(slow_path);
6400 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006401 if (zero.IsLinked()) {
6402 __ jmp(&done);
6403 }
6404 break;
6405 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006406 }
6407
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006408 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006409 __ Bind(&zero);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006410 __ xorl(out, out);
6411 }
6412
6413 if (done.IsLinked()) {
6414 __ Bind(&done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006415 }
6416
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006417 if (slow_path != nullptr) {
6418 __ Bind(slow_path->GetExitLabel());
6419 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006420}
6421
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006422void LocationsBuilderX86::VisitCheckCast(HCheckCast* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006423 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
6424 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006425 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01006426 bool baker_read_barrier_slow_path = false;
Roland Levillain0d5a2812015-11-13 10:07:31 +00006427 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006428 case TypeCheckKind::kExactCheck:
6429 case TypeCheckKind::kAbstractClassCheck:
6430 case TypeCheckKind::kClassHierarchyCheck:
6431 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006432 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
6433 LocationSummary::kCallOnSlowPath :
6434 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Vladimir Marko70e97462016-08-09 11:04:26 +01006435 baker_read_barrier_slow_path = kUseBakerReadBarrier && !throws_into_catch;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006436 break;
6437 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006438 case TypeCheckKind::kUnresolvedCheck:
6439 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006440 call_kind = LocationSummary::kCallOnSlowPath;
6441 break;
6442 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006443 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01006444 if (baker_read_barrier_slow_path) {
6445 locations->SetCustomSlowPathCallerSaves(RegisterSet()); // No caller-save registers.
6446 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006447 locations->SetInAt(0, Location::RequiresRegister());
6448 locations->SetInAt(1, Location::Any());
6449 // Note that TypeCheckSlowPathX86 uses this "temp" register too.
6450 locations->AddTemp(Location::RequiresRegister());
6451 // When read barriers are enabled, we need an additional temporary
6452 // register for some cases.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006453 if (TypeCheckNeedsATemporary(type_check_kind)) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006454 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006455 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006456}
6457
6458void InstructionCodeGeneratorX86::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006459 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006460 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006461 Location obj_loc = locations->InAt(0);
6462 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006463 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006464 Location temp_loc = locations->GetTemp(0);
6465 Register temp = temp_loc.AsRegister<Register>();
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006466 Location maybe_temp2_loc = TypeCheckNeedsATemporary(type_check_kind) ?
Roland Levillain7c1559a2015-12-15 10:55:36 +00006467 locations->GetTemp(1) :
6468 Location::NoLocation();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006469 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
6470 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6471 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6472 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006473
Roland Levillain0d5a2812015-11-13 10:07:31 +00006474 bool is_type_check_slow_path_fatal =
6475 (type_check_kind == TypeCheckKind::kExactCheck ||
6476 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
6477 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
6478 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
6479 !instruction->CanThrowIntoCatchBlock();
6480 SlowPathCode* type_check_slow_path =
6481 new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
6482 is_type_check_slow_path_fatal);
6483 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006484
Roland Levillain0d5a2812015-11-13 10:07:31 +00006485 NearLabel done;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006486 // Avoid null check if we know obj is not null.
6487 if (instruction->MustDoNullCheck()) {
6488 __ testl(obj, obj);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006489 __ j(kEqual, &done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006490 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006491
Roland Levillain0d5a2812015-11-13 10:07:31 +00006492 // /* HeapReference<Class> */ temp = obj->klass_
Vladimir Marko953437b2016-08-24 08:30:46 +00006493 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006494
Roland Levillain0d5a2812015-11-13 10:07:31 +00006495 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006496 case TypeCheckKind::kExactCheck:
6497 case TypeCheckKind::kArrayCheck: {
6498 if (cls.IsRegister()) {
6499 __ cmpl(temp, cls.AsRegister<Register>());
6500 } else {
6501 DCHECK(cls.IsStackSlot()) << cls;
6502 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6503 }
6504 // Jump to slow path for throwing the exception or doing a
6505 // more involved array check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006506 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006507 break;
6508 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006509
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006510 case TypeCheckKind::kAbstractClassCheck: {
6511 // If the class is abstract, we eagerly fetch the super class of the
6512 // object to avoid doing a comparison we know will fail.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006513 NearLabel loop, compare_classes;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006514 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006515 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006516 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006517
6518 // If the class reference currently in `temp` is not null, jump
6519 // to the `compare_classes` label to compare it with the checked
6520 // class.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006521 __ testl(temp, temp);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006522 __ j(kNotEqual, &compare_classes);
6523 // Otherwise, jump to the slow path to throw the exception.
6524 //
6525 // But before, move back the object's class into `temp` before
6526 // going into the slow path, as it has been overwritten in the
6527 // meantime.
6528 // /* HeapReference<Class> */ temp = obj->klass_
Vladimir Marko953437b2016-08-24 08:30:46 +00006529 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006530 __ jmp(type_check_slow_path->GetEntryLabel());
6531
6532 __ Bind(&compare_classes);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006533 if (cls.IsRegister()) {
6534 __ cmpl(temp, cls.AsRegister<Register>());
6535 } else {
6536 DCHECK(cls.IsStackSlot()) << cls;
6537 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6538 }
6539 __ j(kNotEqual, &loop);
6540 break;
6541 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006542
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006543 case TypeCheckKind::kClassHierarchyCheck: {
6544 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006545 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006546 __ Bind(&loop);
6547 if (cls.IsRegister()) {
6548 __ cmpl(temp, cls.AsRegister<Register>());
6549 } else {
6550 DCHECK(cls.IsStackSlot()) << cls;
6551 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6552 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006553 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006554
Roland Levillain0d5a2812015-11-13 10:07:31 +00006555 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006556 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006557
6558 // If the class reference currently in `temp` is not null, jump
6559 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006560 __ testl(temp, temp);
6561 __ j(kNotEqual, &loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006562 // Otherwise, jump to the slow path to throw the exception.
6563 //
6564 // But before, move back the object's class into `temp` before
6565 // going into the slow path, as it has been overwritten in the
6566 // meantime.
6567 // /* HeapReference<Class> */ temp = obj->klass_
Vladimir Marko953437b2016-08-24 08:30:46 +00006568 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006569 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006570 break;
6571 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006572
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006573 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006574 // Do an exact check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006575 NearLabel check_non_primitive_component_type;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006576 if (cls.IsRegister()) {
6577 __ cmpl(temp, cls.AsRegister<Register>());
6578 } else {
6579 DCHECK(cls.IsStackSlot()) << cls;
6580 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6581 }
6582 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006583
6584 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006585 // /* HeapReference<Class> */ temp = temp->component_type_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006586 GenerateReferenceLoadOneRegister(instruction, temp_loc, component_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006587
6588 // If the component type is not null (i.e. the object is indeed
6589 // an array), jump to label `check_non_primitive_component_type`
6590 // to further check that this component type is not a primitive
6591 // type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006592 __ testl(temp, temp);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006593 __ j(kNotEqual, &check_non_primitive_component_type);
6594 // Otherwise, jump to the slow path to throw the exception.
6595 //
6596 // But before, move back the object's class into `temp` before
6597 // going into the slow path, as it has been overwritten in the
6598 // meantime.
6599 // /* HeapReference<Class> */ temp = obj->klass_
Vladimir Marko953437b2016-08-24 08:30:46 +00006600 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006601 __ jmp(type_check_slow_path->GetEntryLabel());
6602
6603 __ Bind(&check_non_primitive_component_type);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006604 __ cmpw(Address(temp, primitive_offset), Immediate(Primitive::kPrimNot));
Roland Levillain0d5a2812015-11-13 10:07:31 +00006605 __ j(kEqual, &done);
6606 // Same comment as above regarding `temp` and the slow path.
6607 // /* HeapReference<Class> */ temp = obj->klass_
Vladimir Marko953437b2016-08-24 08:30:46 +00006608 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006609 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006610 break;
6611 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006612
Calin Juravle98893e12015-10-02 21:05:03 +01006613 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006614 case TypeCheckKind::kInterfaceCheck:
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006615 // We always go into the type check slow path for the unresolved
6616 // and interface check cases.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006617 //
6618 // We cannot directly call the CheckCast runtime entry point
6619 // without resorting to a type checking slow path here (i.e. by
6620 // calling InvokeRuntime directly), as it would require to
6621 // assign fixed registers for the inputs of this HInstanceOf
6622 // instruction (following the runtime calling convention), which
6623 // might be cluttered by the potential first read barrier
6624 // emission at the beginning of this method.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006625 //
6626 // TODO: Introduce a new runtime entry point taking the object
6627 // to test (instead of its class) as argument, and let it deal
6628 // with the read barrier issues. This will let us refactor this
6629 // case of the `switch` code as it was previously (with a direct
6630 // call to the runtime not using a type checking slow path).
6631 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006632 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006633 break;
6634 }
6635 __ Bind(&done);
6636
Roland Levillain0d5a2812015-11-13 10:07:31 +00006637 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006638}
6639
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006640void LocationsBuilderX86::VisitMonitorOperation(HMonitorOperation* instruction) {
6641 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006642 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006643 InvokeRuntimeCallingConvention calling_convention;
6644 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6645}
6646
6647void InstructionCodeGeneratorX86::VisitMonitorOperation(HMonitorOperation* instruction) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01006648 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject
6649 : kQuickUnlockObject,
Alexandre Rames8158f282015-08-07 10:26:17 +01006650 instruction,
Serban Constantinescuba45db02016-07-12 22:53:02 +01006651 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006652 if (instruction->IsEnter()) {
6653 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
6654 } else {
6655 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
6656 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006657}
6658
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006659void LocationsBuilderX86::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
6660void LocationsBuilderX86::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
6661void LocationsBuilderX86::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
6662
6663void LocationsBuilderX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
6664 LocationSummary* locations =
6665 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6666 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
6667 || instruction->GetResultType() == Primitive::kPrimLong);
6668 locations->SetInAt(0, Location::RequiresRegister());
6669 locations->SetInAt(1, Location::Any());
6670 locations->SetOut(Location::SameAsFirstInput());
6671}
6672
6673void InstructionCodeGeneratorX86::VisitAnd(HAnd* instruction) {
6674 HandleBitwiseOperation(instruction);
6675}
6676
6677void InstructionCodeGeneratorX86::VisitOr(HOr* instruction) {
6678 HandleBitwiseOperation(instruction);
6679}
6680
6681void InstructionCodeGeneratorX86::VisitXor(HXor* instruction) {
6682 HandleBitwiseOperation(instruction);
6683}
6684
6685void InstructionCodeGeneratorX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
6686 LocationSummary* locations = instruction->GetLocations();
6687 Location first = locations->InAt(0);
6688 Location second = locations->InAt(1);
6689 DCHECK(first.Equals(locations->Out()));
6690
6691 if (instruction->GetResultType() == Primitive::kPrimInt) {
6692 if (second.IsRegister()) {
6693 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006694 __ andl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006695 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006696 __ orl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006697 } else {
6698 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006699 __ xorl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006700 }
6701 } else if (second.IsConstant()) {
6702 if (instruction->IsAnd()) {
Roland Levillain199f3362014-11-27 17:15:16 +00006703 __ andl(first.AsRegister<Register>(),
6704 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006705 } else if (instruction->IsOr()) {
Roland Levillain199f3362014-11-27 17:15:16 +00006706 __ orl(first.AsRegister<Register>(),
6707 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006708 } else {
6709 DCHECK(instruction->IsXor());
Roland Levillain199f3362014-11-27 17:15:16 +00006710 __ xorl(first.AsRegister<Register>(),
6711 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006712 }
6713 } else {
6714 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006715 __ andl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006716 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006717 __ orl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006718 } else {
6719 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006720 __ xorl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006721 }
6722 }
6723 } else {
6724 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
6725 if (second.IsRegisterPair()) {
6726 if (instruction->IsAnd()) {
6727 __ andl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
6728 __ andl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
6729 } else if (instruction->IsOr()) {
6730 __ orl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
6731 __ orl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
6732 } else {
6733 DCHECK(instruction->IsXor());
6734 __ xorl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
6735 __ xorl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
6736 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006737 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006738 if (instruction->IsAnd()) {
6739 __ andl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
6740 __ andl(first.AsRegisterPairHigh<Register>(),
6741 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
6742 } else if (instruction->IsOr()) {
6743 __ orl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
6744 __ orl(first.AsRegisterPairHigh<Register>(),
6745 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
6746 } else {
6747 DCHECK(instruction->IsXor());
6748 __ xorl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
6749 __ xorl(first.AsRegisterPairHigh<Register>(),
6750 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
6751 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006752 } else {
6753 DCHECK(second.IsConstant()) << second;
6754 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006755 int32_t low_value = Low32Bits(value);
6756 int32_t high_value = High32Bits(value);
6757 Immediate low(low_value);
6758 Immediate high(high_value);
6759 Register first_low = first.AsRegisterPairLow<Register>();
6760 Register first_high = first.AsRegisterPairHigh<Register>();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006761 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006762 if (low_value == 0) {
6763 __ xorl(first_low, first_low);
6764 } else if (low_value != -1) {
6765 __ andl(first_low, low);
6766 }
6767 if (high_value == 0) {
6768 __ xorl(first_high, first_high);
6769 } else if (high_value != -1) {
6770 __ andl(first_high, high);
6771 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006772 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006773 if (low_value != 0) {
6774 __ orl(first_low, low);
6775 }
6776 if (high_value != 0) {
6777 __ orl(first_high, high);
6778 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006779 } else {
6780 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006781 if (low_value != 0) {
6782 __ xorl(first_low, low);
6783 }
6784 if (high_value != 0) {
6785 __ xorl(first_high, high);
6786 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006787 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006788 }
6789 }
6790}
6791
Roland Levillain7c1559a2015-12-15 10:55:36 +00006792void InstructionCodeGeneratorX86::GenerateReferenceLoadOneRegister(HInstruction* instruction,
6793 Location out,
6794 uint32_t offset,
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006795 Location maybe_temp) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006796 Register out_reg = out.AsRegister<Register>();
6797 if (kEmitCompilerReadBarrier) {
6798 if (kUseBakerReadBarrier) {
6799 // Load with fast path based Baker's read barrier.
6800 // /* HeapReference<Object> */ out = *(out + offset)
6801 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00006802 instruction, out, out_reg, offset, /* needs_null_check */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006803 } else {
6804 // Load with slow path based read barrier.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006805 // Save the value of `out` into `maybe_temp` before overwriting it
Roland Levillain7c1559a2015-12-15 10:55:36 +00006806 // in the following move operation, as we will need it for the
6807 // read barrier below.
Vladimir Marko953437b2016-08-24 08:30:46 +00006808 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006809 __ movl(maybe_temp.AsRegister<Register>(), out_reg);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006810 // /* HeapReference<Object> */ out = *(out + offset)
6811 __ movl(out_reg, Address(out_reg, offset));
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006812 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006813 }
6814 } else {
6815 // Plain load with no read barrier.
6816 // /* HeapReference<Object> */ out = *(out + offset)
6817 __ movl(out_reg, Address(out_reg, offset));
6818 __ MaybeUnpoisonHeapReference(out_reg);
6819 }
6820}
6821
6822void InstructionCodeGeneratorX86::GenerateReferenceLoadTwoRegisters(HInstruction* instruction,
6823 Location out,
6824 Location obj,
Vladimir Marko953437b2016-08-24 08:30:46 +00006825 uint32_t offset) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006826 Register out_reg = out.AsRegister<Register>();
6827 Register obj_reg = obj.AsRegister<Register>();
6828 if (kEmitCompilerReadBarrier) {
6829 if (kUseBakerReadBarrier) {
6830 // Load with fast path based Baker's read barrier.
6831 // /* HeapReference<Object> */ out = *(obj + offset)
6832 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00006833 instruction, out, obj_reg, offset, /* needs_null_check */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006834 } else {
6835 // Load with slow path based read barrier.
6836 // /* HeapReference<Object> */ out = *(obj + offset)
6837 __ movl(out_reg, Address(obj_reg, offset));
6838 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
6839 }
6840 } else {
6841 // Plain load with no read barrier.
6842 // /* HeapReference<Object> */ out = *(obj + offset)
6843 __ movl(out_reg, Address(obj_reg, offset));
6844 __ MaybeUnpoisonHeapReference(out_reg);
6845 }
6846}
6847
6848void InstructionCodeGeneratorX86::GenerateGcRootFieldLoad(HInstruction* instruction,
6849 Location root,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006850 const Address& address,
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006851 Label* fixup_label,
6852 bool requires_read_barrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006853 Register root_reg = root.AsRegister<Register>();
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006854 if (requires_read_barrier) {
6855 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006856 if (kUseBakerReadBarrier) {
6857 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
6858 // Baker's read barrier are used:
6859 //
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006860 // root = *address;
Roland Levillain7c1559a2015-12-15 10:55:36 +00006861 // if (Thread::Current()->GetIsGcMarking()) {
6862 // root = ReadBarrier::Mark(root)
6863 // }
6864
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006865 // /* GcRoot<mirror::Object> */ root = *address
6866 __ movl(root_reg, address);
6867 if (fixup_label != nullptr) {
6868 __ Bind(fixup_label);
6869 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00006870 static_assert(
6871 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
6872 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
6873 "have different sizes.");
6874 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
6875 "art::mirror::CompressedReference<mirror::Object> and int32_t "
6876 "have different sizes.");
6877
Vladimir Marko953437b2016-08-24 08:30:46 +00006878 // Slow path marking the GC root `root`.
6879 SlowPathCode* slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathX86(
6880 instruction, root, /* unpoison */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006881 codegen_->AddSlowPath(slow_path);
6882
Andreas Gampe542451c2016-07-26 09:02:02 -07006883 __ fs()->cmpl(Address::Absolute(Thread::IsGcMarkingOffset<kX86PointerSize>().Int32Value()),
Roland Levillain7c1559a2015-12-15 10:55:36 +00006884 Immediate(0));
6885 __ j(kNotEqual, slow_path->GetEntryLabel());
6886 __ Bind(slow_path->GetExitLabel());
6887 } else {
6888 // GC root loaded through a slow path for read barriers other
6889 // than Baker's.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006890 // /* GcRoot<mirror::Object>* */ root = address
6891 __ leal(root_reg, address);
6892 if (fixup_label != nullptr) {
6893 __ Bind(fixup_label);
6894 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00006895 // /* mirror::Object* */ root = root->Read()
6896 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
6897 }
6898 } else {
6899 // Plain GC root load with no read barrier.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006900 // /* GcRoot<mirror::Object> */ root = *address
6901 __ movl(root_reg, address);
6902 if (fixup_label != nullptr) {
6903 __ Bind(fixup_label);
6904 }
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006905 // Note that GC roots are not affected by heap poisoning, thus we
6906 // do not have to unpoison `root_reg` here.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006907 }
6908}
6909
6910void CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
6911 Location ref,
6912 Register obj,
6913 uint32_t offset,
Roland Levillain7c1559a2015-12-15 10:55:36 +00006914 bool needs_null_check) {
6915 DCHECK(kEmitCompilerReadBarrier);
6916 DCHECK(kUseBakerReadBarrier);
6917
6918 // /* HeapReference<Object> */ ref = *(obj + offset)
6919 Address src(obj, offset);
Vladimir Marko953437b2016-08-24 08:30:46 +00006920 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, needs_null_check);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006921}
6922
6923void CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
6924 Location ref,
6925 Register obj,
6926 uint32_t data_offset,
6927 Location index,
Roland Levillain7c1559a2015-12-15 10:55:36 +00006928 bool needs_null_check) {
6929 DCHECK(kEmitCompilerReadBarrier);
6930 DCHECK(kUseBakerReadBarrier);
6931
Roland Levillain3d312422016-06-23 13:53:42 +01006932 static_assert(
6933 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
6934 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain7c1559a2015-12-15 10:55:36 +00006935 // /* HeapReference<Object> */ ref =
6936 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
6937 Address src = index.IsConstant() ?
6938 Address(obj, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset) :
6939 Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset);
Vladimir Marko953437b2016-08-24 08:30:46 +00006940 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, needs_null_check);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006941}
6942
6943void CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
6944 Location ref,
6945 Register obj,
6946 const Address& src,
Roland Levillain7c1559a2015-12-15 10:55:36 +00006947 bool needs_null_check) {
6948 DCHECK(kEmitCompilerReadBarrier);
6949 DCHECK(kUseBakerReadBarrier);
6950
6951 // In slow path based read barriers, the read barrier call is
6952 // inserted after the original load. However, in fast path based
6953 // Baker's read barriers, we need to perform the load of
6954 // mirror::Object::monitor_ *before* the original reference load.
6955 // This load-load ordering is required by the read barrier.
6956 // The fast path/slow path (for Baker's algorithm) should look like:
6957 //
6958 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
6959 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
6960 // HeapReference<Object> ref = *src; // Original reference load.
6961 // bool is_gray = (rb_state == ReadBarrier::gray_ptr_);
6962 // if (is_gray) {
6963 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
6964 // }
6965 //
6966 // Note: the original implementation in ReadBarrier::Barrier is
6967 // slightly more complex as:
6968 // - it implements the load-load fence using a data dependency on
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006969 // the high-bits of rb_state, which are expected to be all zeroes
6970 // (we use CodeGeneratorX86::GenerateMemoryBarrier instead here,
6971 // which is a no-op thanks to the x86 memory model);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006972 // - it performs additional checks that we do not do here for
6973 // performance reasons.
6974
6975 Register ref_reg = ref.AsRegister<Register>();
Roland Levillain7c1559a2015-12-15 10:55:36 +00006976 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
6977
Vladimir Marko953437b2016-08-24 08:30:46 +00006978 // Given the numeric representation, it's enough to check the low bit of the rb_state.
6979 static_assert(ReadBarrier::white_ptr_ == 0, "Expecting white to have value 0");
6980 static_assert(ReadBarrier::gray_ptr_ == 1, "Expecting gray to have value 1");
6981 static_assert(ReadBarrier::black_ptr_ == 2, "Expecting black to have value 2");
6982 constexpr uint32_t gray_byte_position = LockWord::kReadBarrierStateShift / kBitsPerByte;
6983 constexpr uint32_t gray_bit_position = LockWord::kReadBarrierStateShift % kBitsPerByte;
6984 constexpr int32_t test_value = static_cast<int8_t>(1 << gray_bit_position);
6985
6986 // if (rb_state == ReadBarrier::gray_ptr_)
6987 // ref = ReadBarrier::Mark(ref);
6988 // At this point, just do the "if" and make sure that flags are preserved until the branch.
6989 __ testb(Address(obj, monitor_offset + gray_byte_position), Immediate(test_value));
Roland Levillain7c1559a2015-12-15 10:55:36 +00006990 if (needs_null_check) {
6991 MaybeRecordImplicitNullCheck(instruction);
6992 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00006993
6994 // Load fence to prevent load-load reordering.
6995 // Note that this is a no-op, thanks to the x86 memory model.
6996 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
6997
6998 // The actual reference load.
6999 // /* HeapReference<Object> */ ref = *src
Vladimir Marko953437b2016-08-24 08:30:46 +00007000 __ movl(ref_reg, src); // Flags are unaffected.
7001
7002 // Note: Reference unpoisoning modifies the flags, so we need to delay it after the branch.
7003 // Slow path marking the object `ref` when it is gray.
7004 SlowPathCode* slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathX86(
7005 instruction, ref, /* unpoison */ true);
7006 AddSlowPath(slow_path);
7007
7008 // We have done the "if" of the gray bit check above, now branch based on the flags.
7009 __ j(kNotZero, slow_path->GetEntryLabel());
Roland Levillain7c1559a2015-12-15 10:55:36 +00007010
7011 // Object* ref = ref_addr->AsMirrorPtr()
7012 __ MaybeUnpoisonHeapReference(ref_reg);
7013
Roland Levillain7c1559a2015-12-15 10:55:36 +00007014 __ Bind(slow_path->GetExitLabel());
7015}
7016
7017void CodeGeneratorX86::GenerateReadBarrierSlow(HInstruction* instruction,
7018 Location out,
7019 Location ref,
7020 Location obj,
7021 uint32_t offset,
7022 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007023 DCHECK(kEmitCompilerReadBarrier);
7024
Roland Levillain7c1559a2015-12-15 10:55:36 +00007025 // Insert a slow path based read barrier *after* the reference load.
7026 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00007027 // If heap poisoning is enabled, the unpoisoning of the loaded
7028 // reference will be carried out by the runtime within the slow
7029 // path.
7030 //
7031 // Note that `ref` currently does not get unpoisoned (when heap
7032 // poisoning is enabled), which is alright as the `ref` argument is
7033 // not used by the artReadBarrierSlow entry point.
7034 //
7035 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
7036 SlowPathCode* slow_path = new (GetGraph()->GetArena())
7037 ReadBarrierForHeapReferenceSlowPathX86(instruction, out, ref, obj, offset, index);
7038 AddSlowPath(slow_path);
7039
Roland Levillain0d5a2812015-11-13 10:07:31 +00007040 __ jmp(slow_path->GetEntryLabel());
7041 __ Bind(slow_path->GetExitLabel());
7042}
7043
Roland Levillain7c1559a2015-12-15 10:55:36 +00007044void CodeGeneratorX86::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
7045 Location out,
7046 Location ref,
7047 Location obj,
7048 uint32_t offset,
7049 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007050 if (kEmitCompilerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007051 // Baker's read barriers shall be handled by the fast path
7052 // (CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier).
7053 DCHECK(!kUseBakerReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007054 // If heap poisoning is enabled, unpoisoning will be taken care of
7055 // by the runtime within the slow path.
Roland Levillain7c1559a2015-12-15 10:55:36 +00007056 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007057 } else if (kPoisonHeapReferences) {
7058 __ UnpoisonHeapReference(out.AsRegister<Register>());
7059 }
7060}
7061
Roland Levillain7c1559a2015-12-15 10:55:36 +00007062void CodeGeneratorX86::GenerateReadBarrierForRootSlow(HInstruction* instruction,
7063 Location out,
7064 Location root) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007065 DCHECK(kEmitCompilerReadBarrier);
7066
Roland Levillain7c1559a2015-12-15 10:55:36 +00007067 // Insert a slow path based read barrier *after* the GC root load.
7068 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00007069 // Note that GC roots are not affected by heap poisoning, so we do
7070 // not need to do anything special for this here.
7071 SlowPathCode* slow_path =
7072 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathX86(instruction, out, root);
7073 AddSlowPath(slow_path);
7074
Roland Levillain0d5a2812015-11-13 10:07:31 +00007075 __ jmp(slow_path->GetEntryLabel());
7076 __ Bind(slow_path->GetExitLabel());
7077}
7078
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007079void LocationsBuilderX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00007080 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00007081 LOG(FATAL) << "Unreachable";
7082}
7083
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007084void InstructionCodeGeneratorX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00007085 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00007086 LOG(FATAL) << "Unreachable";
7087}
7088
Mark Mendellfe57faa2015-09-18 09:26:15 -04007089// Simple implementation of packed switch - generate cascaded compare/jumps.
7090void LocationsBuilderX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7091 LocationSummary* locations =
7092 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
7093 locations->SetInAt(0, Location::RequiresRegister());
7094}
7095
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007096void InstructionCodeGeneratorX86::GenPackedSwitchWithCompares(Register value_reg,
7097 int32_t lower_bound,
7098 uint32_t num_entries,
7099 HBasicBlock* switch_block,
7100 HBasicBlock* default_block) {
7101 // Figure out the correct compare values and jump conditions.
7102 // Handle the first compare/branch as a special case because it might
7103 // jump to the default case.
7104 DCHECK_GT(num_entries, 2u);
7105 Condition first_condition;
7106 uint32_t index;
7107 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
7108 if (lower_bound != 0) {
7109 first_condition = kLess;
7110 __ cmpl(value_reg, Immediate(lower_bound));
7111 __ j(first_condition, codegen_->GetLabelOf(default_block));
7112 __ j(kEqual, codegen_->GetLabelOf(successors[0]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007113
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007114 index = 1;
7115 } else {
7116 // Handle all the compare/jumps below.
7117 first_condition = kBelow;
7118 index = 0;
7119 }
7120
7121 // Handle the rest of the compare/jumps.
7122 for (; index + 1 < num_entries; index += 2) {
7123 int32_t compare_to_value = lower_bound + index + 1;
7124 __ cmpl(value_reg, Immediate(compare_to_value));
7125 // Jump to successors[index] if value < case_value[index].
7126 __ j(first_condition, codegen_->GetLabelOf(successors[index]));
7127 // Jump to successors[index + 1] if value == case_value[index + 1].
7128 __ j(kEqual, codegen_->GetLabelOf(successors[index + 1]));
7129 }
7130
7131 if (index != num_entries) {
7132 // There are an odd number of entries. Handle the last one.
7133 DCHECK_EQ(index + 1, num_entries);
7134 __ cmpl(value_reg, Immediate(lower_bound + index));
7135 __ j(kEqual, codegen_->GetLabelOf(successors[index]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007136 }
7137
7138 // And the default for any other value.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007139 if (!codegen_->GoesToNextBlock(switch_block, default_block)) {
7140 __ jmp(codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007141 }
7142}
7143
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007144void InstructionCodeGeneratorX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7145 int32_t lower_bound = switch_instr->GetStartValue();
7146 uint32_t num_entries = switch_instr->GetNumEntries();
7147 LocationSummary* locations = switch_instr->GetLocations();
7148 Register value_reg = locations->InAt(0).AsRegister<Register>();
7149
7150 GenPackedSwitchWithCompares(value_reg,
7151 lower_bound,
7152 num_entries,
7153 switch_instr->GetBlock(),
7154 switch_instr->GetDefaultBlock());
7155}
7156
Mark Mendell805b3b52015-09-18 14:10:29 -04007157void LocationsBuilderX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
7158 LocationSummary* locations =
7159 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
7160 locations->SetInAt(0, Location::RequiresRegister());
7161
7162 // Constant area pointer.
7163 locations->SetInAt(1, Location::RequiresRegister());
7164
7165 // And the temporary we need.
7166 locations->AddTemp(Location::RequiresRegister());
7167}
7168
7169void InstructionCodeGeneratorX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
7170 int32_t lower_bound = switch_instr->GetStartValue();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007171 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendell805b3b52015-09-18 14:10:29 -04007172 LocationSummary* locations = switch_instr->GetLocations();
7173 Register value_reg = locations->InAt(0).AsRegister<Register>();
7174 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
7175
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007176 if (num_entries <= kPackedSwitchJumpTableThreshold) {
7177 GenPackedSwitchWithCompares(value_reg,
7178 lower_bound,
7179 num_entries,
7180 switch_instr->GetBlock(),
7181 default_block);
7182 return;
7183 }
7184
Mark Mendell805b3b52015-09-18 14:10:29 -04007185 // Optimizing has a jump area.
7186 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
7187 Register constant_area = locations->InAt(1).AsRegister<Register>();
7188
7189 // Remove the bias, if needed.
7190 if (lower_bound != 0) {
7191 __ leal(temp_reg, Address(value_reg, -lower_bound));
7192 value_reg = temp_reg;
7193 }
7194
7195 // Is the value in range?
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007196 DCHECK_GE(num_entries, 1u);
Mark Mendell805b3b52015-09-18 14:10:29 -04007197 __ cmpl(value_reg, Immediate(num_entries - 1));
7198 __ j(kAbove, codegen_->GetLabelOf(default_block));
7199
7200 // We are in the range of the table.
7201 // Load (target-constant_area) from the jump table, indexing by the value.
7202 __ movl(temp_reg, codegen_->LiteralCaseTable(switch_instr, constant_area, value_reg));
7203
7204 // Compute the actual target address by adding in constant_area.
7205 __ addl(temp_reg, constant_area);
7206
7207 // And jump.
7208 __ jmp(temp_reg);
7209}
7210
Mark Mendell0616ae02015-04-17 12:49:27 -04007211void LocationsBuilderX86::VisitX86ComputeBaseMethodAddress(
7212 HX86ComputeBaseMethodAddress* insn) {
7213 LocationSummary* locations =
7214 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
7215 locations->SetOut(Location::RequiresRegister());
7216}
7217
7218void InstructionCodeGeneratorX86::VisitX86ComputeBaseMethodAddress(
7219 HX86ComputeBaseMethodAddress* insn) {
7220 LocationSummary* locations = insn->GetLocations();
7221 Register reg = locations->Out().AsRegister<Register>();
7222
7223 // Generate call to next instruction.
7224 Label next_instruction;
7225 __ call(&next_instruction);
7226 __ Bind(&next_instruction);
7227
7228 // Remember this offset for later use with constant area.
7229 codegen_->SetMethodAddressOffset(GetAssembler()->CodeSize());
7230
7231 // Grab the return address off the stack.
7232 __ popl(reg);
7233}
7234
7235void LocationsBuilderX86::VisitX86LoadFromConstantTable(
7236 HX86LoadFromConstantTable* insn) {
7237 LocationSummary* locations =
7238 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
7239
7240 locations->SetInAt(0, Location::RequiresRegister());
7241 locations->SetInAt(1, Location::ConstantLocation(insn->GetConstant()));
7242
7243 // If we don't need to be materialized, we only need the inputs to be set.
David Brazdilb3e773e2016-01-26 11:28:37 +00007244 if (insn->IsEmittedAtUseSite()) {
Mark Mendell0616ae02015-04-17 12:49:27 -04007245 return;
7246 }
7247
7248 switch (insn->GetType()) {
7249 case Primitive::kPrimFloat:
7250 case Primitive::kPrimDouble:
7251 locations->SetOut(Location::RequiresFpuRegister());
7252 break;
7253
7254 case Primitive::kPrimInt:
7255 locations->SetOut(Location::RequiresRegister());
7256 break;
7257
7258 default:
7259 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
7260 }
7261}
7262
7263void InstructionCodeGeneratorX86::VisitX86LoadFromConstantTable(HX86LoadFromConstantTable* insn) {
David Brazdilb3e773e2016-01-26 11:28:37 +00007264 if (insn->IsEmittedAtUseSite()) {
Mark Mendell0616ae02015-04-17 12:49:27 -04007265 return;
7266 }
7267
7268 LocationSummary* locations = insn->GetLocations();
7269 Location out = locations->Out();
7270 Register const_area = locations->InAt(0).AsRegister<Register>();
7271 HConstant *value = insn->GetConstant();
7272
7273 switch (insn->GetType()) {
7274 case Primitive::kPrimFloat:
7275 __ movss(out.AsFpuRegister<XmmRegister>(),
7276 codegen_->LiteralFloatAddress(value->AsFloatConstant()->GetValue(), const_area));
7277 break;
7278
7279 case Primitive::kPrimDouble:
7280 __ movsd(out.AsFpuRegister<XmmRegister>(),
7281 codegen_->LiteralDoubleAddress(value->AsDoubleConstant()->GetValue(), const_area));
7282 break;
7283
7284 case Primitive::kPrimInt:
7285 __ movl(out.AsRegister<Register>(),
7286 codegen_->LiteralInt32Address(value->AsIntConstant()->GetValue(), const_area));
7287 break;
7288
7289 default:
7290 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
7291 }
7292}
7293
Mark Mendell0616ae02015-04-17 12:49:27 -04007294/**
7295 * Class to handle late fixup of offsets into constant area.
7296 */
Vladimir Marko5233f932015-09-29 19:01:15 +01007297class RIPFixup : public AssemblerFixup, public ArenaObject<kArenaAllocCodeGenerator> {
Mark Mendell0616ae02015-04-17 12:49:27 -04007298 public:
Mark Mendell805b3b52015-09-18 14:10:29 -04007299 RIPFixup(CodeGeneratorX86& codegen, size_t offset)
7300 : codegen_(&codegen), offset_into_constant_area_(offset) {}
7301
7302 protected:
7303 void SetOffset(size_t offset) { offset_into_constant_area_ = offset; }
7304
7305 CodeGeneratorX86* codegen_;
Mark Mendell0616ae02015-04-17 12:49:27 -04007306
7307 private:
7308 void Process(const MemoryRegion& region, int pos) OVERRIDE {
7309 // Patch the correct offset for the instruction. The place to patch is the
7310 // last 4 bytes of the instruction.
7311 // The value to patch is the distance from the offset in the constant area
7312 // from the address computed by the HX86ComputeBaseMethodAddress instruction.
Mark Mendell805b3b52015-09-18 14:10:29 -04007313 int32_t constant_offset = codegen_->ConstantAreaStart() + offset_into_constant_area_;
7314 int32_t relative_position = constant_offset - codegen_->GetMethodAddressOffset();;
Mark Mendell0616ae02015-04-17 12:49:27 -04007315
7316 // Patch in the right value.
7317 region.StoreUnaligned<int32_t>(pos - 4, relative_position);
7318 }
7319
Mark Mendell0616ae02015-04-17 12:49:27 -04007320 // Location in constant area that the fixup refers to.
Mark Mendell805b3b52015-09-18 14:10:29 -04007321 int32_t offset_into_constant_area_;
Mark Mendell0616ae02015-04-17 12:49:27 -04007322};
7323
Mark Mendell805b3b52015-09-18 14:10:29 -04007324/**
7325 * Class to handle late fixup of offsets to a jump table that will be created in the
7326 * constant area.
7327 */
7328class JumpTableRIPFixup : public RIPFixup {
7329 public:
7330 JumpTableRIPFixup(CodeGeneratorX86& codegen, HX86PackedSwitch* switch_instr)
7331 : RIPFixup(codegen, static_cast<size_t>(-1)), switch_instr_(switch_instr) {}
7332
7333 void CreateJumpTable() {
7334 X86Assembler* assembler = codegen_->GetAssembler();
7335
7336 // Ensure that the reference to the jump table has the correct offset.
7337 const int32_t offset_in_constant_table = assembler->ConstantAreaSize();
7338 SetOffset(offset_in_constant_table);
7339
7340 // The label values in the jump table are computed relative to the
7341 // instruction addressing the constant area.
7342 const int32_t relative_offset = codegen_->GetMethodAddressOffset();
7343
7344 // Populate the jump table with the correct values for the jump table.
7345 int32_t num_entries = switch_instr_->GetNumEntries();
7346 HBasicBlock* block = switch_instr_->GetBlock();
7347 const ArenaVector<HBasicBlock*>& successors = block->GetSuccessors();
7348 // The value that we want is the target offset - the position of the table.
7349 for (int32_t i = 0; i < num_entries; i++) {
7350 HBasicBlock* b = successors[i];
7351 Label* l = codegen_->GetLabelOf(b);
7352 DCHECK(l->IsBound());
7353 int32_t offset_to_block = l->Position() - relative_offset;
7354 assembler->AppendInt32(offset_to_block);
7355 }
7356 }
7357
7358 private:
7359 const HX86PackedSwitch* switch_instr_;
7360};
7361
7362void CodeGeneratorX86::Finalize(CodeAllocator* allocator) {
7363 // Generate the constant area if needed.
7364 X86Assembler* assembler = GetAssembler();
7365 if (!assembler->IsConstantAreaEmpty() || !fixups_to_jump_tables_.empty()) {
7366 // Align to 4 byte boundary to reduce cache misses, as the data is 4 and 8
7367 // byte values.
7368 assembler->Align(4, 0);
7369 constant_area_start_ = assembler->CodeSize();
7370
7371 // Populate any jump tables.
7372 for (auto jump_table : fixups_to_jump_tables_) {
7373 jump_table->CreateJumpTable();
7374 }
7375
7376 // And now add the constant area to the generated code.
7377 assembler->AddConstantArea();
7378 }
7379
7380 // And finish up.
7381 CodeGenerator::Finalize(allocator);
7382}
7383
Mark Mendell0616ae02015-04-17 12:49:27 -04007384Address CodeGeneratorX86::LiteralDoubleAddress(double v, Register reg) {
7385 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddDouble(v));
7386 return Address(reg, kDummy32BitOffset, fixup);
7387}
7388
7389Address CodeGeneratorX86::LiteralFloatAddress(float v, Register reg) {
7390 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddFloat(v));
7391 return Address(reg, kDummy32BitOffset, fixup);
7392}
7393
7394Address CodeGeneratorX86::LiteralInt32Address(int32_t v, Register reg) {
7395 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt32(v));
7396 return Address(reg, kDummy32BitOffset, fixup);
7397}
7398
7399Address CodeGeneratorX86::LiteralInt64Address(int64_t v, Register reg) {
7400 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt64(v));
7401 return Address(reg, kDummy32BitOffset, fixup);
7402}
7403
Aart Bika19616e2016-02-01 18:57:58 -08007404void CodeGeneratorX86::Load32BitValue(Register dest, int32_t value) {
7405 if (value == 0) {
7406 __ xorl(dest, dest);
7407 } else {
7408 __ movl(dest, Immediate(value));
7409 }
7410}
7411
7412void CodeGeneratorX86::Compare32BitValue(Register dest, int32_t value) {
7413 if (value == 0) {
7414 __ testl(dest, dest);
7415 } else {
7416 __ cmpl(dest, Immediate(value));
7417 }
7418}
7419
Mark Mendell805b3b52015-09-18 14:10:29 -04007420Address CodeGeneratorX86::LiteralCaseTable(HX86PackedSwitch* switch_instr,
7421 Register reg,
7422 Register value) {
7423 // Create a fixup to be used to create and address the jump table.
7424 JumpTableRIPFixup* table_fixup =
7425 new (GetGraph()->GetArena()) JumpTableRIPFixup(*this, switch_instr);
7426
7427 // We have to populate the jump tables.
7428 fixups_to_jump_tables_.push_back(table_fixup);
7429
7430 // We want a scaled address, as we are extracting the correct offset from the table.
7431 return Address(reg, value, TIMES_4, kDummy32BitOffset, table_fixup);
7432}
7433
Andreas Gampe85b62f22015-09-09 13:15:38 -07007434// TODO: target as memory.
7435void CodeGeneratorX86::MoveFromReturnRegister(Location target, Primitive::Type type) {
7436 if (!target.IsValid()) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007437 DCHECK_EQ(type, Primitive::kPrimVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07007438 return;
7439 }
7440
7441 DCHECK_NE(type, Primitive::kPrimVoid);
7442
7443 Location return_loc = InvokeDexCallingConventionVisitorX86().GetReturnLocation(type);
7444 if (target.Equals(return_loc)) {
7445 return;
7446 }
7447
7448 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
7449 // with the else branch.
7450 if (type == Primitive::kPrimLong) {
7451 HParallelMove parallel_move(GetGraph()->GetArena());
7452 parallel_move.AddMove(return_loc.ToLow(), target.ToLow(), Primitive::kPrimInt, nullptr);
7453 parallel_move.AddMove(return_loc.ToHigh(), target.ToHigh(), Primitive::kPrimInt, nullptr);
7454 GetMoveResolver()->EmitNativeCode(&parallel_move);
7455 } else {
7456 // Let the parallel move resolver take care of all of this.
7457 HParallelMove parallel_move(GetGraph()->GetArena());
7458 parallel_move.AddMove(return_loc, target, type, nullptr);
7459 GetMoveResolver()->EmitNativeCode(&parallel_move);
7460 }
7461}
7462
Roland Levillain4d027112015-07-01 15:41:14 +01007463#undef __
7464
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00007465} // namespace x86
7466} // namespace art