blob: 7aca16f8675887c4c46dc207d71a89f74049899d [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 }
Alexandre Rames8158f282015-08-07 10:26:17 +010065 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pThrowNullPointer),
66 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 }
Alexandre Rames8158f282015-08-07 10:26:17 +010091 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pThrowDivZero),
92 instruction_,
93 instruction_->GetDexPc(),
94 this);
Roland Levillain888d0672015-11-23 18:53:50 +000095 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
Calin Juravled0d48522014-11-04 16:40:20 +000096 }
97
Alexandre Rames8158f282015-08-07 10:26:17 +010098 bool IsFatal() const OVERRIDE { return true; }
99
Alexandre Rames9931f312015-06-19 14:47:01 +0100100 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathX86"; }
101
Calin Juravled0d48522014-11-04 16:40:20 +0000102 private:
Calin Juravled0d48522014-11-04 16:40:20 +0000103 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathX86);
104};
105
Andreas Gampe85b62f22015-09-09 13:15:38 -0700106class DivRemMinusOneSlowPathX86 : public SlowPathCode {
Calin Juravled0d48522014-11-04 16:40:20 +0000107 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000108 DivRemMinusOneSlowPathX86(HInstruction* instruction, Register reg, bool is_div)
109 : SlowPathCode(instruction), reg_(reg), is_div_(is_div) {}
Calin Juravled0d48522014-11-04 16:40:20 +0000110
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000111 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +0000112 __ Bind(GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +0000113 if (is_div_) {
114 __ negl(reg_);
115 } else {
116 __ movl(reg_, Immediate(0));
117 }
Calin Juravled0d48522014-11-04 16:40:20 +0000118 __ jmp(GetExitLabel());
119 }
120
Alexandre Rames9931f312015-06-19 14:47:01 +0100121 const char* GetDescription() const OVERRIDE { return "DivRemMinusOneSlowPathX86"; }
122
Calin Juravled0d48522014-11-04 16:40:20 +0000123 private:
124 Register reg_;
Calin Juravlebacfec32014-11-14 15:54:36 +0000125 bool is_div_;
126 DISALLOW_COPY_AND_ASSIGN(DivRemMinusOneSlowPathX86);
Calin Juravled0d48522014-11-04 16:40:20 +0000127};
128
Andreas Gampe85b62f22015-09-09 13:15:38 -0700129class BoundsCheckSlowPathX86 : public SlowPathCode {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100130 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000131 explicit BoundsCheckSlowPathX86(HBoundsCheck* instruction) : SlowPathCode(instruction) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100132
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000133 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100134 LocationSummary* locations = instruction_->GetLocations();
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100135 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100136 __ Bind(GetEntryLabel());
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000137 // We're moving two locations to locations that could overlap, so we need a parallel
138 // move resolver.
David Brazdil77a48ae2015-09-15 12:34:04 +0000139 if (instruction_->CanThrowIntoCatchBlock()) {
140 // Live registers will be restored in the catch block if caught.
141 SaveLiveRegisters(codegen, instruction_->GetLocations());
142 }
Mark Mendellee8d9712016-07-12 11:13:15 -0400143
144 // Are we using an array length from memory?
145 HInstruction* array_length = instruction_->InputAt(1);
146 Location length_loc = locations->InAt(1);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100147 InvokeRuntimeCallingConvention calling_convention;
Mark Mendellee8d9712016-07-12 11:13:15 -0400148 if (array_length->IsArrayLength() && array_length->IsEmittedAtUseSite()) {
149 // Load the array length into our temporary.
150 uint32_t len_offset = CodeGenerator::GetArrayLengthOffset(array_length->AsArrayLength());
151 Location array_loc = array_length->GetLocations()->InAt(0);
152 Address array_len(array_loc.AsRegister<Register>(), len_offset);
153 length_loc = Location::RegisterLocation(calling_convention.GetRegisterAt(1));
154 // Check for conflicts with index.
155 if (length_loc.Equals(locations->InAt(0))) {
156 // We know we aren't using parameter 2.
157 length_loc = Location::RegisterLocation(calling_convention.GetRegisterAt(2));
158 }
159 __ movl(length_loc.AsRegister<Register>(), array_len);
160 }
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000161 x86_codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100162 locations->InAt(0),
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000163 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100164 Primitive::kPrimInt,
Mark Mendellee8d9712016-07-12 11:13:15 -0400165 length_loc,
Nicolas Geoffray90218252015-04-15 11:56:51 +0100166 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
167 Primitive::kPrimInt);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100168 uint32_t entry_point_offset = instruction_->AsBoundsCheck()->IsStringCharAt()
169 ? QUICK_ENTRY_POINT(pThrowStringBounds)
170 : QUICK_ENTRY_POINT(pThrowArrayBounds);
171 x86_codegen->InvokeRuntime(entry_point_offset,
Alexandre Rames8158f282015-08-07 10:26:17 +0100172 instruction_,
173 instruction_->GetDexPc(),
174 this);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100175 CheckEntrypointTypes<kQuickThrowStringBounds, void, int32_t, int32_t>();
Roland Levillain888d0672015-11-23 18:53:50 +0000176 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100177 }
178
Alexandre Rames8158f282015-08-07 10:26:17 +0100179 bool IsFatal() const OVERRIDE { return true; }
180
Alexandre Rames9931f312015-06-19 14:47:01 +0100181 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathX86"; }
182
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100183 private:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100184 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathX86);
185};
186
Andreas Gampe85b62f22015-09-09 13:15:38 -0700187class SuspendCheckSlowPathX86 : public SlowPathCode {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000188 public:
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000189 SuspendCheckSlowPathX86(HSuspendCheck* instruction, HBasicBlock* successor)
David Srbecky9cd6d372016-02-09 15:24:47 +0000190 : SlowPathCode(instruction), successor_(successor) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000191
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000192 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100193 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000194 __ Bind(GetEntryLabel());
Alexandre Rames8158f282015-08-07 10:26:17 +0100195 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pTestSuspend),
196 instruction_,
197 instruction_->GetDexPc(),
198 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000199 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100200 if (successor_ == nullptr) {
201 __ jmp(GetReturnLabel());
202 } else {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100203 __ jmp(x86_codegen->GetLabelOf(successor_));
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100204 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000205 }
206
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100207 Label* GetReturnLabel() {
208 DCHECK(successor_ == nullptr);
209 return &return_label_;
210 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000211
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100212 HBasicBlock* GetSuccessor() const {
213 return successor_;
214 }
215
Alexandre Rames9931f312015-06-19 14:47:01 +0100216 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathX86"; }
217
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000218 private:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100219 HBasicBlock* const successor_;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000220 Label return_label_;
221
222 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathX86);
223};
224
Andreas Gampe85b62f22015-09-09 13:15:38 -0700225class LoadStringSlowPathX86 : public SlowPathCode {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000226 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000227 explicit LoadStringSlowPathX86(HLoadString* instruction): SlowPathCode(instruction) {}
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000228
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000229 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000230 LocationSummary* locations = instruction_->GetLocations();
231 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
232
233 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
234 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000235 SaveLiveRegisters(codegen, locations);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000236
237 InvokeRuntimeCallingConvention calling_convention;
David Srbecky9cd6d372016-02-09 15:24:47 +0000238 const uint32_t string_index = instruction_->AsLoadString()->GetStringIndex();
239 __ movl(calling_convention.GetRegisterAt(0), Immediate(string_index));
Alexandre Rames8158f282015-08-07 10:26:17 +0100240 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pResolveString),
241 instruction_,
242 instruction_->GetDexPc(),
243 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000244 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000245 x86_codegen->Move32(locations->Out(), Location::RegisterLocation(EAX));
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000246 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000247
248 __ jmp(GetExitLabel());
249 }
250
Alexandre Rames9931f312015-06-19 14:47:01 +0100251 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathX86"; }
252
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000253 private:
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000254 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathX86);
255};
256
Andreas Gampe85b62f22015-09-09 13:15:38 -0700257class LoadClassSlowPathX86 : public SlowPathCode {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000258 public:
259 LoadClassSlowPathX86(HLoadClass* cls,
260 HInstruction* at,
261 uint32_t dex_pc,
262 bool do_clinit)
David Srbecky9cd6d372016-02-09 15:24:47 +0000263 : SlowPathCode(at), cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000264 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
265 }
266
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000267 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000268 LocationSummary* locations = at_->GetLocations();
269 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
270 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000271 SaveLiveRegisters(codegen, locations);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000272
273 InvokeRuntimeCallingConvention calling_convention;
274 __ movl(calling_convention.GetRegisterAt(0), Immediate(cls_->GetTypeIndex()));
Alexandre Rames8158f282015-08-07 10:26:17 +0100275 x86_codegen->InvokeRuntime(do_clinit_ ? QUICK_ENTRY_POINT(pInitializeStaticStorage)
276 : QUICK_ENTRY_POINT(pInitializeType),
277 at_, dex_pc_, this);
Roland Levillain888d0672015-11-23 18:53:50 +0000278 if (do_clinit_) {
279 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
280 } else {
281 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
282 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000283
284 // Move the class to the desired location.
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000285 Location out = locations->Out();
286 if (out.IsValid()) {
287 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
288 x86_codegen->Move32(out, Location::RegisterLocation(EAX));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000289 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000290
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000291 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000292 __ jmp(GetExitLabel());
293 }
294
Alexandre Rames9931f312015-06-19 14:47:01 +0100295 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathX86"; }
296
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000297 private:
298 // The class this slow path will load.
299 HLoadClass* const cls_;
300
301 // The instruction where this slow path is happening.
302 // (Might be the load class or an initialization check).
303 HInstruction* const at_;
304
305 // The dex PC of `at_`.
306 const uint32_t dex_pc_;
307
308 // Whether to initialize the class.
309 const bool do_clinit_;
310
311 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathX86);
312};
313
Andreas Gampe85b62f22015-09-09 13:15:38 -0700314class TypeCheckSlowPathX86 : public SlowPathCode {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000315 public:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000316 TypeCheckSlowPathX86(HInstruction* instruction, bool is_fatal)
David Srbecky9cd6d372016-02-09 15:24:47 +0000317 : SlowPathCode(instruction), is_fatal_(is_fatal) {}
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000318
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000319 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000320 LocationSummary* locations = instruction_->GetLocations();
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100321 Location object_class = instruction_->IsCheckCast() ? locations->GetTemp(0)
322 : locations->Out();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000323 DCHECK(instruction_->IsCheckCast()
324 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000325
326 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
327 __ Bind(GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000328
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000329 if (!is_fatal_) {
330 SaveLiveRegisters(codegen, locations);
331 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000332
333 // We're moving two locations to locations that could overlap, so we need a parallel
334 // move resolver.
335 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000336 x86_codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100337 locations->InAt(1),
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000338 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100339 Primitive::kPrimNot,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100340 object_class,
Nicolas Geoffray90218252015-04-15 11:56:51 +0100341 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
342 Primitive::kPrimNot);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000343
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000344 if (instruction_->IsInstanceOf()) {
Alexandre Rames8158f282015-08-07 10:26:17 +0100345 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pInstanceofNonTrivial),
346 instruction_,
347 instruction_->GetDexPc(),
348 this);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000349 CheckEntrypointTypes<
Andreas Gampe67409972016-07-19 22:34:53 -0700350 kQuickInstanceofNonTrivial, size_t, const mirror::Class*, const mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000351 } else {
352 DCHECK(instruction_->IsCheckCast());
Alexandre Rames8158f282015-08-07 10:26:17 +0100353 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pCheckCast),
354 instruction_,
355 instruction_->GetDexPc(),
356 this);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000357 CheckEntrypointTypes<kQuickCheckCast, void, const mirror::Class*, const mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000358 }
359
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000360 if (!is_fatal_) {
361 if (instruction_->IsInstanceOf()) {
362 x86_codegen->Move32(locations->Out(), Location::RegisterLocation(EAX));
363 }
364 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray75374372015-09-17 17:12:19 +0000365
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000366 __ jmp(GetExitLabel());
367 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000368 }
369
Alexandre Rames9931f312015-06-19 14:47:01 +0100370 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathX86"; }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000371 bool IsFatal() const OVERRIDE { return is_fatal_; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100372
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000373 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000374 const bool is_fatal_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000375
376 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathX86);
377};
378
Andreas Gampe85b62f22015-09-09 13:15:38 -0700379class DeoptimizationSlowPathX86 : public SlowPathCode {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700380 public:
Aart Bik42249c32016-01-07 15:33:50 -0800381 explicit DeoptimizationSlowPathX86(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000382 : SlowPathCode(instruction) {}
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700383
384 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames8158f282015-08-07 10:26:17 +0100385 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700386 __ Bind(GetEntryLabel());
387 SaveLiveRegisters(codegen, instruction_->GetLocations());
Alexandre Rames8158f282015-08-07 10:26:17 +0100388 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pDeoptimize),
389 instruction_,
390 instruction_->GetDexPc(),
391 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000392 CheckEntrypointTypes<kQuickDeoptimize, void, void>();
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700393 }
394
Alexandre Rames9931f312015-06-19 14:47:01 +0100395 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathX86"; }
396
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700397 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700398 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathX86);
399};
400
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100401class ArraySetSlowPathX86 : public SlowPathCode {
402 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000403 explicit ArraySetSlowPathX86(HInstruction* instruction) : SlowPathCode(instruction) {}
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100404
405 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
406 LocationSummary* locations = instruction_->GetLocations();
407 __ Bind(GetEntryLabel());
408 SaveLiveRegisters(codegen, locations);
409
410 InvokeRuntimeCallingConvention calling_convention;
411 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
412 parallel_move.AddMove(
413 locations->InAt(0),
414 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
415 Primitive::kPrimNot,
416 nullptr);
417 parallel_move.AddMove(
418 locations->InAt(1),
419 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
420 Primitive::kPrimInt,
421 nullptr);
422 parallel_move.AddMove(
423 locations->InAt(2),
424 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
425 Primitive::kPrimNot,
426 nullptr);
427 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
428
429 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
430 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pAputObject),
431 instruction_,
432 instruction_->GetDexPc(),
433 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000434 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100435 RestoreLiveRegisters(codegen, locations);
436 __ jmp(GetExitLabel());
437 }
438
439 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathX86"; }
440
441 private:
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100442 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathX86);
443};
444
Roland Levillain7c1559a2015-12-15 10:55:36 +0000445// Slow path marking an object during a read barrier.
446class ReadBarrierMarkSlowPathX86 : public SlowPathCode {
447 public:
Vladimir Markoccf15bc2016-08-23 17:48:38 +0000448 ReadBarrierMarkSlowPathX86(HInstruction* instruction, Location obj)
449 : SlowPathCode(instruction), obj_(obj) {
Roland Levillain7c1559a2015-12-15 10:55:36 +0000450 DCHECK(kEmitCompilerReadBarrier);
451 }
452
453 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathX86"; }
454
455 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
456 LocationSummary* locations = instruction_->GetLocations();
Roland Levillain02b75802016-07-13 11:54:35 +0100457 Register reg = obj_.AsRegister<Register>();
Roland Levillain7c1559a2015-12-15 10:55:36 +0000458 DCHECK(locations->CanCall());
Roland Levillain02b75802016-07-13 11:54:35 +0100459 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg));
Roland Levillain7c1559a2015-12-15 10:55:36 +0000460 DCHECK(instruction_->IsInstanceFieldGet() ||
461 instruction_->IsStaticFieldGet() ||
462 instruction_->IsArrayGet() ||
463 instruction_->IsLoadClass() ||
464 instruction_->IsLoadString() ||
465 instruction_->IsInstanceOf() ||
Roland Levillain3d312422016-06-23 13:53:42 +0100466 instruction_->IsCheckCast() ||
Roland Levillain0b671c02016-08-19 12:02:34 +0100467 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()) ||
468 (instruction_->IsInvokeStaticOrDirect() && instruction_->GetLocations()->Intrinsified()))
Roland Levillain7c1559a2015-12-15 10:55:36 +0000469 << "Unexpected instruction in read barrier marking slow path: "
470 << instruction_->DebugName();
471
472 __ Bind(GetEntryLabel());
Roland Levillain4359e612016-07-20 11:32:19 +0100473 // No need to save live registers; it's taken care of by the
474 // entrypoint. Also, there is no need to update the stack mask,
475 // as this runtime call will not trigger a garbage collection.
Roland Levillain7c1559a2015-12-15 10:55:36 +0000476 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Roland Levillain02b75802016-07-13 11:54:35 +0100477 DCHECK_NE(reg, ESP);
478 DCHECK(0 <= reg && reg < kNumberOfCpuRegisters) << reg;
479 // "Compact" slow path, saving two moves.
480 //
481 // Instead of using the standard runtime calling convention (input
482 // and output in EAX):
483 //
484 // EAX <- obj
485 // EAX <- ReadBarrierMark(EAX)
486 // obj <- EAX
487 //
488 // we just use rX (the register holding `obj`) as input and output
489 // of a dedicated entrypoint:
490 //
491 // rX <- ReadBarrierMarkRegX(rX)
492 //
493 int32_t entry_point_offset =
Andreas Gampe542451c2016-07-26 09:02:02 -0700494 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kX86PointerSize>(reg);
Roland Levillaindec8f632016-07-22 17:10:06 +0100495 // This runtime call does not require a stack map.
496 x86_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
Roland Levillain7c1559a2015-12-15 10:55:36 +0000497 __ jmp(GetExitLabel());
498 }
499
500 private:
Roland Levillain7c1559a2015-12-15 10:55:36 +0000501 const Location obj_;
502
503 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathX86);
504};
505
Roland Levillain0d5a2812015-11-13 10:07:31 +0000506// Slow path generating a read barrier for a heap reference.
507class ReadBarrierForHeapReferenceSlowPathX86 : public SlowPathCode {
508 public:
509 ReadBarrierForHeapReferenceSlowPathX86(HInstruction* instruction,
510 Location out,
511 Location ref,
512 Location obj,
513 uint32_t offset,
514 Location index)
David Srbecky9cd6d372016-02-09 15:24:47 +0000515 : SlowPathCode(instruction),
Roland Levillain0d5a2812015-11-13 10:07:31 +0000516 out_(out),
517 ref_(ref),
518 obj_(obj),
519 offset_(offset),
520 index_(index) {
521 DCHECK(kEmitCompilerReadBarrier);
522 // If `obj` is equal to `out` or `ref`, it means the initial object
523 // has been overwritten by (or after) the heap object reference load
524 // to be instrumented, e.g.:
525 //
526 // __ movl(out, Address(out, offset));
Roland Levillain7c1559a2015-12-15 10:55:36 +0000527 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000528 //
529 // In that case, we have lost the information about the original
530 // object, and the emitted read barrier cannot work properly.
531 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
532 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
533 }
534
535 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
536 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
537 LocationSummary* locations = instruction_->GetLocations();
538 Register reg_out = out_.AsRegister<Register>();
539 DCHECK(locations->CanCall());
540 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
Roland Levillain3d312422016-06-23 13:53:42 +0100541 DCHECK(instruction_->IsInstanceFieldGet() ||
542 instruction_->IsStaticFieldGet() ||
543 instruction_->IsArrayGet() ||
544 instruction_->IsInstanceOf() ||
545 instruction_->IsCheckCast() ||
Roland Levillaindec8f632016-07-22 17:10:06 +0100546 (instruction_->IsInvokeVirtual()) && instruction_->GetLocations()->Intrinsified())
Roland Levillain7c1559a2015-12-15 10:55:36 +0000547 << "Unexpected instruction in read barrier for heap reference slow path: "
548 << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000549
550 __ Bind(GetEntryLabel());
551 SaveLiveRegisters(codegen, locations);
552
553 // We may have to change the index's value, but as `index_` is a
554 // constant member (like other "inputs" of this slow path),
555 // introduce a copy of it, `index`.
556 Location index = index_;
557 if (index_.IsValid()) {
Roland Levillain3d312422016-06-23 13:53:42 +0100558 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
Roland Levillain0d5a2812015-11-13 10:07:31 +0000559 if (instruction_->IsArrayGet()) {
560 // Compute the actual memory offset and store it in `index`.
561 Register index_reg = index_.AsRegister<Register>();
562 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg));
563 if (codegen->IsCoreCalleeSaveRegister(index_reg)) {
564 // We are about to change the value of `index_reg` (see the
565 // calls to art::x86::X86Assembler::shll and
566 // art::x86::X86Assembler::AddImmediate below), but it has
567 // not been saved by the previous call to
568 // art::SlowPathCode::SaveLiveRegisters, as it is a
569 // callee-save register --
570 // art::SlowPathCode::SaveLiveRegisters does not consider
571 // callee-save registers, as it has been designed with the
572 // assumption that callee-save registers are supposed to be
573 // handled by the called function. So, as a callee-save
574 // register, `index_reg` _would_ eventually be saved onto
575 // the stack, but it would be too late: we would have
576 // changed its value earlier. Therefore, we manually save
577 // it here into another freely available register,
578 // `free_reg`, chosen of course among the caller-save
579 // registers (as a callee-save `free_reg` register would
580 // exhibit the same problem).
581 //
582 // Note we could have requested a temporary register from
583 // the register allocator instead; but we prefer not to, as
584 // this is a slow path, and we know we can find a
585 // caller-save register that is available.
586 Register free_reg = FindAvailableCallerSaveRegister(codegen);
587 __ movl(free_reg, index_reg);
588 index_reg = free_reg;
589 index = Location::RegisterLocation(index_reg);
590 } else {
591 // The initial register stored in `index_` has already been
592 // saved in the call to art::SlowPathCode::SaveLiveRegisters
593 // (as it is not a callee-save register), so we can freely
594 // use it.
595 }
596 // Shifting the index value contained in `index_reg` by the scale
597 // factor (2) cannot overflow in practice, as the runtime is
598 // unable to allocate object arrays with a size larger than
599 // 2^26 - 1 (that is, 2^28 - 4 bytes).
600 __ shll(index_reg, Immediate(TIMES_4));
601 static_assert(
602 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
603 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
604 __ AddImmediate(index_reg, Immediate(offset_));
605 } else {
Roland Levillain3d312422016-06-23 13:53:42 +0100606 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
607 // intrinsics, `index_` is not shifted by a scale factor of 2
608 // (as in the case of ArrayGet), as it is actually an offset
609 // to an object field within an object.
610 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000611 DCHECK(instruction_->GetLocations()->Intrinsified());
612 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
613 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
614 << instruction_->AsInvoke()->GetIntrinsic();
615 DCHECK_EQ(offset_, 0U);
616 DCHECK(index_.IsRegisterPair());
617 // UnsafeGet's offset location is a register pair, the low
618 // part contains the correct offset.
619 index = index_.ToLow();
620 }
621 }
622
623 // We're moving two or three locations to locations that could
624 // overlap, so we need a parallel move resolver.
625 InvokeRuntimeCallingConvention calling_convention;
626 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
627 parallel_move.AddMove(ref_,
628 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
629 Primitive::kPrimNot,
630 nullptr);
631 parallel_move.AddMove(obj_,
632 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
633 Primitive::kPrimNot,
634 nullptr);
635 if (index.IsValid()) {
636 parallel_move.AddMove(index,
637 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
638 Primitive::kPrimInt,
639 nullptr);
640 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
641 } else {
642 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
643 __ movl(calling_convention.GetRegisterAt(2), Immediate(offset_));
644 }
645 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pReadBarrierSlow),
646 instruction_,
647 instruction_->GetDexPc(),
648 this);
649 CheckEntrypointTypes<
650 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
651 x86_codegen->Move32(out_, Location::RegisterLocation(EAX));
652
653 RestoreLiveRegisters(codegen, locations);
654 __ jmp(GetExitLabel());
655 }
656
657 const char* GetDescription() const OVERRIDE { return "ReadBarrierForHeapReferenceSlowPathX86"; }
658
659 private:
660 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
661 size_t ref = static_cast<int>(ref_.AsRegister<Register>());
662 size_t obj = static_cast<int>(obj_.AsRegister<Register>());
663 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
664 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
665 return static_cast<Register>(i);
666 }
667 }
668 // We shall never fail to find a free caller-save register, as
669 // there are more than two core caller-save registers on x86
670 // (meaning it is possible to find one which is different from
671 // `ref` and `obj`).
672 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
673 LOG(FATAL) << "Could not find a free caller-save register";
674 UNREACHABLE();
675 }
676
Roland Levillain0d5a2812015-11-13 10:07:31 +0000677 const Location out_;
678 const Location ref_;
679 const Location obj_;
680 const uint32_t offset_;
681 // An additional location containing an index to an array.
682 // Only used for HArrayGet and the UnsafeGetObject &
683 // UnsafeGetObjectVolatile intrinsics.
684 const Location index_;
685
686 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathX86);
687};
688
689// Slow path generating a read barrier for a GC root.
690class ReadBarrierForRootSlowPathX86 : public SlowPathCode {
691 public:
692 ReadBarrierForRootSlowPathX86(HInstruction* instruction, Location out, Location root)
David Srbecky9cd6d372016-02-09 15:24:47 +0000693 : SlowPathCode(instruction), out_(out), root_(root) {
Roland Levillain7c1559a2015-12-15 10:55:36 +0000694 DCHECK(kEmitCompilerReadBarrier);
695 }
Roland Levillain0d5a2812015-11-13 10:07:31 +0000696
697 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
698 LocationSummary* locations = instruction_->GetLocations();
699 Register reg_out = out_.AsRegister<Register>();
700 DCHECK(locations->CanCall());
701 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
Roland Levillain7c1559a2015-12-15 10:55:36 +0000702 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
703 << "Unexpected instruction in read barrier for GC root slow path: "
704 << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000705
706 __ Bind(GetEntryLabel());
707 SaveLiveRegisters(codegen, locations);
708
709 InvokeRuntimeCallingConvention calling_convention;
710 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
711 x86_codegen->Move32(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), root_);
712 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pReadBarrierForRootSlow),
713 instruction_,
714 instruction_->GetDexPc(),
715 this);
716 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
717 x86_codegen->Move32(out_, Location::RegisterLocation(EAX));
718
719 RestoreLiveRegisters(codegen, locations);
720 __ jmp(GetExitLabel());
721 }
722
723 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathX86"; }
724
725 private:
Roland Levillain0d5a2812015-11-13 10:07:31 +0000726 const Location out_;
727 const Location root_;
728
729 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathX86);
730};
731
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100732#undef __
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100733// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
734#define __ down_cast<X86Assembler*>(GetAssembler())-> // NOLINT
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100735
Aart Bike9f37602015-10-09 11:15:55 -0700736inline Condition X86Condition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -0700737 switch (cond) {
738 case kCondEQ: return kEqual;
739 case kCondNE: return kNotEqual;
740 case kCondLT: return kLess;
741 case kCondLE: return kLessEqual;
742 case kCondGT: return kGreater;
743 case kCondGE: return kGreaterEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700744 case kCondB: return kBelow;
745 case kCondBE: return kBelowEqual;
746 case kCondA: return kAbove;
747 case kCondAE: return kAboveEqual;
Dave Allison20dfc792014-06-16 20:44:29 -0700748 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100749 LOG(FATAL) << "Unreachable";
750 UNREACHABLE();
751}
752
Aart Bike9f37602015-10-09 11:15:55 -0700753// Maps signed condition to unsigned condition and FP condition to x86 name.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100754inline Condition X86UnsignedOrFPCondition(IfCondition cond) {
755 switch (cond) {
756 case kCondEQ: return kEqual;
757 case kCondNE: return kNotEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700758 // Signed to unsigned, and FP to x86 name.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100759 case kCondLT: return kBelow;
760 case kCondLE: return kBelowEqual;
761 case kCondGT: return kAbove;
762 case kCondGE: return kAboveEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700763 // Unsigned remain unchanged.
764 case kCondB: return kBelow;
765 case kCondBE: return kBelowEqual;
766 case kCondA: return kAbove;
767 case kCondAE: return kAboveEqual;
Roland Levillain4fa13f62015-07-06 18:11:54 +0100768 }
769 LOG(FATAL) << "Unreachable";
770 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -0700771}
772
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100773void CodeGeneratorX86::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100774 stream << Register(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100775}
776
777void CodeGeneratorX86::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100778 stream << XmmRegister(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100779}
780
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100781size_t CodeGeneratorX86::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
782 __ movl(Address(ESP, stack_index), static_cast<Register>(reg_id));
783 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100784}
785
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100786size_t CodeGeneratorX86::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
787 __ movl(static_cast<Register>(reg_id), Address(ESP, stack_index));
788 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100789}
790
Mark Mendell7c8d0092015-01-26 11:21:33 -0500791size_t CodeGeneratorX86::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
792 __ movsd(Address(ESP, stack_index), XmmRegister(reg_id));
793 return GetFloatingPointSpillSlotSize();
794}
795
796size_t CodeGeneratorX86::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
797 __ movsd(XmmRegister(reg_id), Address(ESP, stack_index));
798 return GetFloatingPointSpillSlotSize();
799}
800
Calin Juravle175dc732015-08-25 15:42:32 +0100801void CodeGeneratorX86::InvokeRuntime(QuickEntrypointEnum entrypoint,
802 HInstruction* instruction,
803 uint32_t dex_pc,
804 SlowPathCode* slow_path) {
Andreas Gampe542451c2016-07-26 09:02:02 -0700805 InvokeRuntime(GetThreadOffset<kX86PointerSize>(entrypoint).Int32Value(),
Calin Juravle175dc732015-08-25 15:42:32 +0100806 instruction,
807 dex_pc,
808 slow_path);
809}
810
811void CodeGeneratorX86::InvokeRuntime(int32_t entry_point_offset,
Alexandre Rames8158f282015-08-07 10:26:17 +0100812 HInstruction* instruction,
813 uint32_t dex_pc,
814 SlowPathCode* slow_path) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +0100815 ValidateInvokeRuntime(instruction, slow_path);
Calin Juravle175dc732015-08-25 15:42:32 +0100816 __ fs()->call(Address::Absolute(entry_point_offset));
Alexandre Rames8158f282015-08-07 10:26:17 +0100817 RecordPcInfo(instruction, dex_pc, slow_path);
Alexandre Rames8158f282015-08-07 10:26:17 +0100818}
819
Roland Levillaindec8f632016-07-22 17:10:06 +0100820void CodeGeneratorX86::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
821 HInstruction* instruction,
822 SlowPathCode* slow_path) {
823 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
824 __ fs()->call(Address::Absolute(entry_point_offset));
825}
826
Mark Mendellfb8d2792015-03-31 22:16:59 -0400827CodeGeneratorX86::CodeGeneratorX86(HGraph* graph,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000828 const X86InstructionSetFeatures& isa_features,
829 const CompilerOptions& compiler_options,
830 OptimizingCompilerStats* stats)
Mark Mendell5f874182015-03-04 15:42:45 -0500831 : CodeGenerator(graph,
832 kNumberOfCpuRegisters,
833 kNumberOfXmmRegisters,
834 kNumberOfRegisterPairs,
835 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
836 arraysize(kCoreCalleeSaves))
837 | (1 << kFakeReturnRegister),
Serban Constantinescuecc43662015-08-13 13:33:12 +0100838 0,
839 compiler_options,
840 stats),
Vladimir Marko225b6462015-09-28 12:17:40 +0100841 block_labels_(nullptr),
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100842 location_builder_(graph, this),
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100843 instruction_visitor_(graph, this),
Mark Mendellfb8d2792015-03-31 22:16:59 -0400844 move_resolver_(graph->GetArena(), this),
Vladimir Marko93205e32016-04-13 11:59:46 +0100845 assembler_(graph->GetArena()),
Vladimir Marko58155012015-08-19 12:49:41 +0000846 isa_features_(isa_features),
Vladimir Marko5233f932015-09-29 19:01:15 +0100847 method_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Mark Mendell805b3b52015-09-18 14:10:29 -0400848 relative_call_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000849 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000850 simple_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
851 string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100852 type_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko93205e32016-04-13 11:59:46 +0100853 constant_area_start_(-1),
854 fixups_to_jump_tables_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
855 method_address_offset_(-1) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000856 // Use a fake return address register to mimic Quick.
857 AddAllocatedRegister(Location::RegisterLocation(kFakeReturnRegister));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100858}
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100859
David Brazdil58282f42016-01-14 12:45:10 +0000860void CodeGeneratorX86::SetupBlockedRegisters() const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100861 // Don't allocate the dalvik style register pair passing.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100862 blocked_register_pairs_[ECX_EDX] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100863
864 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100865 blocked_core_registers_[ESP] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100866
Calin Juravle34bacdf2014-10-07 20:23:36 +0100867 UpdateBlockedPairRegisters();
868}
869
870void CodeGeneratorX86::UpdateBlockedPairRegisters() const {
871 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
872 X86ManagedRegister current =
873 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
874 if (blocked_core_registers_[current.AsRegisterPairLow()]
875 || blocked_core_registers_[current.AsRegisterPairHigh()]) {
876 blocked_register_pairs_[i] = true;
877 }
878 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100879}
880
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100881InstructionCodeGeneratorX86::InstructionCodeGeneratorX86(HGraph* graph, CodeGeneratorX86* codegen)
Aart Bik42249c32016-01-07 15:33:50 -0800882 : InstructionCodeGenerator(graph, codegen),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100883 assembler_(codegen->GetAssembler()),
884 codegen_(codegen) {}
885
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100886static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +0100887 return dwarf::Reg::X86Core(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100888}
889
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000890void CodeGeneratorX86::GenerateFrameEntry() {
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100891 __ cfi().SetCurrentCFAOffset(kX86WordSize); // return address
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000892 __ Bind(&frame_entry_label_);
Roland Levillain199f3362014-11-27 17:15:16 +0000893 bool skip_overflow_check =
894 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000895 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Calin Juravle93edf732015-01-20 20:14:07 +0000896
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000897 if (!skip_overflow_check) {
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100898 __ testl(EAX, Address(ESP, -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86))));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100899 RecordPcInfo(nullptr, 0);
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100900 }
901
Mark Mendell5f874182015-03-04 15:42:45 -0500902 if (HasEmptyFrame()) {
903 return;
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000904 }
Mark Mendell5f874182015-03-04 15:42:45 -0500905
906 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
907 Register reg = kCoreCalleeSaves[i];
908 if (allocated_registers_.ContainsCoreRegister(reg)) {
909 __ pushl(reg);
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100910 __ cfi().AdjustCFAOffset(kX86WordSize);
911 __ cfi().RelOffset(DWARFReg(reg), 0);
Mark Mendell5f874182015-03-04 15:42:45 -0500912 }
913 }
914
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100915 int adjust = GetFrameSize() - FrameEntrySpillSize();
916 __ subl(ESP, Immediate(adjust));
917 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100918 __ movl(Address(ESP, kCurrentMethodStackOffset), kMethodRegisterArgument);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000919}
920
921void CodeGeneratorX86::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +0100922 __ cfi().RememberState();
923 if (!HasEmptyFrame()) {
924 int adjust = GetFrameSize() - FrameEntrySpillSize();
925 __ addl(ESP, Immediate(adjust));
926 __ cfi().AdjustCFAOffset(-adjust);
Mark Mendell5f874182015-03-04 15:42:45 -0500927
David Srbeckyc34dc932015-04-12 09:27:43 +0100928 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
929 Register reg = kCoreCalleeSaves[i];
930 if (allocated_registers_.ContainsCoreRegister(reg)) {
931 __ popl(reg);
932 __ cfi().AdjustCFAOffset(-static_cast<int>(kX86WordSize));
933 __ cfi().Restore(DWARFReg(reg));
934 }
Mark Mendell5f874182015-03-04 15:42:45 -0500935 }
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000936 }
David Srbeckyc34dc932015-04-12 09:27:43 +0100937 __ ret();
938 __ cfi().RestoreState();
939 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000940}
941
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100942void CodeGeneratorX86::Bind(HBasicBlock* block) {
943 __ Bind(GetLabelOf(block));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000944}
945
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100946Location InvokeDexCallingConventionVisitorX86::GetReturnLocation(Primitive::Type type) const {
947 switch (type) {
948 case Primitive::kPrimBoolean:
949 case Primitive::kPrimByte:
950 case Primitive::kPrimChar:
951 case Primitive::kPrimShort:
952 case Primitive::kPrimInt:
953 case Primitive::kPrimNot:
954 return Location::RegisterLocation(EAX);
955
956 case Primitive::kPrimLong:
957 return Location::RegisterPairLocation(EAX, EDX);
958
959 case Primitive::kPrimVoid:
960 return Location::NoLocation();
961
962 case Primitive::kPrimDouble:
963 case Primitive::kPrimFloat:
964 return Location::FpuRegisterLocation(XMM0);
965 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +0100966
967 UNREACHABLE();
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100968}
969
970Location InvokeDexCallingConventionVisitorX86::GetMethodLocation() const {
971 return Location::RegisterLocation(kMethodRegisterArgument);
972}
973
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100974Location InvokeDexCallingConventionVisitorX86::GetNextLocation(Primitive::Type type) {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100975 switch (type) {
976 case Primitive::kPrimBoolean:
977 case Primitive::kPrimByte:
978 case Primitive::kPrimChar:
979 case Primitive::kPrimShort:
980 case Primitive::kPrimInt:
981 case Primitive::kPrimNot: {
982 uint32_t index = gp_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000983 stack_index_++;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100984 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100985 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100986 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000987 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100988 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100989 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100990
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000991 case Primitive::kPrimLong: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100992 uint32_t index = gp_index_;
993 gp_index_ += 2;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000994 stack_index_ += 2;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100995 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100996 X86ManagedRegister pair = X86ManagedRegister::FromRegisterPair(
997 calling_convention.GetRegisterPairAt(index));
998 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100999 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001000 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
1001 }
1002 }
1003
1004 case Primitive::kPrimFloat: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001005 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001006 stack_index_++;
1007 if (index < calling_convention.GetNumberOfFpuRegisters()) {
1008 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
1009 } else {
1010 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
1011 }
1012 }
1013
1014 case Primitive::kPrimDouble: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001015 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001016 stack_index_ += 2;
1017 if (index < calling_convention.GetNumberOfFpuRegisters()) {
1018 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
1019 } else {
1020 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001021 }
1022 }
1023
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001024 case Primitive::kPrimVoid:
1025 LOG(FATAL) << "Unexpected parameter type " << type;
1026 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001027 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00001028 return Location::NoLocation();
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001029}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001030
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001031void CodeGeneratorX86::Move32(Location destination, Location source) {
1032 if (source.Equals(destination)) {
1033 return;
1034 }
1035 if (destination.IsRegister()) {
1036 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001037 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001038 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001039 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001040 } else {
1041 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001042 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001043 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001044 } else if (destination.IsFpuRegister()) {
1045 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001046 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001047 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001048 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001049 } else {
1050 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001051 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001052 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001053 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001054 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001055 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001056 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001057 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001058 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Mark Mendell7c8d0092015-01-26 11:21:33 -05001059 } else if (source.IsConstant()) {
1060 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001061 int32_t value = GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05001062 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001063 } else {
1064 DCHECK(source.IsStackSlot());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001065 __ pushl(Address(ESP, source.GetStackIndex()));
1066 __ popl(Address(ESP, destination.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001067 }
1068 }
1069}
1070
1071void CodeGeneratorX86::Move64(Location destination, Location source) {
1072 if (source.Equals(destination)) {
1073 return;
1074 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001075 if (destination.IsRegisterPair()) {
1076 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001077 EmitParallelMoves(
1078 Location::RegisterLocation(source.AsRegisterPairHigh<Register>()),
1079 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001080 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001081 Location::RegisterLocation(source.AsRegisterPairLow<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001082 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()),
1083 Primitive::kPrimInt);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001084 } else if (source.IsFpuRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001085 XmmRegister src_reg = source.AsFpuRegister<XmmRegister>();
1086 __ movd(destination.AsRegisterPairLow<Register>(), src_reg);
1087 __ psrlq(src_reg, Immediate(32));
1088 __ movd(destination.AsRegisterPairHigh<Register>(), src_reg);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001089 } else {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001090 // No conflict possible, so just do the moves.
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001091 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001092 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
1093 __ movl(destination.AsRegisterPairHigh<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001094 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
1095 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001096 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05001097 if (source.IsFpuRegister()) {
1098 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
1099 } else if (source.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001100 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01001101 } else if (source.IsRegisterPair()) {
1102 size_t elem_size = Primitive::ComponentSize(Primitive::kPrimInt);
1103 // Create stack space for 2 elements.
1104 __ subl(ESP, Immediate(2 * elem_size));
1105 __ movl(Address(ESP, 0), source.AsRegisterPairLow<Register>());
1106 __ movl(Address(ESP, elem_size), source.AsRegisterPairHigh<Register>());
1107 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
1108 // And remove the temporary stack space we allocated.
1109 __ addl(ESP, Immediate(2 * elem_size));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001110 } else {
1111 LOG(FATAL) << "Unimplemented";
1112 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001113 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001114 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001115 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001116 // No conflict possible, so just do the moves.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001117 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegisterPairLow<Register>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001118 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001119 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001120 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001121 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001122 } else if (source.IsConstant()) {
1123 HConstant* constant = source.GetConstant();
1124 int64_t value;
1125 if (constant->IsLongConstant()) {
1126 value = constant->AsLongConstant()->GetValue();
1127 } else {
1128 DCHECK(constant->IsDoubleConstant());
Roland Levillainda4d79b2015-03-24 14:36:11 +00001129 value = bit_cast<int64_t, double>(constant->AsDoubleConstant()->GetValue());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001130 }
1131 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(Low32Bits(value)));
1132 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), Immediate(High32Bits(value)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001133 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001134 DCHECK(source.IsDoubleStackSlot()) << source;
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001135 EmitParallelMoves(
1136 Location::StackSlot(source.GetStackIndex()),
1137 Location::StackSlot(destination.GetStackIndex()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001138 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001139 Location::StackSlot(source.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001140 Location::StackSlot(destination.GetHighStackIndex(kX86WordSize)),
1141 Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001142 }
1143 }
1144}
1145
Calin Juravle175dc732015-08-25 15:42:32 +01001146void CodeGeneratorX86::MoveConstant(Location location, int32_t value) {
1147 DCHECK(location.IsRegister());
1148 __ movl(location.AsRegister<Register>(), Immediate(value));
1149}
1150
Calin Juravlee460d1d2015-09-29 04:52:17 +01001151void CodeGeneratorX86::MoveLocation(Location dst, Location src, Primitive::Type dst_type) {
David Brazdil74eb1b22015-12-14 11:44:01 +00001152 HParallelMove move(GetGraph()->GetArena());
1153 if (dst_type == Primitive::kPrimLong && !src.IsConstant() && !src.IsFpuRegister()) {
1154 move.AddMove(src.ToLow(), dst.ToLow(), Primitive::kPrimInt, nullptr);
1155 move.AddMove(src.ToHigh(), dst.ToHigh(), Primitive::kPrimInt, nullptr);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001156 } else {
David Brazdil74eb1b22015-12-14 11:44:01 +00001157 move.AddMove(src, dst, dst_type, nullptr);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001158 }
David Brazdil74eb1b22015-12-14 11:44:01 +00001159 GetMoveResolver()->EmitNativeCode(&move);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001160}
1161
1162void CodeGeneratorX86::AddLocationAsTemp(Location location, LocationSummary* locations) {
1163 if (location.IsRegister()) {
1164 locations->AddTemp(location);
1165 } else if (location.IsRegisterPair()) {
1166 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
1167 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
1168 } else {
1169 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1170 }
1171}
1172
David Brazdilfc6a86a2015-06-26 10:33:45 +00001173void InstructionCodeGeneratorX86::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001174 DCHECK(!successor->IsExitBlock());
1175
1176 HBasicBlock* block = got->GetBlock();
1177 HInstruction* previous = got->GetPrevious();
1178
1179 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +00001180 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001181 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1182 return;
1183 }
1184
1185 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1186 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1187 }
1188 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001189 __ jmp(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001190 }
1191}
1192
David Brazdilfc6a86a2015-06-26 10:33:45 +00001193void LocationsBuilderX86::VisitGoto(HGoto* got) {
1194 got->SetLocations(nullptr);
1195}
1196
1197void InstructionCodeGeneratorX86::VisitGoto(HGoto* got) {
1198 HandleGoto(got, got->GetSuccessor());
1199}
1200
1201void LocationsBuilderX86::VisitTryBoundary(HTryBoundary* try_boundary) {
1202 try_boundary->SetLocations(nullptr);
1203}
1204
1205void InstructionCodeGeneratorX86::VisitTryBoundary(HTryBoundary* try_boundary) {
1206 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
1207 if (!successor->IsExitBlock()) {
1208 HandleGoto(try_boundary, successor);
1209 }
1210}
1211
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001212void LocationsBuilderX86::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001213 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001214}
1215
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001216void InstructionCodeGeneratorX86::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001217}
1218
Mark Mendell152408f2015-12-31 12:28:50 -05001219template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001220void InstructionCodeGeneratorX86::GenerateFPJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001221 LabelType* true_label,
1222 LabelType* false_label) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001223 if (cond->IsFPConditionTrueIfNaN()) {
1224 __ j(kUnordered, true_label);
1225 } else if (cond->IsFPConditionFalseIfNaN()) {
1226 __ j(kUnordered, false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001227 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001228 __ j(X86UnsignedOrFPCondition(cond->GetCondition()), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001229}
1230
Mark Mendell152408f2015-12-31 12:28:50 -05001231template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001232void InstructionCodeGeneratorX86::GenerateLongComparesAndJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001233 LabelType* true_label,
1234 LabelType* false_label) {
Mark Mendellc4701932015-04-10 13:18:51 -04001235 LocationSummary* locations = cond->GetLocations();
1236 Location left = locations->InAt(0);
1237 Location right = locations->InAt(1);
1238 IfCondition if_cond = cond->GetCondition();
1239
Mark Mendellc4701932015-04-10 13:18:51 -04001240 Register left_high = left.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001241 Register left_low = left.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001242 IfCondition true_high_cond = if_cond;
1243 IfCondition false_high_cond = cond->GetOppositeCondition();
Aart Bike9f37602015-10-09 11:15:55 -07001244 Condition final_condition = X86UnsignedOrFPCondition(if_cond); // unsigned on lower part
Mark Mendellc4701932015-04-10 13:18:51 -04001245
1246 // Set the conditions for the test, remembering that == needs to be
1247 // decided using the low words.
1248 switch (if_cond) {
1249 case kCondEQ:
Mark Mendellc4701932015-04-10 13:18:51 -04001250 case kCondNE:
Roland Levillain4fa13f62015-07-06 18:11:54 +01001251 // Nothing to do.
Mark Mendellc4701932015-04-10 13:18:51 -04001252 break;
1253 case kCondLT:
1254 false_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -04001255 break;
1256 case kCondLE:
1257 true_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -04001258 break;
1259 case kCondGT:
1260 false_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -04001261 break;
1262 case kCondGE:
1263 true_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -04001264 break;
Aart Bike9f37602015-10-09 11:15:55 -07001265 case kCondB:
1266 false_high_cond = kCondA;
1267 break;
1268 case kCondBE:
1269 true_high_cond = kCondB;
1270 break;
1271 case kCondA:
1272 false_high_cond = kCondB;
1273 break;
1274 case kCondAE:
1275 true_high_cond = kCondA;
1276 break;
Mark Mendellc4701932015-04-10 13:18:51 -04001277 }
1278
1279 if (right.IsConstant()) {
1280 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
Mark Mendellc4701932015-04-10 13:18:51 -04001281 int32_t val_high = High32Bits(value);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001282 int32_t val_low = Low32Bits(value);
Mark Mendellc4701932015-04-10 13:18:51 -04001283
Aart Bika19616e2016-02-01 18:57:58 -08001284 codegen_->Compare32BitValue(left_high, val_high);
Mark Mendellc4701932015-04-10 13:18:51 -04001285 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001286 __ j(X86Condition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001287 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001288 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001289 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001290 __ j(X86Condition(true_high_cond), true_label);
1291 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001292 }
1293 // Must be equal high, so compare the lows.
Aart Bika19616e2016-02-01 18:57:58 -08001294 codegen_->Compare32BitValue(left_low, val_low);
Mark Mendell8659e842016-02-16 10:41:46 -05001295 } else if (right.IsRegisterPair()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001296 Register right_high = right.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001297 Register right_low = right.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001298
1299 __ cmpl(left_high, right_high);
1300 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001301 __ j(X86Condition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001302 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001303 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001304 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001305 __ j(X86Condition(true_high_cond), true_label);
1306 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001307 }
1308 // Must be equal high, so compare the lows.
1309 __ cmpl(left_low, right_low);
Mark Mendell8659e842016-02-16 10:41:46 -05001310 } else {
1311 DCHECK(right.IsDoubleStackSlot());
1312 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
1313 if (if_cond == kCondNE) {
1314 __ j(X86Condition(true_high_cond), true_label);
1315 } else if (if_cond == kCondEQ) {
1316 __ j(X86Condition(false_high_cond), false_label);
1317 } else {
1318 __ j(X86Condition(true_high_cond), true_label);
1319 __ j(X86Condition(false_high_cond), false_label);
1320 }
1321 // Must be equal high, so compare the lows.
1322 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Mark Mendellc4701932015-04-10 13:18:51 -04001323 }
1324 // The last comparison might be unsigned.
1325 __ j(final_condition, true_label);
1326}
1327
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001328void InstructionCodeGeneratorX86::GenerateFPCompare(Location lhs,
1329 Location rhs,
1330 HInstruction* insn,
1331 bool is_double) {
1332 HX86LoadFromConstantTable* const_area = insn->InputAt(1)->AsX86LoadFromConstantTable();
1333 if (is_double) {
1334 if (rhs.IsFpuRegister()) {
1335 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1336 } else if (const_area != nullptr) {
1337 DCHECK(const_area->IsEmittedAtUseSite());
1338 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(),
1339 codegen_->LiteralDoubleAddress(
1340 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
1341 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
1342 } else {
1343 DCHECK(rhs.IsDoubleStackSlot());
1344 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(), Address(ESP, rhs.GetStackIndex()));
1345 }
1346 } else {
1347 if (rhs.IsFpuRegister()) {
1348 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1349 } else if (const_area != nullptr) {
1350 DCHECK(const_area->IsEmittedAtUseSite());
1351 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(),
1352 codegen_->LiteralFloatAddress(
1353 const_area->GetConstant()->AsFloatConstant()->GetValue(),
1354 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
1355 } else {
1356 DCHECK(rhs.IsStackSlot());
1357 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(), Address(ESP, rhs.GetStackIndex()));
1358 }
1359 }
1360}
1361
Mark Mendell152408f2015-12-31 12:28:50 -05001362template<class LabelType>
David Brazdil0debae72015-11-12 18:37:00 +00001363void InstructionCodeGeneratorX86::GenerateCompareTestAndBranch(HCondition* condition,
Mark Mendell152408f2015-12-31 12:28:50 -05001364 LabelType* true_target_in,
1365 LabelType* false_target_in) {
David Brazdil0debae72015-11-12 18:37:00 +00001366 // Generated branching requires both targets to be explicit. If either of the
1367 // targets is nullptr (fallthrough) use and bind `fallthrough_target` instead.
Mark Mendell152408f2015-12-31 12:28:50 -05001368 LabelType fallthrough_target;
1369 LabelType* true_target = true_target_in == nullptr ? &fallthrough_target : true_target_in;
1370 LabelType* false_target = false_target_in == nullptr ? &fallthrough_target : false_target_in;
David Brazdil0debae72015-11-12 18:37:00 +00001371
Mark Mendellc4701932015-04-10 13:18:51 -04001372 LocationSummary* locations = condition->GetLocations();
1373 Location left = locations->InAt(0);
1374 Location right = locations->InAt(1);
1375
Mark Mendellc4701932015-04-10 13:18:51 -04001376 Primitive::Type type = condition->InputAt(0)->GetType();
1377 switch (type) {
1378 case Primitive::kPrimLong:
1379 GenerateLongComparesAndJumps(condition, true_target, false_target);
1380 break;
1381 case Primitive::kPrimFloat:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001382 GenerateFPCompare(left, right, condition, false);
Mark Mendellc4701932015-04-10 13:18:51 -04001383 GenerateFPJumps(condition, true_target, false_target);
1384 break;
1385 case Primitive::kPrimDouble:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001386 GenerateFPCompare(left, right, condition, true);
Mark Mendellc4701932015-04-10 13:18:51 -04001387 GenerateFPJumps(condition, true_target, false_target);
1388 break;
1389 default:
1390 LOG(FATAL) << "Unexpected compare type " << type;
1391 }
1392
David Brazdil0debae72015-11-12 18:37:00 +00001393 if (false_target != &fallthrough_target) {
Mark Mendellc4701932015-04-10 13:18:51 -04001394 __ jmp(false_target);
1395 }
David Brazdil0debae72015-11-12 18:37:00 +00001396
1397 if (fallthrough_target.IsLinked()) {
1398 __ Bind(&fallthrough_target);
1399 }
Mark Mendellc4701932015-04-10 13:18:51 -04001400}
1401
David Brazdil0debae72015-11-12 18:37:00 +00001402static bool AreEflagsSetFrom(HInstruction* cond, HInstruction* branch) {
1403 // Moves may affect the eflags register (move zero uses xorl), so the EFLAGS
1404 // are set only strictly before `branch`. We can't use the eflags on long/FP
1405 // conditions if they are materialized due to the complex branching.
1406 return cond->IsCondition() &&
1407 cond->GetNext() == branch &&
1408 cond->InputAt(0)->GetType() != Primitive::kPrimLong &&
1409 !Primitive::IsFloatingPointType(cond->InputAt(0)->GetType());
1410}
1411
Mark Mendell152408f2015-12-31 12:28:50 -05001412template<class LabelType>
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001413void InstructionCodeGeneratorX86::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00001414 size_t condition_input_index,
Mark Mendell152408f2015-12-31 12:28:50 -05001415 LabelType* true_target,
1416 LabelType* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00001417 HInstruction* cond = instruction->InputAt(condition_input_index);
1418
1419 if (true_target == nullptr && false_target == nullptr) {
1420 // Nothing to do. The code always falls through.
1421 return;
1422 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00001423 // Constant condition, statically compared against "true" (integer value 1).
1424 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00001425 if (true_target != nullptr) {
1426 __ jmp(true_target);
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001427 }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001428 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00001429 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00001430 if (false_target != nullptr) {
1431 __ jmp(false_target);
1432 }
1433 }
1434 return;
1435 }
1436
1437 // The following code generates these patterns:
1438 // (1) true_target == nullptr && false_target != nullptr
1439 // - opposite condition true => branch to false_target
1440 // (2) true_target != nullptr && false_target == nullptr
1441 // - condition true => branch to true_target
1442 // (3) true_target != nullptr && false_target != nullptr
1443 // - condition true => branch to true_target
1444 // - branch to false_target
1445 if (IsBooleanValueOrMaterializedCondition(cond)) {
1446 if (AreEflagsSetFrom(cond, instruction)) {
1447 if (true_target == nullptr) {
1448 __ j(X86Condition(cond->AsCondition()->GetOppositeCondition()), false_target);
1449 } else {
1450 __ j(X86Condition(cond->AsCondition()->GetCondition()), true_target);
1451 }
1452 } else {
1453 // Materialized condition, compare against 0.
1454 Location lhs = instruction->GetLocations()->InAt(condition_input_index);
1455 if (lhs.IsRegister()) {
1456 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
1457 } else {
1458 __ cmpl(Address(ESP, lhs.GetStackIndex()), Immediate(0));
1459 }
1460 if (true_target == nullptr) {
1461 __ j(kEqual, false_target);
1462 } else {
1463 __ j(kNotEqual, true_target);
1464 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001465 }
1466 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001467 // Condition has not been materialized, use its inputs as the comparison and
1468 // its condition as the branch condition.
Mark Mendellb8b97692015-05-22 16:58:19 -04001469 HCondition* condition = cond->AsCondition();
David Brazdil0debae72015-11-12 18:37:00 +00001470
1471 // If this is a long or FP comparison that has been folded into
1472 // the HCondition, generate the comparison directly.
1473 Primitive::Type type = condition->InputAt(0)->GetType();
1474 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
1475 GenerateCompareTestAndBranch(condition, true_target, false_target);
1476 return;
1477 }
1478
1479 Location lhs = condition->GetLocations()->InAt(0);
1480 Location rhs = condition->GetLocations()->InAt(1);
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001481 // LHS is guaranteed to be in a register (see LocationsBuilderX86::HandleCondition).
David Brazdil0debae72015-11-12 18:37:00 +00001482 if (rhs.IsRegister()) {
1483 __ cmpl(lhs.AsRegister<Register>(), rhs.AsRegister<Register>());
1484 } else if (rhs.IsConstant()) {
1485 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
Aart Bika19616e2016-02-01 18:57:58 -08001486 codegen_->Compare32BitValue(lhs.AsRegister<Register>(), constant);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001487 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001488 __ cmpl(lhs.AsRegister<Register>(), Address(ESP, rhs.GetStackIndex()));
1489 }
1490 if (true_target == nullptr) {
1491 __ j(X86Condition(condition->GetOppositeCondition()), false_target);
1492 } else {
Mark Mendellb8b97692015-05-22 16:58:19 -04001493 __ j(X86Condition(condition->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -07001494 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001495 }
David Brazdil0debae72015-11-12 18:37:00 +00001496
1497 // If neither branch falls through (case 3), the conditional branch to `true_target`
1498 // was already emitted (case 2) and we need to emit a jump to `false_target`.
1499 if (true_target != nullptr && false_target != nullptr) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001500 __ jmp(false_target);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001501 }
1502}
1503
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001504void LocationsBuilderX86::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001505 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
1506 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001507 locations->SetInAt(0, Location::Any());
1508 }
1509}
1510
1511void InstructionCodeGeneratorX86::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001512 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
1513 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
1514 Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
1515 nullptr : codegen_->GetLabelOf(true_successor);
1516 Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
1517 nullptr : codegen_->GetLabelOf(false_successor);
1518 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001519}
1520
1521void LocationsBuilderX86::VisitDeoptimize(HDeoptimize* deoptimize) {
1522 LocationSummary* locations = new (GetGraph()->GetArena())
1523 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
David Brazdil0debae72015-11-12 18:37:00 +00001524 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001525 locations->SetInAt(0, Location::Any());
1526 }
1527}
1528
1529void InstructionCodeGeneratorX86::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08001530 SlowPathCode* slow_path = deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathX86>(deoptimize);
David Brazdil74eb1b22015-12-14 11:44:01 +00001531 GenerateTestAndBranch<Label>(deoptimize,
1532 /* condition_input_index */ 0,
1533 slow_path->GetEntryLabel(),
1534 /* false_target */ nullptr);
1535}
1536
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001537static bool SelectCanUseCMOV(HSelect* select) {
1538 // There are no conditional move instructions for XMMs.
1539 if (Primitive::IsFloatingPointType(select->GetType())) {
1540 return false;
1541 }
1542
1543 // A FP condition doesn't generate the single CC that we need.
1544 // In 32 bit mode, a long condition doesn't generate a single CC either.
1545 HInstruction* condition = select->GetCondition();
1546 if (condition->IsCondition()) {
1547 Primitive::Type compare_type = condition->InputAt(0)->GetType();
1548 if (compare_type == Primitive::kPrimLong ||
1549 Primitive::IsFloatingPointType(compare_type)) {
1550 return false;
1551 }
1552 }
1553
1554 // We can generate a CMOV for this Select.
1555 return true;
1556}
1557
David Brazdil74eb1b22015-12-14 11:44:01 +00001558void LocationsBuilderX86::VisitSelect(HSelect* select) {
1559 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001560 if (Primitive::IsFloatingPointType(select->GetType())) {
David Brazdil74eb1b22015-12-14 11:44:01 +00001561 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001562 locations->SetInAt(1, Location::Any());
David Brazdil74eb1b22015-12-14 11:44:01 +00001563 } else {
1564 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001565 if (SelectCanUseCMOV(select)) {
1566 if (select->InputAt(1)->IsConstant()) {
1567 // Cmov can't handle a constant value.
1568 locations->SetInAt(1, Location::RequiresRegister());
1569 } else {
1570 locations->SetInAt(1, Location::Any());
1571 }
1572 } else {
1573 locations->SetInAt(1, Location::Any());
1574 }
David Brazdil74eb1b22015-12-14 11:44:01 +00001575 }
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001576 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
1577 locations->SetInAt(2, Location::RequiresRegister());
David Brazdil74eb1b22015-12-14 11:44:01 +00001578 }
1579 locations->SetOut(Location::SameAsFirstInput());
1580}
1581
Roland Levillain0b671c02016-08-19 12:02:34 +01001582void CodeGeneratorX86::GenerateIntCompare(Location lhs, Location rhs) {
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001583 Register lhs_reg = lhs.AsRegister<Register>();
1584 if (rhs.IsConstant()) {
1585 int32_t value = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
Roland Levillain0b671c02016-08-19 12:02:34 +01001586 Compare32BitValue(lhs_reg, value);
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001587 } else if (rhs.IsStackSlot()) {
Roland Levillain0b671c02016-08-19 12:02:34 +01001588 assembler_.cmpl(lhs_reg, Address(ESP, rhs.GetStackIndex()));
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001589 } else {
Roland Levillain0b671c02016-08-19 12:02:34 +01001590 assembler_.cmpl(lhs_reg, rhs.AsRegister<Register>());
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001591 }
1592}
1593
David Brazdil74eb1b22015-12-14 11:44:01 +00001594void InstructionCodeGeneratorX86::VisitSelect(HSelect* select) {
1595 LocationSummary* locations = select->GetLocations();
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001596 DCHECK(locations->InAt(0).Equals(locations->Out()));
1597 if (SelectCanUseCMOV(select)) {
1598 // If both the condition and the source types are integer, we can generate
1599 // a CMOV to implement Select.
1600
1601 HInstruction* select_condition = select->GetCondition();
1602 Condition cond = kNotEqual;
1603
1604 // Figure out how to test the 'condition'.
1605 if (select_condition->IsCondition()) {
1606 HCondition* condition = select_condition->AsCondition();
1607 if (!condition->IsEmittedAtUseSite()) {
1608 // This was a previously materialized condition.
1609 // Can we use the existing condition code?
1610 if (AreEflagsSetFrom(condition, select)) {
1611 // Materialization was the previous instruction. Condition codes are right.
1612 cond = X86Condition(condition->GetCondition());
1613 } else {
1614 // No, we have to recreate the condition code.
1615 Register cond_reg = locations->InAt(2).AsRegister<Register>();
1616 __ testl(cond_reg, cond_reg);
1617 }
1618 } else {
1619 // We can't handle FP or long here.
1620 DCHECK_NE(condition->InputAt(0)->GetType(), Primitive::kPrimLong);
1621 DCHECK(!Primitive::IsFloatingPointType(condition->InputAt(0)->GetType()));
1622 LocationSummary* cond_locations = condition->GetLocations();
Roland Levillain0b671c02016-08-19 12:02:34 +01001623 codegen_->GenerateIntCompare(cond_locations->InAt(0), cond_locations->InAt(1));
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001624 cond = X86Condition(condition->GetCondition());
1625 }
1626 } else {
1627 // Must be a boolean condition, which needs to be compared to 0.
1628 Register cond_reg = locations->InAt(2).AsRegister<Register>();
1629 __ testl(cond_reg, cond_reg);
1630 }
1631
1632 // If the condition is true, overwrite the output, which already contains false.
1633 Location false_loc = locations->InAt(0);
1634 Location true_loc = locations->InAt(1);
1635 if (select->GetType() == Primitive::kPrimLong) {
1636 // 64 bit conditional move.
1637 Register false_high = false_loc.AsRegisterPairHigh<Register>();
1638 Register false_low = false_loc.AsRegisterPairLow<Register>();
1639 if (true_loc.IsRegisterPair()) {
1640 __ cmovl(cond, false_high, true_loc.AsRegisterPairHigh<Register>());
1641 __ cmovl(cond, false_low, true_loc.AsRegisterPairLow<Register>());
1642 } else {
1643 __ cmovl(cond, false_high, Address(ESP, true_loc.GetHighStackIndex(kX86WordSize)));
1644 __ cmovl(cond, false_low, Address(ESP, true_loc.GetStackIndex()));
1645 }
1646 } else {
1647 // 32 bit conditional move.
1648 Register false_reg = false_loc.AsRegister<Register>();
1649 if (true_loc.IsRegister()) {
1650 __ cmovl(cond, false_reg, true_loc.AsRegister<Register>());
1651 } else {
1652 __ cmovl(cond, false_reg, Address(ESP, true_loc.GetStackIndex()));
1653 }
1654 }
1655 } else {
1656 NearLabel false_target;
1657 GenerateTestAndBranch<NearLabel>(
1658 select, /* condition_input_index */ 2, /* true_target */ nullptr, &false_target);
1659 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
1660 __ Bind(&false_target);
1661 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001662}
1663
David Srbecky0cf44932015-12-09 14:09:59 +00001664void LocationsBuilderX86::VisitNativeDebugInfo(HNativeDebugInfo* info) {
1665 new (GetGraph()->GetArena()) LocationSummary(info);
1666}
1667
David Srbeckyd28f4a02016-03-14 17:14:24 +00001668void InstructionCodeGeneratorX86::VisitNativeDebugInfo(HNativeDebugInfo*) {
1669 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00001670}
1671
1672void CodeGeneratorX86::GenerateNop() {
1673 __ nop();
David Srbecky0cf44932015-12-09 14:09:59 +00001674}
1675
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001676void LocationsBuilderX86::HandleCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001677 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +01001678 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Mark Mendellc4701932015-04-10 13:18:51 -04001679 // Handle the long/FP comparisons made in instruction simplification.
1680 switch (cond->InputAt(0)->GetType()) {
1681 case Primitive::kPrimLong: {
1682 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell8659e842016-02-16 10:41:46 -05001683 locations->SetInAt(1, Location::Any());
David Brazdilb3e773e2016-01-26 11:28:37 +00001684 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001685 locations->SetOut(Location::RequiresRegister());
1686 }
1687 break;
1688 }
1689 case Primitive::kPrimFloat:
1690 case Primitive::kPrimDouble: {
1691 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001692 if (cond->InputAt(1)->IsX86LoadFromConstantTable()) {
1693 DCHECK(cond->InputAt(1)->IsEmittedAtUseSite());
1694 } else if (cond->InputAt(1)->IsConstant()) {
1695 locations->SetInAt(1, Location::RequiresFpuRegister());
1696 } else {
1697 locations->SetInAt(1, Location::Any());
1698 }
David Brazdilb3e773e2016-01-26 11:28:37 +00001699 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001700 locations->SetOut(Location::RequiresRegister());
1701 }
1702 break;
1703 }
1704 default:
1705 locations->SetInAt(0, Location::RequiresRegister());
1706 locations->SetInAt(1, Location::Any());
David Brazdilb3e773e2016-01-26 11:28:37 +00001707 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001708 // We need a byte register.
1709 locations->SetOut(Location::RegisterLocation(ECX));
1710 }
1711 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001712 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001713}
1714
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001715void InstructionCodeGeneratorX86::HandleCondition(HCondition* cond) {
David Brazdilb3e773e2016-01-26 11:28:37 +00001716 if (cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001717 return;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001718 }
Mark Mendellc4701932015-04-10 13:18:51 -04001719
1720 LocationSummary* locations = cond->GetLocations();
1721 Location lhs = locations->InAt(0);
1722 Location rhs = locations->InAt(1);
1723 Register reg = locations->Out().AsRegister<Register>();
Mark Mendell152408f2015-12-31 12:28:50 -05001724 NearLabel true_label, false_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001725
1726 switch (cond->InputAt(0)->GetType()) {
1727 default: {
1728 // Integer case.
1729
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01001730 // Clear output register: setb only sets the low byte.
Mark Mendellc4701932015-04-10 13:18:51 -04001731 __ xorl(reg, reg);
Roland Levillain0b671c02016-08-19 12:02:34 +01001732 codegen_->GenerateIntCompare(lhs, rhs);
Aart Bike9f37602015-10-09 11:15:55 -07001733 __ setb(X86Condition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001734 return;
1735 }
1736 case Primitive::kPrimLong:
1737 GenerateLongComparesAndJumps(cond, &true_label, &false_label);
1738 break;
1739 case Primitive::kPrimFloat:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001740 GenerateFPCompare(lhs, rhs, cond, false);
Mark Mendellc4701932015-04-10 13:18:51 -04001741 GenerateFPJumps(cond, &true_label, &false_label);
1742 break;
1743 case Primitive::kPrimDouble:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001744 GenerateFPCompare(lhs, rhs, cond, true);
Mark Mendellc4701932015-04-10 13:18:51 -04001745 GenerateFPJumps(cond, &true_label, &false_label);
1746 break;
1747 }
1748
1749 // Convert the jumps into the result.
Mark Mendell0c9497d2015-08-21 09:30:05 -04001750 NearLabel done_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001751
Roland Levillain4fa13f62015-07-06 18:11:54 +01001752 // False case: result = 0.
Mark Mendellc4701932015-04-10 13:18:51 -04001753 __ Bind(&false_label);
1754 __ xorl(reg, reg);
1755 __ jmp(&done_label);
1756
Roland Levillain4fa13f62015-07-06 18:11:54 +01001757 // True case: result = 1.
Mark Mendellc4701932015-04-10 13:18:51 -04001758 __ Bind(&true_label);
1759 __ movl(reg, Immediate(1));
1760 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07001761}
1762
1763void LocationsBuilderX86::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001764 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001765}
1766
1767void InstructionCodeGeneratorX86::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001768 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001769}
1770
1771void LocationsBuilderX86::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001772 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001773}
1774
1775void InstructionCodeGeneratorX86::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001776 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001777}
1778
1779void LocationsBuilderX86::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001780 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001781}
1782
1783void InstructionCodeGeneratorX86::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001784 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001785}
1786
1787void LocationsBuilderX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001788 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001789}
1790
1791void InstructionCodeGeneratorX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001792 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001793}
1794
1795void LocationsBuilderX86::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001796 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001797}
1798
1799void InstructionCodeGeneratorX86::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001800 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001801}
1802
1803void LocationsBuilderX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001804 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001805}
1806
1807void InstructionCodeGeneratorX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001808 HandleCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001809}
1810
Aart Bike9f37602015-10-09 11:15:55 -07001811void LocationsBuilderX86::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001812 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001813}
1814
1815void InstructionCodeGeneratorX86::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001816 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001817}
1818
1819void LocationsBuilderX86::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001820 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001821}
1822
1823void InstructionCodeGeneratorX86::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001824 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001825}
1826
1827void LocationsBuilderX86::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001828 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001829}
1830
1831void InstructionCodeGeneratorX86::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001832 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001833}
1834
1835void LocationsBuilderX86::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001836 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001837}
1838
1839void InstructionCodeGeneratorX86::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001840 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001841}
1842
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001843void LocationsBuilderX86::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001844 LocationSummary* locations =
1845 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001846 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001847}
1848
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001849void InstructionCodeGeneratorX86::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001850 // Will be generated at use site.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001851}
1852
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001853void LocationsBuilderX86::VisitNullConstant(HNullConstant* constant) {
1854 LocationSummary* locations =
1855 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1856 locations->SetOut(Location::ConstantLocation(constant));
1857}
1858
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001859void InstructionCodeGeneratorX86::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001860 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001861}
1862
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001863void LocationsBuilderX86::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001864 LocationSummary* locations =
1865 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001866 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001867}
1868
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001869void InstructionCodeGeneratorX86::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001870 // Will be generated at use site.
1871}
1872
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001873void LocationsBuilderX86::VisitFloatConstant(HFloatConstant* constant) {
1874 LocationSummary* locations =
1875 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1876 locations->SetOut(Location::ConstantLocation(constant));
1877}
1878
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001879void InstructionCodeGeneratorX86::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001880 // Will be generated at use site.
1881}
1882
1883void LocationsBuilderX86::VisitDoubleConstant(HDoubleConstant* constant) {
1884 LocationSummary* locations =
1885 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1886 locations->SetOut(Location::ConstantLocation(constant));
1887}
1888
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001889void InstructionCodeGeneratorX86::VisitDoubleConstant(HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001890 // Will be generated at use site.
1891}
1892
Calin Juravle27df7582015-04-17 19:12:31 +01001893void LocationsBuilderX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1894 memory_barrier->SetLocations(nullptr);
1895}
1896
1897void InstructionCodeGeneratorX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00001898 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01001899}
1900
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001901void LocationsBuilderX86::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001902 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001903}
1904
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001905void InstructionCodeGeneratorX86::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001906 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001907}
1908
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001909void LocationsBuilderX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001910 LocationSummary* locations =
1911 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001912 switch (ret->InputAt(0)->GetType()) {
1913 case Primitive::kPrimBoolean:
1914 case Primitive::kPrimByte:
1915 case Primitive::kPrimChar:
1916 case Primitive::kPrimShort:
1917 case Primitive::kPrimInt:
1918 case Primitive::kPrimNot:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001919 locations->SetInAt(0, Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001920 break;
1921
1922 case Primitive::kPrimLong:
1923 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001924 0, Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001925 break;
1926
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001927 case Primitive::kPrimFloat:
1928 case Primitive::kPrimDouble:
1929 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001930 0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001931 break;
1932
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001933 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001934 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001935 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001936}
1937
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001938void InstructionCodeGeneratorX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001939 if (kIsDebugBuild) {
1940 switch (ret->InputAt(0)->GetType()) {
1941 case Primitive::kPrimBoolean:
1942 case Primitive::kPrimByte:
1943 case Primitive::kPrimChar:
1944 case Primitive::kPrimShort:
1945 case Primitive::kPrimInt:
1946 case Primitive::kPrimNot:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001947 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<Register>(), EAX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001948 break;
1949
1950 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001951 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairLow<Register>(), EAX);
1952 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairHigh<Register>(), EDX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001953 break;
1954
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001955 case Primitive::kPrimFloat:
1956 case Primitive::kPrimDouble:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001957 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>(), XMM0);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001958 break;
1959
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001960 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001961 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001962 }
1963 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001964 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001965}
1966
Calin Juravle175dc732015-08-25 15:42:32 +01001967void LocationsBuilderX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
1968 // The trampoline uses the same calling convention as dex calling conventions,
1969 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
1970 // the method_idx.
1971 HandleInvoke(invoke);
1972}
1973
1974void InstructionCodeGeneratorX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
1975 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
1976}
1977
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001978void LocationsBuilderX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00001979 // Explicit clinit checks triggered by static invokes must have been pruned by
1980 // art::PrepareForRegisterAllocation.
1981 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001982
Mark Mendellfb8d2792015-03-31 22:16:59 -04001983 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001984 if (intrinsic.TryDispatch(invoke)) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00001985 if (invoke->GetLocations()->CanCall() && invoke->HasPcRelativeDexCache()) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00001986 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00001987 }
Mark Mendell09ed1a32015-03-25 08:30:06 -04001988 return;
1989 }
1990
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001991 HandleInvoke(invoke);
Nicolas Geoffray94015b92015-06-04 18:21:04 +01001992
Vladimir Marko0f7dca42015-11-02 14:36:43 +00001993 // For PC-relative dex cache the invoke has an extra input, the PC-relative address base.
1994 if (invoke->HasPcRelativeDexCache()) {
Vladimir Markob4536b72015-11-24 13:45:23 +00001995 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00001996 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001997}
1998
Mark Mendell09ed1a32015-03-25 08:30:06 -04001999static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86* codegen) {
2000 if (invoke->GetLocations()->Intrinsified()) {
2001 IntrinsicCodeGeneratorX86 intrinsic(codegen);
2002 intrinsic.Dispatch(invoke);
2003 return true;
2004 }
2005 return false;
2006}
2007
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002008void InstructionCodeGeneratorX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002009 // Explicit clinit checks triggered by static invokes must have been pruned by
2010 // art::PrepareForRegisterAllocation.
2011 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002012
Mark Mendell09ed1a32015-03-25 08:30:06 -04002013 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2014 return;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002015 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002016
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002017 LocationSummary* locations = invoke->GetLocations();
Mark Mendell09ed1a32015-03-25 08:30:06 -04002018 codegen_->GenerateStaticOrDirectCall(
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002019 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Mingyao Yang8693fe12015-04-17 16:51:08 -07002020 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002021}
2022
2023void LocationsBuilderX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00002024 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
2025 if (intrinsic.TryDispatch(invoke)) {
2026 return;
2027 }
2028
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002029 HandleInvoke(invoke);
2030}
2031
2032void LocationsBuilderX86::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002033 InvokeDexCallingConventionVisitorX86 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002034 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002035}
2036
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002037void InstructionCodeGeneratorX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002038 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2039 return;
2040 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002041
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002042 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002043 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01002044 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002045}
2046
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002047void LocationsBuilderX86::VisitInvokeInterface(HInvokeInterface* invoke) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002048 // This call to HandleInvoke allocates a temporary (core) register
2049 // which is also used to transfer the hidden argument from FP to
2050 // core register.
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002051 HandleInvoke(invoke);
2052 // Add the hidden argument.
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002053 invoke->GetLocations()->AddTemp(Location::FpuRegisterLocation(XMM7));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002054}
2055
2056void InstructionCodeGeneratorX86::VisitInvokeInterface(HInvokeInterface* invoke) {
2057 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain0d5a2812015-11-13 10:07:31 +00002058 LocationSummary* locations = invoke->GetLocations();
2059 Register temp = locations->GetTemp(0).AsRegister<Register>();
2060 XmmRegister hidden_reg = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002061 Location receiver = locations->InAt(0);
2062 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2063
Roland Levillain0d5a2812015-11-13 10:07:31 +00002064 // Set the hidden argument. This is safe to do this here, as XMM7
2065 // won't be modified thereafter, before the `call` instruction.
2066 DCHECK_EQ(XMM7, hidden_reg);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002067 __ movl(temp, Immediate(invoke->GetDexMethodIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002068 __ movd(hidden_reg, temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002069
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002070 if (receiver.IsStackSlot()) {
2071 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002072 // /* HeapReference<Class> */ temp = temp->klass_
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002073 __ movl(temp, Address(temp, class_offset));
2074 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002075 // /* HeapReference<Class> */ temp = receiver->klass_
Roland Levillain271ab9c2014-11-27 15:23:57 +00002076 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002077 }
Roland Levillain4d027112015-07-01 15:41:14 +01002078 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00002079 // Instead of simply (possibly) unpoisoning `temp` here, we should
2080 // emit a read barrier for the previous class reference load.
2081 // However this is not required in practice, as this is an
2082 // intermediate/temporary reference and because the current
2083 // concurrent copying collector keeps the from-space memory
2084 // intact/accessible until the end of the marking phase (the
2085 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01002086 __ MaybeUnpoisonHeapReference(temp);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002087 // temp = temp->GetAddressOfIMT()
2088 __ movl(temp,
2089 Address(temp, mirror::Class::ImtPtrOffset(kX86PointerSize).Uint32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002090 // temp = temp->GetImtEntryAt(method_offset);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002091 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00002092 invoke->GetImtIndex(), kX86PointerSize));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002093 __ movl(temp, Address(temp, method_offset));
2094 // call temp->GetEntryPoint();
Roland Levillain0d5a2812015-11-13 10:07:31 +00002095 __ call(Address(temp,
Andreas Gampe542451c2016-07-26 09:02:02 -07002096 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86PointerSize).Int32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002097
2098 DCHECK(!codegen_->IsLeafMethod());
2099 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2100}
2101
Roland Levillain88cb1752014-10-20 16:36:47 +01002102void LocationsBuilderX86::VisitNeg(HNeg* neg) {
2103 LocationSummary* locations =
2104 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2105 switch (neg->GetResultType()) {
2106 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002107 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01002108 locations->SetInAt(0, Location::RequiresRegister());
2109 locations->SetOut(Location::SameAsFirstInput());
2110 break;
2111
Roland Levillain88cb1752014-10-20 16:36:47 +01002112 case Primitive::kPrimFloat:
Roland Levillain5368c212014-11-27 15:03:41 +00002113 locations->SetInAt(0, Location::RequiresFpuRegister());
2114 locations->SetOut(Location::SameAsFirstInput());
2115 locations->AddTemp(Location::RequiresRegister());
2116 locations->AddTemp(Location::RequiresFpuRegister());
2117 break;
2118
Roland Levillain88cb1752014-10-20 16:36:47 +01002119 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002120 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00002121 locations->SetOut(Location::SameAsFirstInput());
2122 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01002123 break;
2124
2125 default:
2126 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2127 }
2128}
2129
2130void InstructionCodeGeneratorX86::VisitNeg(HNeg* neg) {
2131 LocationSummary* locations = neg->GetLocations();
2132 Location out = locations->Out();
2133 Location in = locations->InAt(0);
2134 switch (neg->GetResultType()) {
2135 case Primitive::kPrimInt:
2136 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002137 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002138 __ negl(out.AsRegister<Register>());
Roland Levillain88cb1752014-10-20 16:36:47 +01002139 break;
2140
2141 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002142 DCHECK(in.IsRegisterPair());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002143 DCHECK(in.Equals(out));
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002144 __ negl(out.AsRegisterPairLow<Register>());
2145 // Negation is similar to subtraction from zero. The least
2146 // significant byte triggers a borrow when it is different from
2147 // zero; to take it into account, add 1 to the most significant
2148 // byte if the carry flag (CF) is set to 1 after the first NEGL
2149 // operation.
2150 __ adcl(out.AsRegisterPairHigh<Register>(), Immediate(0));
2151 __ negl(out.AsRegisterPairHigh<Register>());
2152 break;
2153
Roland Levillain5368c212014-11-27 15:03:41 +00002154 case Primitive::kPrimFloat: {
2155 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002156 Register constant = locations->GetTemp(0).AsRegister<Register>();
2157 XmmRegister mask = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002158 // Implement float negation with an exclusive or with value
2159 // 0x80000000 (mask for bit 31, representing the sign of a
2160 // single-precision floating-point number).
2161 __ movl(constant, Immediate(INT32_C(0x80000000)));
2162 __ movd(mask, constant);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002163 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00002164 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002165 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00002166
Roland Levillain5368c212014-11-27 15:03:41 +00002167 case Primitive::kPrimDouble: {
2168 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002169 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002170 // Implement double negation with an exclusive or with value
2171 // 0x8000000000000000 (mask for bit 63, representing the sign of
2172 // a double-precision floating-point number).
2173 __ LoadLongConstant(mask, INT64_C(0x8000000000000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002174 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01002175 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002176 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002177
2178 default:
2179 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2180 }
2181}
2182
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002183void LocationsBuilderX86::VisitX86FPNeg(HX86FPNeg* neg) {
2184 LocationSummary* locations =
2185 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2186 DCHECK(Primitive::IsFloatingPointType(neg->GetType()));
2187 locations->SetInAt(0, Location::RequiresFpuRegister());
2188 locations->SetInAt(1, Location::RequiresRegister());
2189 locations->SetOut(Location::SameAsFirstInput());
2190 locations->AddTemp(Location::RequiresFpuRegister());
2191}
2192
2193void InstructionCodeGeneratorX86::VisitX86FPNeg(HX86FPNeg* neg) {
2194 LocationSummary* locations = neg->GetLocations();
2195 Location out = locations->Out();
2196 DCHECK(locations->InAt(0).Equals(out));
2197
2198 Register constant_area = locations->InAt(1).AsRegister<Register>();
2199 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
2200 if (neg->GetType() == Primitive::kPrimFloat) {
2201 __ movss(mask, codegen_->LiteralInt32Address(INT32_C(0x80000000), constant_area));
2202 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
2203 } else {
2204 __ movsd(mask, codegen_->LiteralInt64Address(INT64_C(0x8000000000000000), constant_area));
2205 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
2206 }
2207}
2208
Roland Levillaindff1f282014-11-05 14:15:05 +00002209void LocationsBuilderX86::VisitTypeConversion(HTypeConversion* conversion) {
Roland Levillaindff1f282014-11-05 14:15:05 +00002210 Primitive::Type result_type = conversion->GetResultType();
2211 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002212 DCHECK_NE(result_type, input_type);
Roland Levillain624279f2014-12-04 11:54:28 +00002213
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002214 // The float-to-long and double-to-long type conversions rely on a
2215 // call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00002216 LocationSummary::CallKind call_kind =
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002217 ((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
2218 && result_type == Primitive::kPrimLong)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01002219 ? LocationSummary::kCallOnMainOnly
Roland Levillain624279f2014-12-04 11:54:28 +00002220 : LocationSummary::kNoCall;
2221 LocationSummary* locations =
2222 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
2223
David Brazdilb2bd1c52015-03-25 11:17:37 +00002224 // The Java language does not allow treating boolean as an integral type but
2225 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00002226
Roland Levillaindff1f282014-11-05 14:15:05 +00002227 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002228 case Primitive::kPrimByte:
2229 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002230 case Primitive::kPrimLong: {
2231 // Type conversion from long to byte is a result of code transformations.
2232 HInstruction* input = conversion->InputAt(0);
2233 Location input_location = input->IsConstant()
2234 ? Location::ConstantLocation(input->AsConstant())
2235 : Location::RegisterPairLocation(EAX, EDX);
2236 locations->SetInAt(0, input_location);
2237 // Make the output overlap to please the register allocator. This greatly simplifies
2238 // the validation of the linear scan implementation
2239 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2240 break;
2241 }
David Brazdil46e2a392015-03-16 17:31:52 +00002242 case Primitive::kPrimBoolean:
2243 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002244 case Primitive::kPrimShort:
2245 case Primitive::kPrimInt:
2246 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002247 // Processing a Dex `int-to-byte' instruction.
Mark Mendell5f874182015-03-04 15:42:45 -05002248 locations->SetInAt(0, Location::ByteRegisterOrConstant(ECX, conversion->InputAt(0)));
2249 // Make the output overlap to please the register allocator. This greatly simplifies
2250 // the validation of the linear scan implementation
2251 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Roland Levillain51d3fc42014-11-13 14:11:42 +00002252 break;
2253
2254 default:
2255 LOG(FATAL) << "Unexpected type conversion from " << input_type
2256 << " to " << result_type;
2257 }
2258 break;
2259
Roland Levillain01a8d712014-11-14 16:27:39 +00002260 case Primitive::kPrimShort:
2261 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002262 case Primitive::kPrimLong:
2263 // Type conversion from long to short is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002264 case Primitive::kPrimBoolean:
2265 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002266 case Primitive::kPrimByte:
2267 case Primitive::kPrimInt:
2268 case Primitive::kPrimChar:
2269 // Processing a Dex `int-to-short' instruction.
2270 locations->SetInAt(0, Location::Any());
2271 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2272 break;
2273
2274 default:
2275 LOG(FATAL) << "Unexpected type conversion from " << input_type
2276 << " to " << result_type;
2277 }
2278 break;
2279
Roland Levillain946e1432014-11-11 17:35:19 +00002280 case Primitive::kPrimInt:
2281 switch (input_type) {
2282 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002283 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002284 locations->SetInAt(0, Location::Any());
2285 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2286 break;
2287
2288 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00002289 // Processing a Dex `float-to-int' instruction.
2290 locations->SetInAt(0, Location::RequiresFpuRegister());
2291 locations->SetOut(Location::RequiresRegister());
2292 locations->AddTemp(Location::RequiresFpuRegister());
2293 break;
2294
Roland Levillain946e1432014-11-11 17:35:19 +00002295 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002296 // Processing a Dex `double-to-int' instruction.
2297 locations->SetInAt(0, Location::RequiresFpuRegister());
2298 locations->SetOut(Location::RequiresRegister());
2299 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00002300 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::kPrimLong:
2309 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002310 case Primitive::kPrimBoolean:
2311 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002312 case Primitive::kPrimByte:
2313 case Primitive::kPrimShort:
2314 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002315 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002316 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002317 locations->SetInAt(0, Location::RegisterLocation(EAX));
2318 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
2319 break;
2320
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002321 case Primitive::kPrimFloat:
Vladimir Marko949c91f2015-01-27 10:48:44 +00002322 case Primitive::kPrimDouble: {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002323 // Processing a Dex `float-to-long' or 'double-to-long' instruction.
Vladimir Marko949c91f2015-01-27 10:48:44 +00002324 InvokeRuntimeCallingConvention calling_convention;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002325 XmmRegister parameter = calling_convention.GetFpuRegisterAt(0);
2326 locations->SetInAt(0, Location::FpuRegisterLocation(parameter));
2327
Vladimir Marko949c91f2015-01-27 10:48:44 +00002328 // The runtime helper puts the result in EAX, EDX.
2329 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Vladimir Marko949c91f2015-01-27 10:48:44 +00002330 }
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002331 break;
Roland Levillaindff1f282014-11-05 14:15:05 +00002332
2333 default:
2334 LOG(FATAL) << "Unexpected type conversion from " << input_type
2335 << " to " << result_type;
2336 }
2337 break;
2338
Roland Levillain981e4542014-11-14 11:47:14 +00002339 case Primitive::kPrimChar:
2340 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002341 case Primitive::kPrimLong:
2342 // Type conversion from long to char is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002343 case Primitive::kPrimBoolean:
2344 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002345 case Primitive::kPrimByte:
2346 case Primitive::kPrimShort:
2347 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002348 // Processing a Dex `int-to-char' instruction.
2349 locations->SetInAt(0, Location::Any());
2350 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2351 break;
2352
2353 default:
2354 LOG(FATAL) << "Unexpected type conversion from " << input_type
2355 << " to " << result_type;
2356 }
2357 break;
2358
Roland Levillaindff1f282014-11-05 14:15:05 +00002359 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002360 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002361 case Primitive::kPrimBoolean:
2362 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002363 case Primitive::kPrimByte:
2364 case Primitive::kPrimShort:
2365 case Primitive::kPrimInt:
2366 case Primitive::kPrimChar:
2367 // Processing a Dex `int-to-float' instruction.
2368 locations->SetInAt(0, Location::RequiresRegister());
2369 locations->SetOut(Location::RequiresFpuRegister());
2370 break;
2371
2372 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002373 // Processing a Dex `long-to-float' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002374 locations->SetInAt(0, Location::Any());
2375 locations->SetOut(Location::Any());
Roland Levillain6d0e4832014-11-27 18:31:21 +00002376 break;
2377
Roland Levillaincff13742014-11-17 14:32:17 +00002378 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002379 // Processing a Dex `double-to-float' instruction.
2380 locations->SetInAt(0, Location::RequiresFpuRegister());
2381 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002382 break;
2383
2384 default:
2385 LOG(FATAL) << "Unexpected type conversion from " << input_type
2386 << " to " << result_type;
2387 };
2388 break;
2389
Roland Levillaindff1f282014-11-05 14:15:05 +00002390 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002391 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002392 case Primitive::kPrimBoolean:
2393 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002394 case Primitive::kPrimByte:
2395 case Primitive::kPrimShort:
2396 case Primitive::kPrimInt:
2397 case Primitive::kPrimChar:
2398 // Processing a Dex `int-to-double' instruction.
2399 locations->SetInAt(0, Location::RequiresRegister());
2400 locations->SetOut(Location::RequiresFpuRegister());
2401 break;
2402
2403 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002404 // Processing a Dex `long-to-double' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002405 locations->SetInAt(0, Location::Any());
2406 locations->SetOut(Location::Any());
Roland Levillain647b9ed2014-11-27 12:06:00 +00002407 break;
2408
Roland Levillaincff13742014-11-17 14:32:17 +00002409 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002410 // Processing a Dex `float-to-double' instruction.
2411 locations->SetInAt(0, Location::RequiresFpuRegister());
2412 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002413 break;
2414
2415 default:
2416 LOG(FATAL) << "Unexpected type conversion from " << input_type
2417 << " to " << result_type;
2418 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002419 break;
2420
2421 default:
2422 LOG(FATAL) << "Unexpected type conversion from " << input_type
2423 << " to " << result_type;
2424 }
2425}
2426
2427void InstructionCodeGeneratorX86::VisitTypeConversion(HTypeConversion* conversion) {
2428 LocationSummary* locations = conversion->GetLocations();
2429 Location out = locations->Out();
2430 Location in = locations->InAt(0);
2431 Primitive::Type result_type = conversion->GetResultType();
2432 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002433 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00002434 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002435 case Primitive::kPrimByte:
2436 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002437 case Primitive::kPrimLong:
2438 // Type conversion from long to byte is a result of code transformations.
2439 if (in.IsRegisterPair()) {
2440 __ movsxb(out.AsRegister<Register>(), in.AsRegisterPairLow<ByteRegister>());
2441 } else {
2442 DCHECK(in.GetConstant()->IsLongConstant());
2443 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2444 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
2445 }
2446 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002447 case Primitive::kPrimBoolean:
2448 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002449 case Primitive::kPrimShort:
2450 case Primitive::kPrimInt:
2451 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002452 // Processing a Dex `int-to-byte' instruction.
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002453 if (in.IsRegister()) {
2454 __ movsxb(out.AsRegister<Register>(), in.AsRegister<ByteRegister>());
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002455 } else {
2456 DCHECK(in.GetConstant()->IsIntConstant());
2457 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
2458 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
2459 }
Roland Levillain51d3fc42014-11-13 14:11:42 +00002460 break;
2461
2462 default:
2463 LOG(FATAL) << "Unexpected type conversion from " << input_type
2464 << " to " << result_type;
2465 }
2466 break;
2467
Roland Levillain01a8d712014-11-14 16:27:39 +00002468 case Primitive::kPrimShort:
2469 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002470 case Primitive::kPrimLong:
2471 // Type conversion from long to short is a result of code transformations.
2472 if (in.IsRegisterPair()) {
2473 __ movsxw(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
2474 } else if (in.IsDoubleStackSlot()) {
2475 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
2476 } else {
2477 DCHECK(in.GetConstant()->IsLongConstant());
2478 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2479 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
2480 }
2481 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002482 case Primitive::kPrimBoolean:
2483 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002484 case Primitive::kPrimByte:
2485 case Primitive::kPrimInt:
2486 case Primitive::kPrimChar:
2487 // Processing a Dex `int-to-short' instruction.
2488 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002489 __ movsxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain01a8d712014-11-14 16:27:39 +00002490 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002491 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain01a8d712014-11-14 16:27:39 +00002492 } else {
2493 DCHECK(in.GetConstant()->IsIntConstant());
2494 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002495 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
Roland Levillain01a8d712014-11-14 16:27:39 +00002496 }
2497 break;
2498
2499 default:
2500 LOG(FATAL) << "Unexpected type conversion from " << input_type
2501 << " to " << result_type;
2502 }
2503 break;
2504
Roland Levillain946e1432014-11-11 17:35:19 +00002505 case Primitive::kPrimInt:
2506 switch (input_type) {
2507 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002508 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002509 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002510 __ movl(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00002511 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002512 __ movl(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain946e1432014-11-11 17:35:19 +00002513 } else {
2514 DCHECK(in.IsConstant());
2515 DCHECK(in.GetConstant()->IsLongConstant());
2516 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002517 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00002518 }
2519 break;
2520
Roland Levillain3f8f9362014-12-02 17:45:01 +00002521 case Primitive::kPrimFloat: {
2522 // Processing a Dex `float-to-int' instruction.
2523 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2524 Register output = out.AsRegister<Register>();
2525 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002526 NearLabel done, nan;
Roland Levillain3f8f9362014-12-02 17:45:01 +00002527
2528 __ movl(output, Immediate(kPrimIntMax));
2529 // temp = int-to-float(output)
2530 __ cvtsi2ss(temp, output);
2531 // if input >= temp goto done
2532 __ comiss(input, temp);
2533 __ j(kAboveEqual, &done);
2534 // if input == NaN goto nan
2535 __ j(kUnordered, &nan);
2536 // output = float-to-int-truncate(input)
2537 __ cvttss2si(output, input);
2538 __ jmp(&done);
2539 __ Bind(&nan);
2540 // output = 0
2541 __ xorl(output, output);
2542 __ Bind(&done);
2543 break;
2544 }
2545
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002546 case Primitive::kPrimDouble: {
2547 // Processing a Dex `double-to-int' instruction.
2548 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2549 Register output = out.AsRegister<Register>();
2550 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002551 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002552
2553 __ movl(output, Immediate(kPrimIntMax));
2554 // temp = int-to-double(output)
2555 __ cvtsi2sd(temp, output);
2556 // if input >= temp goto done
2557 __ comisd(input, temp);
2558 __ j(kAboveEqual, &done);
2559 // if input == NaN goto nan
2560 __ j(kUnordered, &nan);
2561 // output = double-to-int-truncate(input)
2562 __ cvttsd2si(output, input);
2563 __ jmp(&done);
2564 __ Bind(&nan);
2565 // output = 0
2566 __ xorl(output, output);
2567 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00002568 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002569 }
Roland Levillain946e1432014-11-11 17:35:19 +00002570
2571 default:
2572 LOG(FATAL) << "Unexpected type conversion from " << input_type
2573 << " to " << result_type;
2574 }
2575 break;
2576
Roland Levillaindff1f282014-11-05 14:15:05 +00002577 case Primitive::kPrimLong:
2578 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002579 case Primitive::kPrimBoolean:
2580 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002581 case Primitive::kPrimByte:
2582 case Primitive::kPrimShort:
2583 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002584 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002585 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002586 DCHECK_EQ(out.AsRegisterPairLow<Register>(), EAX);
2587 DCHECK_EQ(out.AsRegisterPairHigh<Register>(), EDX);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002588 DCHECK_EQ(in.AsRegister<Register>(), EAX);
Roland Levillaindff1f282014-11-05 14:15:05 +00002589 __ cdq();
2590 break;
2591
2592 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00002593 // Processing a Dex `float-to-long' instruction.
Alexandre Rames8158f282015-08-07 10:26:17 +01002594 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pF2l),
2595 conversion,
2596 conversion->GetDexPc(),
2597 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00002598 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
Roland Levillain624279f2014-12-04 11:54:28 +00002599 break;
2600
Roland Levillaindff1f282014-11-05 14:15:05 +00002601 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002602 // Processing a Dex `double-to-long' instruction.
Alexandre Rames8158f282015-08-07 10:26:17 +01002603 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pD2l),
2604 conversion,
2605 conversion->GetDexPc(),
2606 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00002607 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
Roland Levillaindff1f282014-11-05 14:15:05 +00002608 break;
2609
2610 default:
2611 LOG(FATAL) << "Unexpected type conversion from " << input_type
2612 << " to " << result_type;
2613 }
2614 break;
2615
Roland Levillain981e4542014-11-14 11:47:14 +00002616 case Primitive::kPrimChar:
2617 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002618 case Primitive::kPrimLong:
2619 // Type conversion from long to short is a result of code transformations.
2620 if (in.IsRegisterPair()) {
2621 __ movzxw(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
2622 } else if (in.IsDoubleStackSlot()) {
2623 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
2624 } else {
2625 DCHECK(in.GetConstant()->IsLongConstant());
2626 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2627 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
2628 }
2629 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002630 case Primitive::kPrimBoolean:
2631 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002632 case Primitive::kPrimByte:
2633 case Primitive::kPrimShort:
2634 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002635 // Processing a Dex `Process a Dex `int-to-char'' instruction.
2636 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002637 __ movzxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain981e4542014-11-14 11:47:14 +00002638 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002639 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain981e4542014-11-14 11:47:14 +00002640 } else {
2641 DCHECK(in.GetConstant()->IsIntConstant());
2642 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002643 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
Roland Levillain981e4542014-11-14 11:47:14 +00002644 }
2645 break;
2646
2647 default:
2648 LOG(FATAL) << "Unexpected type conversion from " << input_type
2649 << " to " << result_type;
2650 }
2651 break;
2652
Roland Levillaindff1f282014-11-05 14:15:05 +00002653 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002654 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002655 case Primitive::kPrimBoolean:
2656 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002657 case Primitive::kPrimByte:
2658 case Primitive::kPrimShort:
2659 case Primitive::kPrimInt:
2660 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002661 // Processing a Dex `int-to-float' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002662 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002663 break;
2664
Roland Levillain6d0e4832014-11-27 18:31:21 +00002665 case Primitive::kPrimLong: {
2666 // Processing a Dex `long-to-float' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002667 size_t adjustment = 0;
Roland Levillain6d0e4832014-11-27 18:31:21 +00002668
Roland Levillain232ade02015-04-20 15:14:36 +01002669 // Create stack space for the call to
2670 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstps below.
2671 // TODO: enhance register allocator to ask for stack temporaries.
2672 if (!in.IsDoubleStackSlot() || !out.IsStackSlot()) {
2673 adjustment = Primitive::ComponentSize(Primitive::kPrimLong);
2674 __ subl(ESP, Immediate(adjustment));
2675 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002676
Roland Levillain232ade02015-04-20 15:14:36 +01002677 // Load the value to the FP stack, using temporaries if needed.
2678 PushOntoFPStack(in, 0, adjustment, false, true);
2679
2680 if (out.IsStackSlot()) {
2681 __ fstps(Address(ESP, out.GetStackIndex() + adjustment));
2682 } else {
2683 __ fstps(Address(ESP, 0));
2684 Location stack_temp = Location::StackSlot(0);
2685 codegen_->Move32(out, stack_temp);
2686 }
2687
2688 // Remove the temporary stack space we allocated.
2689 if (adjustment != 0) {
2690 __ addl(ESP, Immediate(adjustment));
2691 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002692 break;
2693 }
2694
Roland Levillaincff13742014-11-17 14:32:17 +00002695 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002696 // Processing a Dex `double-to-float' instruction.
2697 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002698 break;
2699
2700 default:
2701 LOG(FATAL) << "Unexpected type conversion from " << input_type
2702 << " to " << result_type;
2703 };
2704 break;
2705
Roland Levillaindff1f282014-11-05 14:15:05 +00002706 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002707 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002708 case Primitive::kPrimBoolean:
2709 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002710 case Primitive::kPrimByte:
2711 case Primitive::kPrimShort:
2712 case Primitive::kPrimInt:
2713 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002714 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002715 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002716 break;
2717
Roland Levillain647b9ed2014-11-27 12:06:00 +00002718 case Primitive::kPrimLong: {
2719 // Processing a Dex `long-to-double' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002720 size_t adjustment = 0;
Roland Levillain647b9ed2014-11-27 12:06:00 +00002721
Roland Levillain232ade02015-04-20 15:14:36 +01002722 // Create stack space for the call to
2723 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstpl below.
2724 // TODO: enhance register allocator to ask for stack temporaries.
2725 if (!in.IsDoubleStackSlot() || !out.IsDoubleStackSlot()) {
2726 adjustment = Primitive::ComponentSize(Primitive::kPrimLong);
2727 __ subl(ESP, Immediate(adjustment));
2728 }
2729
2730 // Load the value to the FP stack, using temporaries if needed.
2731 PushOntoFPStack(in, 0, adjustment, false, true);
2732
2733 if (out.IsDoubleStackSlot()) {
2734 __ fstpl(Address(ESP, out.GetStackIndex() + adjustment));
2735 } else {
2736 __ fstpl(Address(ESP, 0));
2737 Location stack_temp = Location::DoubleStackSlot(0);
2738 codegen_->Move64(out, stack_temp);
2739 }
2740
2741 // Remove the temporary stack space we allocated.
2742 if (adjustment != 0) {
2743 __ addl(ESP, Immediate(adjustment));
2744 }
Roland Levillain647b9ed2014-11-27 12:06:00 +00002745 break;
2746 }
2747
Roland Levillaincff13742014-11-17 14:32:17 +00002748 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002749 // Processing a Dex `float-to-double' instruction.
2750 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002751 break;
2752
2753 default:
2754 LOG(FATAL) << "Unexpected type conversion from " << input_type
2755 << " to " << result_type;
2756 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002757 break;
2758
2759 default:
2760 LOG(FATAL) << "Unexpected type conversion from " << input_type
2761 << " to " << result_type;
2762 }
2763}
2764
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002765void LocationsBuilderX86::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002766 LocationSummary* locations =
2767 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002768 switch (add->GetResultType()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002769 case Primitive::kPrimInt: {
2770 locations->SetInAt(0, Location::RequiresRegister());
2771 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
2772 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2773 break;
2774 }
2775
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002776 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002777 locations->SetInAt(0, Location::RequiresRegister());
2778 locations->SetInAt(1, Location::Any());
2779 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002780 break;
2781 }
2782
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002783 case Primitive::kPrimFloat:
2784 case Primitive::kPrimDouble: {
2785 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002786 if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
2787 DCHECK(add->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00002788 } else if (add->InputAt(1)->IsConstant()) {
2789 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002790 } else {
2791 locations->SetInAt(1, Location::Any());
2792 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002793 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002794 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002795 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002796
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002797 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002798 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
2799 break;
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002800 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002801}
2802
2803void InstructionCodeGeneratorX86::VisitAdd(HAdd* add) {
2804 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002805 Location first = locations->InAt(0);
2806 Location second = locations->InAt(1);
Mark Mendell09b84632015-02-13 17:48:38 -05002807 Location out = locations->Out();
2808
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002809 switch (add->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002810 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002811 if (second.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002812 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2813 __ addl(out.AsRegister<Register>(), second.AsRegister<Register>());
Mark Mendell33bf2452015-05-27 10:08:24 -04002814 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
2815 __ addl(out.AsRegister<Register>(), first.AsRegister<Register>());
Mark Mendell09b84632015-02-13 17:48:38 -05002816 } else {
2817 __ leal(out.AsRegister<Register>(), Address(
2818 first.AsRegister<Register>(), second.AsRegister<Register>(), TIMES_1, 0));
2819 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002820 } else if (second.IsConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002821 int32_t value = second.GetConstant()->AsIntConstant()->GetValue();
2822 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2823 __ addl(out.AsRegister<Register>(), Immediate(value));
2824 } else {
2825 __ leal(out.AsRegister<Register>(), Address(first.AsRegister<Register>(), value));
2826 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002827 } else {
Mark Mendell09b84632015-02-13 17:48:38 -05002828 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002829 __ addl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002830 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002831 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002832 }
2833
2834 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002835 if (second.IsRegisterPair()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002836 __ addl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
2837 __ adcl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002838 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002839 __ addl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
2840 __ adcl(first.AsRegisterPairHigh<Register>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002841 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002842 } else {
2843 DCHECK(second.IsConstant()) << second;
2844 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2845 __ addl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
2846 __ adcl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002847 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002848 break;
2849 }
2850
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002851 case Primitive::kPrimFloat: {
2852 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002853 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04002854 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
2855 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00002856 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04002857 __ addss(first.AsFpuRegister<XmmRegister>(),
2858 codegen_->LiteralFloatAddress(
2859 const_area->GetConstant()->AsFloatConstant()->GetValue(),
2860 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2861 } else {
2862 DCHECK(second.IsStackSlot());
2863 __ addss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002864 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002865 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002866 }
2867
2868 case Primitive::kPrimDouble: {
2869 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002870 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04002871 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
2872 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00002873 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04002874 __ addsd(first.AsFpuRegister<XmmRegister>(),
2875 codegen_->LiteralDoubleAddress(
2876 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
2877 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2878 } else {
2879 DCHECK(second.IsDoubleStackSlot());
2880 __ addsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002881 }
2882 break;
2883 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002884
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002885 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002886 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002887 }
2888}
2889
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002890void LocationsBuilderX86::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002891 LocationSummary* locations =
2892 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002893 switch (sub->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002894 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002895 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002896 locations->SetInAt(0, Location::RequiresRegister());
2897 locations->SetInAt(1, Location::Any());
2898 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002899 break;
2900 }
Calin Juravle11351682014-10-23 15:38:15 +01002901 case Primitive::kPrimFloat:
2902 case Primitive::kPrimDouble: {
2903 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002904 if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
2905 DCHECK(sub->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00002906 } else if (sub->InputAt(1)->IsConstant()) {
2907 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002908 } else {
2909 locations->SetInAt(1, Location::Any());
2910 }
Calin Juravle11351682014-10-23 15:38:15 +01002911 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002912 break;
Calin Juravle11351682014-10-23 15:38:15 +01002913 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002914
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002915 default:
Calin Juravle11351682014-10-23 15:38:15 +01002916 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002917 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002918}
2919
2920void InstructionCodeGeneratorX86::VisitSub(HSub* sub) {
2921 LocationSummary* locations = sub->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002922 Location first = locations->InAt(0);
2923 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01002924 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002925 switch (sub->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002926 case Primitive::kPrimInt: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002927 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002928 __ subl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002929 } else if (second.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002930 __ subl(first.AsRegister<Register>(),
2931 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002932 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002933 __ subl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002934 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002935 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002936 }
2937
2938 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002939 if (second.IsRegisterPair()) {
Calin Juravle11351682014-10-23 15:38:15 +01002940 __ subl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
2941 __ sbbl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002942 } else if (second.IsDoubleStackSlot()) {
Calin Juravle11351682014-10-23 15:38:15 +01002943 __ subl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002944 __ sbbl(first.AsRegisterPairHigh<Register>(),
2945 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002946 } else {
2947 DCHECK(second.IsConstant()) << second;
2948 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2949 __ subl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
2950 __ sbbl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002951 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002952 break;
2953 }
2954
Calin Juravle11351682014-10-23 15:38:15 +01002955 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04002956 if (second.IsFpuRegister()) {
2957 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2958 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
2959 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00002960 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04002961 __ subss(first.AsFpuRegister<XmmRegister>(),
2962 codegen_->LiteralFloatAddress(
2963 const_area->GetConstant()->AsFloatConstant()->GetValue(),
2964 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2965 } else {
2966 DCHECK(second.IsStackSlot());
2967 __ subss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
2968 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002969 break;
Calin Juravle11351682014-10-23 15:38:15 +01002970 }
2971
2972 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04002973 if (second.IsFpuRegister()) {
2974 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2975 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
2976 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00002977 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04002978 __ subsd(first.AsFpuRegister<XmmRegister>(),
2979 codegen_->LiteralDoubleAddress(
2980 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
2981 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2982 } else {
2983 DCHECK(second.IsDoubleStackSlot());
2984 __ subsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
2985 }
Calin Juravle11351682014-10-23 15:38:15 +01002986 break;
2987 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002988
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002989 default:
Calin Juravle11351682014-10-23 15:38:15 +01002990 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002991 }
2992}
2993
Calin Juravle34bacdf2014-10-07 20:23:36 +01002994void LocationsBuilderX86::VisitMul(HMul* mul) {
2995 LocationSummary* locations =
2996 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
2997 switch (mul->GetResultType()) {
2998 case Primitive::kPrimInt:
2999 locations->SetInAt(0, Location::RequiresRegister());
3000 locations->SetInAt(1, Location::Any());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003001 if (mul->InputAt(1)->IsIntConstant()) {
3002 // Can use 3 operand multiply.
3003 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3004 } else {
3005 locations->SetOut(Location::SameAsFirstInput());
3006 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003007 break;
3008 case Primitive::kPrimLong: {
3009 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003010 locations->SetInAt(1, Location::Any());
3011 locations->SetOut(Location::SameAsFirstInput());
3012 // Needed for imul on 32bits with 64bits output.
3013 locations->AddTemp(Location::RegisterLocation(EAX));
3014 locations->AddTemp(Location::RegisterLocation(EDX));
3015 break;
3016 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003017 case Primitive::kPrimFloat:
3018 case Primitive::kPrimDouble: {
3019 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003020 if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3021 DCHECK(mul->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003022 } else if (mul->InputAt(1)->IsConstant()) {
3023 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003024 } else {
3025 locations->SetInAt(1, Location::Any());
3026 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003027 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003028 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003029 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003030
3031 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003032 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003033 }
3034}
3035
3036void InstructionCodeGeneratorX86::VisitMul(HMul* mul) {
3037 LocationSummary* locations = mul->GetLocations();
3038 Location first = locations->InAt(0);
3039 Location second = locations->InAt(1);
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003040 Location out = locations->Out();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003041
3042 switch (mul->GetResultType()) {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003043 case Primitive::kPrimInt:
3044 // The constant may have ended up in a register, so test explicitly to avoid
3045 // problems where the output may not be the same as the first operand.
3046 if (mul->InputAt(1)->IsIntConstant()) {
3047 Immediate imm(mul->InputAt(1)->AsIntConstant()->GetValue());
3048 __ imull(out.AsRegister<Register>(), first.AsRegister<Register>(), imm);
3049 } else if (second.IsRegister()) {
3050 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003051 __ imull(first.AsRegister<Register>(), second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003052 } else {
3053 DCHECK(second.IsStackSlot());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003054 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003055 __ imull(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01003056 }
3057 break;
Calin Juravle34bacdf2014-10-07 20:23:36 +01003058
3059 case Primitive::kPrimLong: {
Calin Juravle34bacdf2014-10-07 20:23:36 +01003060 Register in1_hi = first.AsRegisterPairHigh<Register>();
3061 Register in1_lo = first.AsRegisterPairLow<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003062 Register eax = locations->GetTemp(0).AsRegister<Register>();
3063 Register edx = locations->GetTemp(1).AsRegister<Register>();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003064
3065 DCHECK_EQ(EAX, eax);
3066 DCHECK_EQ(EDX, edx);
3067
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003068 // input: in1 - 64 bits, in2 - 64 bits.
Calin Juravle34bacdf2014-10-07 20:23:36 +01003069 // output: in1
3070 // formula: in1.hi : in1.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
3071 // parts: in1.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
3072 // parts: in1.lo = (in1.lo * in2.lo)[31:0]
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003073 if (second.IsConstant()) {
3074 DCHECK(second.GetConstant()->IsLongConstant());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003075
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003076 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3077 int32_t low_value = Low32Bits(value);
3078 int32_t high_value = High32Bits(value);
3079 Immediate low(low_value);
3080 Immediate high(high_value);
3081
3082 __ movl(eax, high);
3083 // eax <- in1.lo * in2.hi
3084 __ imull(eax, in1_lo);
3085 // in1.hi <- in1.hi * in2.lo
3086 __ imull(in1_hi, low);
3087 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3088 __ addl(in1_hi, eax);
3089 // move in2_lo to eax to prepare for double precision
3090 __ movl(eax, low);
3091 // edx:eax <- in1.lo * in2.lo
3092 __ mull(in1_lo);
3093 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3094 __ addl(in1_hi, edx);
3095 // in1.lo <- (in1.lo * in2.lo)[31:0];
3096 __ movl(in1_lo, eax);
3097 } else if (second.IsRegisterPair()) {
3098 Register in2_hi = second.AsRegisterPairHigh<Register>();
3099 Register in2_lo = second.AsRegisterPairLow<Register>();
3100
3101 __ movl(eax, in2_hi);
3102 // eax <- in1.lo * in2.hi
3103 __ imull(eax, in1_lo);
3104 // in1.hi <- in1.hi * in2.lo
3105 __ imull(in1_hi, in2_lo);
3106 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3107 __ addl(in1_hi, eax);
3108 // move in1_lo to eax to prepare for double precision
3109 __ movl(eax, in1_lo);
3110 // edx:eax <- in1.lo * in2.lo
3111 __ mull(in2_lo);
3112 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3113 __ addl(in1_hi, edx);
3114 // in1.lo <- (in1.lo * in2.lo)[31:0];
3115 __ movl(in1_lo, eax);
3116 } else {
3117 DCHECK(second.IsDoubleStackSlot()) << second;
3118 Address in2_hi(ESP, second.GetHighStackIndex(kX86WordSize));
3119 Address in2_lo(ESP, second.GetStackIndex());
3120
3121 __ movl(eax, in2_hi);
3122 // eax <- in1.lo * in2.hi
3123 __ imull(eax, in1_lo);
3124 // in1.hi <- in1.hi * in2.lo
3125 __ imull(in1_hi, in2_lo);
3126 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3127 __ addl(in1_hi, eax);
3128 // move in1_lo to eax to prepare for double precision
3129 __ movl(eax, in1_lo);
3130 // edx:eax <- in1.lo * in2.lo
3131 __ mull(in2_lo);
3132 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3133 __ addl(in1_hi, edx);
3134 // in1.lo <- (in1.lo * in2.lo)[31:0];
3135 __ movl(in1_lo, eax);
3136 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003137
3138 break;
3139 }
3140
Calin Juravleb5bfa962014-10-21 18:02:24 +01003141 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003142 DCHECK(first.Equals(locations->Out()));
3143 if (second.IsFpuRegister()) {
3144 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3145 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3146 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003147 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003148 __ mulss(first.AsFpuRegister<XmmRegister>(),
3149 codegen_->LiteralFloatAddress(
3150 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3151 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3152 } else {
3153 DCHECK(second.IsStackSlot());
3154 __ mulss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3155 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003156 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003157 }
3158
3159 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003160 DCHECK(first.Equals(locations->Out()));
3161 if (second.IsFpuRegister()) {
3162 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3163 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3164 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003165 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003166 __ mulsd(first.AsFpuRegister<XmmRegister>(),
3167 codegen_->LiteralDoubleAddress(
3168 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3169 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3170 } else {
3171 DCHECK(second.IsDoubleStackSlot());
3172 __ mulsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3173 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003174 break;
3175 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003176
3177 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003178 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003179 }
3180}
3181
Roland Levillain232ade02015-04-20 15:14:36 +01003182void InstructionCodeGeneratorX86::PushOntoFPStack(Location source,
3183 uint32_t temp_offset,
3184 uint32_t stack_adjustment,
3185 bool is_fp,
3186 bool is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003187 if (source.IsStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01003188 DCHECK(!is_wide);
3189 if (is_fp) {
3190 __ flds(Address(ESP, source.GetStackIndex() + stack_adjustment));
3191 } else {
3192 __ filds(Address(ESP, source.GetStackIndex() + stack_adjustment));
3193 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003194 } else if (source.IsDoubleStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01003195 DCHECK(is_wide);
3196 if (is_fp) {
3197 __ fldl(Address(ESP, source.GetStackIndex() + stack_adjustment));
3198 } else {
3199 __ fildl(Address(ESP, source.GetStackIndex() + stack_adjustment));
3200 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003201 } else {
3202 // Write the value to the temporary location on the stack and load to FP stack.
Roland Levillain232ade02015-04-20 15:14:36 +01003203 if (!is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003204 Location stack_temp = Location::StackSlot(temp_offset);
3205 codegen_->Move32(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01003206 if (is_fp) {
3207 __ flds(Address(ESP, temp_offset));
3208 } else {
3209 __ filds(Address(ESP, temp_offset));
3210 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003211 } else {
3212 Location stack_temp = Location::DoubleStackSlot(temp_offset);
3213 codegen_->Move64(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01003214 if (is_fp) {
3215 __ fldl(Address(ESP, temp_offset));
3216 } else {
3217 __ fildl(Address(ESP, temp_offset));
3218 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003219 }
3220 }
3221}
3222
3223void InstructionCodeGeneratorX86::GenerateRemFP(HRem *rem) {
3224 Primitive::Type type = rem->GetResultType();
3225 bool is_float = type == Primitive::kPrimFloat;
3226 size_t elem_size = Primitive::ComponentSize(type);
3227 LocationSummary* locations = rem->GetLocations();
3228 Location first = locations->InAt(0);
3229 Location second = locations->InAt(1);
3230 Location out = locations->Out();
3231
3232 // Create stack space for 2 elements.
3233 // TODO: enhance register allocator to ask for stack temporaries.
3234 __ subl(ESP, Immediate(2 * elem_size));
3235
3236 // Load the values to the FP stack in reverse order, using temporaries if needed.
Roland Levillain232ade02015-04-20 15:14:36 +01003237 const bool is_wide = !is_float;
3238 PushOntoFPStack(second, elem_size, 2 * elem_size, /* is_fp */ true, is_wide);
3239 PushOntoFPStack(first, 0, 2 * elem_size, /* is_fp */ true, is_wide);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003240
3241 // Loop doing FPREM until we stabilize.
Mark Mendell0c9497d2015-08-21 09:30:05 -04003242 NearLabel retry;
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003243 __ Bind(&retry);
3244 __ fprem();
3245
3246 // Move FP status to AX.
3247 __ fstsw();
3248
3249 // And see if the argument reduction is complete. This is signaled by the
3250 // C2 FPU flag bit set to 0.
3251 __ andl(EAX, Immediate(kC2ConditionMask));
3252 __ j(kNotEqual, &retry);
3253
3254 // We have settled on the final value. Retrieve it into an XMM register.
3255 // Store FP top of stack to real stack.
3256 if (is_float) {
3257 __ fsts(Address(ESP, 0));
3258 } else {
3259 __ fstl(Address(ESP, 0));
3260 }
3261
3262 // Pop the 2 items from the FP stack.
3263 __ fucompp();
3264
3265 // Load the value from the stack into an XMM register.
3266 DCHECK(out.IsFpuRegister()) << out;
3267 if (is_float) {
3268 __ movss(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
3269 } else {
3270 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
3271 }
3272
3273 // And remove the temporary stack space we allocated.
3274 __ addl(ESP, Immediate(2 * elem_size));
3275}
3276
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003277
3278void InstructionCodeGeneratorX86::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3279 DCHECK(instruction->IsDiv() || instruction->IsRem());
3280
3281 LocationSummary* locations = instruction->GetLocations();
3282 DCHECK(locations->InAt(1).IsConstant());
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003283 DCHECK(locations->InAt(1).GetConstant()->IsIntConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003284
3285 Register out_register = locations->Out().AsRegister<Register>();
3286 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003287 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003288
3289 DCHECK(imm == 1 || imm == -1);
3290
3291 if (instruction->IsRem()) {
3292 __ xorl(out_register, out_register);
3293 } else {
3294 __ movl(out_register, input_register);
3295 if (imm == -1) {
3296 __ negl(out_register);
3297 }
3298 }
3299}
3300
3301
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003302void InstructionCodeGeneratorX86::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003303 LocationSummary* locations = instruction->GetLocations();
3304
3305 Register out_register = locations->Out().AsRegister<Register>();
3306 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003307 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003308 DCHECK(IsPowerOfTwo(AbsOrMin(imm)));
3309 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003310
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003311 Register num = locations->GetTemp(0).AsRegister<Register>();
3312
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003313 __ leal(num, Address(input_register, abs_imm - 1));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003314 __ testl(input_register, input_register);
3315 __ cmovl(kGreaterEqual, num, input_register);
3316 int shift = CTZ(imm);
3317 __ sarl(num, Immediate(shift));
3318
3319 if (imm < 0) {
3320 __ negl(num);
3321 }
3322
3323 __ movl(out_register, num);
3324}
3325
3326void InstructionCodeGeneratorX86::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3327 DCHECK(instruction->IsDiv() || instruction->IsRem());
3328
3329 LocationSummary* locations = instruction->GetLocations();
3330 int imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
3331
3332 Register eax = locations->InAt(0).AsRegister<Register>();
3333 Register out = locations->Out().AsRegister<Register>();
3334 Register num;
3335 Register edx;
3336
3337 if (instruction->IsDiv()) {
3338 edx = locations->GetTemp(0).AsRegister<Register>();
3339 num = locations->GetTemp(1).AsRegister<Register>();
3340 } else {
3341 edx = locations->Out().AsRegister<Register>();
3342 num = locations->GetTemp(0).AsRegister<Register>();
3343 }
3344
3345 DCHECK_EQ(EAX, eax);
3346 DCHECK_EQ(EDX, edx);
3347 if (instruction->IsDiv()) {
3348 DCHECK_EQ(EAX, out);
3349 } else {
3350 DCHECK_EQ(EDX, out);
3351 }
3352
3353 int64_t magic;
3354 int shift;
3355 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
3356
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003357 // Save the numerator.
3358 __ movl(num, eax);
3359
3360 // EAX = magic
3361 __ movl(eax, Immediate(magic));
3362
3363 // EDX:EAX = magic * numerator
3364 __ imull(num);
3365
3366 if (imm > 0 && magic < 0) {
3367 // EDX += num
3368 __ addl(edx, num);
3369 } else if (imm < 0 && magic > 0) {
3370 __ subl(edx, num);
3371 }
3372
3373 // Shift if needed.
3374 if (shift != 0) {
3375 __ sarl(edx, Immediate(shift));
3376 }
3377
3378 // EDX += 1 if EDX < 0
3379 __ movl(eax, edx);
3380 __ shrl(edx, Immediate(31));
3381 __ addl(edx, eax);
3382
3383 if (instruction->IsRem()) {
3384 __ movl(eax, num);
3385 __ imull(edx, Immediate(imm));
3386 __ subl(eax, edx);
3387 __ movl(edx, eax);
3388 } else {
3389 __ movl(eax, edx);
3390 }
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003391}
3392
Calin Juravlebacfec32014-11-14 15:54:36 +00003393void InstructionCodeGeneratorX86::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3394 DCHECK(instruction->IsDiv() || instruction->IsRem());
3395
3396 LocationSummary* locations = instruction->GetLocations();
3397 Location out = locations->Out();
3398 Location first = locations->InAt(0);
3399 Location second = locations->InAt(1);
3400 bool is_div = instruction->IsDiv();
3401
3402 switch (instruction->GetResultType()) {
3403 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003404 DCHECK_EQ(EAX, first.AsRegister<Register>());
3405 DCHECK_EQ(is_div ? EAX : EDX, out.AsRegister<Register>());
Calin Juravlebacfec32014-11-14 15:54:36 +00003406
Vladimir Marko13c86fd2015-11-11 12:37:46 +00003407 if (second.IsConstant()) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003408 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003409
3410 if (imm == 0) {
3411 // Do not generate anything for 0. DivZeroCheck would forbid any generated code.
3412 } else if (imm == 1 || imm == -1) {
3413 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003414 } else if (is_div && IsPowerOfTwo(AbsOrMin(imm))) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003415 DivByPowerOfTwo(instruction->AsDiv());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003416 } else {
3417 DCHECK(imm <= -2 || imm >= 2);
3418 GenerateDivRemWithAnyConstant(instruction);
3419 }
3420 } else {
David Srbecky9cd6d372016-02-09 15:24:47 +00003421 SlowPathCode* slow_path = new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86(
3422 instruction, out.AsRegister<Register>(), is_div);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003423 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00003424
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003425 Register second_reg = second.AsRegister<Register>();
3426 // 0x80000000/-1 triggers an arithmetic exception!
3427 // Dividing by -1 is actually negation and -0x800000000 = 0x80000000 so
3428 // it's safe to just use negl instead of more complex comparisons.
Calin Juravlebacfec32014-11-14 15:54:36 +00003429
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003430 __ cmpl(second_reg, Immediate(-1));
3431 __ j(kEqual, slow_path->GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +00003432
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003433 // edx:eax <- sign-extended of eax
3434 __ cdq();
3435 // eax = quotient, edx = remainder
3436 __ idivl(second_reg);
3437 __ Bind(slow_path->GetExitLabel());
3438 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003439 break;
3440 }
3441
3442 case Primitive::kPrimLong: {
3443 InvokeRuntimeCallingConvention calling_convention;
3444 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
3445 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
3446 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
3447 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
3448 DCHECK_EQ(EAX, out.AsRegisterPairLow<Register>());
3449 DCHECK_EQ(EDX, out.AsRegisterPairHigh<Register>());
3450
3451 if (is_div) {
Alexandre Rames8158f282015-08-07 10:26:17 +01003452 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLdiv),
3453 instruction,
3454 instruction->GetDexPc(),
3455 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003456 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003457 } else {
Alexandre Rames8158f282015-08-07 10:26:17 +01003458 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLmod),
3459 instruction,
3460 instruction->GetDexPc(),
3461 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003462 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003463 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003464 break;
3465 }
3466
3467 default:
3468 LOG(FATAL) << "Unexpected type for GenerateDivRemIntegral " << instruction->GetResultType();
3469 }
3470}
3471
Calin Juravle7c4954d2014-10-28 16:57:40 +00003472void LocationsBuilderX86::VisitDiv(HDiv* div) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003473 LocationSummary::CallKind call_kind = (div->GetResultType() == Primitive::kPrimLong)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003474 ? LocationSummary::kCallOnMainOnly
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003475 : LocationSummary::kNoCall;
3476 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
3477
Calin Juravle7c4954d2014-10-28 16:57:40 +00003478 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00003479 case Primitive::kPrimInt: {
3480 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003481 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00003482 locations->SetOut(Location::SameAsFirstInput());
3483 // Intel uses edx:eax as the dividend.
3484 locations->AddTemp(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003485 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3486 // which enforces results to be in EAX and EDX, things are simpler if we use EAX also as
3487 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003488 if (div->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003489 locations->AddTemp(Location::RequiresRegister());
3490 }
Calin Juravled0d48522014-11-04 16:40:20 +00003491 break;
3492 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003493 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003494 InvokeRuntimeCallingConvention calling_convention;
3495 locations->SetInAt(0, Location::RegisterPairLocation(
3496 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3497 locations->SetInAt(1, Location::RegisterPairLocation(
3498 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3499 // Runtime helper puts the result in EAX, EDX.
3500 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Calin Juravle7c4954d2014-10-28 16:57:40 +00003501 break;
3502 }
3503 case Primitive::kPrimFloat:
3504 case Primitive::kPrimDouble: {
3505 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003506 if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3507 DCHECK(div->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003508 } else if (div->InputAt(1)->IsConstant()) {
3509 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003510 } else {
3511 locations->SetInAt(1, Location::Any());
3512 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003513 locations->SetOut(Location::SameAsFirstInput());
3514 break;
3515 }
3516
3517 default:
3518 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3519 }
3520}
3521
3522void InstructionCodeGeneratorX86::VisitDiv(HDiv* div) {
3523 LocationSummary* locations = div->GetLocations();
3524 Location first = locations->InAt(0);
3525 Location second = locations->InAt(1);
Calin Juravle7c4954d2014-10-28 16:57:40 +00003526
3527 switch (div->GetResultType()) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003528 case Primitive::kPrimInt:
Calin Juravle7c4954d2014-10-28 16:57:40 +00003529 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003530 GenerateDivRemIntegral(div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00003531 break;
3532 }
3533
3534 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003535 if (second.IsFpuRegister()) {
3536 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3537 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3538 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003539 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003540 __ divss(first.AsFpuRegister<XmmRegister>(),
3541 codegen_->LiteralFloatAddress(
3542 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3543 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3544 } else {
3545 DCHECK(second.IsStackSlot());
3546 __ divss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3547 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003548 break;
3549 }
3550
3551 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003552 if (second.IsFpuRegister()) {
3553 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3554 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3555 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003556 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003557 __ divsd(first.AsFpuRegister<XmmRegister>(),
3558 codegen_->LiteralDoubleAddress(
3559 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3560 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3561 } else {
3562 DCHECK(second.IsDoubleStackSlot());
3563 __ divsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3564 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003565 break;
3566 }
3567
3568 default:
3569 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3570 }
3571}
3572
Calin Juravlebacfec32014-11-14 15:54:36 +00003573void LocationsBuilderX86::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00003574 Primitive::Type type = rem->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003575
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003576 LocationSummary::CallKind call_kind = (rem->GetResultType() == Primitive::kPrimLong)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003577 ? LocationSummary::kCallOnMainOnly
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003578 : LocationSummary::kNoCall;
3579 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
Calin Juravlebacfec32014-11-14 15:54:36 +00003580
Calin Juravled2ec87d2014-12-08 14:24:46 +00003581 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003582 case Primitive::kPrimInt: {
3583 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003584 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00003585 locations->SetOut(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003586 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3587 // which enforces results to be in EAX and EDX, things are simpler if we use EDX also as
3588 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003589 if (rem->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003590 locations->AddTemp(Location::RequiresRegister());
3591 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003592 break;
3593 }
3594 case Primitive::kPrimLong: {
3595 InvokeRuntimeCallingConvention calling_convention;
3596 locations->SetInAt(0, Location::RegisterPairLocation(
3597 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3598 locations->SetInAt(1, Location::RegisterPairLocation(
3599 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3600 // Runtime helper puts the result in EAX, EDX.
3601 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
3602 break;
3603 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003604 case Primitive::kPrimDouble:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003605 case Primitive::kPrimFloat: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003606 locations->SetInAt(0, Location::Any());
3607 locations->SetInAt(1, Location::Any());
3608 locations->SetOut(Location::RequiresFpuRegister());
3609 locations->AddTemp(Location::RegisterLocation(EAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00003610 break;
3611 }
3612
3613 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003614 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003615 }
3616}
3617
3618void InstructionCodeGeneratorX86::VisitRem(HRem* rem) {
3619 Primitive::Type type = rem->GetResultType();
3620 switch (type) {
3621 case Primitive::kPrimInt:
3622 case Primitive::kPrimLong: {
3623 GenerateDivRemIntegral(rem);
3624 break;
3625 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003626 case Primitive::kPrimFloat:
Calin Juravlebacfec32014-11-14 15:54:36 +00003627 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003628 GenerateRemFP(rem);
Calin Juravlebacfec32014-11-14 15:54:36 +00003629 break;
3630 }
3631 default:
3632 LOG(FATAL) << "Unexpected rem type " << type;
3633 }
3634}
3635
Calin Juravled0d48522014-11-04 16:40:20 +00003636void LocationsBuilderX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00003637 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
3638 ? LocationSummary::kCallOnSlowPath
3639 : LocationSummary::kNoCall;
3640 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003641 switch (instruction->GetType()) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003642 case Primitive::kPrimBoolean:
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003643 case Primitive::kPrimByte:
3644 case Primitive::kPrimChar:
3645 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003646 case Primitive::kPrimInt: {
3647 locations->SetInAt(0, Location::Any());
3648 break;
3649 }
3650 case Primitive::kPrimLong: {
3651 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
3652 if (!instruction->IsConstant()) {
3653 locations->AddTemp(Location::RequiresRegister());
3654 }
3655 break;
3656 }
3657 default:
3658 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
3659 }
Calin Juravled0d48522014-11-04 16:40:20 +00003660 if (instruction->HasUses()) {
3661 locations->SetOut(Location::SameAsFirstInput());
3662 }
3663}
3664
3665void InstructionCodeGeneratorX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003666 SlowPathCode* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86(instruction);
Calin Juravled0d48522014-11-04 16:40:20 +00003667 codegen_->AddSlowPath(slow_path);
3668
3669 LocationSummary* locations = instruction->GetLocations();
3670 Location value = locations->InAt(0);
3671
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003672 switch (instruction->GetType()) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003673 case Primitive::kPrimBoolean:
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003674 case Primitive::kPrimByte:
3675 case Primitive::kPrimChar:
3676 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003677 case Primitive::kPrimInt: {
3678 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003679 __ testl(value.AsRegister<Register>(), value.AsRegister<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003680 __ j(kEqual, slow_path->GetEntryLabel());
3681 } else if (value.IsStackSlot()) {
3682 __ cmpl(Address(ESP, value.GetStackIndex()), Immediate(0));
3683 __ j(kEqual, slow_path->GetEntryLabel());
3684 } else {
3685 DCHECK(value.IsConstant()) << value;
3686 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
3687 __ jmp(slow_path->GetEntryLabel());
3688 }
3689 }
3690 break;
Calin Juravled0d48522014-11-04 16:40:20 +00003691 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003692 case Primitive::kPrimLong: {
3693 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003694 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003695 __ movl(temp, value.AsRegisterPairLow<Register>());
3696 __ orl(temp, value.AsRegisterPairHigh<Register>());
3697 __ j(kEqual, slow_path->GetEntryLabel());
3698 } else {
3699 DCHECK(value.IsConstant()) << value;
3700 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
3701 __ jmp(slow_path->GetEntryLabel());
3702 }
3703 }
3704 break;
3705 }
3706 default:
3707 LOG(FATAL) << "Unexpected type for HDivZeroCheck" << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00003708 }
Calin Juravled0d48522014-11-04 16:40:20 +00003709}
3710
Calin Juravle9aec02f2014-11-18 23:06:35 +00003711void LocationsBuilderX86::HandleShift(HBinaryOperation* op) {
3712 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3713
3714 LocationSummary* locations =
3715 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
3716
3717 switch (op->GetResultType()) {
Mark P Mendell73945692015-04-29 14:56:17 +00003718 case Primitive::kPrimInt:
Calin Juravle9aec02f2014-11-18 23:06:35 +00003719 case Primitive::kPrimLong: {
Mark P Mendell73945692015-04-29 14:56:17 +00003720 // Can't have Location::Any() and output SameAsFirstInput()
Calin Juravle9aec02f2014-11-18 23:06:35 +00003721 locations->SetInAt(0, Location::RequiresRegister());
Mark P Mendell73945692015-04-29 14:56:17 +00003722 // The shift count needs to be in CL or a constant.
3723 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, op->InputAt(1)));
Calin Juravle9aec02f2014-11-18 23:06:35 +00003724 locations->SetOut(Location::SameAsFirstInput());
3725 break;
3726 }
3727 default:
3728 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
3729 }
3730}
3731
3732void InstructionCodeGeneratorX86::HandleShift(HBinaryOperation* op) {
3733 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3734
3735 LocationSummary* locations = op->GetLocations();
3736 Location first = locations->InAt(0);
3737 Location second = locations->InAt(1);
3738 DCHECK(first.Equals(locations->Out()));
3739
3740 switch (op->GetResultType()) {
3741 case Primitive::kPrimInt: {
Mark P Mendell73945692015-04-29 14:56:17 +00003742 DCHECK(first.IsRegister());
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003743 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003744 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003745 Register second_reg = second.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003746 DCHECK_EQ(ECX, second_reg);
3747 if (op->IsShl()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003748 __ shll(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003749 } else if (op->IsShr()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003750 __ sarl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003751 } else {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003752 __ shrl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003753 }
3754 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003755 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance;
Mark P Mendell73945692015-04-29 14:56:17 +00003756 if (shift == 0) {
3757 return;
3758 }
3759 Immediate imm(shift);
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003760 if (op->IsShl()) {
3761 __ shll(first_reg, imm);
3762 } else if (op->IsShr()) {
3763 __ sarl(first_reg, imm);
3764 } else {
3765 __ shrl(first_reg, imm);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003766 }
3767 }
3768 break;
3769 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003770 case Primitive::kPrimLong: {
Mark P Mendell73945692015-04-29 14:56:17 +00003771 if (second.IsRegister()) {
3772 Register second_reg = second.AsRegister<Register>();
3773 DCHECK_EQ(ECX, second_reg);
3774 if (op->IsShl()) {
3775 GenerateShlLong(first, second_reg);
3776 } else if (op->IsShr()) {
3777 GenerateShrLong(first, second_reg);
3778 } else {
3779 GenerateUShrLong(first, second_reg);
3780 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003781 } else {
Mark P Mendell73945692015-04-29 14:56:17 +00003782 // Shift by a constant.
Roland Levillain5b5b9312016-03-22 14:57:31 +00003783 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance;
Mark P Mendell73945692015-04-29 14:56:17 +00003784 // Nothing to do if the shift is 0, as the input is already the output.
3785 if (shift != 0) {
3786 if (op->IsShl()) {
3787 GenerateShlLong(first, shift);
3788 } else if (op->IsShr()) {
3789 GenerateShrLong(first, shift);
3790 } else {
3791 GenerateUShrLong(first, shift);
3792 }
3793 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003794 }
3795 break;
3796 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003797 default:
3798 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
3799 }
3800}
3801
Mark P Mendell73945692015-04-29 14:56:17 +00003802void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, int shift) {
3803 Register low = loc.AsRegisterPairLow<Register>();
3804 Register high = loc.AsRegisterPairHigh<Register>();
Mark Mendellba56d062015-05-05 21:34:03 -04003805 if (shift == 1) {
3806 // This is just an addition.
3807 __ addl(low, low);
3808 __ adcl(high, high);
3809 } else if (shift == 32) {
Mark P Mendell73945692015-04-29 14:56:17 +00003810 // Shift by 32 is easy. High gets low, and low gets 0.
3811 codegen_->EmitParallelMoves(
3812 loc.ToLow(),
3813 loc.ToHigh(),
3814 Primitive::kPrimInt,
3815 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
3816 loc.ToLow(),
3817 Primitive::kPrimInt);
3818 } else if (shift > 32) {
3819 // Low part becomes 0. High part is low part << (shift-32).
3820 __ movl(high, low);
3821 __ shll(high, Immediate(shift - 32));
3822 __ xorl(low, low);
3823 } else {
3824 // Between 1 and 31.
3825 __ shld(high, low, Immediate(shift));
3826 __ shll(low, Immediate(shift));
3827 }
3828}
3829
Calin Juravle9aec02f2014-11-18 23:06:35 +00003830void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04003831 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00003832 __ shld(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>(), shifter);
3833 __ shll(loc.AsRegisterPairLow<Register>(), shifter);
3834 __ testl(shifter, Immediate(32));
3835 __ j(kEqual, &done);
3836 __ movl(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>());
3837 __ movl(loc.AsRegisterPairLow<Register>(), Immediate(0));
3838 __ Bind(&done);
3839}
3840
Mark P Mendell73945692015-04-29 14:56:17 +00003841void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, int shift) {
3842 Register low = loc.AsRegisterPairLow<Register>();
3843 Register high = loc.AsRegisterPairHigh<Register>();
3844 if (shift == 32) {
3845 // Need to copy the sign.
3846 DCHECK_NE(low, high);
3847 __ movl(low, high);
3848 __ sarl(high, Immediate(31));
3849 } else if (shift > 32) {
3850 DCHECK_NE(low, high);
3851 // High part becomes sign. Low part is shifted by shift - 32.
3852 __ movl(low, high);
3853 __ sarl(high, Immediate(31));
3854 __ sarl(low, Immediate(shift - 32));
3855 } else {
3856 // Between 1 and 31.
3857 __ shrd(low, high, Immediate(shift));
3858 __ sarl(high, Immediate(shift));
3859 }
3860}
3861
Calin Juravle9aec02f2014-11-18 23:06:35 +00003862void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04003863 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00003864 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
3865 __ sarl(loc.AsRegisterPairHigh<Register>(), shifter);
3866 __ testl(shifter, Immediate(32));
3867 __ j(kEqual, &done);
3868 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
3869 __ sarl(loc.AsRegisterPairHigh<Register>(), Immediate(31));
3870 __ Bind(&done);
3871}
3872
Mark P Mendell73945692015-04-29 14:56:17 +00003873void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, int shift) {
3874 Register low = loc.AsRegisterPairLow<Register>();
3875 Register high = loc.AsRegisterPairHigh<Register>();
3876 if (shift == 32) {
3877 // Shift by 32 is easy. Low gets high, and high gets 0.
3878 codegen_->EmitParallelMoves(
3879 loc.ToHigh(),
3880 loc.ToLow(),
3881 Primitive::kPrimInt,
3882 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
3883 loc.ToHigh(),
3884 Primitive::kPrimInt);
3885 } else if (shift > 32) {
3886 // Low part is high >> (shift - 32). High part becomes 0.
3887 __ movl(low, high);
3888 __ shrl(low, Immediate(shift - 32));
3889 __ xorl(high, high);
3890 } else {
3891 // Between 1 and 31.
3892 __ shrd(low, high, Immediate(shift));
3893 __ shrl(high, Immediate(shift));
3894 }
3895}
3896
Calin Juravle9aec02f2014-11-18 23:06:35 +00003897void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04003898 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00003899 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
3900 __ shrl(loc.AsRegisterPairHigh<Register>(), shifter);
3901 __ testl(shifter, Immediate(32));
3902 __ j(kEqual, &done);
3903 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
3904 __ movl(loc.AsRegisterPairHigh<Register>(), Immediate(0));
3905 __ Bind(&done);
3906}
3907
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003908void LocationsBuilderX86::VisitRor(HRor* ror) {
3909 LocationSummary* locations =
3910 new (GetGraph()->GetArena()) LocationSummary(ror, LocationSummary::kNoCall);
3911
3912 switch (ror->GetResultType()) {
3913 case Primitive::kPrimLong:
3914 // Add the temporary needed.
3915 locations->AddTemp(Location::RequiresRegister());
3916 FALLTHROUGH_INTENDED;
3917 case Primitive::kPrimInt:
3918 locations->SetInAt(0, Location::RequiresRegister());
3919 // The shift count needs to be in CL (unless it is a constant).
3920 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, ror->InputAt(1)));
3921 locations->SetOut(Location::SameAsFirstInput());
3922 break;
3923 default:
3924 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
3925 UNREACHABLE();
3926 }
3927}
3928
3929void InstructionCodeGeneratorX86::VisitRor(HRor* ror) {
3930 LocationSummary* locations = ror->GetLocations();
3931 Location first = locations->InAt(0);
3932 Location second = locations->InAt(1);
3933
3934 if (ror->GetResultType() == Primitive::kPrimInt) {
3935 Register first_reg = first.AsRegister<Register>();
3936 if (second.IsRegister()) {
3937 Register second_reg = second.AsRegister<Register>();
3938 __ rorl(first_reg, second_reg);
3939 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003940 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003941 __ rorl(first_reg, imm);
3942 }
3943 return;
3944 }
3945
3946 DCHECK_EQ(ror->GetResultType(), Primitive::kPrimLong);
3947 Register first_reg_lo = first.AsRegisterPairLow<Register>();
3948 Register first_reg_hi = first.AsRegisterPairHigh<Register>();
3949 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
3950 if (second.IsRegister()) {
3951 Register second_reg = second.AsRegister<Register>();
3952 DCHECK_EQ(second_reg, ECX);
3953 __ movl(temp_reg, first_reg_hi);
3954 __ shrd(first_reg_hi, first_reg_lo, second_reg);
3955 __ shrd(first_reg_lo, temp_reg, second_reg);
3956 __ movl(temp_reg, first_reg_hi);
3957 __ testl(second_reg, Immediate(32));
3958 __ cmovl(kNotEqual, first_reg_hi, first_reg_lo);
3959 __ cmovl(kNotEqual, first_reg_lo, temp_reg);
3960 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003961 int32_t shift_amt = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance;
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003962 if (shift_amt == 0) {
3963 // Already fine.
3964 return;
3965 }
3966 if (shift_amt == 32) {
3967 // Just swap.
3968 __ movl(temp_reg, first_reg_lo);
3969 __ movl(first_reg_lo, first_reg_hi);
3970 __ movl(first_reg_hi, temp_reg);
3971 return;
3972 }
3973
3974 Immediate imm(shift_amt);
3975 // Save the constents of the low value.
3976 __ movl(temp_reg, first_reg_lo);
3977
3978 // Shift right into low, feeding bits from high.
3979 __ shrd(first_reg_lo, first_reg_hi, imm);
3980
3981 // Shift right into high, feeding bits from the original low.
3982 __ shrd(first_reg_hi, temp_reg, imm);
3983
3984 // Swap if needed.
3985 if (shift_amt > 32) {
3986 __ movl(temp_reg, first_reg_lo);
3987 __ movl(first_reg_lo, first_reg_hi);
3988 __ movl(first_reg_hi, temp_reg);
3989 }
3990 }
3991}
3992
Calin Juravle9aec02f2014-11-18 23:06:35 +00003993void LocationsBuilderX86::VisitShl(HShl* shl) {
3994 HandleShift(shl);
3995}
3996
3997void InstructionCodeGeneratorX86::VisitShl(HShl* shl) {
3998 HandleShift(shl);
3999}
4000
4001void LocationsBuilderX86::VisitShr(HShr* shr) {
4002 HandleShift(shr);
4003}
4004
4005void InstructionCodeGeneratorX86::VisitShr(HShr* shr) {
4006 HandleShift(shr);
4007}
4008
4009void LocationsBuilderX86::VisitUShr(HUShr* ushr) {
4010 HandleShift(ushr);
4011}
4012
4013void InstructionCodeGeneratorX86::VisitUShr(HUShr* ushr) {
4014 HandleShift(ushr);
4015}
4016
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004017void LocationsBuilderX86::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004018 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004019 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004020 locations->SetOut(Location::RegisterLocation(EAX));
David Brazdil6de19382016-01-08 17:37:10 +00004021 if (instruction->IsStringAlloc()) {
4022 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
4023 } else {
4024 InvokeRuntimeCallingConvention calling_convention;
4025 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4026 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
4027 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004028}
4029
4030void InstructionCodeGeneratorX86::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01004031 // Note: if heap poisoning is enabled, the entry point takes cares
4032 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00004033 if (instruction->IsStringAlloc()) {
4034 // String is allocated through StringFactory. Call NewEmptyString entry point.
4035 Register temp = instruction->GetLocations()->GetTemp(0).AsRegister<Register>();
Andreas Gampe542451c2016-07-26 09:02:02 -07004036 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86PointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00004037 __ fs()->movl(temp, Address::Absolute(QUICK_ENTRY_POINT(pNewEmptyString)));
4038 __ call(Address(temp, code_offset.Int32Value()));
4039 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4040 } else {
4041 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
4042 instruction,
4043 instruction->GetDexPc(),
4044 nullptr);
4045 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
4046 DCHECK(!codegen_->IsLeafMethod());
4047 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004048}
4049
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004050void LocationsBuilderX86::VisitNewArray(HNewArray* instruction) {
4051 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004052 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004053 locations->SetOut(Location::RegisterLocation(EAX));
4054 InvokeRuntimeCallingConvention calling_convention;
4055 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08004056 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004057 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004058}
4059
4060void InstructionCodeGeneratorX86::VisitNewArray(HNewArray* instruction) {
4061 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004062 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
Roland Levillain4d027112015-07-01 15:41:14 +01004063 // Note: if heap poisoning is enabled, the entry point takes cares
4064 // of poisoning the reference.
Calin Juravle175dc732015-08-25 15:42:32 +01004065 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
4066 instruction,
4067 instruction->GetDexPc(),
4068 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00004069 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004070 DCHECK(!codegen_->IsLeafMethod());
4071}
4072
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004073void LocationsBuilderX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004074 LocationSummary* locations =
4075 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01004076 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
4077 if (location.IsStackSlot()) {
4078 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4079 } else if (location.IsDoubleStackSlot()) {
4080 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004081 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01004082 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004083}
4084
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004085void InstructionCodeGeneratorX86::VisitParameterValue(
4086 HParameterValue* instruction ATTRIBUTE_UNUSED) {
4087}
4088
4089void LocationsBuilderX86::VisitCurrentMethod(HCurrentMethod* instruction) {
4090 LocationSummary* locations =
4091 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4092 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
4093}
4094
4095void InstructionCodeGeneratorX86::VisitCurrentMethod(HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004096}
4097
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004098void LocationsBuilderX86::VisitClassTableGet(HClassTableGet* instruction) {
4099 LocationSummary* locations =
4100 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4101 locations->SetInAt(0, Location::RequiresRegister());
4102 locations->SetOut(Location::RequiresRegister());
4103}
4104
4105void InstructionCodeGeneratorX86::VisitClassTableGet(HClassTableGet* instruction) {
4106 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004107 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004108 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004109 instruction->GetIndex(), kX86PointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004110 __ movl(locations->Out().AsRegister<Register>(),
4111 Address(locations->InAt(0).AsRegister<Register>(), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004112 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004113 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00004114 instruction->GetIndex(), kX86PointerSize));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004115 __ movl(locations->Out().AsRegister<Register>(),
4116 Address(locations->InAt(0).AsRegister<Register>(),
4117 mirror::Class::ImtPtrOffset(kX86PointerSize).Uint32Value()));
4118 // temp = temp->GetImtEntryAt(method_offset);
4119 __ movl(locations->Out().AsRegister<Register>(),
4120 Address(locations->Out().AsRegister<Register>(), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004121 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004122}
4123
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004124void LocationsBuilderX86::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004125 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004126 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01004127 locations->SetInAt(0, Location::RequiresRegister());
4128 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004129}
4130
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004131void InstructionCodeGeneratorX86::VisitNot(HNot* not_) {
4132 LocationSummary* locations = not_->GetLocations();
Roland Levillain70566432014-10-24 16:20:17 +01004133 Location in = locations->InAt(0);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01004134 Location out = locations->Out();
Roland Levillain70566432014-10-24 16:20:17 +01004135 DCHECK(in.Equals(out));
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00004136 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004137 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00004138 __ notl(out.AsRegister<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004139 break;
4140
4141 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01004142 __ notl(out.AsRegisterPairLow<Register>());
4143 __ notl(out.AsRegisterPairHigh<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004144 break;
4145
4146 default:
4147 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
4148 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004149}
4150
David Brazdil66d126e2015-04-03 16:02:44 +01004151void LocationsBuilderX86::VisitBooleanNot(HBooleanNot* bool_not) {
4152 LocationSummary* locations =
4153 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
4154 locations->SetInAt(0, Location::RequiresRegister());
4155 locations->SetOut(Location::SameAsFirstInput());
4156}
4157
4158void InstructionCodeGeneratorX86::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01004159 LocationSummary* locations = bool_not->GetLocations();
4160 Location in = locations->InAt(0);
4161 Location out = locations->Out();
4162 DCHECK(in.Equals(out));
4163 __ xorl(out.AsRegister<Register>(), Immediate(1));
4164}
4165
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004166void LocationsBuilderX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004167 LocationSummary* locations =
4168 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00004169 switch (compare->InputAt(0)->GetType()) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00004170 case Primitive::kPrimBoolean:
4171 case Primitive::kPrimByte:
4172 case Primitive::kPrimShort:
4173 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08004174 case Primitive::kPrimInt:
Calin Juravleddb7df22014-11-25 20:56:51 +00004175 case Primitive::kPrimLong: {
4176 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravleddb7df22014-11-25 20:56:51 +00004177 locations->SetInAt(1, Location::Any());
4178 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4179 break;
4180 }
4181 case Primitive::kPrimFloat:
4182 case Primitive::kPrimDouble: {
4183 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004184 if (compare->InputAt(1)->IsX86LoadFromConstantTable()) {
4185 DCHECK(compare->InputAt(1)->IsEmittedAtUseSite());
4186 } else if (compare->InputAt(1)->IsConstant()) {
4187 locations->SetInAt(1, Location::RequiresFpuRegister());
4188 } else {
4189 locations->SetInAt(1, Location::Any());
4190 }
Calin Juravleddb7df22014-11-25 20:56:51 +00004191 locations->SetOut(Location::RequiresRegister());
4192 break;
4193 }
4194 default:
4195 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
4196 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004197}
4198
4199void InstructionCodeGeneratorX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004200 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004201 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00004202 Location left = locations->InAt(0);
4203 Location right = locations->InAt(1);
4204
Mark Mendell0c9497d2015-08-21 09:30:05 -04004205 NearLabel less, greater, done;
Aart Bika19616e2016-02-01 18:57:58 -08004206 Condition less_cond = kLess;
4207
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004208 switch (compare->InputAt(0)->GetType()) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00004209 case Primitive::kPrimBoolean:
4210 case Primitive::kPrimByte:
4211 case Primitive::kPrimShort:
4212 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08004213 case Primitive::kPrimInt: {
Roland Levillain0b671c02016-08-19 12:02:34 +01004214 codegen_->GenerateIntCompare(left, right);
Aart Bika19616e2016-02-01 18:57:58 -08004215 break;
4216 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004217 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004218 Register left_low = left.AsRegisterPairLow<Register>();
4219 Register left_high = left.AsRegisterPairHigh<Register>();
4220 int32_t val_low = 0;
4221 int32_t val_high = 0;
4222 bool right_is_const = false;
4223
4224 if (right.IsConstant()) {
4225 DCHECK(right.GetConstant()->IsLongConstant());
4226 right_is_const = true;
4227 int64_t val = right.GetConstant()->AsLongConstant()->GetValue();
4228 val_low = Low32Bits(val);
4229 val_high = High32Bits(val);
4230 }
4231
Calin Juravleddb7df22014-11-25 20:56:51 +00004232 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004233 __ cmpl(left_high, right.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004234 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004235 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004236 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004237 DCHECK(right_is_const) << right;
Aart Bika19616e2016-02-01 18:57:58 -08004238 codegen_->Compare32BitValue(left_high, val_high);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004239 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004240 __ j(kLess, &less); // Signed compare.
4241 __ j(kGreater, &greater); // Signed compare.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004242 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004243 __ cmpl(left_low, right.AsRegisterPairLow<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004244 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004245 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004246 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004247 DCHECK(right_is_const) << right;
Aart Bika19616e2016-02-01 18:57:58 -08004248 codegen_->Compare32BitValue(left_low, val_low);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004249 }
Aart Bika19616e2016-02-01 18:57:58 -08004250 less_cond = kBelow; // for CF (unsigned).
Calin Juravleddb7df22014-11-25 20:56:51 +00004251 break;
4252 }
4253 case Primitive::kPrimFloat: {
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004254 GenerateFPCompare(left, right, compare, false);
Calin Juravleddb7df22014-11-25 20:56:51 +00004255 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08004256 less_cond = kBelow; // for CF (floats).
Calin Juravleddb7df22014-11-25 20:56:51 +00004257 break;
4258 }
4259 case Primitive::kPrimDouble: {
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004260 GenerateFPCompare(left, right, compare, true);
Calin Juravleddb7df22014-11-25 20:56:51 +00004261 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08004262 less_cond = kBelow; // for CF (floats).
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004263 break;
4264 }
4265 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00004266 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004267 }
Aart Bika19616e2016-02-01 18:57:58 -08004268
Calin Juravleddb7df22014-11-25 20:56:51 +00004269 __ movl(out, Immediate(0));
4270 __ j(kEqual, &done);
Aart Bika19616e2016-02-01 18:57:58 -08004271 __ j(less_cond, &less);
Calin Juravleddb7df22014-11-25 20:56:51 +00004272
4273 __ Bind(&greater);
4274 __ movl(out, Immediate(1));
4275 __ jmp(&done);
4276
4277 __ Bind(&less);
4278 __ movl(out, Immediate(-1));
4279
4280 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004281}
4282
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004283void LocationsBuilderX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004284 LocationSummary* locations =
4285 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko372f10e2016-05-17 16:30:10 +01004286 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01004287 locations->SetInAt(i, Location::Any());
4288 }
4289 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004290}
4291
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004292void InstructionCodeGeneratorX86::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004293 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004294}
4295
Roland Levillain7c1559a2015-12-15 10:55:36 +00004296void CodeGeneratorX86::GenerateMemoryBarrier(MemBarrierKind kind) {
Calin Juravle52c48962014-12-16 17:02:57 +00004297 /*
4298 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
4299 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
4300 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
4301 */
4302 switch (kind) {
4303 case MemBarrierKind::kAnyAny: {
Mark P Mendell17077d82015-12-16 19:15:59 +00004304 MemoryFence();
Calin Juravle52c48962014-12-16 17:02:57 +00004305 break;
4306 }
4307 case MemBarrierKind::kAnyStore:
4308 case MemBarrierKind::kLoadAny:
4309 case MemBarrierKind::kStoreStore: {
4310 // nop
4311 break;
4312 }
Mark Mendell7aa04a12016-01-27 22:39:07 -05004313 case MemBarrierKind::kNTStoreStore:
4314 // Non-Temporal Store/Store needs an explicit fence.
4315 MemoryFence(/* non-temporal */ true);
4316 break;
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004317 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004318}
4319
Vladimir Markodc151b22015-10-15 18:02:30 +01004320HInvokeStaticOrDirect::DispatchInfo CodeGeneratorX86::GetSupportedInvokeStaticOrDirectDispatch(
4321 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
4322 MethodReference target_method ATTRIBUTE_UNUSED) {
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00004323 HInvokeStaticOrDirect::DispatchInfo dispatch_info = desired_dispatch_info;
4324
4325 // We disable pc-relative load when there is an irreducible loop, as the optimization
4326 // is incompatible with it.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004327 // TODO: Create as many X86ComputeBaseMethodAddress instructions
4328 // as needed for methods with irreducible loops.
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00004329 if (GetGraph()->HasIrreducibleLoops() &&
4330 (dispatch_info.method_load_kind ==
4331 HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative)) {
4332 dispatch_info.method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod;
4333 }
4334 switch (dispatch_info.code_ptr_location) {
Vladimir Markodc151b22015-10-15 18:02:30 +01004335 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
4336 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
4337 // For direct code, we actually prefer to call via the code pointer from ArtMethod*.
4338 // (Though the direct CALL ptr16:32 is available for consideration).
4339 return HInvokeStaticOrDirect::DispatchInfo {
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00004340 dispatch_info.method_load_kind,
Vladimir Markodc151b22015-10-15 18:02:30 +01004341 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00004342 dispatch_info.method_load_data,
Vladimir Markodc151b22015-10-15 18:02:30 +01004343 0u
4344 };
4345 default:
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00004346 return dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01004347 }
4348}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004349
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004350Register CodeGeneratorX86::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
4351 Register temp) {
4352 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
Vladimir Markoc53c0792015-11-19 15:48:33 +00004353 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004354 if (!invoke->GetLocations()->Intrinsified()) {
4355 return location.AsRegister<Register>();
4356 }
4357 // For intrinsics we allow any location, so it may be on the stack.
4358 if (!location.IsRegister()) {
4359 __ movl(temp, Address(ESP, location.GetStackIndex()));
4360 return temp;
4361 }
4362 // For register locations, check if the register was saved. If so, get it from the stack.
4363 // Note: There is a chance that the register was saved but not overwritten, so we could
4364 // save one load. However, since this is just an intrinsic slow path we prefer this
4365 // simple and more robust approach rather that trying to determine if that's the case.
4366 SlowPathCode* slow_path = GetCurrentSlowPath();
Serguei Katkov288c7a82016-05-16 11:53:15 +06004367 if (slow_path != nullptr) {
4368 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
4369 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
4370 __ movl(temp, Address(ESP, stack_offset));
4371 return temp;
4372 }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004373 }
4374 return location.AsRegister<Register>();
4375}
4376
Serguei Katkov288c7a82016-05-16 11:53:15 +06004377Location CodeGeneratorX86::GenerateCalleeMethodStaticOrDirectCall(HInvokeStaticOrDirect* invoke,
4378 Location temp) {
Vladimir Marko58155012015-08-19 12:49:41 +00004379 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
4380 switch (invoke->GetMethodLoadKind()) {
4381 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
4382 // temp = thread->string_init_entrypoint
4383 __ fs()->movl(temp.AsRegister<Register>(), Address::Absolute(invoke->GetStringInitOffset()));
4384 break;
4385 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00004386 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004387 break;
4388 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
4389 __ movl(temp.AsRegister<Register>(), Immediate(invoke->GetMethodAddress()));
4390 break;
4391 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004392 __ movl(temp.AsRegister<Register>(), Immediate(/* placeholder */ 0));
Vladimir Marko58155012015-08-19 12:49:41 +00004393 method_patches_.emplace_back(invoke->GetTargetMethod());
4394 __ Bind(&method_patches_.back().label); // Bind the label at the end of the "movl" insn.
4395 break;
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004396 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
4397 Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
4398 temp.AsRegister<Register>());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004399 __ movl(temp.AsRegister<Register>(), Address(base_reg, kDummy32BitOffset));
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004400 // Bind a new fixup label at the end of the "movl" insn.
4401 uint32_t offset = invoke->GetDexCacheArrayOffset();
4402 __ Bind(NewPcRelativeDexCacheArrayPatch(*invoke->GetTargetMethod().dex_file, offset));
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004403 break;
4404 }
Vladimir Marko58155012015-08-19 12:49:41 +00004405 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004406 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004407 Register method_reg;
4408 Register reg = temp.AsRegister<Register>();
4409 if (current_method.IsRegister()) {
4410 method_reg = current_method.AsRegister<Register>();
4411 } else {
David Brazdil58282f42016-01-14 12:45:10 +00004412 DCHECK(invoke->GetLocations()->Intrinsified());
Vladimir Marko58155012015-08-19 12:49:41 +00004413 DCHECK(!current_method.IsValid());
4414 method_reg = reg;
4415 __ movl(reg, Address(ESP, kCurrentMethodStackOffset));
4416 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00004417 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +01004418 __ movl(reg, Address(method_reg,
4419 ArtMethod::DexCacheResolvedMethodsOffset(kX86PointerSize).Int32Value()));
Vladimir Marko40ecb122016-04-06 17:33:41 +01004420 // temp = temp[index_in_cache];
4421 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
4422 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Vladimir Marko58155012015-08-19 12:49:41 +00004423 __ movl(reg, Address(reg, CodeGenerator::GetCachePointerOffset(index_in_cache)));
4424 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +01004425 }
Vladimir Marko58155012015-08-19 12:49:41 +00004426 }
Serguei Katkov288c7a82016-05-16 11:53:15 +06004427 return callee_method;
4428}
4429
4430void CodeGeneratorX86::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
4431 Location callee_method = GenerateCalleeMethodStaticOrDirectCall(invoke, temp);
Vladimir Marko58155012015-08-19 12:49:41 +00004432
4433 switch (invoke->GetCodePtrLocation()) {
4434 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
4435 __ call(GetFrameEntryLabel());
4436 break;
4437 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative: {
4438 relative_call_patches_.emplace_back(invoke->GetTargetMethod());
4439 Label* label = &relative_call_patches_.back().label;
4440 __ call(label); // Bind to the patch label, override at link time.
4441 __ Bind(label); // Bind the label at the end of the "call" insn.
4442 break;
4443 }
4444 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
4445 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
Vladimir Markodc151b22015-10-15 18:02:30 +01004446 // Filtered out by GetSupportedInvokeStaticOrDirectDispatch().
4447 LOG(FATAL) << "Unsupported";
4448 UNREACHABLE();
Vladimir Marko58155012015-08-19 12:49:41 +00004449 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
4450 // (callee_method + offset_of_quick_compiled_code)()
4451 __ call(Address(callee_method.AsRegister<Register>(),
4452 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07004453 kX86PointerSize).Int32Value()));
Vladimir Marko58155012015-08-19 12:49:41 +00004454 break;
Mark Mendell09ed1a32015-03-25 08:30:06 -04004455 }
4456
4457 DCHECK(!IsLeafMethod());
Mark Mendell09ed1a32015-03-25 08:30:06 -04004458}
4459
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004460void CodeGeneratorX86::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_in) {
4461 Register temp = temp_in.AsRegister<Register>();
4462 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
4463 invoke->GetVTableIndex(), kX86PointerSize).Uint32Value();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004464
4465 // Use the calling convention instead of the location of the receiver, as
4466 // intrinsics may have put the receiver in a different register. In the intrinsics
4467 // slow path, the arguments have been moved to the right place, so here we are
4468 // guaranteed that the receiver is the first register of the calling convention.
4469 InvokeDexCallingConvention calling_convention;
4470 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004471 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004472 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004473 __ movl(temp, Address(receiver, class_offset));
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004474 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00004475 // Instead of simply (possibly) unpoisoning `temp` here, we should
4476 // emit a read barrier for the previous class reference load.
4477 // However this is not required in practice, as this is an
4478 // intermediate/temporary reference and because the current
4479 // concurrent copying collector keeps the from-space memory
4480 // intact/accessible until the end of the marking phase (the
4481 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004482 __ MaybeUnpoisonHeapReference(temp);
4483 // temp = temp->GetMethodAt(method_offset);
4484 __ movl(temp, Address(temp, method_offset));
4485 // call temp->GetEntryPoint();
4486 __ call(Address(
Andreas Gampe542451c2016-07-26 09:02:02 -07004487 temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86PointerSize).Int32Value()));
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004488}
4489
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004490void CodeGeneratorX86::RecordSimplePatch() {
4491 if (GetCompilerOptions().GetIncludePatchInformation()) {
4492 simple_patches_.emplace_back();
4493 __ Bind(&simple_patches_.back());
4494 }
4495}
4496
4497void CodeGeneratorX86::RecordStringPatch(HLoadString* load_string) {
4498 string_patches_.emplace_back(load_string->GetDexFile(), load_string->GetStringIndex());
4499 __ Bind(&string_patches_.back().label);
4500}
4501
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004502void CodeGeneratorX86::RecordTypePatch(HLoadClass* load_class) {
4503 type_patches_.emplace_back(load_class->GetDexFile(), load_class->GetTypeIndex());
4504 __ Bind(&type_patches_.back().label);
4505}
4506
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004507Label* CodeGeneratorX86::NewPcRelativeDexCacheArrayPatch(const DexFile& dex_file,
4508 uint32_t element_offset) {
4509 // Add the patch entry and bind its label at the end of the instruction.
4510 pc_relative_dex_cache_patches_.emplace_back(dex_file, element_offset);
4511 return &pc_relative_dex_cache_patches_.back().label;
4512}
4513
Vladimir Marko58155012015-08-19 12:49:41 +00004514void CodeGeneratorX86::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
4515 DCHECK(linker_patches->empty());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004516 size_t size =
4517 method_patches_.size() +
4518 relative_call_patches_.size() +
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004519 pc_relative_dex_cache_patches_.size() +
4520 simple_patches_.size() +
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004521 string_patches_.size() +
4522 type_patches_.size();
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004523 linker_patches->reserve(size);
4524 // The label points to the end of the "movl" insn but the literal offset for method
4525 // patch needs to point to the embedded constant which occupies the last 4 bytes.
4526 constexpr uint32_t kLabelPositionToLiteralOffsetAdjustment = 4u;
Vladimir Marko58155012015-08-19 12:49:41 +00004527 for (const MethodPatchInfo<Label>& info : method_patches_) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004528 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Marko58155012015-08-19 12:49:41 +00004529 linker_patches->push_back(LinkerPatch::MethodPatch(literal_offset,
4530 info.target_method.dex_file,
4531 info.target_method.dex_method_index));
4532 }
4533 for (const MethodPatchInfo<Label>& info : relative_call_patches_) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004534 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Marko58155012015-08-19 12:49:41 +00004535 linker_patches->push_back(LinkerPatch::RelativeCodePatch(literal_offset,
4536 info.target_method.dex_file,
4537 info.target_method.dex_method_index));
4538 }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004539 for (const PcRelativeDexCacheAccessInfo& info : pc_relative_dex_cache_patches_) {
4540 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
4541 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(literal_offset,
4542 &info.target_dex_file,
4543 GetMethodAddressOffset(),
4544 info.element_offset));
4545 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004546 for (const Label& label : simple_patches_) {
4547 uint32_t literal_offset = label.Position() - kLabelPositionToLiteralOffsetAdjustment;
4548 linker_patches->push_back(LinkerPatch::RecordPosition(literal_offset));
4549 }
4550 if (GetCompilerOptions().GetCompilePic()) {
4551 for (const StringPatchInfo<Label>& info : string_patches_) {
4552 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
4553 linker_patches->push_back(LinkerPatch::RelativeStringPatch(literal_offset,
4554 &info.dex_file,
4555 GetMethodAddressOffset(),
4556 info.string_index));
4557 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004558 for (const TypePatchInfo<Label>& info : type_patches_) {
4559 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
4560 linker_patches->push_back(LinkerPatch::RelativeTypePatch(literal_offset,
4561 &info.dex_file,
4562 GetMethodAddressOffset(),
4563 info.type_index));
4564 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004565 } else {
4566 for (const StringPatchInfo<Label>& info : string_patches_) {
4567 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
4568 linker_patches->push_back(LinkerPatch::StringPatch(literal_offset,
4569 &info.dex_file,
4570 info.string_index));
4571 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004572 for (const TypePatchInfo<Label>& info : type_patches_) {
4573 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
4574 linker_patches->push_back(LinkerPatch::TypePatch(literal_offset,
4575 &info.dex_file,
4576 info.type_index));
4577 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004578 }
Vladimir Marko58155012015-08-19 12:49:41 +00004579}
4580
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004581void CodeGeneratorX86::MarkGCCard(Register temp,
4582 Register card,
4583 Register object,
4584 Register value,
4585 bool value_can_be_null) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004586 NearLabel is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004587 if (value_can_be_null) {
4588 __ testl(value, value);
4589 __ j(kEqual, &is_null);
4590 }
Andreas Gampe542451c2016-07-26 09:02:02 -07004591 __ fs()->movl(card, Address::Absolute(Thread::CardTableOffset<kX86PointerSize>().Int32Value()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004592 __ movl(temp, object);
4593 __ shrl(temp, Immediate(gc::accounting::CardTable::kCardShift));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00004594 __ movb(Address(temp, card, TIMES_1, 0),
4595 X86ManagedRegister::FromCpuRegister(card).AsByteRegister());
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004596 if (value_can_be_null) {
4597 __ Bind(&is_null);
4598 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004599}
4600
Calin Juravle52c48962014-12-16 17:02:57 +00004601void LocationsBuilderX86::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
4602 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain0d5a2812015-11-13 10:07:31 +00004603
4604 bool object_field_get_with_read_barrier =
4605 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004606 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00004607 new (GetGraph()->GetArena()) LocationSummary(instruction,
4608 kEmitCompilerReadBarrier ?
4609 LocationSummary::kCallOnSlowPath :
4610 LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004611 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004612
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004613 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4614 locations->SetOut(Location::RequiresFpuRegister());
4615 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004616 // The output overlaps in case of long: we don't want the low move
4617 // to overwrite the object's location. Likewise, in the case of
4618 // an object field get with read barriers enabled, we do not want
4619 // the move to overwrite the object's location, as we need it to emit
4620 // the read barrier.
4621 locations->SetOut(
4622 Location::RequiresRegister(),
4623 (object_field_get_with_read_barrier || instruction->GetType() == Primitive::kPrimLong) ?
4624 Location::kOutputOverlap :
4625 Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004626 }
Calin Juravle52c48962014-12-16 17:02:57 +00004627
4628 if (field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong)) {
4629 // Long values can be loaded atomically into an XMM using movsd.
Roland Levillain7c1559a2015-12-15 10:55:36 +00004630 // So we use an XMM register as a temp to achieve atomicity (first
4631 // load the temp into the XMM and then copy the XMM into the
4632 // output, 32 bits at a time).
Calin Juravle52c48962014-12-16 17:02:57 +00004633 locations->AddTemp(Location::RequiresFpuRegister());
Vladimir Markoccf15bc2016-08-23 17:48:38 +00004634 } else if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
4635 // We need a temporary register for the read barrier marking slow
4636 // path in CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier.
4637 locations->AddTemp(Location::RequiresRegister());
Calin Juravle52c48962014-12-16 17:02:57 +00004638 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004639}
4640
Calin Juravle52c48962014-12-16 17:02:57 +00004641void InstructionCodeGeneratorX86::HandleFieldGet(HInstruction* instruction,
4642 const FieldInfo& field_info) {
4643 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004644
Calin Juravle52c48962014-12-16 17:02:57 +00004645 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004646 Location base_loc = locations->InAt(0);
4647 Register base = base_loc.AsRegister<Register>();
Calin Juravle52c48962014-12-16 17:02:57 +00004648 Location out = locations->Out();
4649 bool is_volatile = field_info.IsVolatile();
4650 Primitive::Type field_type = field_info.GetFieldType();
4651 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4652
4653 switch (field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004654 case Primitive::kPrimBoolean: {
Calin Juravle52c48962014-12-16 17:02:57 +00004655 __ movzxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004656 break;
4657 }
4658
4659 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00004660 __ movsxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004661 break;
4662 }
4663
4664 case Primitive::kPrimShort: {
Calin Juravle52c48962014-12-16 17:02:57 +00004665 __ movsxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004666 break;
4667 }
4668
4669 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00004670 __ movzxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004671 break;
4672 }
4673
4674 case Primitive::kPrimInt:
Calin Juravle52c48962014-12-16 17:02:57 +00004675 __ movl(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004676 break;
Roland Levillain7c1559a2015-12-15 10:55:36 +00004677
4678 case Primitive::kPrimNot: {
4679 // /* HeapReference<Object> */ out = *(base + offset)
4680 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Vladimir Markoccf15bc2016-08-23 17:48:38 +00004681 Location temp_loc = locations->GetTemp(0);
Roland Levillain7c1559a2015-12-15 10:55:36 +00004682 // Note that a potential implicit null check is handled in this
4683 // CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier call.
4684 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Markoccf15bc2016-08-23 17:48:38 +00004685 instruction, out, base, offset, temp_loc, /* needs_null_check */ true);
Roland Levillain7c1559a2015-12-15 10:55:36 +00004686 if (is_volatile) {
4687 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4688 }
4689 } else {
4690 __ movl(out.AsRegister<Register>(), Address(base, offset));
4691 codegen_->MaybeRecordImplicitNullCheck(instruction);
4692 if (is_volatile) {
4693 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4694 }
4695 // If read barriers are enabled, emit read barriers other than
4696 // Baker's using a slow path (and also unpoison the loaded
4697 // reference, if heap poisoning is enabled).
4698 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
4699 }
4700 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004701 }
4702
4703 case Primitive::kPrimLong: {
Calin Juravle52c48962014-12-16 17:02:57 +00004704 if (is_volatile) {
4705 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4706 __ movsd(temp, Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004707 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004708 __ movd(out.AsRegisterPairLow<Register>(), temp);
4709 __ psrlq(temp, Immediate(32));
4710 __ movd(out.AsRegisterPairHigh<Register>(), temp);
4711 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004712 DCHECK_NE(base, out.AsRegisterPairLow<Register>());
Calin Juravle52c48962014-12-16 17:02:57 +00004713 __ movl(out.AsRegisterPairLow<Register>(), Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004714 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004715 __ movl(out.AsRegisterPairHigh<Register>(), Address(base, kX86WordSize + offset));
4716 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004717 break;
4718 }
4719
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004720 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00004721 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004722 break;
4723 }
4724
4725 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00004726 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004727 break;
4728 }
4729
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004730 case Primitive::kPrimVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00004731 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004732 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004733 }
Calin Juravle52c48962014-12-16 17:02:57 +00004734
Roland Levillain7c1559a2015-12-15 10:55:36 +00004735 if (field_type == Primitive::kPrimNot || field_type == Primitive::kPrimLong) {
4736 // Potential implicit null checks, in the case of reference or
4737 // long fields, are handled in the previous switch statement.
4738 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00004739 codegen_->MaybeRecordImplicitNullCheck(instruction);
4740 }
4741
Calin Juravle52c48962014-12-16 17:02:57 +00004742 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004743 if (field_type == Primitive::kPrimNot) {
4744 // Memory barriers, in the case of references, are also handled
4745 // in the previous switch statement.
4746 } else {
4747 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4748 }
Roland Levillain4d027112015-07-01 15:41:14 +01004749 }
Calin Juravle52c48962014-12-16 17:02:57 +00004750}
4751
4752void LocationsBuilderX86::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
4753 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4754
4755 LocationSummary* locations =
4756 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4757 locations->SetInAt(0, Location::RequiresRegister());
4758 bool is_volatile = field_info.IsVolatile();
4759 Primitive::Type field_type = field_info.GetFieldType();
4760 bool is_byte_type = (field_type == Primitive::kPrimBoolean)
4761 || (field_type == Primitive::kPrimByte);
4762
4763 // The register allocator does not support multiple
4764 // inputs that die at entry with one in a specific register.
4765 if (is_byte_type) {
4766 // Ensure the value is in a byte register.
4767 locations->SetInAt(1, Location::RegisterLocation(EAX));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004768 } else if (Primitive::IsFloatingPointType(field_type)) {
Mark Mendell81489372015-11-04 11:30:41 -05004769 if (is_volatile && field_type == Primitive::kPrimDouble) {
4770 // In order to satisfy the semantics of volatile, this must be a single instruction store.
4771 locations->SetInAt(1, Location::RequiresFpuRegister());
4772 } else {
4773 locations->SetInAt(1, Location::FpuRegisterOrConstant(instruction->InputAt(1)));
4774 }
4775 } else if (is_volatile && field_type == Primitive::kPrimLong) {
4776 // In order to satisfy the semantics of volatile, this must be a single instruction store.
Calin Juravle52c48962014-12-16 17:02:57 +00004777 locations->SetInAt(1, Location::RequiresRegister());
Mark Mendell81489372015-11-04 11:30:41 -05004778
Calin Juravle52c48962014-12-16 17:02:57 +00004779 // 64bits value can be atomically written to an address with movsd and an XMM register.
4780 // We need two XMM registers because there's no easier way to (bit) copy a register pair
4781 // into a single XMM register (we copy each pair part into the XMMs and then interleave them).
4782 // NB: We could make the register allocator understand fp_reg <-> core_reg moves but given the
4783 // isolated cases when we need this it isn't worth adding the extra complexity.
4784 locations->AddTemp(Location::RequiresFpuRegister());
4785 locations->AddTemp(Location::RequiresFpuRegister());
Mark Mendell81489372015-11-04 11:30:41 -05004786 } else {
4787 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4788
4789 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
4790 // Temporary registers for the write barrier.
4791 locations->AddTemp(Location::RequiresRegister()); // May be used for reference poisoning too.
4792 // Ensure the card is in a byte register.
4793 locations->AddTemp(Location::RegisterLocation(ECX));
4794 }
Calin Juravle52c48962014-12-16 17:02:57 +00004795 }
4796}
4797
4798void InstructionCodeGeneratorX86::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004799 const FieldInfo& field_info,
4800 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00004801 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4802
4803 LocationSummary* locations = instruction->GetLocations();
4804 Register base = locations->InAt(0).AsRegister<Register>();
4805 Location value = locations->InAt(1);
4806 bool is_volatile = field_info.IsVolatile();
4807 Primitive::Type field_type = field_info.GetFieldType();
4808 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01004809 bool needs_write_barrier =
4810 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00004811
4812 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004813 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
Calin Juravle52c48962014-12-16 17:02:57 +00004814 }
4815
Mark Mendell81489372015-11-04 11:30:41 -05004816 bool maybe_record_implicit_null_check_done = false;
4817
Calin Juravle52c48962014-12-16 17:02:57 +00004818 switch (field_type) {
4819 case Primitive::kPrimBoolean:
4820 case Primitive::kPrimByte: {
4821 __ movb(Address(base, offset), value.AsRegister<ByteRegister>());
4822 break;
4823 }
4824
4825 case Primitive::kPrimShort:
4826 case Primitive::kPrimChar: {
Mark Mendell81489372015-11-04 11:30:41 -05004827 if (value.IsConstant()) {
4828 int16_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4829 __ movw(Address(base, offset), Immediate(v));
4830 } else {
4831 __ movw(Address(base, offset), value.AsRegister<Register>());
4832 }
Calin Juravle52c48962014-12-16 17:02:57 +00004833 break;
4834 }
4835
4836 case Primitive::kPrimInt:
4837 case Primitive::kPrimNot: {
Roland Levillain4d027112015-07-01 15:41:14 +01004838 if (kPoisonHeapReferences && needs_write_barrier) {
4839 // Note that in the case where `value` is a null reference,
4840 // we do not enter this block, as the reference does not
4841 // need poisoning.
4842 DCHECK_EQ(field_type, Primitive::kPrimNot);
4843 Register temp = locations->GetTemp(0).AsRegister<Register>();
4844 __ movl(temp, value.AsRegister<Register>());
4845 __ PoisonHeapReference(temp);
4846 __ movl(Address(base, offset), temp);
Mark Mendell81489372015-11-04 11:30:41 -05004847 } else if (value.IsConstant()) {
4848 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4849 __ movl(Address(base, offset), Immediate(v));
Roland Levillain4d027112015-07-01 15:41:14 +01004850 } else {
Nicolas Geoffray03971632016-03-17 10:44:24 +00004851 DCHECK(value.IsRegister()) << value;
Roland Levillain4d027112015-07-01 15:41:14 +01004852 __ movl(Address(base, offset), value.AsRegister<Register>());
4853 }
Calin Juravle52c48962014-12-16 17:02:57 +00004854 break;
4855 }
4856
4857 case Primitive::kPrimLong: {
4858 if (is_volatile) {
4859 XmmRegister temp1 = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4860 XmmRegister temp2 = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
4861 __ movd(temp1, value.AsRegisterPairLow<Register>());
4862 __ movd(temp2, value.AsRegisterPairHigh<Register>());
4863 __ punpckldq(temp1, temp2);
4864 __ movsd(Address(base, offset), temp1);
Calin Juravle77520bc2015-01-12 18:45:46 +00004865 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell81489372015-11-04 11:30:41 -05004866 } else if (value.IsConstant()) {
4867 int64_t v = CodeGenerator::GetInt64ValueOf(value.GetConstant());
4868 __ movl(Address(base, offset), Immediate(Low32Bits(v)));
4869 codegen_->MaybeRecordImplicitNullCheck(instruction);
4870 __ movl(Address(base, kX86WordSize + offset), Immediate(High32Bits(v)));
Calin Juravle52c48962014-12-16 17:02:57 +00004871 } else {
4872 __ movl(Address(base, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00004873 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004874 __ movl(Address(base, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
4875 }
Mark Mendell81489372015-11-04 11:30:41 -05004876 maybe_record_implicit_null_check_done = true;
Calin Juravle52c48962014-12-16 17:02:57 +00004877 break;
4878 }
4879
4880 case Primitive::kPrimFloat: {
Mark Mendell81489372015-11-04 11:30:41 -05004881 if (value.IsConstant()) {
4882 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4883 __ movl(Address(base, offset), Immediate(v));
4884 } else {
4885 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4886 }
Calin Juravle52c48962014-12-16 17:02:57 +00004887 break;
4888 }
4889
4890 case Primitive::kPrimDouble: {
Mark Mendell81489372015-11-04 11:30:41 -05004891 if (value.IsConstant()) {
4892 int64_t v = CodeGenerator::GetInt64ValueOf(value.GetConstant());
4893 __ movl(Address(base, offset), Immediate(Low32Bits(v)));
4894 codegen_->MaybeRecordImplicitNullCheck(instruction);
4895 __ movl(Address(base, kX86WordSize + offset), Immediate(High32Bits(v)));
4896 maybe_record_implicit_null_check_done = true;
4897 } else {
4898 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4899 }
Calin Juravle52c48962014-12-16 17:02:57 +00004900 break;
4901 }
4902
4903 case Primitive::kPrimVoid:
4904 LOG(FATAL) << "Unreachable type " << field_type;
4905 UNREACHABLE();
4906 }
4907
Mark Mendell81489372015-11-04 11:30:41 -05004908 if (!maybe_record_implicit_null_check_done) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004909 codegen_->MaybeRecordImplicitNullCheck(instruction);
4910 }
4911
Roland Levillain4d027112015-07-01 15:41:14 +01004912 if (needs_write_barrier) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004913 Register temp = locations->GetTemp(0).AsRegister<Register>();
4914 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004915 codegen_->MarkGCCard(temp, card, base, value.AsRegister<Register>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00004916 }
4917
Calin Juravle52c48962014-12-16 17:02:57 +00004918 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004919 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
Calin Juravle52c48962014-12-16 17:02:57 +00004920 }
4921}
4922
4923void LocationsBuilderX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4924 HandleFieldGet(instruction, instruction->GetFieldInfo());
4925}
4926
4927void InstructionCodeGeneratorX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4928 HandleFieldGet(instruction, instruction->GetFieldInfo());
4929}
4930
4931void LocationsBuilderX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
4932 HandleFieldSet(instruction, instruction->GetFieldInfo());
4933}
4934
4935void InstructionCodeGeneratorX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004936 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00004937}
4938
4939void LocationsBuilderX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
4940 HandleFieldSet(instruction, instruction->GetFieldInfo());
4941}
4942
4943void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004944 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00004945}
4946
4947void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4948 HandleFieldGet(instruction, instruction->GetFieldInfo());
4949}
4950
4951void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4952 HandleFieldGet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004953}
4954
Calin Juravlee460d1d2015-09-29 04:52:17 +01004955void LocationsBuilderX86::VisitUnresolvedInstanceFieldGet(
4956 HUnresolvedInstanceFieldGet* instruction) {
4957 FieldAccessCallingConventionX86 calling_convention;
4958 codegen_->CreateUnresolvedFieldLocationSummary(
4959 instruction, instruction->GetFieldType(), calling_convention);
4960}
4961
4962void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldGet(
4963 HUnresolvedInstanceFieldGet* instruction) {
4964 FieldAccessCallingConventionX86 calling_convention;
4965 codegen_->GenerateUnresolvedFieldAccess(instruction,
4966 instruction->GetFieldType(),
4967 instruction->GetFieldIndex(),
4968 instruction->GetDexPc(),
4969 calling_convention);
4970}
4971
4972void LocationsBuilderX86::VisitUnresolvedInstanceFieldSet(
4973 HUnresolvedInstanceFieldSet* instruction) {
4974 FieldAccessCallingConventionX86 calling_convention;
4975 codegen_->CreateUnresolvedFieldLocationSummary(
4976 instruction, instruction->GetFieldType(), calling_convention);
4977}
4978
4979void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldSet(
4980 HUnresolvedInstanceFieldSet* instruction) {
4981 FieldAccessCallingConventionX86 calling_convention;
4982 codegen_->GenerateUnresolvedFieldAccess(instruction,
4983 instruction->GetFieldType(),
4984 instruction->GetFieldIndex(),
4985 instruction->GetDexPc(),
4986 calling_convention);
4987}
4988
4989void LocationsBuilderX86::VisitUnresolvedStaticFieldGet(
4990 HUnresolvedStaticFieldGet* instruction) {
4991 FieldAccessCallingConventionX86 calling_convention;
4992 codegen_->CreateUnresolvedFieldLocationSummary(
4993 instruction, instruction->GetFieldType(), calling_convention);
4994}
4995
4996void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldGet(
4997 HUnresolvedStaticFieldGet* instruction) {
4998 FieldAccessCallingConventionX86 calling_convention;
4999 codegen_->GenerateUnresolvedFieldAccess(instruction,
5000 instruction->GetFieldType(),
5001 instruction->GetFieldIndex(),
5002 instruction->GetDexPc(),
5003 calling_convention);
5004}
5005
5006void LocationsBuilderX86::VisitUnresolvedStaticFieldSet(
5007 HUnresolvedStaticFieldSet* instruction) {
5008 FieldAccessCallingConventionX86 calling_convention;
5009 codegen_->CreateUnresolvedFieldLocationSummary(
5010 instruction, instruction->GetFieldType(), calling_convention);
5011}
5012
5013void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldSet(
5014 HUnresolvedStaticFieldSet* instruction) {
5015 FieldAccessCallingConventionX86 calling_convention;
5016 codegen_->GenerateUnresolvedFieldAccess(instruction,
5017 instruction->GetFieldType(),
5018 instruction->GetFieldIndex(),
5019 instruction->GetDexPc(),
5020 calling_convention);
5021}
5022
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005023void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00005024 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
5025 ? LocationSummary::kCallOnSlowPath
5026 : LocationSummary::kNoCall;
5027 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
5028 Location loc = codegen_->IsImplicitNullCheckAllowed(instruction)
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005029 ? Location::RequiresRegister()
5030 : Location::Any();
5031 locations->SetInAt(0, loc);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005032 if (instruction->HasUses()) {
5033 locations->SetOut(Location::SameAsFirstInput());
5034 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005035}
5036
Calin Juravle2ae48182016-03-16 14:05:09 +00005037void CodeGeneratorX86::GenerateImplicitNullCheck(HNullCheck* instruction) {
5038 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005039 return;
5040 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005041 LocationSummary* locations = instruction->GetLocations();
5042 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00005043
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005044 __ testl(EAX, Address(obj.AsRegister<Register>(), 0));
Calin Juravle2ae48182016-03-16 14:05:09 +00005045 RecordPcInfo(instruction, instruction->GetDexPc());
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005046}
5047
Calin Juravle2ae48182016-03-16 14:05:09 +00005048void CodeGeneratorX86::GenerateExplicitNullCheck(HNullCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07005049 SlowPathCode* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00005050 AddSlowPath(slow_path);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005051
5052 LocationSummary* locations = instruction->GetLocations();
5053 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005054
5055 if (obj.IsRegister()) {
Mark Mendell42514f62015-03-31 11:34:22 -04005056 __ testl(obj.AsRegister<Register>(), obj.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005057 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005058 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005059 } else {
5060 DCHECK(obj.IsConstant()) << obj;
David Brazdil77a48ae2015-09-15 12:34:04 +00005061 DCHECK(obj.GetConstant()->IsNullConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005062 __ jmp(slow_path->GetEntryLabel());
5063 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005064 }
5065 __ j(kEqual, slow_path->GetEntryLabel());
5066}
5067
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005068void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00005069 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005070}
5071
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005072void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005073 bool object_array_get_with_read_barrier =
5074 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005075 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00005076 new (GetGraph()->GetArena()) LocationSummary(instruction,
5077 object_array_get_with_read_barrier ?
5078 LocationSummary::kCallOnSlowPath :
5079 LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005080 locations->SetInAt(0, Location::RequiresRegister());
5081 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005082 if (Primitive::IsFloatingPointType(instruction->GetType())) {
5083 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5084 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005085 // The output overlaps in case of long: we don't want the low move
5086 // to overwrite the array's location. Likewise, in the case of an
5087 // object array get with read barriers enabled, we do not want the
5088 // move to overwrite the array's location, as we need it to emit
5089 // the read barrier.
5090 locations->SetOut(
5091 Location::RequiresRegister(),
5092 (instruction->GetType() == Primitive::kPrimLong || object_array_get_with_read_barrier) ?
5093 Location::kOutputOverlap :
5094 Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005095 }
Vladimir Markoccf15bc2016-08-23 17:48:38 +00005096 // We need a temporary register for the read barrier marking slow
5097 // path in CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier.
5098 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
5099 locations->AddTemp(Location::RequiresRegister());
5100 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005101}
5102
5103void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
5104 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005105 Location obj_loc = locations->InAt(0);
5106 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005107 Location index = locations->InAt(1);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005108 Location out_loc = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005109 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005110
Calin Juravle77520bc2015-01-12 18:45:46 +00005111 Primitive::Type type = instruction->GetType();
5112 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005113 case Primitive::kPrimBoolean: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005114 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005115 if (index.IsConstant()) {
5116 __ movzxb(out, Address(obj,
5117 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
5118 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005119 __ movzxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005120 }
5121 break;
5122 }
5123
5124 case Primitive::kPrimByte: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005125 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005126 if (index.IsConstant()) {
5127 __ movsxb(out, Address(obj,
5128 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
5129 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005130 __ movsxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005131 }
5132 break;
5133 }
5134
5135 case Primitive::kPrimShort: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005136 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005137 if (index.IsConstant()) {
5138 __ movsxw(out, Address(obj,
5139 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
5140 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005141 __ movsxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005142 }
5143 break;
5144 }
5145
5146 case Primitive::kPrimChar: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005147 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005148 if (index.IsConstant()) {
5149 __ movzxw(out, Address(obj,
5150 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
5151 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005152 __ movzxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005153 }
5154 break;
5155 }
5156
Roland Levillain7c1559a2015-12-15 10:55:36 +00005157 case Primitive::kPrimInt: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005158 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005159 if (index.IsConstant()) {
5160 __ movl(out, Address(obj,
5161 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
5162 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005163 __ movl(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005164 }
5165 break;
5166 }
5167
Roland Levillain7c1559a2015-12-15 10:55:36 +00005168 case Primitive::kPrimNot: {
5169 static_assert(
5170 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
5171 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain7c1559a2015-12-15 10:55:36 +00005172 // /* HeapReference<Object> */ out =
5173 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
5174 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Vladimir Markoccf15bc2016-08-23 17:48:38 +00005175 Location temp = locations->GetTemp(0);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005176 // Note that a potential implicit null check is handled in this
5177 // CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier call.
5178 codegen_->GenerateArrayLoadWithBakerReadBarrier(
Vladimir Markoccf15bc2016-08-23 17:48:38 +00005179 instruction, out_loc, obj, data_offset, index, temp, /* needs_null_check */ true);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005180 } else {
5181 Register out = out_loc.AsRegister<Register>();
5182 if (index.IsConstant()) {
5183 uint32_t offset =
5184 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
5185 __ movl(out, Address(obj, offset));
5186 codegen_->MaybeRecordImplicitNullCheck(instruction);
5187 // If read barriers are enabled, emit read barriers other than
5188 // Baker's using a slow path (and also unpoison the loaded
5189 // reference, if heap poisoning is enabled).
5190 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
5191 } else {
5192 __ movl(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
5193 codegen_->MaybeRecordImplicitNullCheck(instruction);
5194 // If read barriers are enabled, emit read barriers other than
5195 // Baker's using a slow path (and also unpoison the loaded
5196 // reference, if heap poisoning is enabled).
5197 codegen_->MaybeGenerateReadBarrierSlow(
5198 instruction, out_loc, out_loc, obj_loc, data_offset, index);
5199 }
5200 }
5201 break;
5202 }
5203
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005204 case Primitive::kPrimLong: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005205 DCHECK_NE(obj, out_loc.AsRegisterPairLow<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005206 if (index.IsConstant()) {
5207 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Roland Levillain7c1559a2015-12-15 10:55:36 +00005208 __ movl(out_loc.AsRegisterPairLow<Register>(), Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005209 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005210 __ movl(out_loc.AsRegisterPairHigh<Register>(), Address(obj, offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005211 } else {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005212 __ movl(out_loc.AsRegisterPairLow<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005213 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005214 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005215 __ movl(out_loc.AsRegisterPairHigh<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005216 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005217 }
5218 break;
5219 }
5220
Mark Mendell7c8d0092015-01-26 11:21:33 -05005221 case Primitive::kPrimFloat: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005222 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Mark Mendell7c8d0092015-01-26 11:21:33 -05005223 if (index.IsConstant()) {
5224 __ movss(out, Address(obj,
5225 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
5226 } else {
5227 __ movss(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
5228 }
5229 break;
5230 }
5231
5232 case Primitive::kPrimDouble: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005233 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Mark Mendell7c8d0092015-01-26 11:21:33 -05005234 if (index.IsConstant()) {
5235 __ movsd(out, Address(obj,
5236 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
5237 } else {
5238 __ movsd(out, Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
5239 }
5240 break;
5241 }
5242
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005243 case Primitive::kPrimVoid:
Calin Juravle77520bc2015-01-12 18:45:46 +00005244 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07005245 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005246 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005247
Roland Levillain7c1559a2015-12-15 10:55:36 +00005248 if (type == Primitive::kPrimNot || type == Primitive::kPrimLong) {
5249 // Potential implicit null checks, in the case of reference or
5250 // long arrays, are handled in the previous switch statement.
5251 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00005252 codegen_->MaybeRecordImplicitNullCheck(instruction);
5253 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005254}
5255
5256void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01005257 Primitive::Type value_type = instruction->GetComponentType();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005258
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005259 bool needs_write_barrier =
5260 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Roland Levillain0d5a2812015-11-13 10:07:31 +00005261 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
5262 bool object_array_set_with_read_barrier =
5263 kEmitCompilerReadBarrier && (value_type == Primitive::kPrimNot);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005264
Nicolas Geoffray39468442014-09-02 15:17:15 +01005265 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
5266 instruction,
Roland Levillain0d5a2812015-11-13 10:07:31 +00005267 (may_need_runtime_call_for_type_check || object_array_set_with_read_barrier) ?
5268 LocationSummary::kCallOnSlowPath :
5269 LocationSummary::kNoCall);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005270
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005271 bool is_byte_type = (value_type == Primitive::kPrimBoolean)
5272 || (value_type == Primitive::kPrimByte);
5273 // We need the inputs to be different than the output in case of long operation.
5274 // In case of a byte operation, the register allocator does not support multiple
5275 // inputs that die at entry with one in a specific register.
5276 locations->SetInAt(0, Location::RequiresRegister());
5277 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
5278 if (is_byte_type) {
5279 // Ensure the value is in a byte register.
5280 locations->SetInAt(2, Location::ByteRegisterOrConstant(EAX, instruction->InputAt(2)));
5281 } else if (Primitive::IsFloatingPointType(value_type)) {
Mark Mendell81489372015-11-04 11:30:41 -05005282 locations->SetInAt(2, Location::FpuRegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005283 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005284 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
5285 }
5286 if (needs_write_barrier) {
5287 // Temporary registers for the write barrier.
5288 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
5289 // Ensure the card is in a byte register.
Roland Levillain4f6b0b52015-11-23 19:29:22 +00005290 locations->AddTemp(Location::RegisterLocation(ECX));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005291 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005292}
5293
5294void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
5295 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005296 Location array_loc = locations->InAt(0);
5297 Register array = array_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005298 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005299 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005300 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005301 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
5302 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5303 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005304 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005305 bool needs_write_barrier =
5306 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005307
5308 switch (value_type) {
5309 case Primitive::kPrimBoolean:
5310 case Primitive::kPrimByte: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005311 uint32_t offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
5312 Address address = index.IsConstant()
5313 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + offset)
5314 : Address(array, index.AsRegister<Register>(), TIMES_1, offset);
5315 if (value.IsRegister()) {
5316 __ movb(address, value.AsRegister<ByteRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005317 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005318 __ movb(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005319 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005320 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005321 break;
5322 }
5323
5324 case Primitive::kPrimShort:
5325 case Primitive::kPrimChar: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005326 uint32_t offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
5327 Address address = index.IsConstant()
5328 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + offset)
5329 : Address(array, index.AsRegister<Register>(), TIMES_2, offset);
5330 if (value.IsRegister()) {
5331 __ movw(address, value.AsRegister<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005332 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005333 __ movw(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005334 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005335 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005336 break;
5337 }
5338
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005339 case Primitive::kPrimNot: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005340 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
5341 Address address = index.IsConstant()
5342 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
5343 : Address(array, index.AsRegister<Register>(), TIMES_4, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005344
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005345 if (!value.IsRegister()) {
5346 // Just setting null.
5347 DCHECK(instruction->InputAt(2)->IsNullConstant());
5348 DCHECK(value.IsConstant()) << value;
5349 __ movl(address, Immediate(0));
Calin Juravle77520bc2015-01-12 18:45:46 +00005350 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005351 DCHECK(!needs_write_barrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005352 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005353 break;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005354 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005355
5356 DCHECK(needs_write_barrier);
5357 Register register_value = value.AsRegister<Register>();
5358 NearLabel done, not_null, do_put;
5359 SlowPathCode* slow_path = nullptr;
5360 Register temp = locations->GetTemp(0).AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005361 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005362 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathX86(instruction);
5363 codegen_->AddSlowPath(slow_path);
5364 if (instruction->GetValueCanBeNull()) {
5365 __ testl(register_value, register_value);
5366 __ j(kNotEqual, &not_null);
5367 __ movl(address, Immediate(0));
5368 codegen_->MaybeRecordImplicitNullCheck(instruction);
5369 __ jmp(&done);
5370 __ Bind(&not_null);
5371 }
5372
Roland Levillain0d5a2812015-11-13 10:07:31 +00005373 if (kEmitCompilerReadBarrier) {
5374 // When read barriers are enabled, the type checking
5375 // instrumentation requires two read barriers:
5376 //
5377 // __ movl(temp2, temp);
5378 // // /* HeapReference<Class> */ temp = temp->component_type_
5379 // __ movl(temp, Address(temp, component_offset));
Roland Levillain7c1559a2015-12-15 10:55:36 +00005380 // codegen_->GenerateReadBarrierSlow(
Roland Levillain0d5a2812015-11-13 10:07:31 +00005381 // instruction, temp_loc, temp_loc, temp2_loc, component_offset);
5382 //
5383 // // /* HeapReference<Class> */ temp2 = register_value->klass_
5384 // __ movl(temp2, Address(register_value, class_offset));
Roland Levillain7c1559a2015-12-15 10:55:36 +00005385 // codegen_->GenerateReadBarrierSlow(
Roland Levillain0d5a2812015-11-13 10:07:31 +00005386 // instruction, temp2_loc, temp2_loc, value, class_offset, temp_loc);
5387 //
5388 // __ cmpl(temp, temp2);
5389 //
5390 // However, the second read barrier may trash `temp`, as it
5391 // is a temporary register, and as such would not be saved
5392 // along with live registers before calling the runtime (nor
5393 // restored afterwards). So in this case, we bail out and
5394 // delegate the work to the array set slow path.
5395 //
5396 // TODO: Extend the register allocator to support a new
5397 // "(locally) live temp" location so as to avoid always
5398 // going into the slow path when read barriers are enabled.
5399 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005400 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005401 // /* HeapReference<Class> */ temp = array->klass_
5402 __ movl(temp, Address(array, class_offset));
5403 codegen_->MaybeRecordImplicitNullCheck(instruction);
5404 __ MaybeUnpoisonHeapReference(temp);
5405
5406 // /* HeapReference<Class> */ temp = temp->component_type_
5407 __ movl(temp, Address(temp, component_offset));
5408 // If heap poisoning is enabled, no need to unpoison `temp`
5409 // nor the object reference in `register_value->klass`, as
5410 // we are comparing two poisoned references.
5411 __ cmpl(temp, Address(register_value, class_offset));
5412
5413 if (instruction->StaticTypeOfArrayIsObjectArray()) {
5414 __ j(kEqual, &do_put);
5415 // If heap poisoning is enabled, the `temp` reference has
5416 // not been unpoisoned yet; unpoison it now.
5417 __ MaybeUnpoisonHeapReference(temp);
5418
5419 // /* HeapReference<Class> */ temp = temp->super_class_
5420 __ movl(temp, Address(temp, super_offset));
5421 // If heap poisoning is enabled, no need to unpoison
5422 // `temp`, as we are comparing against null below.
5423 __ testl(temp, temp);
5424 __ j(kNotEqual, slow_path->GetEntryLabel());
5425 __ Bind(&do_put);
5426 } else {
5427 __ j(kNotEqual, slow_path->GetEntryLabel());
5428 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005429 }
5430 }
5431
5432 if (kPoisonHeapReferences) {
5433 __ movl(temp, register_value);
5434 __ PoisonHeapReference(temp);
5435 __ movl(address, temp);
5436 } else {
5437 __ movl(address, register_value);
5438 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005439 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005440 codegen_->MaybeRecordImplicitNullCheck(instruction);
5441 }
5442
5443 Register card = locations->GetTemp(1).AsRegister<Register>();
5444 codegen_->MarkGCCard(
5445 temp, card, array, value.AsRegister<Register>(), instruction->GetValueCanBeNull());
5446 __ Bind(&done);
5447
5448 if (slow_path != nullptr) {
5449 __ Bind(slow_path->GetExitLabel());
5450 }
5451
5452 break;
5453 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005454
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005455 case Primitive::kPrimInt: {
5456 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
5457 Address address = index.IsConstant()
5458 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
5459 : Address(array, index.AsRegister<Register>(), TIMES_4, offset);
5460 if (value.IsRegister()) {
5461 __ movl(address, value.AsRegister<Register>());
5462 } else {
5463 DCHECK(value.IsConstant()) << value;
5464 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
5465 __ movl(address, Immediate(v));
5466 }
5467 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005468 break;
5469 }
5470
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005471 case Primitive::kPrimLong: {
5472 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005473 if (index.IsConstant()) {
5474 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005475 if (value.IsRegisterPair()) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005476 __ movl(Address(array, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00005477 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005478 __ movl(Address(array, offset + kX86WordSize), value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005479 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005480 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005481 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005482 __ movl(Address(array, offset), Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00005483 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005484 __ movl(Address(array, offset + kX86WordSize), Immediate(High32Bits(val)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005485 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005486 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005487 if (value.IsRegisterPair()) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005488 __ movl(Address(array, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005489 value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00005490 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005491 __ movl(Address(array, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005492 value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005493 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005494 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005495 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005496 __ movl(Address(array, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005497 Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00005498 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005499 __ movl(Address(array, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005500 Immediate(High32Bits(val)));
5501 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005502 }
5503 break;
5504 }
5505
Mark Mendell7c8d0092015-01-26 11:21:33 -05005506 case Primitive::kPrimFloat: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005507 uint32_t offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
5508 Address address = index.IsConstant()
5509 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
5510 : Address(array, index.AsRegister<Register>(), TIMES_4, offset);
Mark Mendell81489372015-11-04 11:30:41 -05005511 if (value.IsFpuRegister()) {
5512 __ movss(address, value.AsFpuRegister<XmmRegister>());
5513 } else {
5514 DCHECK(value.IsConstant());
5515 int32_t v = bit_cast<int32_t, float>(value.GetConstant()->AsFloatConstant()->GetValue());
5516 __ movl(address, Immediate(v));
5517 }
5518 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005519 break;
5520 }
5521
5522 case Primitive::kPrimDouble: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005523 uint32_t offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
5524 Address address = index.IsConstant()
5525 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + offset)
5526 : Address(array, index.AsRegister<Register>(), TIMES_8, offset);
Mark Mendell81489372015-11-04 11:30:41 -05005527 if (value.IsFpuRegister()) {
5528 __ movsd(address, value.AsFpuRegister<XmmRegister>());
5529 } else {
5530 DCHECK(value.IsConstant());
5531 Address address_hi = index.IsConstant() ?
5532 Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) +
5533 offset + kX86WordSize) :
5534 Address(array, index.AsRegister<Register>(), TIMES_8, offset + kX86WordSize);
5535 int64_t v = bit_cast<int64_t, double>(value.GetConstant()->AsDoubleConstant()->GetValue());
5536 __ movl(address, Immediate(Low32Bits(v)));
5537 codegen_->MaybeRecordImplicitNullCheck(instruction);
5538 __ movl(address_hi, Immediate(High32Bits(v)));
5539 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005540 break;
5541 }
5542
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005543 case Primitive::kPrimVoid:
5544 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07005545 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005546 }
5547}
5548
5549void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
5550 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005551 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellee8d9712016-07-12 11:13:15 -04005552 if (!instruction->IsEmittedAtUseSite()) {
5553 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5554 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005555}
5556
5557void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
Mark Mendellee8d9712016-07-12 11:13:15 -04005558 if (instruction->IsEmittedAtUseSite()) {
5559 return;
5560 }
5561
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005562 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01005563 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00005564 Register obj = locations->InAt(0).AsRegister<Register>();
5565 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005566 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005567 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005568}
5569
5570void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00005571 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
5572 ? LocationSummary::kCallOnSlowPath
5573 : LocationSummary::kNoCall;
5574 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Mark Mendellf60c90b2015-03-04 15:12:59 -05005575 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendellee8d9712016-07-12 11:13:15 -04005576 HInstruction* length = instruction->InputAt(1);
5577 if (!length->IsEmittedAtUseSite()) {
5578 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
5579 }
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005580 if (instruction->HasUses()) {
5581 locations->SetOut(Location::SameAsFirstInput());
5582 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005583}
5584
5585void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
5586 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05005587 Location index_loc = locations->InAt(0);
5588 Location length_loc = locations->InAt(1);
Andreas Gampe85b62f22015-09-09 13:15:38 -07005589 SlowPathCode* slow_path =
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01005590 new (GetGraph()->GetArena()) BoundsCheckSlowPathX86(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005591
Mark Mendell99dbd682015-04-22 16:18:52 -04005592 if (length_loc.IsConstant()) {
5593 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
5594 if (index_loc.IsConstant()) {
5595 // BCE will remove the bounds check if we are guarenteed to pass.
5596 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5597 if (index < 0 || index >= length) {
5598 codegen_->AddSlowPath(slow_path);
5599 __ jmp(slow_path->GetEntryLabel());
5600 } else {
5601 // Some optimization after BCE may have generated this, and we should not
5602 // generate a bounds check if it is a valid range.
5603 }
5604 return;
5605 }
5606
5607 // We have to reverse the jump condition because the length is the constant.
5608 Register index_reg = index_loc.AsRegister<Register>();
5609 __ cmpl(index_reg, Immediate(length));
5610 codegen_->AddSlowPath(slow_path);
5611 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005612 } else {
Mark Mendellee8d9712016-07-12 11:13:15 -04005613 HInstruction* array_length = instruction->InputAt(1);
5614 if (array_length->IsEmittedAtUseSite()) {
5615 // Address the length field in the array.
5616 DCHECK(array_length->IsArrayLength());
5617 uint32_t len_offset = CodeGenerator::GetArrayLengthOffset(array_length->AsArrayLength());
5618 Location array_loc = array_length->GetLocations()->InAt(0);
5619 Address array_len(array_loc.AsRegister<Register>(), len_offset);
5620 if (index_loc.IsConstant()) {
5621 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5622 __ cmpl(array_len, Immediate(value));
5623 } else {
5624 __ cmpl(array_len, index_loc.AsRegister<Register>());
5625 }
5626 codegen_->MaybeRecordImplicitNullCheck(array_length);
Mark Mendell99dbd682015-04-22 16:18:52 -04005627 } else {
Mark Mendellee8d9712016-07-12 11:13:15 -04005628 Register length = length_loc.AsRegister<Register>();
5629 if (index_loc.IsConstant()) {
5630 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5631 __ cmpl(length, Immediate(value));
5632 } else {
5633 __ cmpl(length, index_loc.AsRegister<Register>());
5634 }
Mark Mendell99dbd682015-04-22 16:18:52 -04005635 }
5636 codegen_->AddSlowPath(slow_path);
5637 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005638 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005639}
5640
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005641void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005642 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005643}
5644
5645void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005646 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5647}
5648
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005649void LocationsBuilderX86::VisitSuspendCheck(HSuspendCheck* instruction) {
5650 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
5651}
5652
5653void InstructionCodeGeneratorX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005654 HBasicBlock* block = instruction->GetBlock();
5655 if (block->GetLoopInformation() != nullptr) {
5656 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5657 // The back edge will generate the suspend check.
5658 return;
5659 }
5660 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5661 // The goto will generate the suspend check.
5662 return;
5663 }
5664 GenerateSuspendCheck(instruction, nullptr);
5665}
5666
5667void InstructionCodeGeneratorX86::GenerateSuspendCheck(HSuspendCheck* instruction,
5668 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005669 SuspendCheckSlowPathX86* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005670 down_cast<SuspendCheckSlowPathX86*>(instruction->GetSlowPath());
5671 if (slow_path == nullptr) {
5672 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathX86(instruction, successor);
5673 instruction->SetSlowPath(slow_path);
5674 codegen_->AddSlowPath(slow_path);
5675 if (successor != nullptr) {
5676 DCHECK(successor->IsLoopHeader());
5677 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
5678 }
5679 } else {
5680 DCHECK_EQ(slow_path->GetSuccessor(), successor);
5681 }
5682
Andreas Gampe542451c2016-07-26 09:02:02 -07005683 __ fs()->cmpw(Address::Absolute(Thread::ThreadFlagsOffset<kX86PointerSize>().Int32Value()),
Roland Levillain7c1559a2015-12-15 10:55:36 +00005684 Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005685 if (successor == nullptr) {
5686 __ j(kNotEqual, slow_path->GetEntryLabel());
5687 __ Bind(slow_path->GetReturnLabel());
5688 } else {
5689 __ j(kEqual, codegen_->GetLabelOf(successor));
5690 __ jmp(slow_path->GetEntryLabel());
5691 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005692}
5693
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005694X86Assembler* ParallelMoveResolverX86::GetAssembler() const {
5695 return codegen_->GetAssembler();
5696}
5697
Mark Mendell7c8d0092015-01-26 11:21:33 -05005698void ParallelMoveResolverX86::MoveMemoryToMemory32(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005699 ScratchRegisterScope ensure_scratch(
5700 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5701 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5702 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5703 __ movl(temp_reg, Address(ESP, src + stack_offset));
5704 __ movl(Address(ESP, dst + stack_offset), temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005705}
5706
5707void ParallelMoveResolverX86::MoveMemoryToMemory64(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005708 ScratchRegisterScope ensure_scratch(
5709 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5710 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5711 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5712 __ movl(temp_reg, Address(ESP, src + stack_offset));
5713 __ movl(Address(ESP, dst + stack_offset), temp_reg);
5714 __ movl(temp_reg, Address(ESP, src + stack_offset + kX86WordSize));
5715 __ movl(Address(ESP, dst + stack_offset + kX86WordSize), temp_reg);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005716}
5717
5718void ParallelMoveResolverX86::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005719 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005720 Location source = move->GetSource();
5721 Location destination = move->GetDestination();
5722
5723 if (source.IsRegister()) {
5724 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005725 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
David Brazdil74eb1b22015-12-14 11:44:01 +00005726 } else if (destination.IsFpuRegister()) {
5727 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005728 } else {
5729 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00005730 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005731 }
David Brazdil74eb1b22015-12-14 11:44:01 +00005732 } else if (source.IsRegisterPair()) {
5733 size_t elem_size = Primitive::ComponentSize(Primitive::kPrimInt);
5734 // Create stack space for 2 elements.
5735 __ subl(ESP, Immediate(2 * elem_size));
5736 __ movl(Address(ESP, 0), source.AsRegisterPairLow<Register>());
5737 __ movl(Address(ESP, elem_size), source.AsRegisterPairHigh<Register>());
5738 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
5739 // And remove the temporary stack space we allocated.
5740 __ addl(ESP, Immediate(2 * elem_size));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005741 } else if (source.IsFpuRegister()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00005742 if (destination.IsRegister()) {
5743 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
5744 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005745 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
David Brazdil74eb1b22015-12-14 11:44:01 +00005746 } else if (destination.IsRegisterPair()) {
5747 XmmRegister src_reg = source.AsFpuRegister<XmmRegister>();
5748 __ movd(destination.AsRegisterPairLow<Register>(), src_reg);
5749 __ psrlq(src_reg, Immediate(32));
5750 __ movd(destination.AsRegisterPairHigh<Register>(), src_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005751 } else if (destination.IsStackSlot()) {
5752 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
5753 } else {
5754 DCHECK(destination.IsDoubleStackSlot());
5755 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
5756 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005757 } else if (source.IsStackSlot()) {
5758 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005759 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005760 } else if (destination.IsFpuRegister()) {
5761 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005762 } else {
5763 DCHECK(destination.IsStackSlot());
Mark Mendell7c8d0092015-01-26 11:21:33 -05005764 MoveMemoryToMemory32(destination.GetStackIndex(), source.GetStackIndex());
5765 }
5766 } else if (source.IsDoubleStackSlot()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00005767 if (destination.IsRegisterPair()) {
5768 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
5769 __ movl(destination.AsRegisterPairHigh<Register>(),
5770 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
5771 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005772 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
5773 } else {
5774 DCHECK(destination.IsDoubleStackSlot()) << destination;
5775 MoveMemoryToMemory64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005776 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005777 } else if (source.IsConstant()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005778 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00005779 if (constant->IsIntConstant() || constant->IsNullConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05005780 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005781 if (destination.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05005782 if (value == 0) {
5783 __ xorl(destination.AsRegister<Register>(), destination.AsRegister<Register>());
5784 } else {
5785 __ movl(destination.AsRegister<Register>(), Immediate(value));
5786 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005787 } else {
5788 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell09b84632015-02-13 17:48:38 -05005789 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005790 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005791 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005792 float fp_value = constant->AsFloatConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00005793 int32_t value = bit_cast<int32_t, float>(fp_value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005794 Immediate imm(value);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005795 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005796 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
5797 if (value == 0) {
5798 // Easy handling of 0.0.
5799 __ xorps(dest, dest);
5800 } else {
5801 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005802 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5803 Register temp = static_cast<Register>(ensure_scratch.GetRegister());
5804 __ movl(temp, Immediate(value));
5805 __ movd(dest, temp);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005806 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005807 } else {
5808 DCHECK(destination.IsStackSlot()) << destination;
5809 __ movl(Address(ESP, destination.GetStackIndex()), imm);
5810 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005811 } else if (constant->IsLongConstant()) {
5812 int64_t value = constant->AsLongConstant()->GetValue();
5813 int32_t low_value = Low32Bits(value);
5814 int32_t high_value = High32Bits(value);
5815 Immediate low(low_value);
5816 Immediate high(high_value);
5817 if (destination.IsDoubleStackSlot()) {
5818 __ movl(Address(ESP, destination.GetStackIndex()), low);
5819 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
5820 } else {
5821 __ movl(destination.AsRegisterPairLow<Register>(), low);
5822 __ movl(destination.AsRegisterPairHigh<Register>(), high);
5823 }
5824 } else {
5825 DCHECK(constant->IsDoubleConstant());
5826 double dbl_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00005827 int64_t value = bit_cast<int64_t, double>(dbl_value);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005828 int32_t low_value = Low32Bits(value);
5829 int32_t high_value = High32Bits(value);
5830 Immediate low(low_value);
5831 Immediate high(high_value);
5832 if (destination.IsFpuRegister()) {
5833 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
5834 if (value == 0) {
5835 // Easy handling of 0.0.
5836 __ xorpd(dest, dest);
5837 } else {
5838 __ pushl(high);
5839 __ pushl(low);
5840 __ movsd(dest, Address(ESP, 0));
5841 __ addl(ESP, Immediate(8));
5842 }
5843 } else {
5844 DCHECK(destination.IsDoubleStackSlot()) << destination;
5845 __ movl(Address(ESP, destination.GetStackIndex()), low);
5846 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
5847 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005848 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005849 } else {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00005850 LOG(FATAL) << "Unimplemented move: " << destination << " <- " << source;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005851 }
5852}
5853
Mark Mendella5c19ce2015-04-01 12:51:05 -04005854void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005855 Register suggested_scratch = reg == EAX ? EBX : EAX;
5856 ScratchRegisterScope ensure_scratch(
5857 this, reg, suggested_scratch, codegen_->GetNumberOfCoreRegisters());
5858
5859 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5860 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset));
5861 __ movl(Address(ESP, mem + stack_offset), reg);
5862 __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005863}
5864
Mark Mendell7c8d0092015-01-26 11:21:33 -05005865void ParallelMoveResolverX86::Exchange32(XmmRegister reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005866 ScratchRegisterScope ensure_scratch(
5867 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5868
5869 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5870 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5871 __ movl(temp_reg, Address(ESP, mem + stack_offset));
5872 __ movss(Address(ESP, mem + stack_offset), reg);
5873 __ movd(reg, temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005874}
5875
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005876void ParallelMoveResolverX86::Exchange(int mem1, int mem2) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005877 ScratchRegisterScope ensure_scratch1(
5878 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005879
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005880 Register suggested_scratch = ensure_scratch1.GetRegister() == EAX ? EBX : EAX;
5881 ScratchRegisterScope ensure_scratch2(
5882 this, ensure_scratch1.GetRegister(), suggested_scratch, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005883
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005884 int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0;
5885 stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0;
5886 __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset));
5887 __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset));
5888 __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister()));
5889 __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005890}
5891
5892void ParallelMoveResolverX86::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005893 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005894 Location source = move->GetSource();
5895 Location destination = move->GetDestination();
5896
5897 if (source.IsRegister() && destination.IsRegister()) {
Mark Mendell90979812015-07-28 16:41:21 -04005898 // Use XOR swap algorithm to avoid serializing XCHG instruction or using a temporary.
5899 DCHECK_NE(destination.AsRegister<Register>(), source.AsRegister<Register>());
5900 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
5901 __ xorl(source.AsRegister<Register>(), destination.AsRegister<Register>());
5902 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005903 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005904 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005905 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005906 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005907 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
5908 Exchange(destination.GetStackIndex(), source.GetStackIndex());
Mark Mendell7c8d0092015-01-26 11:21:33 -05005909 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
5910 // Use XOR Swap algorithm to avoid a temporary.
5911 DCHECK_NE(source.reg(), destination.reg());
5912 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
5913 __ xorpd(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
5914 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
5915 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
5916 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
5917 } else if (destination.IsFpuRegister() && source.IsStackSlot()) {
5918 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005919 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
5920 // Take advantage of the 16 bytes in the XMM register.
5921 XmmRegister reg = source.AsFpuRegister<XmmRegister>();
5922 Address stack(ESP, destination.GetStackIndex());
5923 // Load the double into the high doubleword.
5924 __ movhpd(reg, stack);
5925
5926 // Store the low double into the destination.
5927 __ movsd(stack, reg);
5928
5929 // Move the high double to the low double.
5930 __ psrldq(reg, Immediate(8));
5931 } else if (destination.IsFpuRegister() && source.IsDoubleStackSlot()) {
5932 // Take advantage of the 16 bytes in the XMM register.
5933 XmmRegister reg = destination.AsFpuRegister<XmmRegister>();
5934 Address stack(ESP, source.GetStackIndex());
5935 // Load the double into the high doubleword.
5936 __ movhpd(reg, stack);
5937
5938 // Store the low double into the destination.
5939 __ movsd(stack, reg);
5940
5941 // Move the high double to the low double.
5942 __ psrldq(reg, Immediate(8));
5943 } else if (destination.IsDoubleStackSlot() && source.IsDoubleStackSlot()) {
5944 Exchange(destination.GetStackIndex(), source.GetStackIndex());
5945 Exchange(destination.GetHighStackIndex(kX86WordSize), source.GetHighStackIndex(kX86WordSize));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005946 } else {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005947 LOG(FATAL) << "Unimplemented: source: " << source << ", destination: " << destination;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005948 }
5949}
5950
5951void ParallelMoveResolverX86::SpillScratch(int reg) {
5952 __ pushl(static_cast<Register>(reg));
5953}
5954
5955void ParallelMoveResolverX86::RestoreScratch(int reg) {
5956 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005957}
5958
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005959HLoadClass::LoadKind CodeGeneratorX86::GetSupportedLoadClassKind(
5960 HLoadClass::LoadKind desired_class_load_kind) {
5961 if (kEmitCompilerReadBarrier) {
5962 switch (desired_class_load_kind) {
5963 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
5964 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
5965 case HLoadClass::LoadKind::kBootImageAddress:
5966 // TODO: Implement for read barrier.
5967 return HLoadClass::LoadKind::kDexCacheViaMethod;
5968 default:
5969 break;
5970 }
5971 }
5972 switch (desired_class_load_kind) {
5973 case HLoadClass::LoadKind::kReferrersClass:
5974 break;
5975 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
5976 DCHECK(!GetCompilerOptions().GetCompilePic());
5977 break;
5978 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
5979 DCHECK(GetCompilerOptions().GetCompilePic());
5980 FALLTHROUGH_INTENDED;
5981 case HLoadClass::LoadKind::kDexCachePcRelative:
5982 DCHECK(!Runtime::Current()->UseJitCompilation()); // Note: boot image is also non-JIT.
5983 // We disable pc-relative load when there is an irreducible loop, as the optimization
5984 // is incompatible with it.
5985 // TODO: Create as many X86ComputeBaseMethodAddress instructions as needed for methods
5986 // with irreducible loops.
5987 if (GetGraph()->HasIrreducibleLoops()) {
5988 return HLoadClass::LoadKind::kDexCacheViaMethod;
5989 }
5990 break;
5991 case HLoadClass::LoadKind::kBootImageAddress:
5992 break;
5993 case HLoadClass::LoadKind::kDexCacheAddress:
5994 DCHECK(Runtime::Current()->UseJitCompilation());
5995 break;
5996 case HLoadClass::LoadKind::kDexCacheViaMethod:
5997 break;
5998 }
5999 return desired_class_load_kind;
6000}
6001
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006002void LocationsBuilderX86::VisitLoadClass(HLoadClass* cls) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006003 if (cls->NeedsAccessCheck()) {
6004 InvokeRuntimeCallingConvention calling_convention;
6005 CodeGenerator::CreateLoadClassLocationSummary(
6006 cls,
6007 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
6008 Location::RegisterLocation(EAX),
6009 /* code_generator_supports_read_barrier */ true);
6010 return;
6011 }
6012
6013 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || kEmitCompilerReadBarrier)
6014 ? LocationSummary::kCallOnSlowPath
6015 : LocationSummary::kNoCall;
6016 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
6017 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
6018 if (load_kind == HLoadClass::LoadKind::kReferrersClass ||
6019 load_kind == HLoadClass::LoadKind::kDexCacheViaMethod ||
6020 load_kind == HLoadClass::LoadKind::kBootImageLinkTimePcRelative ||
6021 load_kind == HLoadClass::LoadKind::kDexCachePcRelative) {
6022 locations->SetInAt(0, Location::RequiresRegister());
6023 }
6024 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006025}
6026
6027void InstructionCodeGeneratorX86::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01006028 LocationSummary* locations = cls->GetLocations();
Calin Juravle98893e12015-10-02 21:05:03 +01006029 if (cls->NeedsAccessCheck()) {
6030 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex());
6031 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pInitializeTypeAndVerifyAccess),
6032 cls,
6033 cls->GetDexPc(),
6034 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00006035 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Calin Juravle580b6092015-10-06 17:35:58 +01006036 return;
6037 }
6038
Roland Levillain0d5a2812015-11-13 10:07:31 +00006039 Location out_loc = locations->Out();
6040 Register out = out_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006041
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006042 bool generate_null_check = false;
6043 switch (cls->GetLoadKind()) {
6044 case HLoadClass::LoadKind::kReferrersClass: {
6045 DCHECK(!cls->CanCallRuntime());
6046 DCHECK(!cls->MustGenerateClinitCheck());
6047 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
6048 Register current_method = locations->InAt(0).AsRegister<Register>();
6049 GenerateGcRootFieldLoad(
6050 cls, out_loc, Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
6051 break;
6052 }
6053 case HLoadClass::LoadKind::kBootImageLinkTimeAddress: {
6054 DCHECK(!kEmitCompilerReadBarrier);
6055 __ movl(out, Immediate(/* placeholder */ 0));
6056 codegen_->RecordTypePatch(cls);
6057 break;
6058 }
6059 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
6060 DCHECK(!kEmitCompilerReadBarrier);
6061 Register method_address = locations->InAt(0).AsRegister<Register>();
6062 __ leal(out, Address(method_address, CodeGeneratorX86::kDummy32BitOffset));
6063 codegen_->RecordTypePatch(cls);
6064 break;
6065 }
6066 case HLoadClass::LoadKind::kBootImageAddress: {
6067 DCHECK(!kEmitCompilerReadBarrier);
6068 DCHECK_NE(cls->GetAddress(), 0u);
6069 uint32_t address = dchecked_integral_cast<uint32_t>(cls->GetAddress());
6070 __ movl(out, Immediate(address));
6071 codegen_->RecordSimplePatch();
6072 break;
6073 }
6074 case HLoadClass::LoadKind::kDexCacheAddress: {
6075 DCHECK_NE(cls->GetAddress(), 0u);
6076 uint32_t address = dchecked_integral_cast<uint32_t>(cls->GetAddress());
6077 // /* GcRoot<mirror::Class> */ out = *address
6078 GenerateGcRootFieldLoad(cls, out_loc, Address::Absolute(address));
6079 generate_null_check = !cls->IsInDexCache();
6080 break;
6081 }
6082 case HLoadClass::LoadKind::kDexCachePcRelative: {
6083 Register base_reg = locations->InAt(0).AsRegister<Register>();
6084 uint32_t offset = cls->GetDexCacheElementOffset();
6085 Label* fixup_label = codegen_->NewPcRelativeDexCacheArrayPatch(cls->GetDexFile(), offset);
6086 // /* GcRoot<mirror::Class> */ out = *(base + offset) /* PC-relative */
6087 GenerateGcRootFieldLoad(
6088 cls, out_loc, Address(base_reg, CodeGeneratorX86::kDummy32BitOffset), fixup_label);
6089 generate_null_check = !cls->IsInDexCache();
6090 break;
6091 }
6092 case HLoadClass::LoadKind::kDexCacheViaMethod: {
6093 // /* GcRoot<mirror::Class>[] */ out =
6094 // current_method.ptr_sized_fields_->dex_cache_resolved_types_
6095 Register current_method = locations->InAt(0).AsRegister<Register>();
6096 __ movl(out, Address(current_method,
6097 ArtMethod::DexCacheResolvedTypesOffset(kX86PointerSize).Int32Value()));
6098 // /* GcRoot<mirror::Class> */ out = out[type_index]
6099 GenerateGcRootFieldLoad(
6100 cls, out_loc, Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
6101 generate_null_check = !cls->IsInDexCache();
6102 break;
6103 }
6104 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006105
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006106 if (generate_null_check || cls->MustGenerateClinitCheck()) {
6107 DCHECK(cls->CanCallRuntime());
6108 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
6109 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
6110 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006111
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006112 if (generate_null_check) {
6113 __ testl(out, out);
6114 __ j(kEqual, slow_path->GetEntryLabel());
6115 }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006116
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006117 if (cls->MustGenerateClinitCheck()) {
6118 GenerateClassInitializationCheck(slow_path, out);
6119 } else {
6120 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006121 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006122 }
6123}
6124
6125void LocationsBuilderX86::VisitClinitCheck(HClinitCheck* check) {
6126 LocationSummary* locations =
6127 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
6128 locations->SetInAt(0, Location::RequiresRegister());
6129 if (check->HasUses()) {
6130 locations->SetOut(Location::SameAsFirstInput());
6131 }
6132}
6133
6134void InstructionCodeGeneratorX86::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006135 // We assume the class to not be null.
Andreas Gampe85b62f22015-09-09 13:15:38 -07006136 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006137 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006138 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00006139 GenerateClassInitializationCheck(slow_path,
6140 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006141}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006142
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006143void InstructionCodeGeneratorX86::GenerateClassInitializationCheck(
Andreas Gampe85b62f22015-09-09 13:15:38 -07006144 SlowPathCode* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006145 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
6146 Immediate(mirror::Class::kStatusInitialized));
6147 __ j(kLess, slow_path->GetEntryLabel());
6148 __ Bind(slow_path->GetExitLabel());
6149 // No need for memory fence, thanks to the X86 memory model.
6150}
6151
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006152HLoadString::LoadKind CodeGeneratorX86::GetSupportedLoadStringKind(
6153 HLoadString::LoadKind desired_string_load_kind) {
6154 if (kEmitCompilerReadBarrier) {
6155 switch (desired_string_load_kind) {
6156 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
6157 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
6158 case HLoadString::LoadKind::kBootImageAddress:
6159 // TODO: Implement for read barrier.
6160 return HLoadString::LoadKind::kDexCacheViaMethod;
6161 default:
6162 break;
6163 }
6164 }
6165 switch (desired_string_load_kind) {
6166 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
6167 DCHECK(!GetCompilerOptions().GetCompilePic());
6168 break;
6169 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
6170 DCHECK(GetCompilerOptions().GetCompilePic());
6171 FALLTHROUGH_INTENDED;
6172 case HLoadString::LoadKind::kDexCachePcRelative:
Calin Juravleffc87072016-04-20 14:22:09 +01006173 DCHECK(!Runtime::Current()->UseJitCompilation()); // Note: boot image is also non-JIT.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006174 // We disable pc-relative load when there is an irreducible loop, as the optimization
6175 // is incompatible with it.
6176 // TODO: Create as many X86ComputeBaseMethodAddress instructions as needed for methods
6177 // with irreducible loops.
6178 if (GetGraph()->HasIrreducibleLoops()) {
6179 return HLoadString::LoadKind::kDexCacheViaMethod;
6180 }
6181 break;
6182 case HLoadString::LoadKind::kBootImageAddress:
6183 break;
6184 case HLoadString::LoadKind::kDexCacheAddress:
Calin Juravleffc87072016-04-20 14:22:09 +01006185 DCHECK(Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006186 break;
6187 case HLoadString::LoadKind::kDexCacheViaMethod:
6188 break;
6189 }
6190 return desired_string_load_kind;
6191}
6192
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006193void LocationsBuilderX86::VisitLoadString(HLoadString* load) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006194 LocationSummary::CallKind call_kind = (load->NeedsEnvironment() || kEmitCompilerReadBarrier)
Nicolas Geoffray917d0162015-11-24 18:25:35 +00006195 ? LocationSummary::kCallOnSlowPath
6196 : LocationSummary::kNoCall;
6197 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006198 HLoadString::LoadKind load_kind = load->GetLoadKind();
6199 if (load_kind == HLoadString::LoadKind::kDexCacheViaMethod ||
6200 load_kind == HLoadString::LoadKind::kBootImageLinkTimePcRelative ||
6201 load_kind == HLoadString::LoadKind::kDexCachePcRelative) {
6202 locations->SetInAt(0, Location::RequiresRegister());
6203 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006204 locations->SetOut(Location::RequiresRegister());
6205}
6206
6207void InstructionCodeGeneratorX86::VisitLoadString(HLoadString* load) {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01006208 LocationSummary* locations = load->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006209 Location out_loc = locations->Out();
6210 Register out = out_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006211
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006212 switch (load->GetLoadKind()) {
6213 case HLoadString::LoadKind::kBootImageLinkTimeAddress: {
6214 DCHECK(!kEmitCompilerReadBarrier);
6215 __ movl(out, Immediate(/* placeholder */ 0));
6216 codegen_->RecordStringPatch(load);
6217 return; // No dex cache slow path.
6218 }
6219 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
6220 DCHECK(!kEmitCompilerReadBarrier);
6221 Register method_address = locations->InAt(0).AsRegister<Register>();
6222 __ leal(out, Address(method_address, CodeGeneratorX86::kDummy32BitOffset));
6223 codegen_->RecordStringPatch(load);
6224 return; // No dex cache slow path.
6225 }
6226 case HLoadString::LoadKind::kBootImageAddress: {
6227 DCHECK(!kEmitCompilerReadBarrier);
6228 DCHECK_NE(load->GetAddress(), 0u);
6229 uint32_t address = dchecked_integral_cast<uint32_t>(load->GetAddress());
6230 __ movl(out, Immediate(address));
6231 codegen_->RecordSimplePatch();
6232 return; // No dex cache slow path.
6233 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006234 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07006235 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006236 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006237
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07006238 // TODO: Re-add the compiler code to do string dex cache lookup again.
6239 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86(load);
6240 codegen_->AddSlowPath(slow_path);
6241 __ jmp(slow_path->GetEntryLabel());
6242 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006243}
6244
David Brazdilcb1c0552015-08-04 16:22:25 +01006245static Address GetExceptionTlsAddress() {
Andreas Gampe542451c2016-07-26 09:02:02 -07006246 return Address::Absolute(Thread::ExceptionOffset<kX86PointerSize>().Int32Value());
David Brazdilcb1c0552015-08-04 16:22:25 +01006247}
6248
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006249void LocationsBuilderX86::VisitLoadException(HLoadException* load) {
6250 LocationSummary* locations =
6251 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
6252 locations->SetOut(Location::RequiresRegister());
6253}
6254
6255void InstructionCodeGeneratorX86::VisitLoadException(HLoadException* load) {
David Brazdilcb1c0552015-08-04 16:22:25 +01006256 __ fs()->movl(load->GetLocations()->Out().AsRegister<Register>(), GetExceptionTlsAddress());
6257}
6258
6259void LocationsBuilderX86::VisitClearException(HClearException* clear) {
6260 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
6261}
6262
6263void InstructionCodeGeneratorX86::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
6264 __ fs()->movl(GetExceptionTlsAddress(), Immediate(0));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006265}
6266
6267void LocationsBuilderX86::VisitThrow(HThrow* instruction) {
6268 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006269 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006270 InvokeRuntimeCallingConvention calling_convention;
6271 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6272}
6273
6274void InstructionCodeGeneratorX86::VisitThrow(HThrow* instruction) {
Alexandre Rames8158f282015-08-07 10:26:17 +01006275 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pDeliverException),
6276 instruction,
6277 instruction->GetDexPc(),
6278 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00006279 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006280}
6281
Roland Levillain7c1559a2015-12-15 10:55:36 +00006282static bool TypeCheckNeedsATemporary(TypeCheckKind type_check_kind) {
6283 return kEmitCompilerReadBarrier &&
Vladimir Markoccf15bc2016-08-23 17:48:38 +00006284 (kUseBakerReadBarrier ||
6285 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
Roland Levillain7c1559a2015-12-15 10:55:36 +00006286 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
6287 type_check_kind == TypeCheckKind::kArrayObjectCheck);
6288}
6289
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006290void LocationsBuilderX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006291 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain0d5a2812015-11-13 10:07:31 +00006292 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
6293 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006294 case TypeCheckKind::kExactCheck:
6295 case TypeCheckKind::kAbstractClassCheck:
6296 case TypeCheckKind::kClassHierarchyCheck:
6297 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006298 call_kind =
6299 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006300 break;
6301 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006302 case TypeCheckKind::kUnresolvedCheck:
6303 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006304 call_kind = LocationSummary::kCallOnSlowPath;
6305 break;
6306 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006307
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006308 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006309 locations->SetInAt(0, Location::RequiresRegister());
6310 locations->SetInAt(1, Location::Any());
6311 // Note that TypeCheckSlowPathX86 uses this "out" register too.
6312 locations->SetOut(Location::RequiresRegister());
6313 // When read barriers are enabled, we need a temporary register for
6314 // some cases.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006315 if (TypeCheckNeedsATemporary(type_check_kind)) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006316 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006317 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006318}
6319
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006320void InstructionCodeGeneratorX86::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006321 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006322 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006323 Location obj_loc = locations->InAt(0);
6324 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006325 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006326 Location out_loc = locations->Out();
6327 Register out = out_loc.AsRegister<Register>();
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006328 Location maybe_temp_loc = TypeCheckNeedsATemporary(type_check_kind) ?
Roland Levillain7c1559a2015-12-15 10:55:36 +00006329 locations->GetTemp(0) :
6330 Location::NoLocation();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006331 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006332 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6333 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6334 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Andreas Gampe85b62f22015-09-09 13:15:38 -07006335 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006336 NearLabel done, zero;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006337
6338 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006339 // Avoid null check if we know obj is not null.
6340 if (instruction->MustDoNullCheck()) {
6341 __ testl(obj, obj);
6342 __ j(kEqual, &zero);
6343 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006344
Roland Levillain0d5a2812015-11-13 10:07:31 +00006345 // /* HeapReference<Class> */ out = obj->klass_
Vladimir Markoccf15bc2016-08-23 17:48:38 +00006346 GenerateReferenceLoadTwoRegisters(instruction, out_loc, obj_loc, class_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006347
Roland Levillain7c1559a2015-12-15 10:55:36 +00006348 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006349 case TypeCheckKind::kExactCheck: {
6350 if (cls.IsRegister()) {
6351 __ cmpl(out, cls.AsRegister<Register>());
6352 } else {
6353 DCHECK(cls.IsStackSlot()) << cls;
6354 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6355 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006356
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006357 // Classes must be equal for the instanceof to succeed.
6358 __ j(kNotEqual, &zero);
6359 __ movl(out, Immediate(1));
6360 __ jmp(&done);
6361 break;
6362 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006363
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006364 case TypeCheckKind::kAbstractClassCheck: {
6365 // If the class is abstract, we eagerly fetch the super class of the
6366 // object to avoid doing a comparison we know will fail.
6367 NearLabel loop;
6368 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006369 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006370 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006371 __ testl(out, out);
6372 // If `out` is null, we use it for the result, and jump to `done`.
6373 __ j(kEqual, &done);
6374 if (cls.IsRegister()) {
6375 __ cmpl(out, cls.AsRegister<Register>());
6376 } else {
6377 DCHECK(cls.IsStackSlot()) << cls;
6378 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6379 }
6380 __ j(kNotEqual, &loop);
6381 __ movl(out, Immediate(1));
6382 if (zero.IsLinked()) {
6383 __ jmp(&done);
6384 }
6385 break;
6386 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006387
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006388 case TypeCheckKind::kClassHierarchyCheck: {
6389 // Walk over the class hierarchy to find a match.
6390 NearLabel loop, success;
6391 __ Bind(&loop);
6392 if (cls.IsRegister()) {
6393 __ cmpl(out, cls.AsRegister<Register>());
6394 } else {
6395 DCHECK(cls.IsStackSlot()) << cls;
6396 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6397 }
6398 __ j(kEqual, &success);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006399 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006400 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006401 __ testl(out, out);
6402 __ j(kNotEqual, &loop);
6403 // If `out` is null, we use it for the result, and jump to `done`.
6404 __ jmp(&done);
6405 __ Bind(&success);
6406 __ movl(out, Immediate(1));
6407 if (zero.IsLinked()) {
6408 __ jmp(&done);
6409 }
6410 break;
6411 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006412
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006413 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006414 // Do an exact check.
6415 NearLabel exact_check;
6416 if (cls.IsRegister()) {
6417 __ cmpl(out, cls.AsRegister<Register>());
6418 } else {
6419 DCHECK(cls.IsStackSlot()) << cls;
6420 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6421 }
6422 __ j(kEqual, &exact_check);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006423 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006424 // /* HeapReference<Class> */ out = out->component_type_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006425 GenerateReferenceLoadOneRegister(instruction, out_loc, component_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006426 __ testl(out, out);
6427 // If `out` is null, we use it for the result, and jump to `done`.
6428 __ j(kEqual, &done);
6429 __ cmpw(Address(out, primitive_offset), Immediate(Primitive::kPrimNot));
6430 __ j(kNotEqual, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006431 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006432 __ movl(out, Immediate(1));
6433 __ jmp(&done);
6434 break;
6435 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006436
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006437 case TypeCheckKind::kArrayCheck: {
6438 if (cls.IsRegister()) {
6439 __ cmpl(out, cls.AsRegister<Register>());
6440 } else {
6441 DCHECK(cls.IsStackSlot()) << cls;
6442 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6443 }
6444 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain0d5a2812015-11-13 10:07:31 +00006445 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
6446 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006447 codegen_->AddSlowPath(slow_path);
6448 __ j(kNotEqual, slow_path->GetEntryLabel());
6449 __ movl(out, Immediate(1));
6450 if (zero.IsLinked()) {
6451 __ jmp(&done);
6452 }
6453 break;
6454 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006455
Calin Juravle98893e12015-10-02 21:05:03 +01006456 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006457 case TypeCheckKind::kInterfaceCheck: {
6458 // Note that we indeed only call on slow path, but we always go
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006459 // into the slow path for the unresolved and interface check
Roland Levillain0d5a2812015-11-13 10:07:31 +00006460 // cases.
6461 //
6462 // We cannot directly call the InstanceofNonTrivial runtime
6463 // entry point without resorting to a type checking slow path
6464 // here (i.e. by calling InvokeRuntime directly), as it would
6465 // require to assign fixed registers for the inputs of this
6466 // HInstanceOf instruction (following the runtime calling
6467 // convention), which might be cluttered by the potential first
6468 // read barrier emission at the beginning of this method.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006469 //
6470 // TODO: Introduce a new runtime entry point taking the object
6471 // to test (instead of its class) as argument, and let it deal
6472 // with the read barrier issues. This will let us refactor this
6473 // case of the `switch` code as it was previously (with a direct
6474 // call to the runtime not using a type checking slow path).
6475 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006476 DCHECK(locations->OnlyCallsOnSlowPath());
6477 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
6478 /* is_fatal */ false);
6479 codegen_->AddSlowPath(slow_path);
6480 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006481 if (zero.IsLinked()) {
6482 __ jmp(&done);
6483 }
6484 break;
6485 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006486 }
6487
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006488 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006489 __ Bind(&zero);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006490 __ xorl(out, out);
6491 }
6492
6493 if (done.IsLinked()) {
6494 __ Bind(&done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006495 }
6496
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006497 if (slow_path != nullptr) {
6498 __ Bind(slow_path->GetExitLabel());
6499 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006500}
6501
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006502void LocationsBuilderX86::VisitCheckCast(HCheckCast* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006503 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
6504 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006505 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
6506 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006507 case TypeCheckKind::kExactCheck:
6508 case TypeCheckKind::kAbstractClassCheck:
6509 case TypeCheckKind::kClassHierarchyCheck:
6510 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006511 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
6512 LocationSummary::kCallOnSlowPath :
6513 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006514 break;
6515 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006516 case TypeCheckKind::kUnresolvedCheck:
6517 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006518 call_kind = LocationSummary::kCallOnSlowPath;
6519 break;
6520 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006521 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
6522 locations->SetInAt(0, Location::RequiresRegister());
6523 locations->SetInAt(1, Location::Any());
6524 // Note that TypeCheckSlowPathX86 uses this "temp" register too.
6525 locations->AddTemp(Location::RequiresRegister());
6526 // When read barriers are enabled, we need an additional temporary
6527 // register for some cases.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006528 if (TypeCheckNeedsATemporary(type_check_kind)) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006529 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006530 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006531}
6532
6533void InstructionCodeGeneratorX86::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006534 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006535 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006536 Location obj_loc = locations->InAt(0);
6537 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006538 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006539 Location temp_loc = locations->GetTemp(0);
6540 Register temp = temp_loc.AsRegister<Register>();
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006541 Location maybe_temp2_loc = TypeCheckNeedsATemporary(type_check_kind) ?
Roland Levillain7c1559a2015-12-15 10:55:36 +00006542 locations->GetTemp(1) :
6543 Location::NoLocation();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006544 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
6545 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6546 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6547 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006548
Roland Levillain0d5a2812015-11-13 10:07:31 +00006549 bool is_type_check_slow_path_fatal =
6550 (type_check_kind == TypeCheckKind::kExactCheck ||
6551 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
6552 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
6553 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
6554 !instruction->CanThrowIntoCatchBlock();
6555 SlowPathCode* type_check_slow_path =
6556 new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
6557 is_type_check_slow_path_fatal);
6558 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006559
Roland Levillain0d5a2812015-11-13 10:07:31 +00006560 NearLabel done;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006561 // Avoid null check if we know obj is not null.
6562 if (instruction->MustDoNullCheck()) {
6563 __ testl(obj, obj);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006564 __ j(kEqual, &done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006565 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006566
Roland Levillain0d5a2812015-11-13 10:07:31 +00006567 // /* HeapReference<Class> */ temp = obj->klass_
Vladimir Markoccf15bc2016-08-23 17:48:38 +00006568 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006569
Roland Levillain0d5a2812015-11-13 10:07:31 +00006570 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006571 case TypeCheckKind::kExactCheck:
6572 case TypeCheckKind::kArrayCheck: {
6573 if (cls.IsRegister()) {
6574 __ cmpl(temp, cls.AsRegister<Register>());
6575 } else {
6576 DCHECK(cls.IsStackSlot()) << cls;
6577 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6578 }
6579 // Jump to slow path for throwing the exception or doing a
6580 // more involved array check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006581 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006582 break;
6583 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006584
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006585 case TypeCheckKind::kAbstractClassCheck: {
6586 // If the class is abstract, we eagerly fetch the super class of the
6587 // object to avoid doing a comparison we know will fail.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006588 NearLabel loop, compare_classes;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006589 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006590 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006591 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006592
6593 // If the class reference currently in `temp` is not null, jump
6594 // to the `compare_classes` label to compare it with the checked
6595 // class.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006596 __ testl(temp, temp);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006597 __ j(kNotEqual, &compare_classes);
6598 // Otherwise, jump to the slow path to throw the exception.
6599 //
6600 // But before, move back the object's class into `temp` before
6601 // going into the slow path, as it has been overwritten in the
6602 // meantime.
6603 // /* HeapReference<Class> */ temp = obj->klass_
Vladimir Markoccf15bc2016-08-23 17:48:38 +00006604 GenerateReferenceLoadTwoRegisters(
6605 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006606 __ jmp(type_check_slow_path->GetEntryLabel());
6607
6608 __ Bind(&compare_classes);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006609 if (cls.IsRegister()) {
6610 __ cmpl(temp, cls.AsRegister<Register>());
6611 } else {
6612 DCHECK(cls.IsStackSlot()) << cls;
6613 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6614 }
6615 __ j(kNotEqual, &loop);
6616 break;
6617 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006618
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006619 case TypeCheckKind::kClassHierarchyCheck: {
6620 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006621 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006622 __ Bind(&loop);
6623 if (cls.IsRegister()) {
6624 __ cmpl(temp, cls.AsRegister<Register>());
6625 } else {
6626 DCHECK(cls.IsStackSlot()) << cls;
6627 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6628 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006629 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006630
Roland Levillain0d5a2812015-11-13 10:07:31 +00006631 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006632 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006633
6634 // If the class reference currently in `temp` is not null, jump
6635 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006636 __ testl(temp, temp);
6637 __ j(kNotEqual, &loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006638 // Otherwise, jump to the slow path to throw the exception.
6639 //
6640 // But before, move back the object's class into `temp` before
6641 // going into the slow path, as it has been overwritten in the
6642 // meantime.
6643 // /* HeapReference<Class> */ temp = obj->klass_
Vladimir Markoccf15bc2016-08-23 17:48:38 +00006644 GenerateReferenceLoadTwoRegisters(
6645 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006646 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006647 break;
6648 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006649
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006650 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006651 // Do an exact check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006652 NearLabel check_non_primitive_component_type;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006653 if (cls.IsRegister()) {
6654 __ cmpl(temp, cls.AsRegister<Register>());
6655 } else {
6656 DCHECK(cls.IsStackSlot()) << cls;
6657 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6658 }
6659 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006660
6661 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006662 // /* HeapReference<Class> */ temp = temp->component_type_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006663 GenerateReferenceLoadOneRegister(instruction, temp_loc, component_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006664
6665 // If the component type is not null (i.e. the object is indeed
6666 // an array), jump to label `check_non_primitive_component_type`
6667 // to further check that this component type is not a primitive
6668 // type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006669 __ testl(temp, temp);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006670 __ j(kNotEqual, &check_non_primitive_component_type);
6671 // Otherwise, jump to the slow path to throw the exception.
6672 //
6673 // But before, move back the object's class into `temp` before
6674 // going into the slow path, as it has been overwritten in the
6675 // meantime.
6676 // /* HeapReference<Class> */ temp = obj->klass_
Vladimir Markoccf15bc2016-08-23 17:48:38 +00006677 GenerateReferenceLoadTwoRegisters(
6678 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006679 __ jmp(type_check_slow_path->GetEntryLabel());
6680
6681 __ Bind(&check_non_primitive_component_type);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006682 __ cmpw(Address(temp, primitive_offset), Immediate(Primitive::kPrimNot));
Roland Levillain0d5a2812015-11-13 10:07:31 +00006683 __ j(kEqual, &done);
6684 // Same comment as above regarding `temp` and the slow path.
6685 // /* HeapReference<Class> */ temp = obj->klass_
Vladimir Markoccf15bc2016-08-23 17:48:38 +00006686 GenerateReferenceLoadTwoRegisters(
6687 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006688 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006689 break;
6690 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006691
Calin Juravle98893e12015-10-02 21:05:03 +01006692 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006693 case TypeCheckKind::kInterfaceCheck:
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006694 // We always go into the type check slow path for the unresolved
6695 // and interface check cases.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006696 //
6697 // We cannot directly call the CheckCast runtime entry point
6698 // without resorting to a type checking slow path here (i.e. by
6699 // calling InvokeRuntime directly), as it would require to
6700 // assign fixed registers for the inputs of this HInstanceOf
6701 // instruction (following the runtime calling convention), which
6702 // might be cluttered by the potential first read barrier
6703 // emission at the beginning of this method.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006704 //
6705 // TODO: Introduce a new runtime entry point taking the object
6706 // to test (instead of its class) as argument, and let it deal
6707 // with the read barrier issues. This will let us refactor this
6708 // case of the `switch` code as it was previously (with a direct
6709 // call to the runtime not using a type checking slow path).
6710 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006711 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006712 break;
6713 }
6714 __ Bind(&done);
6715
Roland Levillain0d5a2812015-11-13 10:07:31 +00006716 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006717}
6718
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006719void LocationsBuilderX86::VisitMonitorOperation(HMonitorOperation* instruction) {
6720 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006721 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006722 InvokeRuntimeCallingConvention calling_convention;
6723 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6724}
6725
6726void InstructionCodeGeneratorX86::VisitMonitorOperation(HMonitorOperation* instruction) {
Alexandre Rames8158f282015-08-07 10:26:17 +01006727 codegen_->InvokeRuntime(instruction->IsEnter() ? QUICK_ENTRY_POINT(pLockObject)
6728 : QUICK_ENTRY_POINT(pUnlockObject),
6729 instruction,
6730 instruction->GetDexPc(),
6731 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00006732 if (instruction->IsEnter()) {
6733 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
6734 } else {
6735 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
6736 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006737}
6738
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006739void LocationsBuilderX86::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
6740void LocationsBuilderX86::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
6741void LocationsBuilderX86::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
6742
6743void LocationsBuilderX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
6744 LocationSummary* locations =
6745 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6746 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
6747 || instruction->GetResultType() == Primitive::kPrimLong);
6748 locations->SetInAt(0, Location::RequiresRegister());
6749 locations->SetInAt(1, Location::Any());
6750 locations->SetOut(Location::SameAsFirstInput());
6751}
6752
6753void InstructionCodeGeneratorX86::VisitAnd(HAnd* instruction) {
6754 HandleBitwiseOperation(instruction);
6755}
6756
6757void InstructionCodeGeneratorX86::VisitOr(HOr* instruction) {
6758 HandleBitwiseOperation(instruction);
6759}
6760
6761void InstructionCodeGeneratorX86::VisitXor(HXor* instruction) {
6762 HandleBitwiseOperation(instruction);
6763}
6764
6765void InstructionCodeGeneratorX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
6766 LocationSummary* locations = instruction->GetLocations();
6767 Location first = locations->InAt(0);
6768 Location second = locations->InAt(1);
6769 DCHECK(first.Equals(locations->Out()));
6770
6771 if (instruction->GetResultType() == Primitive::kPrimInt) {
6772 if (second.IsRegister()) {
6773 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006774 __ andl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006775 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006776 __ orl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006777 } else {
6778 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006779 __ xorl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006780 }
6781 } else if (second.IsConstant()) {
6782 if (instruction->IsAnd()) {
Roland Levillain199f3362014-11-27 17:15:16 +00006783 __ andl(first.AsRegister<Register>(),
6784 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006785 } else if (instruction->IsOr()) {
Roland Levillain199f3362014-11-27 17:15:16 +00006786 __ orl(first.AsRegister<Register>(),
6787 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006788 } else {
6789 DCHECK(instruction->IsXor());
Roland Levillain199f3362014-11-27 17:15:16 +00006790 __ xorl(first.AsRegister<Register>(),
6791 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006792 }
6793 } else {
6794 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006795 __ andl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006796 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006797 __ orl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006798 } else {
6799 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006800 __ xorl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006801 }
6802 }
6803 } else {
6804 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
6805 if (second.IsRegisterPair()) {
6806 if (instruction->IsAnd()) {
6807 __ andl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
6808 __ andl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
6809 } else if (instruction->IsOr()) {
6810 __ orl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
6811 __ orl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
6812 } else {
6813 DCHECK(instruction->IsXor());
6814 __ xorl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
6815 __ xorl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
6816 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006817 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006818 if (instruction->IsAnd()) {
6819 __ andl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
6820 __ andl(first.AsRegisterPairHigh<Register>(),
6821 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
6822 } else if (instruction->IsOr()) {
6823 __ orl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
6824 __ orl(first.AsRegisterPairHigh<Register>(),
6825 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
6826 } else {
6827 DCHECK(instruction->IsXor());
6828 __ xorl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
6829 __ xorl(first.AsRegisterPairHigh<Register>(),
6830 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
6831 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006832 } else {
6833 DCHECK(second.IsConstant()) << second;
6834 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006835 int32_t low_value = Low32Bits(value);
6836 int32_t high_value = High32Bits(value);
6837 Immediate low(low_value);
6838 Immediate high(high_value);
6839 Register first_low = first.AsRegisterPairLow<Register>();
6840 Register first_high = first.AsRegisterPairHigh<Register>();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006841 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006842 if (low_value == 0) {
6843 __ xorl(first_low, first_low);
6844 } else if (low_value != -1) {
6845 __ andl(first_low, low);
6846 }
6847 if (high_value == 0) {
6848 __ xorl(first_high, first_high);
6849 } else if (high_value != -1) {
6850 __ andl(first_high, high);
6851 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006852 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006853 if (low_value != 0) {
6854 __ orl(first_low, low);
6855 }
6856 if (high_value != 0) {
6857 __ orl(first_high, high);
6858 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006859 } else {
6860 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006861 if (low_value != 0) {
6862 __ xorl(first_low, low);
6863 }
6864 if (high_value != 0) {
6865 __ xorl(first_high, high);
6866 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006867 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006868 }
6869 }
6870}
6871
Roland Levillain7c1559a2015-12-15 10:55:36 +00006872void InstructionCodeGeneratorX86::GenerateReferenceLoadOneRegister(HInstruction* instruction,
6873 Location out,
6874 uint32_t offset,
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006875 Location maybe_temp) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006876 Register out_reg = out.AsRegister<Register>();
6877 if (kEmitCompilerReadBarrier) {
Vladimir Markoccf15bc2016-08-23 17:48:38 +00006878 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillain7c1559a2015-12-15 10:55:36 +00006879 if (kUseBakerReadBarrier) {
6880 // Load with fast path based Baker's read barrier.
6881 // /* HeapReference<Object> */ out = *(out + offset)
6882 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Markoccf15bc2016-08-23 17:48:38 +00006883 instruction, out, out_reg, offset, maybe_temp, /* needs_null_check */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006884 } else {
6885 // Load with slow path based read barrier.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006886 // Save the value of `out` into `maybe_temp` before overwriting it
Roland Levillain7c1559a2015-12-15 10:55:36 +00006887 // in the following move operation, as we will need it for the
6888 // read barrier below.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006889 __ movl(maybe_temp.AsRegister<Register>(), out_reg);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006890 // /* HeapReference<Object> */ out = *(out + offset)
6891 __ movl(out_reg, Address(out_reg, offset));
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006892 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006893 }
6894 } else {
6895 // Plain load with no read barrier.
6896 // /* HeapReference<Object> */ out = *(out + offset)
6897 __ movl(out_reg, Address(out_reg, offset));
6898 __ MaybeUnpoisonHeapReference(out_reg);
6899 }
6900}
6901
6902void InstructionCodeGeneratorX86::GenerateReferenceLoadTwoRegisters(HInstruction* instruction,
6903 Location out,
6904 Location obj,
Vladimir Markoccf15bc2016-08-23 17:48:38 +00006905 uint32_t offset,
6906 Location maybe_temp) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006907 Register out_reg = out.AsRegister<Register>();
6908 Register obj_reg = obj.AsRegister<Register>();
6909 if (kEmitCompilerReadBarrier) {
6910 if (kUseBakerReadBarrier) {
Vladimir Markoccf15bc2016-08-23 17:48:38 +00006911 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillain7c1559a2015-12-15 10:55:36 +00006912 // Load with fast path based Baker's read barrier.
6913 // /* HeapReference<Object> */ out = *(obj + offset)
6914 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Markoccf15bc2016-08-23 17:48:38 +00006915 instruction, out, obj_reg, offset, maybe_temp, /* needs_null_check */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006916 } else {
6917 // Load with slow path based read barrier.
6918 // /* HeapReference<Object> */ out = *(obj + offset)
6919 __ movl(out_reg, Address(obj_reg, offset));
6920 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
6921 }
6922 } else {
6923 // Plain load with no read barrier.
6924 // /* HeapReference<Object> */ out = *(obj + offset)
6925 __ movl(out_reg, Address(obj_reg, offset));
6926 __ MaybeUnpoisonHeapReference(out_reg);
6927 }
6928}
6929
6930void InstructionCodeGeneratorX86::GenerateGcRootFieldLoad(HInstruction* instruction,
6931 Location root,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006932 const Address& address,
6933 Label* fixup_label) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006934 Register root_reg = root.AsRegister<Register>();
6935 if (kEmitCompilerReadBarrier) {
6936 if (kUseBakerReadBarrier) {
6937 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
6938 // Baker's read barrier are used:
6939 //
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006940 // root = *address;
Roland Levillain7c1559a2015-12-15 10:55:36 +00006941 // if (Thread::Current()->GetIsGcMarking()) {
6942 // root = ReadBarrier::Mark(root)
6943 // }
6944
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006945 // /* GcRoot<mirror::Object> */ root = *address
6946 __ movl(root_reg, address);
6947 if (fixup_label != nullptr) {
6948 __ Bind(fixup_label);
6949 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00006950 static_assert(
6951 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
6952 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
6953 "have different sizes.");
6954 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
6955 "art::mirror::CompressedReference<mirror::Object> and int32_t "
6956 "have different sizes.");
6957
Vladimir Markoccf15bc2016-08-23 17:48:38 +00006958 // Slow path used to mark the GC root `root`.
6959 SlowPathCode* slow_path =
6960 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathX86(instruction, root);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006961 codegen_->AddSlowPath(slow_path);
6962
Andreas Gampe542451c2016-07-26 09:02:02 -07006963 __ fs()->cmpl(Address::Absolute(Thread::IsGcMarkingOffset<kX86PointerSize>().Int32Value()),
Roland Levillain7c1559a2015-12-15 10:55:36 +00006964 Immediate(0));
6965 __ j(kNotEqual, slow_path->GetEntryLabel());
6966 __ Bind(slow_path->GetExitLabel());
6967 } else {
6968 // GC root loaded through a slow path for read barriers other
6969 // than Baker's.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006970 // /* GcRoot<mirror::Object>* */ root = address
6971 __ leal(root_reg, address);
6972 if (fixup_label != nullptr) {
6973 __ Bind(fixup_label);
6974 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00006975 // /* mirror::Object* */ root = root->Read()
6976 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
6977 }
6978 } else {
6979 // Plain GC root load with no read barrier.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006980 // /* GcRoot<mirror::Object> */ root = *address
6981 __ movl(root_reg, address);
6982 if (fixup_label != nullptr) {
6983 __ Bind(fixup_label);
6984 }
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006985 // Note that GC roots are not affected by heap poisoning, thus we
6986 // do not have to unpoison `root_reg` here.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006987 }
6988}
6989
6990void CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
6991 Location ref,
6992 Register obj,
6993 uint32_t offset,
Vladimir Markoccf15bc2016-08-23 17:48:38 +00006994 Location temp,
Roland Levillain7c1559a2015-12-15 10:55:36 +00006995 bool needs_null_check) {
6996 DCHECK(kEmitCompilerReadBarrier);
6997 DCHECK(kUseBakerReadBarrier);
6998
6999 // /* HeapReference<Object> */ ref = *(obj + offset)
7000 Address src(obj, offset);
Vladimir Markoccf15bc2016-08-23 17:48:38 +00007001 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, temp, needs_null_check);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007002}
7003
7004void CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
7005 Location ref,
7006 Register obj,
7007 uint32_t data_offset,
7008 Location index,
Vladimir Markoccf15bc2016-08-23 17:48:38 +00007009 Location temp,
Roland Levillain7c1559a2015-12-15 10:55:36 +00007010 bool needs_null_check) {
7011 DCHECK(kEmitCompilerReadBarrier);
7012 DCHECK(kUseBakerReadBarrier);
7013
Roland Levillain3d312422016-06-23 13:53:42 +01007014 static_assert(
7015 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
7016 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain7c1559a2015-12-15 10:55:36 +00007017 // /* HeapReference<Object> */ ref =
7018 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
7019 Address src = index.IsConstant() ?
7020 Address(obj, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset) :
7021 Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset);
Vladimir Markoccf15bc2016-08-23 17:48:38 +00007022 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, temp, needs_null_check);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007023}
7024
7025void CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
7026 Location ref,
7027 Register obj,
7028 const Address& src,
Vladimir Markoccf15bc2016-08-23 17:48:38 +00007029 Location temp,
Roland Levillain7c1559a2015-12-15 10:55:36 +00007030 bool needs_null_check) {
7031 DCHECK(kEmitCompilerReadBarrier);
7032 DCHECK(kUseBakerReadBarrier);
7033
7034 // In slow path based read barriers, the read barrier call is
7035 // inserted after the original load. However, in fast path based
7036 // Baker's read barriers, we need to perform the load of
7037 // mirror::Object::monitor_ *before* the original reference load.
7038 // This load-load ordering is required by the read barrier.
7039 // The fast path/slow path (for Baker's algorithm) should look like:
7040 //
7041 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
7042 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
7043 // HeapReference<Object> ref = *src; // Original reference load.
7044 // bool is_gray = (rb_state == ReadBarrier::gray_ptr_);
7045 // if (is_gray) {
7046 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
7047 // }
7048 //
7049 // Note: the original implementation in ReadBarrier::Barrier is
7050 // slightly more complex as:
7051 // - it implements the load-load fence using a data dependency on
Roland Levillaine3f43ac2016-01-19 15:07:47 +00007052 // the high-bits of rb_state, which are expected to be all zeroes
7053 // (we use CodeGeneratorX86::GenerateMemoryBarrier instead here,
7054 // which is a no-op thanks to the x86 memory model);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007055 // - it performs additional checks that we do not do here for
7056 // performance reasons.
7057
7058 Register ref_reg = ref.AsRegister<Register>();
Vladimir Markoccf15bc2016-08-23 17:48:38 +00007059 Register temp_reg = temp.AsRegister<Register>();
Roland Levillain7c1559a2015-12-15 10:55:36 +00007060 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
7061
Vladimir Markoccf15bc2016-08-23 17:48:38 +00007062 // /* int32_t */ monitor = obj->monitor_
7063 __ movl(temp_reg, Address(obj, monitor_offset));
Roland Levillain7c1559a2015-12-15 10:55:36 +00007064 if (needs_null_check) {
7065 MaybeRecordImplicitNullCheck(instruction);
7066 }
Vladimir Markoccf15bc2016-08-23 17:48:38 +00007067 // /* LockWord */ lock_word = LockWord(monitor)
7068 static_assert(sizeof(LockWord) == sizeof(int32_t),
7069 "art::LockWord and int32_t have different sizes.");
Roland Levillain7c1559a2015-12-15 10:55:36 +00007070
7071 // Load fence to prevent load-load reordering.
7072 // Note that this is a no-op, thanks to the x86 memory model.
7073 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
7074
7075 // The actual reference load.
7076 // /* HeapReference<Object> */ ref = *src
Vladimir Markoccf15bc2016-08-23 17:48:38 +00007077 __ movl(ref_reg, src);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007078
7079 // Object* ref = ref_addr->AsMirrorPtr()
7080 __ MaybeUnpoisonHeapReference(ref_reg);
7081
Vladimir Markoccf15bc2016-08-23 17:48:38 +00007082 // Slow path used to mark the object `ref` when it is gray.
7083 SlowPathCode* slow_path =
7084 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathX86(instruction, ref);
7085 AddSlowPath(slow_path);
7086
7087 // if (rb_state == ReadBarrier::gray_ptr_)
7088 // ref = ReadBarrier::Mark(ref);
7089 // Given the numeric representation, it's enough to check the low bit of the
7090 // rb_state. We do that by shifting the bit out of the lock word with SHR.
7091 static_assert(ReadBarrier::white_ptr_ == 0, "Expecting white to have value 0");
7092 static_assert(ReadBarrier::gray_ptr_ == 1, "Expecting gray to have value 1");
7093 static_assert(ReadBarrier::black_ptr_ == 2, "Expecting black to have value 2");
7094 __ shrl(temp_reg, Immediate(LockWord::kReadBarrierStateShift + 1));
7095 __ j(kCarrySet, slow_path->GetEntryLabel());
Roland Levillain7c1559a2015-12-15 10:55:36 +00007096 __ Bind(slow_path->GetExitLabel());
7097}
7098
7099void CodeGeneratorX86::GenerateReadBarrierSlow(HInstruction* instruction,
7100 Location out,
7101 Location ref,
7102 Location obj,
7103 uint32_t offset,
7104 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007105 DCHECK(kEmitCompilerReadBarrier);
7106
Roland Levillain7c1559a2015-12-15 10:55:36 +00007107 // Insert a slow path based read barrier *after* the reference load.
7108 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00007109 // If heap poisoning is enabled, the unpoisoning of the loaded
7110 // reference will be carried out by the runtime within the slow
7111 // path.
7112 //
7113 // Note that `ref` currently does not get unpoisoned (when heap
7114 // poisoning is enabled), which is alright as the `ref` argument is
7115 // not used by the artReadBarrierSlow entry point.
7116 //
7117 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
7118 SlowPathCode* slow_path = new (GetGraph()->GetArena())
7119 ReadBarrierForHeapReferenceSlowPathX86(instruction, out, ref, obj, offset, index);
7120 AddSlowPath(slow_path);
7121
Roland Levillain0d5a2812015-11-13 10:07:31 +00007122 __ jmp(slow_path->GetEntryLabel());
7123 __ Bind(slow_path->GetExitLabel());
7124}
7125
Roland Levillain7c1559a2015-12-15 10:55:36 +00007126void CodeGeneratorX86::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
7127 Location out,
7128 Location ref,
7129 Location obj,
7130 uint32_t offset,
7131 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007132 if (kEmitCompilerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007133 // Baker's read barriers shall be handled by the fast path
7134 // (CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier).
7135 DCHECK(!kUseBakerReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007136 // If heap poisoning is enabled, unpoisoning will be taken care of
7137 // by the runtime within the slow path.
Roland Levillain7c1559a2015-12-15 10:55:36 +00007138 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007139 } else if (kPoisonHeapReferences) {
7140 __ UnpoisonHeapReference(out.AsRegister<Register>());
7141 }
7142}
7143
Roland Levillain7c1559a2015-12-15 10:55:36 +00007144void CodeGeneratorX86::GenerateReadBarrierForRootSlow(HInstruction* instruction,
7145 Location out,
7146 Location root) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007147 DCHECK(kEmitCompilerReadBarrier);
7148
Roland Levillain7c1559a2015-12-15 10:55:36 +00007149 // Insert a slow path based read barrier *after* the GC root load.
7150 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00007151 // Note that GC roots are not affected by heap poisoning, so we do
7152 // not need to do anything special for this here.
7153 SlowPathCode* slow_path =
7154 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathX86(instruction, out, root);
7155 AddSlowPath(slow_path);
7156
Roland Levillain0d5a2812015-11-13 10:07:31 +00007157 __ jmp(slow_path->GetEntryLabel());
7158 __ Bind(slow_path->GetExitLabel());
7159}
7160
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007161void LocationsBuilderX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00007162 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00007163 LOG(FATAL) << "Unreachable";
7164}
7165
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007166void InstructionCodeGeneratorX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00007167 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00007168 LOG(FATAL) << "Unreachable";
7169}
7170
Mark Mendellfe57faa2015-09-18 09:26:15 -04007171// Simple implementation of packed switch - generate cascaded compare/jumps.
7172void LocationsBuilderX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7173 LocationSummary* locations =
7174 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
7175 locations->SetInAt(0, Location::RequiresRegister());
7176}
7177
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007178void InstructionCodeGeneratorX86::GenPackedSwitchWithCompares(Register value_reg,
7179 int32_t lower_bound,
7180 uint32_t num_entries,
7181 HBasicBlock* switch_block,
7182 HBasicBlock* default_block) {
7183 // Figure out the correct compare values and jump conditions.
7184 // Handle the first compare/branch as a special case because it might
7185 // jump to the default case.
7186 DCHECK_GT(num_entries, 2u);
7187 Condition first_condition;
7188 uint32_t index;
7189 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
7190 if (lower_bound != 0) {
7191 first_condition = kLess;
7192 __ cmpl(value_reg, Immediate(lower_bound));
7193 __ j(first_condition, codegen_->GetLabelOf(default_block));
7194 __ j(kEqual, codegen_->GetLabelOf(successors[0]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007195
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007196 index = 1;
7197 } else {
7198 // Handle all the compare/jumps below.
7199 first_condition = kBelow;
7200 index = 0;
7201 }
7202
7203 // Handle the rest of the compare/jumps.
7204 for (; index + 1 < num_entries; index += 2) {
7205 int32_t compare_to_value = lower_bound + index + 1;
7206 __ cmpl(value_reg, Immediate(compare_to_value));
7207 // Jump to successors[index] if value < case_value[index].
7208 __ j(first_condition, codegen_->GetLabelOf(successors[index]));
7209 // Jump to successors[index + 1] if value == case_value[index + 1].
7210 __ j(kEqual, codegen_->GetLabelOf(successors[index + 1]));
7211 }
7212
7213 if (index != num_entries) {
7214 // There are an odd number of entries. Handle the last one.
7215 DCHECK_EQ(index + 1, num_entries);
7216 __ cmpl(value_reg, Immediate(lower_bound + index));
7217 __ j(kEqual, codegen_->GetLabelOf(successors[index]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007218 }
7219
7220 // And the default for any other value.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007221 if (!codegen_->GoesToNextBlock(switch_block, default_block)) {
7222 __ jmp(codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007223 }
7224}
7225
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007226void InstructionCodeGeneratorX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7227 int32_t lower_bound = switch_instr->GetStartValue();
7228 uint32_t num_entries = switch_instr->GetNumEntries();
7229 LocationSummary* locations = switch_instr->GetLocations();
7230 Register value_reg = locations->InAt(0).AsRegister<Register>();
7231
7232 GenPackedSwitchWithCompares(value_reg,
7233 lower_bound,
7234 num_entries,
7235 switch_instr->GetBlock(),
7236 switch_instr->GetDefaultBlock());
7237}
7238
Mark Mendell805b3b52015-09-18 14:10:29 -04007239void LocationsBuilderX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
7240 LocationSummary* locations =
7241 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
7242 locations->SetInAt(0, Location::RequiresRegister());
7243
7244 // Constant area pointer.
7245 locations->SetInAt(1, Location::RequiresRegister());
7246
7247 // And the temporary we need.
7248 locations->AddTemp(Location::RequiresRegister());
7249}
7250
7251void InstructionCodeGeneratorX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
7252 int32_t lower_bound = switch_instr->GetStartValue();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007253 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendell805b3b52015-09-18 14:10:29 -04007254 LocationSummary* locations = switch_instr->GetLocations();
7255 Register value_reg = locations->InAt(0).AsRegister<Register>();
7256 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
7257
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007258 if (num_entries <= kPackedSwitchJumpTableThreshold) {
7259 GenPackedSwitchWithCompares(value_reg,
7260 lower_bound,
7261 num_entries,
7262 switch_instr->GetBlock(),
7263 default_block);
7264 return;
7265 }
7266
Mark Mendell805b3b52015-09-18 14:10:29 -04007267 // Optimizing has a jump area.
7268 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
7269 Register constant_area = locations->InAt(1).AsRegister<Register>();
7270
7271 // Remove the bias, if needed.
7272 if (lower_bound != 0) {
7273 __ leal(temp_reg, Address(value_reg, -lower_bound));
7274 value_reg = temp_reg;
7275 }
7276
7277 // Is the value in range?
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007278 DCHECK_GE(num_entries, 1u);
Mark Mendell805b3b52015-09-18 14:10:29 -04007279 __ cmpl(value_reg, Immediate(num_entries - 1));
7280 __ j(kAbove, codegen_->GetLabelOf(default_block));
7281
7282 // We are in the range of the table.
7283 // Load (target-constant_area) from the jump table, indexing by the value.
7284 __ movl(temp_reg, codegen_->LiteralCaseTable(switch_instr, constant_area, value_reg));
7285
7286 // Compute the actual target address by adding in constant_area.
7287 __ addl(temp_reg, constant_area);
7288
7289 // And jump.
7290 __ jmp(temp_reg);
7291}
7292
Mark Mendell0616ae02015-04-17 12:49:27 -04007293void LocationsBuilderX86::VisitX86ComputeBaseMethodAddress(
7294 HX86ComputeBaseMethodAddress* insn) {
7295 LocationSummary* locations =
7296 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
7297 locations->SetOut(Location::RequiresRegister());
7298}
7299
7300void InstructionCodeGeneratorX86::VisitX86ComputeBaseMethodAddress(
7301 HX86ComputeBaseMethodAddress* insn) {
7302 LocationSummary* locations = insn->GetLocations();
7303 Register reg = locations->Out().AsRegister<Register>();
7304
7305 // Generate call to next instruction.
7306 Label next_instruction;
7307 __ call(&next_instruction);
7308 __ Bind(&next_instruction);
7309
7310 // Remember this offset for later use with constant area.
7311 codegen_->SetMethodAddressOffset(GetAssembler()->CodeSize());
7312
7313 // Grab the return address off the stack.
7314 __ popl(reg);
7315}
7316
7317void LocationsBuilderX86::VisitX86LoadFromConstantTable(
7318 HX86LoadFromConstantTable* insn) {
7319 LocationSummary* locations =
7320 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
7321
7322 locations->SetInAt(0, Location::RequiresRegister());
7323 locations->SetInAt(1, Location::ConstantLocation(insn->GetConstant()));
7324
7325 // If we don't need to be materialized, we only need the inputs to be set.
David Brazdilb3e773e2016-01-26 11:28:37 +00007326 if (insn->IsEmittedAtUseSite()) {
Mark Mendell0616ae02015-04-17 12:49:27 -04007327 return;
7328 }
7329
7330 switch (insn->GetType()) {
7331 case Primitive::kPrimFloat:
7332 case Primitive::kPrimDouble:
7333 locations->SetOut(Location::RequiresFpuRegister());
7334 break;
7335
7336 case Primitive::kPrimInt:
7337 locations->SetOut(Location::RequiresRegister());
7338 break;
7339
7340 default:
7341 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
7342 }
7343}
7344
7345void InstructionCodeGeneratorX86::VisitX86LoadFromConstantTable(HX86LoadFromConstantTable* insn) {
David Brazdilb3e773e2016-01-26 11:28:37 +00007346 if (insn->IsEmittedAtUseSite()) {
Mark Mendell0616ae02015-04-17 12:49:27 -04007347 return;
7348 }
7349
7350 LocationSummary* locations = insn->GetLocations();
7351 Location out = locations->Out();
7352 Register const_area = locations->InAt(0).AsRegister<Register>();
7353 HConstant *value = insn->GetConstant();
7354
7355 switch (insn->GetType()) {
7356 case Primitive::kPrimFloat:
7357 __ movss(out.AsFpuRegister<XmmRegister>(),
7358 codegen_->LiteralFloatAddress(value->AsFloatConstant()->GetValue(), const_area));
7359 break;
7360
7361 case Primitive::kPrimDouble:
7362 __ movsd(out.AsFpuRegister<XmmRegister>(),
7363 codegen_->LiteralDoubleAddress(value->AsDoubleConstant()->GetValue(), const_area));
7364 break;
7365
7366 case Primitive::kPrimInt:
7367 __ movl(out.AsRegister<Register>(),
7368 codegen_->LiteralInt32Address(value->AsIntConstant()->GetValue(), const_area));
7369 break;
7370
7371 default:
7372 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
7373 }
7374}
7375
Mark Mendell0616ae02015-04-17 12:49:27 -04007376/**
7377 * Class to handle late fixup of offsets into constant area.
7378 */
Vladimir Marko5233f932015-09-29 19:01:15 +01007379class RIPFixup : public AssemblerFixup, public ArenaObject<kArenaAllocCodeGenerator> {
Mark Mendell0616ae02015-04-17 12:49:27 -04007380 public:
Mark Mendell805b3b52015-09-18 14:10:29 -04007381 RIPFixup(CodeGeneratorX86& codegen, size_t offset)
7382 : codegen_(&codegen), offset_into_constant_area_(offset) {}
7383
7384 protected:
7385 void SetOffset(size_t offset) { offset_into_constant_area_ = offset; }
7386
7387 CodeGeneratorX86* codegen_;
Mark Mendell0616ae02015-04-17 12:49:27 -04007388
7389 private:
7390 void Process(const MemoryRegion& region, int pos) OVERRIDE {
7391 // Patch the correct offset for the instruction. The place to patch is the
7392 // last 4 bytes of the instruction.
7393 // The value to patch is the distance from the offset in the constant area
7394 // from the address computed by the HX86ComputeBaseMethodAddress instruction.
Mark Mendell805b3b52015-09-18 14:10:29 -04007395 int32_t constant_offset = codegen_->ConstantAreaStart() + offset_into_constant_area_;
7396 int32_t relative_position = constant_offset - codegen_->GetMethodAddressOffset();;
Mark Mendell0616ae02015-04-17 12:49:27 -04007397
7398 // Patch in the right value.
7399 region.StoreUnaligned<int32_t>(pos - 4, relative_position);
7400 }
7401
Mark Mendell0616ae02015-04-17 12:49:27 -04007402 // Location in constant area that the fixup refers to.
Mark Mendell805b3b52015-09-18 14:10:29 -04007403 int32_t offset_into_constant_area_;
Mark Mendell0616ae02015-04-17 12:49:27 -04007404};
7405
Mark Mendell805b3b52015-09-18 14:10:29 -04007406/**
7407 * Class to handle late fixup of offsets to a jump table that will be created in the
7408 * constant area.
7409 */
7410class JumpTableRIPFixup : public RIPFixup {
7411 public:
7412 JumpTableRIPFixup(CodeGeneratorX86& codegen, HX86PackedSwitch* switch_instr)
7413 : RIPFixup(codegen, static_cast<size_t>(-1)), switch_instr_(switch_instr) {}
7414
7415 void CreateJumpTable() {
7416 X86Assembler* assembler = codegen_->GetAssembler();
7417
7418 // Ensure that the reference to the jump table has the correct offset.
7419 const int32_t offset_in_constant_table = assembler->ConstantAreaSize();
7420 SetOffset(offset_in_constant_table);
7421
7422 // The label values in the jump table are computed relative to the
7423 // instruction addressing the constant area.
7424 const int32_t relative_offset = codegen_->GetMethodAddressOffset();
7425
7426 // Populate the jump table with the correct values for the jump table.
7427 int32_t num_entries = switch_instr_->GetNumEntries();
7428 HBasicBlock* block = switch_instr_->GetBlock();
7429 const ArenaVector<HBasicBlock*>& successors = block->GetSuccessors();
7430 // The value that we want is the target offset - the position of the table.
7431 for (int32_t i = 0; i < num_entries; i++) {
7432 HBasicBlock* b = successors[i];
7433 Label* l = codegen_->GetLabelOf(b);
7434 DCHECK(l->IsBound());
7435 int32_t offset_to_block = l->Position() - relative_offset;
7436 assembler->AppendInt32(offset_to_block);
7437 }
7438 }
7439
7440 private:
7441 const HX86PackedSwitch* switch_instr_;
7442};
7443
7444void CodeGeneratorX86::Finalize(CodeAllocator* allocator) {
7445 // Generate the constant area if needed.
7446 X86Assembler* assembler = GetAssembler();
7447 if (!assembler->IsConstantAreaEmpty() || !fixups_to_jump_tables_.empty()) {
7448 // Align to 4 byte boundary to reduce cache misses, as the data is 4 and 8
7449 // byte values.
7450 assembler->Align(4, 0);
7451 constant_area_start_ = assembler->CodeSize();
7452
7453 // Populate any jump tables.
7454 for (auto jump_table : fixups_to_jump_tables_) {
7455 jump_table->CreateJumpTable();
7456 }
7457
7458 // And now add the constant area to the generated code.
7459 assembler->AddConstantArea();
7460 }
7461
7462 // And finish up.
7463 CodeGenerator::Finalize(allocator);
7464}
7465
Mark Mendell0616ae02015-04-17 12:49:27 -04007466Address CodeGeneratorX86::LiteralDoubleAddress(double v, Register reg) {
7467 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddDouble(v));
7468 return Address(reg, kDummy32BitOffset, fixup);
7469}
7470
7471Address CodeGeneratorX86::LiteralFloatAddress(float v, Register reg) {
7472 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddFloat(v));
7473 return Address(reg, kDummy32BitOffset, fixup);
7474}
7475
7476Address CodeGeneratorX86::LiteralInt32Address(int32_t v, Register reg) {
7477 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt32(v));
7478 return Address(reg, kDummy32BitOffset, fixup);
7479}
7480
7481Address CodeGeneratorX86::LiteralInt64Address(int64_t v, Register reg) {
7482 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt64(v));
7483 return Address(reg, kDummy32BitOffset, fixup);
7484}
7485
Aart Bika19616e2016-02-01 18:57:58 -08007486void CodeGeneratorX86::Load32BitValue(Register dest, int32_t value) {
7487 if (value == 0) {
7488 __ xorl(dest, dest);
7489 } else {
7490 __ movl(dest, Immediate(value));
7491 }
7492}
7493
7494void CodeGeneratorX86::Compare32BitValue(Register dest, int32_t value) {
7495 if (value == 0) {
7496 __ testl(dest, dest);
7497 } else {
7498 __ cmpl(dest, Immediate(value));
7499 }
7500}
7501
Mark Mendell805b3b52015-09-18 14:10:29 -04007502Address CodeGeneratorX86::LiteralCaseTable(HX86PackedSwitch* switch_instr,
7503 Register reg,
7504 Register value) {
7505 // Create a fixup to be used to create and address the jump table.
7506 JumpTableRIPFixup* table_fixup =
7507 new (GetGraph()->GetArena()) JumpTableRIPFixup(*this, switch_instr);
7508
7509 // We have to populate the jump tables.
7510 fixups_to_jump_tables_.push_back(table_fixup);
7511
7512 // We want a scaled address, as we are extracting the correct offset from the table.
7513 return Address(reg, value, TIMES_4, kDummy32BitOffset, table_fixup);
7514}
7515
Andreas Gampe85b62f22015-09-09 13:15:38 -07007516// TODO: target as memory.
7517void CodeGeneratorX86::MoveFromReturnRegister(Location target, Primitive::Type type) {
7518 if (!target.IsValid()) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007519 DCHECK_EQ(type, Primitive::kPrimVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07007520 return;
7521 }
7522
7523 DCHECK_NE(type, Primitive::kPrimVoid);
7524
7525 Location return_loc = InvokeDexCallingConventionVisitorX86().GetReturnLocation(type);
7526 if (target.Equals(return_loc)) {
7527 return;
7528 }
7529
7530 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
7531 // with the else branch.
7532 if (type == Primitive::kPrimLong) {
7533 HParallelMove parallel_move(GetGraph()->GetArena());
7534 parallel_move.AddMove(return_loc.ToLow(), target.ToLow(), Primitive::kPrimInt, nullptr);
7535 parallel_move.AddMove(return_loc.ToHigh(), target.ToHigh(), Primitive::kPrimInt, nullptr);
7536 GetMoveResolver()->EmitNativeCode(&parallel_move);
7537 } else {
7538 // Let the parallel move resolver take care of all of this.
7539 HParallelMove parallel_move(GetGraph()->GetArena());
7540 parallel_move.AddMove(return_loc, target, type, nullptr);
7541 GetMoveResolver()->EmitNativeCode(&parallel_move);
7542 }
7543}
7544
Roland Levillain4d027112015-07-01 15:41:14 +01007545#undef __
7546
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00007547} // namespace x86
7548} // namespace art